// File: /workspace/github_cpp_makefile_star2_O2/biotomas[P]ipasir/app/ipasir-check-satunsat/ipasir-check-satunsat.cc
// Function: main
int main(int argc, char* argv[])
{
  std::cout << "Norbert Manthey -- IPASIR iterative satunsat checker" << std::endl;
  std::cout << "  add formula that has sub formulas of blocks" << std::endl
            << "  use almost full stack of assumptions, except last variable for each block" << std::endl
            << "  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) 
  {
    // do not add last variable per block
    if((i % periodsize) != 0 ) assumptions.push_back(-i);
  }

  // create a map with pointers to the assumption vector
  int *map[maxvar + 1];
  int fakeentry = 0;
  int pointat = 0;
  for(int i = 1; i <= maxvar; ++i)
  {
    if((i % periodsize) != 0 ) {
      map[i] = &(assumptions[pointat]);
      pointat++;
    } else {
      map[i] = &fakeentry;
    }
  }

  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, satresults = 0, unsatresults = 0;
  for(int i = 2; i <= X; ++ i)
  {
    // modify last bits of assumptions
    int mask = call;
    int index = 1;
    bool expectUNSAT = false;
    while(mask > 0 && index <= maxvar) {
      if((mask & 1)==1) {
        *(map[maxvar - index]) = -(*(map[maxvar - index]));
        if(map[maxvar - index] != &fakeentry && !expectUNSAT) {
          expectUNSAT = true;
        }
      }
      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) {
      ++ unsatresults;
      solver.addClause(conflict);
    } else {
      ++ satresults;
    }

    if(expectUNSAT && ret != 20 && 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;
    } else if (!expectUNSAT && ret != 10) {
      std::cout << "error: in call " << call << ", did not detect model (but " << ret << ") on satisfiable input, abort" << std::endl;
      std::cout << "used assumptions: " << assumptions << std::endl;
      return 1;
    }

    // modify last bits of assumptions back to negative
    mask = call;
    index = 1;
    while(mask > 0 && index <= maxvar) {
      if(*(map[maxvar - index]) > 0) {
        *(map[maxvar - index]) = -(*(map[maxvar - index]));
      }
      mask = mask >> 1;
      index ++;
    }
  }
  runtime = cpuTime() - runtime;
  std::cout << "performed " << (double)X / runtime << " calls per second, " 
            << satresults << " SAT, " << unsatresults << " UNSAT"
            << std::endl;
  
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movq 0x14bbb(%rip), %r15     # 0x16fc8
leaq 0xdbfc(%rip), %rsi      # 0x10010
movq %r15, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
leaq 0xdc1a(%rip), %rsi      # 0x10045
movq %r15, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
leaq 0xdc31(%rip), %rsi      # 0x10073
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
leaq 0xdc66(%rip), %rsi      # 0x100bf
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
cmpl $0x2, %r14d
jg 0x2499
movq 0x14b52(%rip), %rdi     # 0x16fc8
leaq 0xdc68(%rip), %rsi      # 0x100e5
callq 0x2150
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x2150
leaq 0xdc59(%rip), %rsi      # 0x100ed
jmp 0x2535
movq 0x8(%rbx), %rdi
callq 0x2240
movl %eax, %r12d
movq 0x10(%rbx), %rdi
callq 0x2240
movl %eax, %r14d
movq 0x18(%rbx), %rdi
callq 0x2240
cmpl $0x2, %r12d
jg 0x24db
movq 0x14b01(%rip), %rdi     # 0x16fc8
leaq 0xdc4f(%rip), %rsi      # 0x1011d
callq 0x2150
movq %rax, %rdi
movl %r12d, %esi
jmp 0x2529
movl %eax, %r15d
movl %r12d, -0x3c(%rbp)
movl %r14d, %r12d
leal -0xa(%r12), %eax
cmpl $-0xa, %eax
ja 0x250a
movq 0x14ad2(%rip), %rdi     # 0x16fc8
leaq 0xdc4d(%rip), %rsi      # 0x1014a
callq 0x2150
movq %rax, %rdi
movl %r12d, %esi
jmp 0x2529
cmpl $0x1, %r15d
jg 0x255b
movq 0x14ab1(%rip), %rdi     # 0x16fc8
leaq 0xdc48(%rip), %rsi      # 0x10166
callq 0x2150
movq %rax, %rdi
movl %r15d, %esi
callq 0x2290
leaq 0xdc03(%rip), %rsi      # 0x10138
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
pushq $0x1
popq %r14
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %r13
leaq -0x68(%rbp), %rdi
callq 0x2bca
movl %r13d, %eax
movl %r12d, %ecx
shll %cl, %eax
movl %eax, -0x30(%rbp)
leaq -0xa0(%rbp), %rdx
andl $0x0, (%rdx)
leaq -0xc8(%rbp), %rdi
leaq -0xe0(%rbp), %rcx
movq %r12, %rsi
callq 0x2c4e
movl %r12d, %ebx
negl %ebx
movq $0x0, -0x38(%rbp)
xorl %r14d, %r14d
cmpl -0x30(%rbp), %r13d
jae 0x266e
xorl %edi, %edi
movq -0xc8(%rbp), %rcx
pushq $-0x1
popq %rdx
movl %r13d, %esi
xorl %eax, %eax
cmpq %rdi, %r12
je 0x25ed
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 0x25be
movl %r13d, -0x50(%rbp)
movq -0xc0(%rbp), %rcx
subq -0xc8(%rbp), %rcx
shrq $0x2, %rcx
cmpl %ecx, %eax
jge 0x2b57
movq -0x38(%rbp), %rax
addl %r15d, %eax
movq %rax, -0x38(%rbp)
xorl %r13d, %r13d
cmpl %r15d, %r13d
je 0x2662
testl %r13d, %r13d
je 0x264d
movq -0xc8(%rbp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x264d
movl (%rax,%rcx,4), %edx
testl %edx, %edx
movl %ebx, %esi
cmovgl %r12d, %esi
addl %edx, %esi
movl %esi, (%rax,%rcx,4)
cmpl %esi, %r14d
cmovlel %esi, %r14d
incq %rcx
jmp 0x262c
leaq -0x68(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x2ca4
incl %r13d
jmp 0x2619
movl -0x50(%rbp), %r13d
incl %r13d
jmp 0x25a3
movq 0x14953(%rip), %rdi     # 0x16fc8
leaq 0xdb8c(%rip), %rsi      # 0x10208
callq 0x2150
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x2290
leaq 0xdb8a(%rip), %rsi      # 0x1021e
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
movl %r14d, %esi
callq 0x2290
movq %rax, %rdi
callq 0x2090
leaq -0xa0(%rbp), %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
andq $0x0, -0xd0(%rbp)
movapd %xmm0, (%rax)
andq $0x0, -0x70(%rbp)
movapd %xmm0, -0xe0(%rbp)
movapd %xmm0, -0x80(%rbp)
decl -0x30(%rbp)
xorl %eax, %eax
testl %r14d, %r14d
cmovgl %r14d, %eax
movl %eax, -0x38(%rbp)
movl %eax, %r12d
negl %r12d
pushq $0x1
popq %r13
pushq $-0x1
popq %rbx
leaq -0x48(%rbp), %r15
leal (%r12,%r13), %eax
cmpl $0x1, %eax
je 0x2725
testl %r13d, -0x30(%rbp)
je 0x271e
movl %ebx, -0x48(%rbp)
leaq -0xa0(%rbp), %rdi
movq %r15, %rsi
callq 0x321a
incl %r13d
decl %ebx
jmp 0x26fd
movq %rsp, %r13
movl %r14d, %r12d
movq %rsp, %r14
leaq 0x17(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
andl $0x0, -0x48(%rbp)
movq -0xa0(%rbp), %rax
movl -0x38(%rbp), %ecx
negq %rcx
xorl %edx, %edx
pushq $0x1
popq %rsi
leaq (%rcx,%rsi), %rdi
cmpq $0x1, %rdi
je 0x277f
xorl %edi, %edi
testl %esi, -0x30(%rbp)
movl %edx, %r8d
leaq (%rax,%r8,4), %r8
cmoveq %r15, %r8
setne %dil
addl %edi, %edx
movq %r8, (%r14,%rsi,8)
incq %rsi
jmp 0x2756
leaq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0x2d06
movl %eax, %ebx
movq 0x14825(%rip), %rdi     # 0x16fc8
leaq 0xda8a(%rip), %rsi      # 0x10234
callq 0x2150
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
movq %rax, %rdi
callq 0x2090
cmpl $0xa, %ebx
jne 0x2987
movq %r13, -0x58(%rbp)
callq 0x2e9a
movsd %xmm0, -0xa8(%rbp)
movl -0x3c(%rbp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xb0(%rbp)
decl %eax
movl %eax, -0x3c(%rbp)
leaq (%r14,%r12,8), %rax
addq $-0x8, %rax
movq %rax, -0x50(%rbp)
pushq $0x2
popq %rax
movl %eax, -0x38(%rbp)
xorl %eax, %eax
pushq $0x1
popq %r14
movl $0x0, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
cmpl -0x3c(%rbp), %eax
je 0x29c4
movq %rax, %rdx
xorl %r13d, %r13d
movq -0x50(%rbp), %rax
pushq $0x1
popq %rcx
movq %rdx, -0xe8(%rbp)
testl %edx, %edx
je 0x2861
cmpq %r12, %rcx
ja 0x2861
testb $0x1, %dl
je 0x2856
movq (%rax), %rsi
negl (%rsi)
cmpq %r15, %rsi
je 0x2856
testb $0x1, %r13b
movzbl %r13b, %r13d
cmovel %r14d, %r13d
shrl %edx
incq %rcx
addq $-0x8, %rax
jmp 0x2832
movq -0x80(%rbp), %rax
cmpq %rax, -0x78(%rbp)
je 0x286f
movq %rax, -0x78(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0x2d06
movl %eax, -0x30(%rbp)
cmpb $0x0, -0x38(%rbp)
jne 0x28ff
movq 0x1472e(%rip), %rdi     # 0x16fc8
leaq 0xd9df(%rip), %rsi      # 0x10280
callq 0x2150
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x2290
movq %rax, %rdi
leaq 0xd9ce(%rip), %rsi      # 0x10289
callq 0x2150
movq %rax, %rbx
callq 0x2e9a
subsd -0xa8(%rbp), %xmm0
movsd -0xb0(%rbp), %xmm1
divsd %xmm0, %xmm1
movq %rbx, %rdi
movapd %xmm1, %xmm0
callq 0x2250
movq %rax, %rdi
leaq 0xd9a0(%rip), %rsi      # 0x10292
callq 0x2150
movq %rax, %rdi
callq 0x2090
cmpl $0x14, -0x30(%rbp)
jne 0x2917
leaq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2ca4
incl -0x40(%rbp)
jmp 0x291a
incl -0x44(%rbp)
movq -0xe8(%rbp), %rcx
leal 0x1(%rcx), %ebx
movl -0x30(%rbp), %edx
cmpl $0x14, %edx
setne %al
testl %ecx, %ecx
setne %cl
andb %r13b, %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x2a59
xorb $0x1, %r13b
cmpl $0xa, %edx
setne %al
testb %r13b, %al
jne 0x2a9d
movq -0x50(%rbp), %rax
pushq $0x1
popq %rcx
movl %ebx, %edx
testl %edx, %edx
je 0x297d
cmpq %r12, %rcx
ja 0x297d
movq (%rax), %rsi
movl (%rsi), %edi
testl %edi, %edi
jle 0x2972
negl %edi
movl %edi, (%rsi)
shrl %edx
incq %rcx
addq $-0x8, %rax
jmp 0x295c
incl -0x38(%rbp)
movl %ebx, %eax
jmp 0x2815
movq 0x1463a(%rip), %rdi     # 0x16fc8
leaq 0xd8bb(%rip), %rsi      # 0x10250
callq 0x2150
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
leaq 0xd98f(%rip), %rsi      # 0x1033a
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
pushq $0x1
popq %r14
jmp 0x2b19
callq 0x2e9a
movsd %xmm0, -0x30(%rbp)
movq 0x145f3(%rip), %rdi     # 0x16fc8
leaq 0xd966(%rip), %rsi      # 0x10342
callq 0x2150
movq -0x58(%rbp), %r13
movsd -0x30(%rbp), %xmm1
subsd -0xa8(%rbp), %xmm1
movsd -0xb0(%rbp), %xmm0
divsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x2250
leaq 0xd940(%rip), %rsi      # 0x1034d
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
movl -0x44(%rbp), %esi
callq 0x2290
leaq 0xd93a(%rip), %rsi      # 0x10361
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
movl -0x40(%rbp), %esi
callq 0x2290
leaq 0xd927(%rip), %rsi      # 0x10368
movq %rax, %rdi
callq 0x2150
xorl %r14d, %r14d
movq %rax, %rdi
callq 0x2090
jmp 0x2b19
movq 0x14568(%rip), %rdi     # 0x16fc8
leaq 0xd83d(%rip), %rsi      # 0x102a4
callq 0x2150
movq -0x58(%rbp), %r13
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
leaq 0xd833(%rip), %rsi      # 0x102b4
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x2290
leaq 0xd839(%rip), %rsi      # 0x102d4
jmp 0x2adf
movq 0x14524(%rip), %rdi     # 0x16fc8
leaq 0xd7f9(%rip), %rsi      # 0x102a4
callq 0x2150
movq -0x58(%rbp), %r13
movq %rax, %rdi
movl %ebx, %esi
callq 0x2290
leaq 0xd842(%rip), %rsi      # 0x10307
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
movl -0x30(%rbp), %esi
callq 0x2290
leaq 0xd845(%rip), %rsi      # 0x10324
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x2090
movq 0x144d2(%rip), %rdi     # 0x16fc8
leaq 0xd7f7(%rip), %rsi      # 0x102f4
callq 0x2150
leaq -0xa0(%rbp), %rsi
movq %rax, %rdi
callq 0x2ecf
movq %rax, %rdi
callq 0x2090
movq %r13, %rsp
leaq -0x80(%rbp), %rdi
callq 0x333c
leaq -0xe0(%rbp), %rdi
callq 0x333c
leaq -0xa0(%rbp), %rdi
callq 0x333c
leaq -0xc8(%rbp), %rdi
callq 0x333c
leaq -0x68(%rbp), %rdi
callq 0x2f26
jmp 0x2549
leaq 0xd623(%rip), %rdi      # 0x10181
leaq 0xd673(%rip), %rsi      # 0x101d8
leaq 0xd685(%rip), %rcx      # 0x101f1
pushq $0x47
popq %rdx
callq 0x20e0
movq %rax, %rbx
jmp 0x2bb8
jmp 0x2b83
jmp 0x2b83
jmp 0x2ba9
jmp 0x2b83
jmp 0x2b83
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x333c
leaq -0xe0(%rbp), %rdi
callq 0x333c
leaq -0xa0(%rbp), %rdi
callq 0x333c
jmp 0x2bac
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x333c
leaq -0x68(%rbp), %rdi
callq 0x2f26
movq %rbx, %rdi
callq 0x22a0
nop