// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq 0x14bbf(%rip), %r14     # 0x16fc8
leaq 0xdc00(%rip), %rsi      # 0x10010
movq %r14, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
leaq 0xdc1a(%rip), %rsi      # 0x10041
movq %r14, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
leaq 0xdc31(%rip), %rsi      # 0x1006f
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
cmpl $0x2, %ebp
jg 0x247d
movq 0x14b6e(%rip), %rdi     # 0x16fc8
leaq 0xdc52(%rip), %rsi      # 0x100b3
callq 0x2150
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x2150
leaq 0xdc43(%rip), %rsi      # 0x100bb
jmp 0x2513
movq 0x8(%rbx), %rdi
callq 0x2240
movl %eax, %r12d
movq 0x10(%rbx), %rdi
callq 0x2240
movl %eax, %ebp
movq 0x18(%rbx), %rdi
callq 0x2240
cmpl $0x2, %r12d
jg 0x24be
movq 0x14b1e(%rip), %rdi     # 0x16fc8
leaq 0xdc3a(%rip), %rsi      # 0x100eb
callq 0x2150
movq %rax, %rdi
movl %r12d, %esi
jmp 0x2507
movl %eax, %r14d
movl %ebp, %r15d
leal -0xa(%r15), %eax
cmpl $-0xa, %eax
ja 0x24e8
movq 0x14af4(%rip), %rdi     # 0x16fc8
leaq 0xdc3d(%rip), %rsi      # 0x10118
callq 0x2150
movq %rax, %rdi
movl %r15d, %esi
jmp 0x2507
cmpl $0x1, %r14d
jg 0x253a
movq 0x14ad3(%rip), %rdi     # 0x16fc8
leaq 0xdc38(%rip), %rsi      # 0x10134
callq 0x2150
movq %rax, %rdi
movl %r14d, %esi
callq 0x2290
leaq 0xdbf3(%rip), %rsi      # 0x10106
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, 0x10(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x2a32
leaq 0x20(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x68(%rsp), %rdi
leaq 0x80(%rsp), %rcx
movq %r15, %rsi
callq 0x2ab6
pushq $0x1
popq %rsi
movl %r15d, %r12d
negl %r12d
xorl %r13d, %r13d
leaq 0x68(%rsp), %rbx
xorl %ebp, %ebp
movl %esi, %eax
movl %r15d, %ecx
shrl %cl, %eax
testl %eax, %eax
jne 0x2639
xorl %edi, %edi
movq 0x68(%rsp), %rcx
pushq $-0x1
popq %rdx
movl %esi, 0x8(%rsp)
xorl %eax, %eax
cmpq %rdi, %r15
je 0x25c7
leaq 0x1(%rdi), %r8
testb $0x1, %sil
movl %edx, %r9d
cmovel %r8d, %r9d
movl %r9d, (%rcx,%rdi,4)
xorl %edi, %edi
testl %r9d, %r9d
setg %dil
leal (%rax,%rdi,2), %eax
decl %eax
shrl %esi
decl %edx
movq %r8, %rdi
jmp 0x2598
movq 0x70(%rsp), %rcx
subq 0x68(%rsp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
jge 0x29c1
addl %r14d, %r13d
movq %r13, (%rsp)
xorl %r13d, %r13d
cmpl %r14d, %r13d
je 0x262a
testl %r13d, %r13d
je 0x2618
movq 0x68(%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x2618
movl (%rax,%rcx,4), %edx
testl %edx, %edx
movl %r12d, %esi
cmovgl %r15d, %esi
addl %edx, %esi
movl %esi, (%rax,%rcx,4)
cmpl %esi, %ebp
cmovlel %esi, %ebp
incq %rcx
jmp 0x25f8
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x2b0c
incl %r13d
jmp 0x25e7
movl 0x8(%rsp), %esi
incl %esi
movq (%rsp), %r13
jmp 0x2579
movq 0x14988(%rip), %rdi     # 0x16fc8
leaq 0xdb90(%rip), %rsi      # 0x101d7
callq 0x2150
movq %rax, %rdi
movl %r13d, %esi
callq 0x2290
leaq 0xdb8f(%rip), %rsi      # 0x101ed
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
movl %ebp, %esi
callq 0x2290
movq %rax, %rdi
callq 0x2090
leaq 0x20(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorpd %xmm0, %xmm0
andq $0x0, 0x90(%rsp)
movapd %xmm0, (%rbx)
andq $0x0, 0x60(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %r15d, %r15d
testl %ebp, %ebp
cmovgl %ebp, %r15d
pushq $-0x1
popq %r12
leaq 0x1c(%rsp), %r14
leal (%r15,%r12), %eax
cmpl $-0x1, %eax
je 0x26d8
movl %r12d, 0x1c(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3082
decl %r12d
jmp 0x26ba
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x2b6e
movl %eax, %ebx
movq 0x148cb(%rip), %rdi     # 0x16fc8
leaq 0xdaff(%rip), %rsi      # 0x10203
callq 0x2150
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
movq %rax, %rdi
callq 0x2090
cmpl $0xa, %ebx
movl 0x10(%rsp), %r14d
jne 0x287f
callq 0x2d02
cvtsi2sd %r14d, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rsp)
decl %r14d
pushq $0x2
popq %rbp
xorl %r12d, %r12d
leaq 0x50(%rsp), %r13
cmpl %r14d, %r12d
je 0x28b8
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movl %r12d, %edx
subq $0x1, %rcx
setb %sil
testl %edx, %edx
je 0x2786
testb %sil, %sil
jne 0x2786
testb $0x1, %dl
je 0x2782
negl (%rax,%rcx,4)
shrl %edx
jmp 0x2769
movq 0x50(%rsp), %rax
cmpq %rax, 0x58(%rsp)
je 0x2797
movq %rax, 0x58(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %r13, %rcx
callq 0x2b6e
movl %eax, %ebx
testb %bpl, %bpl
jne 0x281e
movq 0x14809(%rip), %rdi     # 0x16fc8
leaq 0xda89(%rip), %rsi      # 0x1024f
callq 0x2150
movq %rax, %rdi
movl %ebp, %esi
callq 0x2290
movq %rax, %rdi
leaq 0xda79(%rip), %rsi      # 0x10258
callq 0x2150
movq %rax, %r15
callq 0x2d02
subsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movq %r15, %rdi
movapd %xmm1, %xmm0
callq 0x2250
movq %rax, %rdi
leaq 0xda50(%rip), %rsi      # 0x10261
callq 0x2150
movq %rax, %rdi
callq 0x2090
leal 0x1(%r12), %r15d
cmpl $0x14, %ebx
jne 0x2837
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x2b0c
jmp 0x2840
testl %r12d, %r12d
jne 0x290e
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movl %r15d, %edx
subq $0x1, %rcx
setb %sil
testl %edx, %edx
je 0x2875
testb %sil, %sil
jne 0x2875
movl (%rax,%rcx,4), %esi
testl %esi, %esi
jle 0x2871
negl %esi
movl %esi, (%rax,%rcx,4)
shrl %edx
jmp 0x2854
incl %ebp
movl %r15d, %r12d
jmp 0x274c
movq 0x14742(%rip), %rdi     # 0x16fc8
leaq 0xd992(%rip), %rsi      # 0x1021f
callq 0x2150
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
leaq 0xda18(%rip), %rsi      # 0x102bb
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
jmp 0x2984
callq 0x2d02
movsd %xmm0, 0x10(%rsp)
movq 0x146fe(%rip), %rdi     # 0x16fc8
leaq 0xda05(%rip), %rsi      # 0x102d6
callq 0x2150
movsd 0x10(%rsp), %xmm1
subsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x2250
leaq 0xd967(%rip), %rsi      # 0x10261
movq %rax, %rdi
callq 0x2150
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x2090
jmp 0x2987
movq 0x146b3(%rip), %rdi     # 0x16fc8
leaq 0xd957(%rip), %rsi      # 0x10273
callq 0x2150
movq %rax, %rdi
movl %r15d, %esi
callq 0x2290
leaq 0xd950(%rip), %rsi      # 0x10283
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
leaq 0xd957(%rip), %rsi      # 0x102a3
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
movq 0x14665(%rip), %rdi     # 0x16fc8
leaq 0xd959(%rip), %rsi      # 0x102c3
callq 0x2150
leaq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x2d37
movq %rax, %rdi
callq 0x2090
pushq $0x1
popq %rbx
leaq 0x50(%rsp), %rdi
callq 0x31a4
leaq 0x80(%rsp), %rdi
callq 0x31a4
leaq 0x20(%rsp), %rdi
callq 0x31a4
leaq 0x68(%rsp), %rdi
callq 0x31a4
leaq 0x40(%rsp), %rdi
callq 0x2d8e
jmp 0x2526
leaq 0xd787(%rip), %rdi      # 0x1014f
leaq 0xd7d7(%rip), %rsi      # 0x101a6
leaq 0xd7ea(%rip), %rcx      # 0x101c0
pushq $0x46
popq %rdx
callq 0x20e0
movq %rax, %rbx
jmp 0x2a20
jmp 0x29ed
jmp 0x29ed
jmp 0x2a13
jmp 0x29ed
jmp 0x29ed
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x31a4
leaq 0x80(%rsp), %rdi
callq 0x31a4
leaq 0x20(%rsp), %rdi
callq 0x31a4
jmp 0x2a16
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x31a4
leaq 0x40(%rsp), %rdi
callq 0x2d8e
movq %rbx, %rdi
callq 0x22a0