// File: /workspace/github_cpp_makefile_star2_O2/canzarlab[P]Trajan/src/BnG.cpp
// Function: GenericBnBSolver::SolveNode(BnBNode*, double, double)
bool GenericBnBSolver::SolveNode(BnBNode* node, double pgtol, double numtol)
{
    Vector x = (node->warm) ? *(node->warm) : Vector::Zero(nr_cols);

#if DEBUG == 1    
    size_t debug_node_genocnt  = 0;
    double debug_node_genotime = 0;
    debug_log << "id: " << node->debug_nodeid << endl;
    debug_log << "parent: " << node->debug_parent << endl;    
    debug_log << "depth: " << node->debug_depth << endl;
    debug_log << "best: " << -sys_ub << endl;
#endif

    if (!OnNodeStart(node)) return false;

    while(1)
    {
        SpMat A(node->rows, node->cols);
        A.setFromTriplets(node->Triplets.begin(), node->Triplets.end());
        Vector b = Vector::Ones(node->rows);       
        Vector y = Vector::Zero(node->rows);        
        Vector d = -c;                    

        BranchingJRF simpleJRF(A, b, d, x, y);
        simpleJRF.lo = node->var_lb;
        simpleJRF.hi = node->var_ub;
        AugmentedLagrangian solver(simpleJRF, 15);
        solver.setParameter("verbose", false);
        solver.setParameter("pgtol", pgtol); 
        solver.setParameter("constraintsTol", 1e-4);

        if (!OnNodeLP(node))
        { 
#if DEBUG == 1    
            debug_log << "ABORT" << endl << endl;
#endif
            return false;
        }
#if DEBUG == 1    
        Timer debug_T; debug_T.start();        
#endif      
        SolverStatus status = solver.solve();    
#if DEBUG == 1    
        debug_T.stop();
        debug_genocnt++;    
        debug_node_genocnt++;
        debug_node_genotime += debug_T.secs();
        debug_genotime += debug_T.secs();
        debug_genomin = min(debug_genomin, (double)debug_T.secs());
        debug_genomax = max(debug_genomax, (double)debug_T.secs());
        if (status == SUBOPTIMAL)
            debug_log << "SUBOPTIMAL(" << status << ")" << endl; 
        if (status == UNBOUNDED)
            debug_log << "UNBOUNDED(" << status << ")" << endl; 
        if (status == INFEASIBLE)
            debug_log << "INFEASIBLE(" << status << ")" << endl; 
        if (status == NUM_ERROR)
            debug_log << "NUM_ERROR(" << status << ")" << endl; 
#endif
        
        if (status == INFEASIBLE || CheckUB(solver.f(), numtol))
        {
#if DEBUG == 1    
            debug_log << "geno calls: " << debug_node_genocnt << endl;
            debug_log << "geno time: " << debug_node_genotime << endl;
            debug_log << "CUT: " << -solver.f() << endl << endl;
#endif
            return false;
        } 

        x = Vector::ConstMapType(solver.x(), node->cols);     

        if ((LP::cf == 1 && Add<1>(node->Triplets, x, node->rows)) || (LP::cf == 2 && (Add<1>(node->Triplets, x, node->rows) + Add<2>(node->Triplets, x, node->rows))))
            continue;
        
        node->obj = solver.f();        
        node->sol = x;

        if (!OnNodeFinish(node, true))
        {
#if DEBUG == 1    
            debug_log << "geno calls: " << debug_node_genocnt << endl;
            debug_log << "geno time: " << debug_node_genotime << endl;
            debug_log << "GREEDY CUT: " << -solver.f() << endl << endl;
#endif
            return false;
        }

        break;
    }

#if DEBUG == 1    
    debug_log << "geno calls: " << debug_node_genocnt << endl;
    debug_log << "geno time: " << debug_node_genotime << endl;
    debug_log << node->rows << " x " << node->cols << endl;
    debug_log << "lp: " << -node->obj << endl;
    size_t debug_fracs = 0;
    for (size_t i = 0; i < x.size(); ++i)
        debug_fracs += IsVarFrac(node->sol(i)) && !node->IsVarFixed(i);
    debug_log << "fracs: " << debug_fracs << endl << endl;
#endif

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x48(%rsp)
movq %rsi, %rbx
movq %rdi, %r12
movq 0x48(%rsi), %rsi
testq %rsi, %rsi
je 0x3d47a
leaq 0x10(%rsp), %rdi
callq 0xe746
jmp 0x3d49f
movslq 0x10c(%r12), %rsi
leaq 0x158(%rsp), %r15
movq %r15, %rdi
callq 0xced2
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x34154
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *0xa0(%rax)
testb %al, %al
je 0x3d7fe
leaq 0x18(%rbx), %rax
movq %rax, 0x8(%rsp)
movq %r12, (%rsp)
leaq 0xf8(%r12), %rax
movq %rax, 0x40(%rsp)
leaq 0x28(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x38(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x50(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x1c0(%rsp), %r13
leaq 0x158(%rsp), %r12
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq %r13, %rdi
callq 0x3331e
movq (%rbx), %rax
movq %rax, 0x158(%rsp)
movq 0x8(%rbx), %rax
movq %rax, 0xf8(%rsp)
movq %r13, %rdi
movq %r12, %rsi
leaq 0xf8(%rsp), %r15
movq %r15, %rdx
callq 0x33360
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0x333bc
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x34154
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0xced2
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x34154
movq 0x40(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x60(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x35f66
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r14, %rcx
leaq 0x10(%rsp), %r8
leaq 0x50(%rsp), %r9
callq 0x3c04e
leaq 0x148(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0xe787
leaq 0x138(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0xe787
movq %r12, %rdi
movq %r15, %rsi
pushq $0xf
popq %rdx
callq 0xc424
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
leaq 0x13e48(%rip), %rsi     # 0x5143a
leaq 0x80(%rsp), %rdx
callq 0xb7a6
xorps %xmm0, %xmm0
movq %r12, %rdi
movq %r14, %rsi
callq 0xcdfc
movq %r14, %rdi
callq 0x9708
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
leaq 0x13df1(%rip), %rsi     # 0x51418
leaq 0x80(%rsp), %rdx
callq 0xb7a6
movq %r12, %rdi
movq %r14, %rsi
movsd 0x48(%rsp), %xmm0
callq 0xcdfc
movq %r14, %rdi
callq 0x9708
leaq 0x98(%rsp), %r14
movq %r14, %rdi
leaq 0x13dcc(%rip), %rsi     # 0x5142b
leaq 0x80(%rsp), %rdx
callq 0xb7a6
movq %r12, %rdi
movq %r14, %rsi
movsd 0x309ce(%rip), %xmm0    # 0x6e048
callq 0xcdfc
movq %r14, %rdi
callq 0x9708
movq (%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
callq *0xa8(%rax)
pushq $0x1
popq %rbp
testb %al, %al
je 0x3d7b1
movq 0x8(%rsp), %r15
movq %r12, %rdi
callq 0xc4b2
pushq $0x1
popq %rbp
cmpl $0x3, %eax
je 0x3d7b1
movq %r12, %rdi
callq 0xcdf6
movq (%rsp), %rdi
movq (%rdi), %rax
movsd 0x28(%rsp), %xmm1
callq *0x68(%rax)
pushq $0x1
popq %rbp
testb %al, %al
jne 0x3d7b1
movq %r12, %rdi
callq 0xcdea
movq 0x20(%rbx), %rdx
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0xebcc
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0xebf9
leaq 0x59cdf(%rip), %rax     # 0x973f0
movl (%rax), %eax
cmpl $0x1, %eax
jne 0x3d73e
movq (%rsp), %rdi
movq %rbx, %rsi
leaq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
callq 0x3f862
pushq $0x2
popq %rbp
testl %eax, %eax
jne 0x3d7b1
leaq 0x59cb4(%rip), %rax     # 0x973f0
movl (%rax), %eax
cmpl $0x2, %eax
jne 0x3d778
movq (%rsp), %rdi
movq %rbx, %rsi
leaq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
callq 0x3f862
movl %eax, %r14d
movq (%rsp), %rdi
movq %rbx, %rsi
leaq 0x10(%rsp), %rdx
movq %r15, %rcx
callq 0x3f4e8
pushq $0x2
popq %rbp
addl %eax, %r14d
jne 0x3d7b1
movq %r12, %rdi
callq 0xcdf6
movsd %xmm0, 0x60(%rbx)
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe787
movq (%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq *0xb0(%rax)
movzbl %al, %eax
leal 0x1(,%rax,2), %ebp
movq %r12, %rdi
callq 0x333e4
leaq 0xf8(%rsp), %rdi
callq 0x3c0f4
movq 0x60(%rsp), %rdi
callq 0x93c0
movq 0x50(%rsp), %rdi
callq 0x93c0
movq 0x70(%rsp), %rdi
callq 0x93c0
movq %r13, %rdi
callq 0x33400
cmpl $0x2, %ebp
je 0x3d4fc
cmpl $0x3, %ebp
jne 0x3d7fe
movb $0x1, %bl
jmp 0x3d800
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
callq 0x93c0
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3d82d
jmp 0x3d871
jmp 0x3d871
jmp 0x3d871
jmp 0x3d883
movq %rax, %rbx
jmp 0x3d89d
movq %rax, %rbx
jmp 0x3d8be
jmp 0x3d871
jmp 0x3d871
jmp 0x3d871
movq %rax, %rbx
jmp 0x3d893
jmp 0x3d8d0
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0x3d86a
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x3d86a
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x9708
jmp 0x3d874
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x333e4
jmp 0x3d886
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x3c0f4
movq 0x60(%rsp), %rdi
callq 0x93c0
movq 0x50(%rsp), %rdi
callq 0x93c0
movq 0x70(%rsp), %rdi
callq 0x93c0
leaq 0x1c0(%rsp), %rdi
callq 0x33400
movq 0x10(%rsp), %rdi
callq 0x93c0
movq %rbx, %rdi
callq 0x9640
movq %rax, %rbx
jmp 0x3d8b1
movq %rax, %rbx
jmp 0x3d8a7