// File: /workspace/github_cpp_makefile_star2_O2/canzarlab[P]Trajan/src/BnG.cpp
// Function: GenericBnBSolver::Solve(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void GenericBnBSolver::Solve(string filename, string outScoreFile)
{
    this->filename = filename;
    
#if DEBUG == 1    
    if (debug_file == "")
        debug_log = ofstream(filename + ".log");
    else
        debug_log = ofstream(debug_file + ".log");
    debug_nodecnt  = 0;
    debug_genocnt  = 0;
    debug_genotime = 0;
    debug_genomin  = 1e20;
    debug_genomax  = 0;
#endif

#if DEBUG == 1    
    Timer T; T.start();
#endif

    sys_sol.conservativeResizeLike(Vector::Zero(nr_cols));
    OnSolverInit();

    PushNode(InitNodeFrom(nullptr));
    while (!OpenEmpty())
    {
        for (auto& it : EvalOpen())
        {
            vector<BnBNode*>* V = EvalBranch(it);
#if DEBUG == 1
            if (V != nullptr)
                debug_log << "fixed: " << V->front()->debug_varid << " (" << V->front()->debug_varval << " <- " << it->sol(V->front()->debug_varid) << ") in node " << it->debug_nodeid << endl << endl;
#endif

            if (V != nullptr)
                PushAll(V);
            else if (sys_ub > it->obj)
            {
                sys_sol = it->sol;
                sys_ub  = it->obj;         
#if DEBUG == 1    
                debug_log << "BOUND " << sys_ub << " (" << it->debug_nodeid << ")" << endl << endl;
#endif            
                OnSolverUpdate();
            }
            delete it;
            
        }         
    }
#if DEBUG == 1    
    T.stop();
#endif

    if (OnSolverFinish())
    {
        x = sys_sol;
        for (size_t i = 0; i < x.size(); ++i)
            x(i) = round(x(i));    
        if (filename != "") 
            WriteSolution(filename);     
        // write the objective function 
        ofstream score(outScoreFile);
        score << moptVal << endl;
        cout << "my score: "  << moptVal << endl;
        score.close();
    }

#if DEBUG == 1    
    debug_log << "total time: " << T.secs() << endl;
    debug_log << "total nodes: " << debug_nodecnt << endl;
    debug_log << "total geno calls: " << debug_genocnt << endl;
    debug_log << "total geno time: " << debug_genotime << " (" << debug_genomin << ", " << debug_genotime / debug_genocnt << "," << debug_genomax << ")" << endl;
    debug_log << "best: " << -sys_ub << endl << endl;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rdx, (%rsp)
movq %rdi, %rbx
addq $0x130, %rdi            # imm = 0x130
movq %rsi, 0x8(%rsp)
callq 0x91c0
leaq 0x120(%rbx), %r12
movslq 0x10c(%rbx), %rsi
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0xced2
movq %r12, %rdi
movq %r14, %rsi
callq 0x3c2be
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x80(%rax)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3d364
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq *0x30(%rcx)
pushq $0x68
popq %rbp
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
testb %al, %al
jne 0x3d215
movq (%rbx), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x40(%rax)
movq 0x38(%rsp), %r15
movq 0x40(%rsp), %r13
cmpq %r13, %r15
je 0x3d203
movq (%r15), %rsi
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x48(%rax)
testq %rax, %rax
je 0x3d19c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3d3f8
jmp 0x3d1da
movsd 0x118(%rbx), %xmm0
movq (%r15), %r14
ucomisd 0x60(%r14), %xmm0
jbe 0x3d1e2
addq $0x50, %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0xe787
movq (%r15), %rax
movsd 0x60(%rax), %xmm0
movsd %xmm0, 0x118(%rbx)
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x88(%rax)
movq (%r15), %r14
testq %r14, %r14
je 0x3d1ec
movq %r14, %rdi
callq 0x3cf40
jmp 0x3d1ef
xorl %r14d, %r14d
movq %r14, %rdi
movq %rbp, %rsi
callq 0x9350
addq $0x8, %r15
jmp 0x3d175
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x3f7d6
jmp 0x3d14e
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x90(%rax)
testb %al, %al
je 0x3d31e
leaq 0xc0(%rbx), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xe787
xorl %r12d, %r12d
cmpq 0xc8(%rbx), %r12
jae 0x3d27b
movq %r13, %rdi
movq %r12, %rsi
callq 0xd27a
movsd (%rax), %xmm0
callq 0x9130
movsd %xmm0, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0xd27a
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
incq %r12
jmp 0x3d23e
leaq 0x2f718(%rip), %rsi     # 0x6c99a
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x312d5
testb %al, %al
je 0x3d2b9
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x9470
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x18(%rax)
leaq 0x18(%rsp), %rdi
callq 0x9708
leaq 0x38(%rsp), %r14
pushq $0x10
popq %rdx
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x96e0
movsd 0x20(%rbx), %xmm0
movq %r14, %rdi
callq 0x9540
movq %rax, %rdi
callq 0x9170
movq 0x59cc7(%rip), %rdi     # 0x96fb0
leaq 0x31100(%rip), %rsi     # 0x6e3f0
callq 0x9330
movsd 0x20(%rbx), %xmm0
movq %rax, %rdi
callq 0x9540
movq %rax, %rdi
callq 0x9170
leaq 0x38(%rsp), %rdi
callq 0x94a0
leaq 0x38(%rsp), %rdi
callq 0x94f0
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x9708
jmp 0x3d35b
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x94f0
jmp 0x3d35b
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3f7d6
movq %rbx, %rdi
callq 0x9640
nop