// File: /workspace/github_cpp_makefile_star2_O3/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 $0x118, %rsp            # imm = 0x118
movq %rsi, %rbx
movl %edi, %ebp
movq 0x167af(%rip), %r14     # 0x18fc8
leaq 0xf820(%rip), %rsi      # 0x12040
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 0xf816(%rip), %rsi      # 0x12071
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 0xf806(%rip), %rsi      # 0x1209f
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 0xf803(%rip), %rsi      # 0x120e3
movl $0x7, %edx
movq %r14, %rdi
callq 0x21b0
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x299b
movq %rbx, %rdi
callq 0x2080
movq 0x166c0(%rip), %rdi     # 0x18fc8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21b0
jmp 0x29b3
movq 0x8(%rbx), %rdi
callq 0x2250
movl %eax, %r15d
movq 0x10(%rbx), %rdi
callq 0x2250
movl %eax, %r14d
movq 0x18(%rbx), %rdi
callq 0x2250
cmpl $0x2, %r15d
jg 0x2965
movq 0x16682(%rip), %rbx     # 0x18fc8
leaq 0xf7ce(%rip), %rsi      # 0x1211b
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %r15d, %esi
jmp 0x2a08
movl %eax, %ebp
movl %r15d, 0x28(%rsp)
movl %r14d, %r15d
leal -0xa(%r15), %eax
cmpl $-0xa, %eax
ja 0x29e3
movq 0x16649(%rip), %rbx     # 0x18fc8
leaq 0xf7c2(%rip), %rsi      # 0x12148
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %r15d, %esi
jmp 0x2a08
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2290
leaq 0xf731(%rip), %rsi      # 0x120eb
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 0x2a3e
cmpl $0x1, %ebp
jg 0x2a64
movq 0x165d9(%rip), %rbx     # 0x18fc8
leaq 0xf76e(%rip), %rsi      # 0x12164
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf71f(%rip), %rsi      # 0x12136
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, %ebx
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x48(%rsp), %rdi
callq 0x31ac
leaq 0x88(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x58(%rsp), %rdi
movq %rsp, %rcx
movq %r15, %rsi
callq 0x3284
movl %r15d, %r12d
negl %r12d
xorl %r14d, %r14d
movl $0x1, %esi
leaq 0x58(%rsp), %rbx
xorl %r13d, %r13d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x58(%rsp), %rax
xorl %edi, %edi
xorl %ecx, %ecx
movl %esi, 0x20(%rsp)
leaq 0x1(%rdi), %r8
testb $0x1, %sil
movl %r8d, %r9d
cmovnel %edx, %r9d
movl %r9d, (%rax,%rdi,4)
xorl %edi, %edi
testl %r9d, %r9d
setg %dil
leal (%rcx,%rdi,2), %ecx
decl %ecx
shrl %esi
decl %edx
movq %r8, %rdi
cmpq %r8, %r15
jne 0x2ab4
movq 0x60(%rsp), %rdx
subq %rax, %rdx
shrq $0x2, %rdx
cmpl %edx, %ecx
jge 0x3104
addl %ebp, %r13d
movl %r13d, 0x18(%rsp)
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x2b2c
movq 0x58(%rsp), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
testl %edx, %edx
movl %r12d, %esi
cmovgl %r15d, %esi
addl %edx, %esi
movl %esi, (%rax,%rcx,4)
cmpl %esi, %r14d
cmovlel %esi, %r14d
incq %rcx
cmpq %rcx, %r15
jne 0x2b0c
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x3400
incl %r13d
cmpl %ebp, %r13d
jne 0x2b00
movl 0x20(%rsp), %esi
incl %esi
movl %esi, %eax
movl %r15d, %ecx
shrl %cl, %eax
testl %eax, %eax
movl 0x18(%rsp), %r13d
je 0x2aa2
movq 0x16466(%rip), %rdi     # 0x18fc8
leaq 0xf69e(%rip), %rsi      # 0x12207
movl $0x15, %edx
callq 0x21b0
movq 0x1644e(%rip), %rdi     # 0x18fc8
movl %r13d, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf691(%rip), %rsi      # 0x1221d
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, (%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movq %rax, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
testl %r14d, %r14d
jle 0x2c4d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %rsp, %rbx
leaq 0x88(%rsp), %r15
movl %r12d, 0x88(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x2c2f
movl %r12d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x2c3a
movq %rbx, %rdi
movq %r15, %rdx
callq 0x38fc
leal (%r14,%r12), %eax
decl %eax
movl %r12d, %ecx
decl %ecx
movl %ecx, %r12d
cmpl $-0x1, %eax
jne 0x2c0d
leaq 0x48(%rsp), %rdi
movq %rsp, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x3466
movl %eax, %ebx
movq 0x1635b(%rip), %rdi     # 0x18fc8
leaq 0xf5bf(%rip), %rsi      # 0x12233
movl $0x1b, %edx
callq 0x21b0
movq 0x16343(%rip), %rdi     # 0x18fc8
movl %ebx, %esi
callq 0x22a0
movq %rax, %r14
movq (%rax), %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 $0xa, %ebx
movl 0x28(%rsp), %r15d
jne 0x2f07
xorl %r13d, %r13d
leaq 0x88(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x20d0
xorps %xmm0, %xmm0
cvtsi2sdq (%rbx), %xmm0
cvtsi2sdq 0x8(%rbx), %xmm2
divsd 0xf319(%rip), %xmm2     # 0x12008
cvtsi2sd %r15d, %xmm1
movsd %xmm1, 0x20(%rsp)
addsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
addl $-0x2, %r15d
movl $0x2, %ebp
leaq 0x30(%rsp), %r14
testl %r13d, %r13d
je 0x2d43
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movl %r13d, %edx
subq $0x1, %rcx
jb 0x2d43
testb $0x1, %dl
je 0x2d38
negl (%rax,%rcx,4)
movl %edx, %esi
shrl %esi
cmpl $0x1, %edx
movl %esi, %edx
ja 0x2d2a
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x2d54
movq %rax, 0x38(%rsp)
leaq 0x48(%rsp), %rdi
movq %rsp, %rsi
leaq 0x70(%rsp), %rdx
movq %r14, %rcx
callq 0x3466
movl %eax, %r12d
testb %bpl, %bpl
jne 0x2e3d
movl $0x8, %edx
movq 0x16247(%rip), %rdi     # 0x18fc8
leaq 0xf4f7(%rip), %rsi      # 0x1227f
callq 0x21b0
movq 0x16234(%rip), %rdi     # 0x18fc8
movl %ebp, %esi
callq 0x22a0
movq %rax, %rbx
movl $0x8, %edx
movq %rax, %rdi
leaq 0xf4db(%rip), %rsi      # 0x12288
callq 0x21b0
xorl %edi, %edi
leaq 0x88(%rsp), %rsi
callq 0x20d0
xorps %xmm0, %xmm0
cvtsi2sdq 0x88(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq 0x90(%rsp), %xmm1
divsd 0xf225(%rip), %xmm1     # 0x12008
addsd %xmm0, %xmm1
subsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %rbx, %rdi
callq 0x2260
movq %rax, %rbx
movl $0x11, %edx
movq %rax, %rdi
leaq 0xf480(%rip), %rsi      # 0x12291
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
leal 0x1(%r13), %ebx
cmpl $0x14, %r12d
jne 0x2e56
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x3400
jmp 0x2e5f
testl %r13d, %r13d
jne 0x2f75
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movl %ebx, %edx
subq $0x1, %rcx
jb 0x2e8e
movl (%rax,%rcx,4), %esi
testl %esi, %esi
jle 0x2e83
negl %esi
movl %esi, (%rax,%rcx,4)
movl %edx, %esi
shrl %esi
cmpl $0x1, %edx
movl %esi, %edx
ja 0x2e71
incl %ebp
cmpl %r15d, %r13d
movl %ebx, %r13d
jne 0x2d12
callq 0x3630
movsd %xmm0, 0x28(%rsp)
movq 0x1611a(%rip), %rdi     # 0x18fc8
leaq 0xf451(%rip), %rsi      # 0x12306
movl $0xa, %edx
callq 0x21b0
movsd 0x28(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
divsd %xmm1, %xmm0
movq 0x160ec(%rip), %rdi     # 0x18fc8
callq 0x2260
movq %rax, %r14
leaq 0xf3a6(%rip), %rsi      # 0x12291
movl $0x11, %edx
movq %rax, %rdi
callq 0x21b0
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x2090
jmp 0x3097
movq 0x160ba(%rip), %rdi     # 0x18fc8
leaq 0xf33a(%rip), %rsi      # 0x1224f
movl $0x2f, %edx
callq 0x21b0
movq 0x160a2(%rip), %rdi     # 0x18fc8
movl %ebx, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf3b4(%rip), %rsi      # 0x122eb
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, %ebx
movq %rax, %rdi
callq 0x20f0
jmp 0x3097
movq 0x1604c(%rip), %rdi     # 0x18fc8
leaq 0xf320(%rip), %rsi      # 0x122a3
movl $0xf, %edx
callq 0x21b0
movq 0x16034(%rip), %rdi     # 0x18fc8
movl %ebx, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf30e(%rip), %rsi      # 0x122b3
movl $0x1f, %edx
movq %rax, %rdi
callq 0x21b0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x22a0
movq %rax, %rbx
leaq 0xf30c(%rip), %rsi      # 0x122d3
movl $0x1f, %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
movq 0x15fc6(%rip), %rbx     # 0x18fc8
leaq 0xf2ea(%rip), %rsi      # 0x122f3
movl $0x12, %edx
movq %rbx, %rdi
callq 0x21b0
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x3067
xorl %r12d, %r12d
movq 0x15f9d(%rip), %r14     # 0x18fc8
leaq 0xf35c(%rip), %r15      # 0x1238e
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x21b0
movq (%rsp), %rax
movl (%rax,%r12,4), %esi
movq %r14, %rdi
callq 0x22a0
incq %r12
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
cmpq %r12, %rax
ja 0x3032
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq 0x15f43(%rip), %rdi     # 0x18fc8
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x20f0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x30ae
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x30c8
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x30de
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x30f5
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x48(%rsp), %rdi
callq 0x3666
jmp 0x2a50
leaq 0xf074(%rip), %rdi      # 0x1217f
leaq 0xf0c4(%rip), %rsi      # 0x121d6
leaq 0xf0d7(%rip), %rcx      # 0x121f0
movl $0x46, %edx
callq 0x20e0
movq %rax, %rbx
jmp 0x319a
jmp 0x3134
jmp 0x3134
jmp 0x3134
jmp 0x3180
jmp 0x3134
jmp 0x3134
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x314e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3168
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x3183
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
jmp 0x3183
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x319a
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2180
leaq 0x48(%rsp), %rdi
callq 0x3666
movq %rbx, %rdi
callq 0x22b0