// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rdi, %r14
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rcx
movaps %xmm0, (%rcx)
xorl %eax, %eax
movq %rax, 0x10(%rcx)
leaq -0x50(%rbp), %r8
movq %rax, 0x10(%r8)
movaps %xmm0, (%r8)
callq 0x4a1c
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x683c
callq 0x21d0
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x684d
callq 0x21d0
callq 0x52fe
movq %r14, %rdi
callq 0x4eae
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3364
movq (%rbx), %rax
cmpq 0x8(%rbx), %rax
je 0x69e9
leaq -0x90(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movq %r14, %rsi
callq 0x59f2
leaq -0xc0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %rdi
callq 0x3220
leaq -0xd0(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
callq 0x5c18
movq -0xd0(%rbp), %rdi
cmpq %r15, %rdi
je 0x68d6
callq 0x21d0
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x5fe6
callq 0x52fe
movq %r14, %rdi
callq 0x4eae
leaq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x3364
movq -0xb0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
je 0x69ff
movl (%r14), %edx
leaq -0x68(%rbp), %rdi
callq 0x6c7a
movq %r12, -0x98(%rbp)
movq -0x60(%rbp), %r13
subq -0x68(%rbp), %r13
shrq $0x2, %r13
decl %r13d
xorl %r15d, %r15d
testl %r13d, %r13d
jle 0x69c2
leaq -0xe8(%rbp), %rbx
movl %r13d, %eax
subl %r15d, %eax
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
addl %r15d, %r12d
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %eax
leal (%rax,%r12), %edx
incl %edx
callq 0x5fe6
movq %r14, %rdi
leaq -0x50(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movl %r12d, %ecx
callq 0x6dfe
callq 0x52fe
movq %r14, %rdi
callq 0x4eae
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3364
movq -0xe8(%rbp), %rdi
leal 0x1(%r12), %eax
cmpq -0xe0(%rbp), %rdi
cmovnel %eax, %r15d
cmovel %r12d, %r13d
testq %rdi, %rdi
je 0x69bd
callq 0x21d0
cmpl %r13d, %r15d
jl 0x6946
leaq -0x50(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movl %r15d, %ecx
movq -0x98(%rbp), %r8
callq 0x6ae8
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x6a17
callq 0x21d0
jmp 0x6a17
leaq -0x50(%rbp), %rdx
movq %rdx, %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %r12, %r8
callq 0x6ae8
jmp 0x6a3a
leaq -0x50(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %r12, %r8
callq 0x6ae8
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x6a28
callq 0x21d0
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6a3a
callq 0x21d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x6a48
callq 0x21d0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6a60
jmp 0x6a76
jmp 0x6a9e
movq %rax, %rbx
jmp 0x6aaf
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
cmpq %r15, %rdi
jne 0x6abb
jmp 0x6ac0
movq %rax, %rbx
jmp 0x6ad2
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x6a8c
callq 0x21d0
movq -0x90(%rbp), %rdi
jmp 0x6ad6
jmp 0x6a97
movq %rax, %rbx
jmp 0x6ac0
jmp 0x6a9e
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x6aaf
callq 0x21d0
movq -0xb0(%rbp), %rdi
testq %rdi, %rdi
je 0x6ac0
callq 0x21d0
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6ad2
callq 0x21d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x6ae0
callq 0x21d0
movq %rbx, %rdi
callq 0x2380