/*
 *  JavaSMT is an API wrapper for a collection of SMT solvers.
 *  This file is part of JavaSMT.
 *
 *  Copyright (C) 2007-2016  Dirk Beyer
 *  All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.sosy_lab.java_smt.solvers.z3;

import com.microsoft.z3.Native;

import org.sosy_lab.java_smt.api.IntegerFormulaManager;
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;

import java.math.BigDecimal;

class Z3IntegerFormulaManager extends Z3NumeralFormulaManager<IntegerFormula, IntegerFormula>
    implements IntegerFormulaManager {

  Z3IntegerFormulaManager(Z3FormulaCreator pCreator) {
    super(pCreator);
  }

  @Override
  protected long getNumeralType() {
    return getFormulaCreator().getIntegerType();
  }

  @Override
  protected Long makeNumberImpl(double pNumber) {
    return makeNumberImpl((long) pNumber);
  }

  @Override
  protected Long makeNumberImpl(BigDecimal pNumber) {
    return decimalAsInteger(pNumber);
  }

  @Override
  public Long modulo(Long pNumber1, Long pNumber2) {
    return Native.mkMod(z3context, pNumber1, pNumber2);
  }

  @Override
  protected Long modularCongruence(Long pNumber1, Long pNumber2, long pModulo) {
    // ((_ divisible n) x)   <==>   (= x (* n (div x n)))
    long n = makeNumberImpl(pModulo);
    long x = subtract(pNumber1, pNumber2);
    return Native.mkEq(
        z3context, x, Native.mkMul(z3context, 2, new long[] {n, Native.mkDiv(z3context, x, n)}));
  }
}

@@ -1722,74 +1722,136 @@
     
-// ((_ divisible n) x)   %3C==%3E   (= x (* n (div x n)))%0A    long n =
+long n = makeNumberImpl(pModulo);%0A    Native.incRef(z3context, n);%0A    try %7B%0A      return modularCongruence0(pNumber1, pNumber2,
  mak
@@ -1874,177 +1874,651 @@
 ulo)
+)
 ;%0A    
-long x = subtract(pNumber1, pNumber2);%0A    return Native.mkEq(%0A        z3context, x, Native.mkMul(z3context, 2, new long%5B%5D %7Bn, Native.mkDiv(z3context, x, n)%7D)
+%7D finally %7B%0A      Native.decRef(z3context, n);%0A    %7D%0A  %7D%0A  protected Long modularCongruence0(Long pNumber1, Long pNumber2, Long n) %7B%0A    // ((_ divisible n) x)   %3C==%3E   (= x (* n (div x n)))%0A    long x = subtract(pNumber1, pNumber2);%0A    Native.incRef(z3context, x);%0A    long div = Native.mkDiv(z3context, x, n);%0A    Native.incRef(z3context, div);%0A    long mul = Native.mkMul(z3context, 2, new long%5B%5D %7Bn, div%7D);%0A    Native.incRef(z3context, mul);%0A    try %7B%0A      return Native.mkEq(z3context, x, mul);%0A    %7D finally %7B%0A      Native.decRef(z3context, x);%0A      Native.decRef(z3context, div);%0A      Native.decRef(z3context, mul
 );%0A  
+ 
-%7D
+ %7D%0A  %7D%0A
 %0A%7D%0A

Add forgotten intermediate incRefs to Z3#modularCongruence