// File: /workspace/github_cpp_makefile_star2_O1/biotomas[P]ipasir/app/ipasir-check-iterative/ipasir-check-iterative.cc
// Function: main
int main(int argc, char* argv[])
{
  std::cout << "Norbert Manthey -- IPASIR iterative load checker" << std::endl;
  std::cout << "  add formula that has sub formulas of blocks" << std::endl
            << "  use full stack of assumptions and modify last polarities per call" << std::endl;
  if(argc < 3) {
    std::cout << "usage: " << argv[0] << " X Y Z ... with 2 < X < 2^30, 1 < Y < 10, 1 < Z" << std::endl;
    return 1;
  }
  
  int X = atoi(argv[1]);
  int Y = atoi(argv[2]);
  int Z = atoi(argv[3]);
  
  if(X<3) {
    std::cout << "error: given first number " << X << " is not in limits" << std::endl;
    return 1;
  }
  
  if(Y<1 || Y>9) {
    std::cout << "error: given second number " << Y << " is not in limits" << std::endl;
    return 1;
  }
  
  if(Z < 2)  {
    std::cout << "error: given third number " << Z << " is not in limits" << std::endl;
    return 1;
  }

  SATSolver solver;
  
  const int periodsize = (1 << Y);
  vector<int> clause(Y,0);
  int index = 0;
  int clauses = 0;
  int maxvar = 0;
  for(int i = 1 ; i < periodsize; ++i) {
    index = i;
    int sum = 0;
    for(int j = 0 ; j < Y; ++j) {
      clause[j] = ((index & 1) == 0) ? j+1 : -j-1;
      sum += clause[j] > 0 ? 1 : -1;
      index = index >> 1;
    }
    assert(sum < (int)clause.size() && "clause with positive literals only should not be created");
    
    for(int j = 0; j < Z; ++ j) {
      if(j>0) {
	for(int k=0; k < Y; ++k) {
	  clause[k] = clause[k] > 0 ? clause[k] + Y : clause[k] - Y;
	  maxvar = maxvar > clause[k] ? maxvar : clause[k];
	}
      }
      solver.addClause(clause);
      clauses ++;
    }
  }
  
  std::cout << "created formula with " << clauses << " clauses, and maxvar " << maxvar << std::endl;
  
  vector<int> assumptions;
  std::vector<int> model;
  std::vector<int> conflict;
  
  for(int i = 1 ; i <= maxvar; ++i) assumptions.push_back(-i);
  int ret = solver.solve(assumptions, model, conflict);
  std::cout << "initial call returned with " << ret << std::endl;
  if(ret != 10)
  {
    std::cout << "error: first call did not result in model, but " << ret << ", abort" << std::endl;
    return 1;
  }
  
  double runtime = cpuTime();
  int call = 0;
  for(int i = 2; i <= X; ++ i)
  {
    // modify last bits of assumptions
    int mask = call;
    int index = 1;
    while(mask > 0 && index <= assumptions.size()) {
      if((mask & 1)==1) assumptions[assumptions.size() - index] = -assumptions[assumptions.size() - index];
      mask = mask >> 1;
      index ++;
    }
    conflict.clear();
    int ret = solver.solve(assumptions, model, conflict);
    call ++;
    if(i % 256 == 0) std::cout << "  after " << i << " calls: " << (double)X / (cpuTime()-runtime) << " calls per second" << std::endl;
    if (ret==20) {
      solver.addClause(conflict);
    } else if (call!=1) {
      std::cout << "error: in call " << call << ", did not detect conflict (but " << ret << ") on unsatisfiable input, abort" << std::endl;
      std::cout << "used assumptions: " << assumptions << std::endl;
      return 1;
    }

    // modify last bits of assumptions
    mask = call;
    index = 1;
    while(mask > 0 && index <= assumptions.size()) {
      if(assumptions[assumptions.size() - index] > 0) assumptions[assumptions.size() - index] = -assumptions[assumptions.size() - index];
      mask = mask >> 1;
      index ++;
    }
  }
  runtime = cpuTime() - runtime;
  std::cout << "performed " << (double)X / runtime << " calls per second" << std::endl;
  
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rsi, %rbx
movl %edi, %ebp
movq 0x167af(%rip), %r14     # 0x18fc8
leaq 0xf7f0(%rip), %rsi      # 0x12010
movl $0x30, %edx
movq %r14, %rdi
callq 0x21b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
leaq 0xf7e6(%rip), %rsi      # 0x12041
movl $0x2d, %edx
movq %r14, %rdi
callq 0x21b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movq %rax, %r14
leaq 0xf7d6(%rip), %rsi      # 0x1206f
movl $0x43, %edx
movq %rax, %rdi
callq 0x21b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
cmpl $0x2, %ebp
jg 0x2918
movq 0x166ef(%rip), %r14     # 0x18fc8
leaq 0xf7d3(%rip), %rsi      # 0x120b3
movl $0x7, %edx
movq %r14, %rdi
callq 0x21b0
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x2996
movq %rbx, %rdi
callq 0x2080
movq 0x166c0(%rip), %rdi     # 0x18fc8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21b0
jmp 0x29ae
movq 0x8(%rbx), %rdi
callq 0x2250
movl %eax, %r12d
movq 0x10(%rbx), %rdi
callq 0x2250
movl %eax, %r14d
movq 0x18(%rbx), %rdi
callq 0x2250
cmpl $0x2, %r12d
jg 0x2965
movq 0x16682(%rip), %rbx     # 0x18fc8
leaq 0xf79e(%rip), %rsi      # 0x120eb
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %r12d, %esi
jmp 0x2a03
movl %eax, %ebp
movl %r14d, %r15d
leal -0xa(%r15), %eax
cmpl $-0xa, %eax
ja 0x29de
movq 0x1664e(%rip), %rbx     # 0x18fc8
leaq 0xf797(%rip), %rsi      # 0x12118
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %r15d, %esi
jmp 0x2a03
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2290
leaq 0xf706(%rip), %rsi      # 0x120bb
movl $0x2f, %edx
movq %r14, %rdi
callq 0x21b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
jmp 0x2a39
cmpl $0x1, %ebp
jg 0x2a61
movq 0x165de(%rip), %rbx     # 0x18fc8
leaq 0xf743(%rip), %rsi      # 0x12134
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf6f4(%rip), %rsi      # 0x12106
movl $0x11, %edx
movq %rax, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movl $0x1, %r13d
movl %r13d, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x58(%rsp), %rdi
callq 0x320c
leaq 0xa8(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x68(%rsp), %rdi
leaq 0x10(%rsp), %rcx
movq %r15, %rsi
callq 0x32e4
movl %r12d, 0x3c(%rsp)
testq %r15, %r15
je 0x2b6a
movl %r15d, %ebx
negl %ebx
xorl %r14d, %r14d
movl $0x1, %esi
leaq 0x68(%rsp), %r13
xorl %r12d, %r12d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x68(%rsp), %rdx
xorl %edi, %edi
xorl %eax, %eax
movl %esi, 0x30(%rsp)
leaq 0x1(%rdi), %r8
testb $0x1, %sil
movl %r8d, %r9d
cmovnel %ecx, %r9d
movl %r9d, (%rdx,%rdi,4)
xorl %edi, %edi
testl %r9d, %r9d
setg %dil
leal (%rax,%rdi,2), %eax
decl %eax
shrl %esi
decl %ecx
movq %r8, %rdi
cmpq %r8, %r15
jne 0x2ac0
movq 0x70(%rsp), %rcx
subq 0x68(%rsp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
jge 0x3162
addl %ebp, %r12d
movl %r12d, 0xc(%rsp)
xorl %r12d, %r12d
testl %r12d, %r12d
je 0x2b39
movq 0x68(%rsp), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
testl %edx, %edx
movl %ebx, %esi
cmovgl %r15d, %esi
addl %edx, %esi
movl %esi, (%rax,%rcx,4)
cmpl %esi, %r14d
cmovlel %esi, %r14d
incq %rcx
cmpq %rcx, %r15
jne 0x2b1a
leaq 0x58(%rsp), %rdi
movq %r13, %rsi
callq 0x3362
incl %r12d
cmpl %ebp, %r12d
jne 0x2b0e
movl 0x30(%rsp), %esi
incl %esi
movl %esi, %eax
movl %r15d, %ecx
shrl %cl, %eax
testl %eax, %eax
movl 0xc(%rsp), %r12d
je 0x2aae
jmp 0x2b70
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x16451(%rip), %rdi     # 0x18fc8
leaq 0xf659(%rip), %rsi      # 0x121d7
movl $0x15, %edx
callq 0x21b0
movq 0x16439(%rip), %rdi     # 0x18fc8
movl %r12d, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf64c(%rip), %rsi      # 0x121ed
movl $0x15, %edx
movq %rax, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %r14d, %esi
callq 0x22a0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movq %rax, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movq %rax, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
testl %r14d, %r14d
movl 0x3c(%rsp), %ebp
jle 0x2c6c
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x10(%rsp), %rbx
leaq 0xa8(%rsp), %r15
movl %r12d, 0xa8(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2c4e
movl %r12d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x2c59
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3856
leal (%r14,%r12), %eax
decl %eax
movl %r12d, %ecx
decl %ecx
movl %ecx, %r12d
cmpl $-0x1, %eax
jne 0x2c2c
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq 0x33ca
movl %eax, %ebx
movq 0x16337(%rip), %rdi     # 0x18fc8
leaq 0xf56b(%rip), %rsi      # 0x12203
movl $0x1b, %edx
callq 0x21b0
movq 0x1631f(%rip), %r14     # 0x18fc8
movq %r14, %rdi
movl %ebx, %esi
callq 0x22a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
cmpl $0xa, %ebx
jne 0x3087
movq $0x0, 0x30(%rsp)
leaq 0xa8(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x20d0
xorps %xmm0, %xmm0
cvtsi2sdq (%rbx), %xmm0
cvtsi2sdq 0x8(%rbx), %xmm1
divsd 0xf2f1(%rip), %xmm1     # 0x12008
addsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x88(%rsp)
cmpl $0x3, %ebp
movl $0x2, %r12d
movl $0x2, %eax
cmovgel %ebp, %eax
addl $-0x2, %eax
movl %eax, 0xc(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
testl %r15d, %r15d
je 0x2d84
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movl %r15d, %edx
subq $0x1, %rcx
jb 0x2d84
testb $0x1, %dl
je 0x2d79
negl (%rax,%rcx,4)
movl %edx, %esi
shrl %esi
cmpl $0x1, %edx
movl %esi, %edx
ja 0x2d6b
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
je 0x2d95
movq %rax, 0x48(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq 0x33ca
movl %eax, %ebx
testb %r12b, %r12b
jne 0x2e84
movl $0x8, %edx
movq %r14, %rdi
leaq 0xf484(%rip), %rsi      # 0x1224f
callq 0x21b0
movq %r14, %rdi
movl %r12d, %esi
callq 0x22a0
movq %rax, %rbp
movl $0x8, %edx
movq %rax, %rdi
leaq 0xf46b(%rip), %rsi      # 0x12258
callq 0x21b0
xorl %edi, %edi
leaq 0xa8(%rsp), %rsi
callq 0x20d0
xorps %xmm0, %xmm0
cvtsi2sdq 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0xb0(%rsp), %xmm1
divsd 0xf1e5(%rip), %xmm1     # 0x12008
addsd %xmm0, %xmm1
subsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %rbp, %rdi
callq 0x2260
movq %rax, %rbp
movl $0x11, %edx
movq %rax, %rdi
leaq 0xf40a(%rip), %rsi      # 0x12261
callq 0x21b0
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
leal 0x1(%r15), %ebp
cmpl $0x14, %ebx
jne 0x2ea1
leaq 0x58(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3362
jmp 0x2fb4
testl %r15d, %r15d
je 0x2fb4
movl $0xf, %edx
movq %r14, %rdi
leaq 0xf3ba(%rip), %rsi      # 0x12273
callq 0x21b0
movq %r14, %rdi
movl %ebp, %esi
callq 0x22a0
movq %rax, %r13
movl $0x1f, %edx
movq %rax, %rdi
leaq 0xf3a9(%rip), %rsi      # 0x12283
callq 0x21b0
movq %r13, %rdi
movl %ebx, %esi
callq 0x22a0
movq %rax, %rbx
movl $0x1f, %edx
movq %rax, %rdi
leaq 0xf3a8(%rip), %rsi      # 0x122a3
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movl $0x12, %edx
movq %r14, %rdi
leaq 0xf38d(%rip), %rsi      # 0x122c3
callq 0x21b0
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
je 0x2f83
xorl %ebx, %ebx
movl $0x1, %edx
movq %r14, %rdi
leaq 0xf406(%rip), %rsi      # 0x1235e
callq 0x21b0
movq 0x10(%rsp), %rax
movl (%rax,%rbx,4), %esi
movq %r14, %rdi
callq 0x22a0
incq %rbx
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x2, %rax
cmpq %rbx, %rax
ja 0x2f49
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20f0
movl $0x1, %r13d
xorl %eax, %eax
jmp 0x2fe6
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movl %ebp, %esi
movb $0x1, %al
subq $0x1, %rdx
jb 0x2fe6
movl (%rcx,%rdx,4), %edi
testl %edi, %edi
jle 0x2fdb
negl %edi
movl %edi, (%rcx,%rdx,4)
movl %esi, %edi
shrl %edi
cmpl $0x1, %esi
movl %edi, %esi
ja 0x2fc7
testb %al, %al
je 0x300d
cmpl 0x3c(%rsp), %r12d
leal 0x1(%r12), %eax
setge %cl
movq %rcx, 0x30(%rsp)
movl %eax, %r12d
cmpl 0xc(%rsp), %r15d
movl %ebp, %r15d
jne 0x2d52
testb $0x1, 0x30(%rsp)
je 0x30f1
callq 0x3597
movsd %xmm0, 0x30(%rsp)
movq 0x15f9e(%rip), %rdi     # 0x18fc8
leaq 0xf2a5(%rip), %rsi      # 0x122d6
movl $0xa, %edx
callq 0x21b0
movsd 0x30(%rsp), %xmm1
subsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm0
divsd %xmm1, %xmm0
movq 0x15f6a(%rip), %rdi     # 0x18fc8
callq 0x2260
movq %rax, %rbx
leaq 0xf1f4(%rip), %rsi      # 0x12261
movl $0x11, %edx
movq %rax, %rdi
callq 0x21b0
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x2090
jmp 0x30f1
movq 0x15f3a(%rip), %rdi     # 0x18fc8
leaq 0xf18a(%rip), %rsi      # 0x1221f
movl $0x2f, %edx
callq 0x21b0
movq 0x15f22(%rip), %rdi     # 0x18fc8
movl %ebx, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf204(%rip), %rsi      # 0x122bb
movl $0x7, %edx
movq %rax, %rdi
callq 0x21b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movl $0x1, %r13d
movq %rax, %rdi
callq 0x20f0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3108
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3125
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x313c
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x3153
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x58(%rsp), %rdi
callq 0x35cc
jmp 0x2a4c
leaq 0xefe6(%rip), %rdi      # 0x1214f
leaq 0xf036(%rip), %rsi      # 0x121a6
leaq 0xf049(%rip), %rcx      # 0x121c0
movl $0x46, %edx
callq 0x20e0
jmp 0x3190
movq %rax, %rbx
jmp 0x31fa
jmp 0x31e0
jmp 0x3190
jmp 0x3190
jmp 0x3190
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x31aa
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x31c7
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x31e3
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
jmp 0x31e3
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x31fa
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x58(%rsp), %rdi
callq 0x35cc
movq %rbx, %rdi
callq 0x22b0