// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rsi, %r12
movq %rdi, -0x38(%rbp)
movq 0x150(%rsi), %r14
movq %rsp, %r15
leaq 0xf(,%r14,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
testq %r14, %r14
je 0x436be
leaq (,%r14,8), %rbx
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x91c0
movq %rsp, %r13
leaq (%rbx,%rbx,2), %rax
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leaq (%rbx,%rbx,2), %rax
addq $-0x18, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq 0x18(,%rax,8), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x91c0
jmp 0x436c2
leaq -0x30(%rbp), %r13
movq %r13, -0x40(%rbp)
movq %r15, -0x48(%rbp)
movq 0x160(%r12), %r15
movq 0x168(%r12), %rbx
xorl %r13d, %r13d
cmpq %rbx, %r15
je 0x43741
movq (%r15), %rcx
movsd 0x60(%rcx), %xmm0
ucomisd 0x18d06(%rip), %xmm0    # 0x5c3f8
jne 0x4372e
jp 0x4372e
movq %r13, %rax
xorl %edx, %edx
divq 0x150(%r12)
leaq (%rdx,%rdx,2), %rax
movq -0x40(%rbp), %rdx
leaq (%rdx,%rax,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x43723
movq %rcx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x4372b
movq %r15, %rdx
callq 0x45a56
incq %r13
addq $0x8, %r15
cmpq %rbx, %r15
jne 0x436e2
movq 0x150(%r12), %rax
jmp 0x43744
movq %r14, %rax
cmpq %r13, %rax
cmovbq %rax, %r13
testq %r13, %r13
je 0x437bf
xorl %r15d, %r15d
movq -0x40(%rbp), %rbx
leaq -0x192(%rip), %rax      # 0x435cc
movq %rax, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movq %r12, -0x58(%rbp)
movq %rbx, -0x50(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0x45168
movq -0x48(%rbp), %rcx
cmpq $0x0, (%rcx,%r15,8)
jne 0x43a04
movq -0x60(%rbp), %rax
movq %rax, (%rcx,%r15,8)
incq %r15
addq $0x18, %rbx
cmpq %r15, %r13
jne 0x43757
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x9630
addq $0x8, %rbx
decq %r13
jne 0x437ae
movq 0x160(%r12), %rbx
movq 0x168(%r12), %r13
cmpq %r13, %rbx
je 0x43864
movq %r13, %r15
subq %rbx, %r15
movq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x44ac7
cmpq $0x81, %r15
jl 0x43859
leaq 0x80(%rbx), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x44d18
cmpq %r13, %r15
je 0x43864
movq -0x8(%r15), %rcx
movq (%r15), %rax
movsd 0x60(%rax), %xmm0
movsd 0x60(%rcx), %xmm1
ucomisd %xmm0, %xmm1
movq %r15, %rdx
jbe 0x43850
movq %r15, %rdx
movq %rcx, (%rdx)
movq -0x10(%rdx), %rcx
movsd 0x60(%rcx), %xmm1
addq $-0x8, %rdx
ucomisd %xmm0, %xmm1
ja 0x4383a
movq %rax, (%rdx)
addq $0x8, %r15
jmp 0x43818
movq %rbx, %rdi
movq %r13, %rsi
callq 0x44d18
movq 0x160(%r12), %rax
movq 0x168(%r12), %rdx
cmpq %rax, %rdx
je 0x438b4
addq $-0x8, %rdx
xorpd %xmm0, %xmm0
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %r13
movq (%rdx), %rcx
movsd 0x60(%rcx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x438be
movq %rdx, 0x168(%r12)
leaq -0x8(%rdx), %rcx
cmpq %rax, %rdx
movq %rcx, %rdx
jne 0x43889
addq $0x8, %rcx
movq %rcx, %rdx
jmp 0x438c2
movq -0x48(%rbp), %r15
movq -0x40(%rbp), %r13
jmp 0x438c2
addq $0x8, %rdx
xorpd %xmm0, %xmm0
movq -0x38(%rbp), %rcx
movupd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
cmpq %rax, %rdx
je 0x43941
cmpq $0x0, 0x150(%r12)
je 0x43941
movl $0x1, %ebx
addq $-0x8, %rdx
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x4390a
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x4390f
callq 0x45a56
movq 0x150(%r12), %rax
movq 0x168(%r12), %rdx
addq $-0x8, %rdx
movq %rdx, 0x168(%r12)
cmpq 0x160(%r12), %rdx
je 0x43941
leaq 0x1(%rbx), %rcx
cmpq %rax, %rbx
movq %rcx, %rbx
jb 0x438eb
testq %r14, %r14
movq -0x38(%rbp), %rax
je 0x43985
shlq $0x3, %r14
leaq (%r14,%r14,2), %rbx
movq -0x18(%r13,%rbx), %rdi
testq %rdi, %rdi
je 0x4396d
movq -0x8(%r13,%rbx), %rsi
subq %rdi, %rsi
callq 0x9350
movq -0x38(%rbp), %rax
addq $-0x18, %rbx
jne 0x43952
cmpq $0x0, -0x8(%r15,%r14)
jne 0x43a04
addq $-0x8, %r14
jne 0x43973
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x439b9
movq %rax, %r12
movq -0x38(%rbp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x439bc
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x9350
jmp 0x439bc
jmp 0x439b9
movq %rax, %r12
testq %r14, %r14
je 0x439fc
shlq $0x3, %r14
leaq (%r14,%r14,2), %rbx
movq -0x40(%rbp), %r15
movq -0x18(%r15,%rbx), %rdi
testq %rdi, %rdi
je 0x439e4
movq -0x8(%r15,%rbx), %rsi
subq %rdi, %rsi
callq 0x9350
addq $-0x18, %rbx
jne 0x439cd
movq -0x48(%rbp), %rax
cmpq $0x0, -0x8(%rax,%r14)
jne 0x43a04
addq $-0x8, %r14
jne 0x439ee
movq %r12, %rdi
callq 0x9680
callq 0x90d0
nop