// File: /workspace/github_cpp_makefile_star2_O2/cauimsouza[P]rikudo-solver/src/rikudo_solver/graph.cpp
// Function: Graph::unique_sol(int, int, std::basic_ofstream<char, std::char_traits<char>>&)
void Graph::unique_sol(int first, int last, std::ofstream &ofile){
    construct_sat(first, last);
    solve_sat();
    auto orig_path = read_sol();

    if(orig_path.empty()){
        write_min_cons(orig_path, orig_path, -1, ofile);
        return;
    }

    std::string ban = create_ban(orig_path);
    int n_vars, n_clauses;
    extend_sat(ban, n_vars, n_clauses);

    recopy(n_vars, n_clauses);
    solve_sat();
    auto extra_path = read_sol();
    if(extra_path.empty()){
        write_min_cons(orig_path, extra_path, -1, ofile);
        return;
    }
    
    std::vector<int> cons = create_cons(n_vertices);

    
    int lo = 0; // adding until lo-1 constraints will always produce solution
    int hi = cons.size() - 1; // adding hi or more will not produce more solutions
    while(lo < hi){
        int mid = lo + (hi-lo)/2;

        recopy(n_vars, n_clauses + mid + 1);
        add_cons(orig_path, cons, mid);
        solve_sat();
        auto extra_path = read_sol();  

        if(extra_path.empty()){
            hi = mid;
        }
        else{
            lo = mid + 1;
        }
    }

    // in the end we have lo == hi
    write_min_cons(orig_path, cons, lo, ofile);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r14
movq %rdi, %r15
xorps %xmm0, %xmm0
leaq -0xb0(%rbp), %rcx
movaps %xmm0, (%rcx)
andq $0x0, 0x10(%rcx)
leaq -0x50(%rbp), %r8
movaps %xmm0, (%r8)
andq $0x0, 0x10(%r8)
callq 0x4198
leaq -0x50(%rbp), %rdi
callq 0x3172
leaq -0xb0(%rbp), %rdi
callq 0x3172
callq 0x48aa
movq %r15, %rdi
callq 0x454e
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2f56
movq (%rbx), %rax
cmpq 0x8(%rbx), %rax
je 0x57ed
leaq -0xb0(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movq %r15, %rsi
callq 0x4bc6
leaq -0xe8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2270
leaq -0xe8(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
callq 0x4cc6
leaq -0xe8(%rbp), %rdi
callq 0x20e0
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x4f70
callq 0x48aa
movq %r15, %rdi
callq 0x454e
leaq -0x90(%rbp), %rdi
movq %rax, %rsi
callq 0x2f56
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
je 0x5801
movq %r14, -0x60(%rbp)
movq %r15, -0x58(%rbp)
movl (%r15), %edx
leaq -0x78(%rbp), %rdi
callq 0x5a06
leaq -0x78(%rbp), %rax
movq 0x8(%rax), %r13
subq (%rax), %r13
shrq $0x2, %r13
decl %r13d
xorl %r12d, %r12d
leaq -0xc8(%rbp), %rbx
movl %r13d, %eax
subl %r12d, %eax
jle 0x57ce
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %eax, %r15d
movl -0x30(%rbp), %esi
leal (%r15,%r12), %r14d
incl %r14d
movl -0x2c(%rbp), %edx
addl %r14d, %edx
callq 0x4f70
addl %r12d, %r15d
movq -0x58(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x78(%rbp), %rdx
movl %r15d, %ecx
callq 0x5b08
callq 0x48aa
movq -0x58(%rbp), %rdi
callq 0x454e
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2f56
movq -0xc8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
cmovel %r12d, %r14d
cmovel %r15d, %r13d
movq %rbx, %rdi
callq 0x2e04
movl %r14d, %r12d
jmp 0x5755
leaq -0x50(%rbp), %rsi
leaq -0x78(%rbp), %rdx
movl %r12d, %ecx
movq -0x60(%rbp), %r8
callq 0x58c4
leaq -0x78(%rbp), %rdi
callq 0x2e04
jmp 0x5817
leaq -0x50(%rbp), %rdx
pushq $-0x1
popq %rcx
movq %rdx, %rsi
movq %r14, %r8
callq 0x58c4
jmp 0x582f
leaq -0x50(%rbp), %rsi
leaq -0x90(%rbp), %rdx
pushq $-0x1
popq %rcx
movq %r14, %r8
callq 0x58c4
leaq -0x90(%rbp), %rdi
callq 0x2e04
leaq -0xb0(%rbp), %rdi
callq 0x20e0
leaq -0x50(%rbp), %rdi
callq 0x2e04
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5850
jmp 0x5866
jmp 0x588e
movq %rax, %rbx
jmp 0x589a
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x20e0
jmp 0x58a6
movq %rax, %rbx
jmp 0x58b2
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x3172
leaq -0xb0(%rbp), %rdi
callq 0x3172
jmp 0x58bb
jmp 0x5887
movq %rax, %rbx
jmp 0x58a6
jmp 0x588e
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x2e04
leaq -0x90(%rbp), %rdi
callq 0x2e04
leaq -0xb0(%rbp), %rdi
callq 0x20e0
leaq -0x50(%rbp), %rdi
callq 0x2e04
movq %rbx, %rdi
callq 0x2360
nop