// File: canzarlab[P]Trajan/src/BnG.cpp
// Function: HybridBnBSolver::EvalOpen()
vector<BnBNode*> HybridBnBSolver::EvalOpen()
{
    if (!GetObjective())
    {
        BnBNode* lt = Open.back(); Open.pop_back();

        if (Open.size() > 1)
        {
            BnBNode* rt = Open.back(); 
            if (lt->obj == 1 && !SolveNode(lt, PGTOL, NUMTOL)) lt->obj = 2;
            if (lt->warm == rt->warm) // TODO this is horrible.
            {
                Open.pop_back();
                if (rt->obj == 1 && !SolveNode(rt, PGTOL, NUMTOL)) rt->obj = 2;
                if (lt->obj < rt->obj) swap(lt, rt); // if (lt->obj > rt->obj) swap(lt, rt);
                if (rt->obj < 1) Open.push_back(rt);         
            }        
        }

        vector<BnBNode*> v;
        if (!(lt->obj == 2 || (lt->obj == 1 && !SolveNode(lt, PGTOL, NUMTOL))))
            v.push_back(lt);
        return v;
        //return (lt->obj == 2 || (lt->obj == 1 && !SolveNode(lt, PGTOL, NUMTOL))) ? nullptr : lt;
    }
    else
    {
        thread thr_obj[thr_no];    
        vector<BnBNode*> thr_vec[thr_no];
        size_t k = 0;    

        for (auto& it : Open)
            if (it->obj == 1) 
                thr_vec[k % thr_no].push_back(it), ++k;
        k = min(k, thr_no);

        for (size_t i = 0; i < k; ++i)
            thr_obj[i] = thread(&HybridBnBSolver::ThreadCallback, this, &thr_vec[i]); 

        for (size_t i = 0; i < k; ++i)
            thr_obj[i].join();

        sort(Open.begin(), Open.end(), [](BnBNode*& l, BnBNode*& r)
        { 
            return l->obj < r->obj; 
        });

        while (Open.size() && Open.back()->obj > 0)
            Open.pop_back();

        vector<BnBNode*> v; 
        for (size_t i = 0; Open.size() && i < thr_no; ++i)
            v.push_back(Open.back()), Open.pop_back();

        return v;
        //return (node->obj < 1) ? node : nullptr;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0xe8(%rbp)
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0xd9610
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xd5830
jp 0xd5830
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd9560
movq -0xd8(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x18(%rbp)
addq $0x160, %rdi            # imm = 0x160
callq 0xd95a0
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd9410
cmpq $0x1, %rax
jbe 0xd5765
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd9560
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movsd 0x60(%rax), %xmm0
movsd 0x3860c(%rip), %xmm1    # 0x10dc68
ucomisd %xmm1, %xmm0
jne 0xd569a
jp 0xd569a
movq -0xd8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq (%rdi), %rax
movsd 0x38606(%rip), %xmm0    # 0x10dc80
movsd 0x567c6(%rip), %xmm1    # 0x12be48
callq *0x28(%rax)
testb $0x1, %al
jne 0xd569a
movq -0x18(%rbp), %rax
movsd 0x35d13(%rip), %xmm0    # 0x10b3a8
movsd %xmm0, 0x60(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jne 0xd5763
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd95a0
movq -0x20(%rbp), %rax
movsd 0x60(%rax), %xmm0
movsd 0x38594(%rip), %xmm1    # 0x10dc68
ucomisd %xmm1, %xmm0
jne 0xd5712
jp 0xd5712
movq -0xd8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq (%rdi), %rax
movsd 0x3858e(%rip), %xmm0    # 0x10dc80
movsd 0x5674e(%rip), %xmm1    # 0x12be48
callq *0x28(%rax)
testb $0x1, %al
jne 0xd5712
movq -0x20(%rbp), %rax
movsd 0x35c9b(%rip), %xmm0    # 0x10b3a8
movsd %xmm0, 0x60(%rax)
movq -0x18(%rbp), %rax
movsd 0x60(%rax), %xmm1
movq -0x20(%rbp), %rax
movsd 0x60(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd5737
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0xd95d0
movq -0x20(%rbp), %rax
movsd 0x38525(%rip), %xmm0    # 0x10dc68
ucomisd 0x60(%rax), %xmm0
jbe 0xd5761
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
leaq -0x20(%rbp), %rsi
callq 0xd93a0
jmp 0xd5763
jmp 0xd5765
movq -0xe8(%rbp), %rdi
movb $0x0, -0x21(%rbp)
callq 0xd9350
movq -0x18(%rbp), %rax
movsd 0x60(%rax), %xmm0
movsd 0x35c22(%rip), %xmm1    # 0x10b3a8
ucomisd %xmm1, %xmm0
jne 0xd5793
jp 0xd5793
jmp 0xd5815
movq -0x18(%rbp), %rax
movsd 0x60(%rax), %xmm0
movsd 0x384c4(%rip), %xmm1    # 0x10dc68
ucomisd %xmm1, %xmm0
jne 0xd57e4
jp 0xd57e4
movq -0xd8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq (%rdi), %rax
movq 0x28(%rax), %rax
movsd 0x384ba(%rip), %xmm0    # 0x10dc80
movsd 0x5667a(%rip), %xmm1    # 0x12be48
callq *%rax
movb %al, -0xe9(%rbp)
jmp 0xd57d8
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0xd57e4
jmp 0xd5815
movq -0xe8(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0xd93a0
jmp 0xd57f6
jmp 0xd5815
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0xd9250
jmp 0xd5e7c
movb $0x1, -0x21(%rbp)
testb $0x1, -0x21(%rbp)
jne 0xd582b
movq -0xe8(%rbp), %rdi
callq 0xd9250
jmp 0xd5e70
movq -0xd8(%rbp), %rax
movq 0x150(%rax), %rax
movq %rax, -0x100(%rbp)
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf8(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0xd58c9
movq -0xf8(%rbp), %rax
movq -0x100(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x110(%rbp)
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0xd9430
movq -0x118(%rbp), %rax
movq -0x110(%rbp), %rcx
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x108(%rbp)
jne 0xd5898
movq -0xd8(%rbp), %rax
movq 0x150(%rax), %rax
movq %rax, -0x128(%rbp)
leaq (%rax,%rax,2), %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x120(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0xd595f
movq -0x120(%rbp), %rax
movq -0x128(%rbp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x138(%rbp)
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0xd9350
movq -0x140(%rbp), %rax
movq -0x138(%rbp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0x130(%rbp)
jne 0xd592e
movq -0xd8(%rbp), %rax
movq $0x0, -0x58(%rbp)
addq $0x160, %rax            # imm = 0x160
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0xd9170
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
callq 0xd91a0
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xd91d0
testb $0x1, %al
jne 0xd59a8
jmp 0xd5a31
leaq -0x68(%rbp), %rdi
callq 0xd9210
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq (%rax), %rax
movsd 0x60(%rax), %xmm0
movsd 0x3829f(%rip), %xmm1    # 0x10dc68
ucomisd %xmm1, %xmm0
jne 0xd5a21
jp 0xd5a21
movq -0xd8(%rbp), %rcx
movq -0x58(%rbp), %rax
movq 0x150(%rcx), %rcx
xorl %edx, %edx
divq %rcx
movq -0x120(%rbp), %rax
leaq (%rdx,%rdx,2), %rcx
leaq (%rax,%rcx,8), %rdi
movq -0x78(%rbp), %rsi
callq 0xd93a0
jmp 0xd5a02
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xd5a21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0xd5d88
jmp 0xd5a23
leaq -0x68(%rbp), %rdi
callq 0xd9230
jmp 0xd5992
movq -0xd8(%rbp), %rsi
addq $0x150, %rsi            # imm = 0x150
leaq -0x58(%rbp), %rdi
callq 0x156f0
movq %rax, -0x148(%rbp)
jmp 0xd5a51
movq -0x148(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0xd5b14
movq -0x120(%rbp), %rax
movq -0xd8(%rbp), %rcx
leaq -0x59a(%rip), %rdx      # 0xd54f0
movq %rdx, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movq %rcx, -0xa0(%rbp)
movq -0x80(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
leaq -0xa8(%rbp), %rcx
callq 0xd9630
jmp 0xd5ad9
movq -0xf8(%rbp), %rdi
movq -0x80(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0x88(%rbp), %rsi
callq 0xd9520
leaq -0x88(%rbp), %rdi
callq 0xbd7d0
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xd5a67
movq $0x0, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0xd5b5b
movq -0xf8(%rbp), %rax
movq -0xb0(%rbp), %rcx
leaq (%rax,%rcx,8), %rdi
callq 0xa770
jmp 0xd5b45
jmp 0xd5b47
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0xd5b1f
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
movq %rdi, -0x150(%rbp)
callq 0xd9170
movq -0x150(%rbp), %rdi
movq %rax, -0xb8(%rbp)
callq 0xd91a0
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0xd5e90
jmp 0xd5ba4
jmp 0xd5ba6
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd9410
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0x151(%rbp)
je 0xd5bf5
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd9560
movq (%rax), %rax
movsd 0x60(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %al
movb %al, -0x151(%rbp)
movb -0x151(%rbp), %al
testb $0x1, %al
jne 0xd5c01
jmp 0xd5c16
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd95a0
jmp 0xd5ba6
movq -0xe8(%rbp), %rdi
movb $0x0, -0xc2(%rbp)
callq 0xd9350
movq $0x0, -0xd0(%rbp)
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd9410
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0x152(%rbp)
je 0xd5c76
movq -0xd8(%rbp), %rcx
movq -0xd0(%rbp), %rax
cmpq 0x150(%rcx), %rax
setb %al
movb %al, -0x152(%rbp)
movb -0x152(%rbp), %al
testb $0x1, %al
jne 0xd5c82
jmp 0xd5ced
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd9560
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0xd93a0
jmp 0xd5ca6
movq -0xd8(%rbp), %rdi
addq $0x160, %rdi            # imm = 0x160
callq 0xd95a0
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0xd5c34
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0xd9250
jmp 0xd5d88
movb $0x1, -0xc2(%rbp)
testb $0x1, -0xc2(%rbp)
jne 0xd5d09
movq -0xe8(%rbp), %rdi
callq 0xd9250
movq -0x120(%rbp), %rcx
movq -0x128(%rbp), %rax
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x160(%rbp)
je 0xd5d5e
movq -0x160(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x168(%rbp)
callq 0xd9250
movq -0x120(%rbp), %rcx
movq -0x168(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x160(%rbp)
jne 0xd5d2d
movq -0xf8(%rbp), %rcx
movq -0x100(%rbp), %rdx
shlq $0x3, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x170(%rbp)
je 0xd5e34
jmp 0xd5e03
movq -0x120(%rbp), %rcx
movq -0x128(%rbp), %rax
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x178(%rbp)
je 0xd5ddd
movq -0x178(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x180(%rbp)
callq 0xd9250
movq -0x120(%rbp), %rcx
movq -0x180(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x178(%rbp)
jne 0xd5dac
movq -0xf8(%rbp), %rcx
movq -0x100(%rbp), %rdx
shlq $0x3, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x188(%rbp)
je 0xd5e6e
jmp 0xd5e3d
movq -0x170(%rbp), %rdi
addq $-0x8, %rdi
movq %rdi, -0x190(%rbp)
callq 0xbd7d0
movq -0xf8(%rbp), %rcx
movq -0x190(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x170(%rbp)
jne 0xd5e03
movq -0x40(%rbp), %rax
movq %rax, %rsp
jmp 0xd5e70
movq -0x188(%rbp), %rdi
addq $-0x8, %rdi
movq %rdi, -0x198(%rbp)
callq 0xbd7d0
movq -0xf8(%rbp), %rcx
movq -0x198(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x188(%rbp)
jne 0xd5e3d
jmp 0xd5e7c
movq -0xe0(%rbp), %rax
movq %rbp, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0xa7c0
nopw %cs:(%rax,%rax)