// File: /workspace/github_cpp_makefile_star2_O2/canzarlab[P]Trajan/src/BnG.cpp
// Function: BFBnBSolver::EvalOpen()
vector<BnBNode*> BFBnBSolver::EvalOpen()
{
    /*
      1. Solve nodes in open with up to thr_no threads.
      2. When all threads are finished pick the best node and return.

      Old code for reference:

      for (auto& it : Open)
      if (it->obj == 1 && !SolveNode(it, PGTOL, NUMTOL))
      it->obj = 2;

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

      BnBNode* node = Open.back(); Open.pop_back();        
      return (node->obj < 1) ? node : nullptr;
    */
    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(&BFBnBSolver::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;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movq 0x150(%rsi), %r13
movq %rsp, %rdi
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
testq %r13, %r13
movq %rdi, -0x48(%rbp)
je 0x3debb
leaq (,%r13,8), %rdx
xorl %esi, %esi
callq 0x91d0
movq %rsp, %rdi
imulq $0x18, %r13, %rcx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
leaq -0x18(%rcx), %rax
pushq $0x18
popq %rsi
xorl %edx, %edx
divq %rsi
negq %rdx
addq %rcx, %rdx
addq $-0x18, %rdx
addq $0x18, %rdx
movq %rdi, -0x38(%rbp)
xorl %esi, %esi
callq 0x91d0
jmp 0x3dec3
leaq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x160(%rbx), %r14
movq %rbx, -0x40(%rbp)
movq 0x168(%rbx), %rbx
xorl %r15d, %r15d
cmpq %rbx, %r14
je 0x3df1a
movq (%r14), %rax
movsd 0x60(%rax), %xmm0
ucomisd 0x15933(%rip), %xmm0    # 0x53820
jne 0x3df14
jp 0x3df14
movq %r15, %rax
xorl %edx, %edx
movq -0x40(%rbp), %rcx
divq 0x150(%rcx)
imulq $0x18, %rdx, %rdi
addq -0x38(%rbp), %rdi
movq %r14, %rsi
callq 0x3f4f8
incq %r15
addq $0x8, %r14
jmp 0x3ded8
movq %r12, -0x58(%rbp)
movq %r13, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x150(%rax), %rax
cmpq %r15, %rax
cmovbq %rax, %r15
leaq -0x80(%rbp), %r14
movq -0x48(%rbp), %rbx
movq -0x38(%rbp), %r12
movq %r15, %r13
subq $0x1, %r13
jb 0x3df96
leaq -0x176(%rip), %rax      # 0x3ddda
movq %rax, -0x78(%rbp)
andq $0x0, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movq %r12, -0x60(%rbp)
movq %r14, %rdi
leaq -0x78(%rbp), %rsi
leaq -0x68(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x3f516
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f59c
movq %r14, %rdi
callq 0x36ca4
addq $0x18, %r12
addq $0x8, %rbx
jmp 0x3df43
movq -0x48(%rbp), %rbx
movq -0x58(%rbp), %r12
movq -0x40(%rbp), %r14
subq $0x1, %r15
jb 0x3dfb6
movq %rbx, %rdi
callq 0x95f0
addq $0x8, %rbx
jmp 0x3dfa2
movq 0x160(%r14), %rbx
movq 0x168(%r14), %r13
cmpq %r13, %rbx
je 0x3e027
movq %r13, %r14
subq %rbx, %r14
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3ef52
cmpq $0x81, %r14
jl 0x3e01c
leaq 0x80(%rbx), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f167
cmpq %r13, %r14
je 0x3e027
movq %r14, %rdi
callq 0x3f1d1
addq $0x8, %r14
jmp 0x3e009
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3f167
movq -0x40(%rbp), %r14
movq 0x160(%r14), %rax
movq 0x168(%r14), %rcx
addq $-0x8, %rcx
xorpd %xmm0, %xmm0
movq -0x50(%rbp), %r13
leaq 0x8(%rcx), %rsi
cmpq %rax, %rsi
je 0x3e069
movq (%rcx), %rdx
movsd 0x60(%rdx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3e069
movq %rcx, 0x168(%r14)
addq $-0x8, %rcx
jmp 0x3e045
xorpd %xmm0, %xmm0
movupd %xmm0, (%r12)
andq $0x0, 0x10(%r12)
xorl %ebx, %ebx
cmpq %rax, %rsi
je 0x3e0b3
cmpq 0x150(%r14), %rbx
jae 0x3e0b3
addq $-0x8, %rsi
movq %r12, %rdi
callq 0x3f4f8
movq 0x160(%r14), %rax
movq 0x168(%r14), %rsi
addq $-0x8, %rsi
movq %rsi, 0x168(%r14)
incq %rbx
jmp 0x3e07b
testq %r13, %r13
movq -0x48(%rbp), %r14
je 0x3e0ee
movq -0x38(%rbp), %r15
addq $-0x18, %r15
imulq $0x18, %r13, %rbx
leaq (%r15,%rbx), %rdi
callq 0x3f7d6
addq $-0x18, %rbx
jne 0x3e0c8
addq $-0x8, %r14
shlq $0x3, %r13
leaq (%r14,%r13), %rdi
callq 0x36ca4
addq $-0x8, %r13
jne 0x3e0df
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
jmp 0x3e11b
movq %r12, %rdi
movq %rax, %r12
callq 0x3f7d6
jmp 0x3e11b
jmp 0x3e114
movq %rax, %r12
movq -0x50(%rbp), %r13
testq %r13, %r13
movq -0x48(%rbp), %r15
je 0x3e15c
movq -0x38(%rbp), %rax
addq $-0x18, %rax
movq %r13, %r14
imulq $0x18, %r13, %rbx
movq %rax, %r13
leaq (%rbx,%r13), %rdi
callq 0x3f7d6
addq $-0x18, %rbx
jne 0x3e136
addq $-0x8, %r15
shlq $0x3, %r14
leaq (%r15,%r14), %rdi
callq 0x36ca4
addq $-0x8, %r14
jne 0x3e14d
movq %r12, %rdi
callq 0x9640