// File: /workspace/github_cpp_makefile_star2_O3/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 -0xb0(%rbp), %rcx
movaps %xmm0, (%rcx)
xorl %eax, %eax
movq %rax, 0x10(%rcx)
leaq -0x50(%rbp), %r8
movq %rax, 0x10(%r8)
movaps %xmm0, (%r8)
callq 0x4974
callq 0x5256
movq %r14, %rdi
callq 0x4e06
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x335e
movq (%rbx), %rax
cmpq 0x8(%rbx), %rax
je 0x68e9
leaq -0xb0(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movq %r14, %rsi
callq 0x5944
leaq -0xc0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %rdi
callq 0x3216
leaq -0xd0(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
callq 0x5b6a
movq -0xd0(%rbp), %rdi
cmpq %r15, %rdi
je 0x67e9
callq 0x21d0
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edx
callq 0x5f26
callq 0x5256
movq %r14, %rdi
callq 0x4e06
leaq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0x335e
movq -0x88(%rbp), %rax
cmpq -0x80(%rbp), %rax
je 0x68ff
movl (%r14), %edx
leaq -0x68(%rbp), %rdi
callq 0x6b7c
movq %r12, -0x70(%rbp)
movq -0x60(%rbp), %r13
subq -0x68(%rbp), %r13
shrq $0x2, %r13
decl %r13d
xorl %r15d, %r15d
testl %r13d, %r13d
jle 0x68c5
movl %r13d, %eax
subl %r15d, %eax
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
movl -0x30(%rbp), %esi
leal (%r12,%r15), %ebx
incl %ebx
movl -0x2c(%rbp), %edx
addl %ebx, %edx
callq 0x5f26
addl %r15d, %r12d
movq %r14, %rdi
leaq -0x50(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movl %r12d, %ecx
callq 0x6d04
callq 0x5256
movq %r14, %rdi
callq 0x4e06
leaq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0x335e
movq -0xe8(%rbp), %rdi
cmpq -0xe0(%rbp), %rdi
cmovnel %ebx, %r15d
cmovel %r12d, %r13d
testq %rdi, %rdi
je 0x68c0
callq 0x21d0
cmpl %r13d, %r15d
jl 0x6848
leaq -0x50(%rbp), %rsi
leaq -0x68(%rbp), %rdx
movl %r15d, %ecx
movq -0x70(%rbp), %r8
callq 0x69d4
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x6917
callq 0x21d0
jmp 0x6917
leaq -0x50(%rbp), %rdx
movq %rdx, %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %r12, %r8
callq 0x69d4
jmp 0x693d
leaq -0x50(%rbp), %rsi
leaq -0x88(%rbp), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %r12, %r8
callq 0x69d4
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x6928
callq 0x21d0
leaq -0xa0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x693d
callq 0x21d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x694b
callq 0x21d0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6963
jmp 0x6979
jmp 0x6987
movq %rax, %rbx
jmp 0x6998
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
cmpq %r15, %rdi
jne 0x69a4
jmp 0x69a9
movq %rax, %rbx
jmp 0x69be
jmp 0x6980
movq %rax, %rbx
jmp 0x69a9
jmp 0x6987
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x6998
callq 0x21d0
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x69a9
callq 0x21d0
leaq -0xa0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x69be
callq 0x21d0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x69cc
callq 0x21d0
movq %rbx, %rdi
callq 0x2380