// File: /workspace/github_cpp_makefile_star2_O1/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, -0x30(%rbp)
movq 0x150(%rsi), %r14
movq %rsp, %rbx
leaq 0xf(,%r14,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
testq %r14, %r14
je 0x43e11
leaq (,%r14,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x91c0
movq %rsp, %r15
leaq (%r14,%r14,2), %rax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r15
movq %r15, %rsp
testq %r14, %r14
je 0x43e5e
leaq -0x18(,%rax,8), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq 0x18(,%rax,8), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x91c0
movq %r15, -0x48(%rbp)
movq %r14, -0x38(%rbp)
movq %rbx, -0x40(%rbp)
movq 0x160(%r12), %r15
movq 0x168(%r12), %rbx
xorl %r13d, %r13d
cmpq %rbx, %r15
je 0x43edd
movq -0x30(%rbp), %r14
movq (%r15), %rcx
movsd 0x60(%rcx), %xmm0
ucomisd 0x18512(%rip), %xmm0    # 0x5c3a8
jne 0x43ed2
jp 0x43ed2
movq %r13, %rax
xorl %edx, %edx
divq 0x150(%r12)
leaq (%rdx,%rdx,2), %rax
movq -0x48(%rbp), %rdx
leaq (%rdx,%rax,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x43ec7
movq %rcx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x43ecf
movq %r15, %rdx
callq 0x462ac
incq %r13
addq $0x8, %r15
cmpq %rbx, %r15
jne 0x43e86
jmp 0x43ee1
movq -0x30(%rbp), %r14
movq 0x150(%r12), %rax
cmpq %r13, %rax
cmovbq %rax, %r13
testq %r13, %r13
je 0x43f6b
xorl %r15d, %r15d
leaq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
leaq -0x19f(%rip), %rax      # 0x43d68
movq %rax, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movq %r12, -0x58(%rbp)
movq %r14, -0x50(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x58(%rbp), %rdx
movq %rbx, %rcx
callq 0x459a6
movq -0x40(%rbp), %rcx
cmpq $0x0, (%rcx,%r15,8)
jne 0x441c8
movq -0x60(%rbp), %rax
movq %rax, (%rcx,%r15,8)
incq %r15
addq $0x18, %r14
cmpq %r15, %r13
jne 0x43f00
movq -0x40(%rbp), %rbx
movq -0x30(%rbp), %r14
movq %rbx, %rdi
callq 0x9620
addq $0x8, %rbx
decq %r13
jne 0x43f5a
movq 0x160(%r12), %rbx
movq 0x168(%r12), %r13
cmpq %r13, %rbx
je 0x4401d
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 0x452a0
cmpq $0x81, %r14
jl 0x4400e
leaq 0x80(%rbx), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x45524
cmpq %r13, %r15
movq -0x30(%rbp), %r14
je 0x4401d
movq -0x8(%r15), %rcx
movq (%r15), %rax
movsd 0x60(%rax), %xmm0
movsd 0x60(%rcx), %xmm1
ucomisd %xmm0, %xmm1
movq %r15, %rdx
jbe 0x44000
movq %r15, %rdx
movq %rcx, (%rdx)
movq -0x10(%rdx), %rcx
movsd 0x60(%rcx), %xmm1
addq $-0x8, %rdx
ucomisd %xmm0, %xmm1
ja 0x43fea
movq %rax, (%rdx)
addq $0x8, %r15
cmpq %r13, %r15
jne 0x43fcd
jmp 0x4401d
movq %rbx, %rdi
movq %r13, %rsi
callq 0x45524
movq -0x30(%rbp), %r14
movq 0x160(%r12), %rax
movq 0x168(%r12), %rcx
cmpq %rax, %rcx
je 0x4405c
addq $-0x8, %rcx
xorpd %xmm0, %xmm0
movq (%rcx), %rdx
movsd 0x60(%rdx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4405c
movq %rcx, 0x168(%r12)
leaq -0x8(%rcx), %rdx
cmpq %rax, %rcx
movq %rdx, %rcx
jne 0x4403a
xorpd %xmm0, %xmm0
movupd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
movq 0x168(%r12), %rdx
cmpq 0x160(%r12), %rdx
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %r13
je 0x440ec
cmpq $0x0, 0x150(%r12)
je 0x440ec
movl $0x1, %ebx
addq $-0x8, %rdx
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x440b2
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x440ba
movq %r14, %rdi
callq 0x462ac
movq 0x150(%r12), %rax
movq 0x168(%r12), %rdx
addq $-0x8, %rdx
movq %rdx, 0x168(%r12)
cmpq 0x160(%r12), %rdx
je 0x440ec
leaq 0x1(%rbx), %rcx
cmpq %rax, %rbx
movq %rcx, %rbx
jb 0x44097
testq %r13, %r13
je 0x44139
leaq (,%r13,8), %rbx
leaq (%rbx,%rbx,2), %r14
movq -0x48(%rbp), %rax
movq -0x18(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x44118
movq -0x8(%rax,%r14), %rsi
subq %rdi, %rsi
callq 0x9340
addq $-0x18, %r14
jne 0x440fd
testq %r13, %r13
movq -0x30(%rbp), %r14
je 0x44139
cmpq $0x0, -0x8(%r15,%rbx)
jne 0x441c8
addq $-0x8, %rbx
jne 0x44127
movq %r14, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4416c
movq %rax, %r12
movq (%r14), %rdi
testq %rdi, %rdi
je 0x4416f
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x9340
jmp 0x4416f
jmp 0x4416c
movq %rax, %r12
cmpq $0x0, -0x38(%rbp)
je 0x441c0
movq -0x38(%rbp), %rax
leaq (,%rax,8), %rbx
leaq (%rbx,%rbx,2), %r14
movq -0x48(%rbp), %r15
movq -0x18(%r15,%r14), %rdi
testq %rdi, %rdi
je 0x441a1
movq -0x8(%r15,%r14), %rsi
subq %rdi, %rsi
callq 0x9340
addq $-0x18, %r14
jne 0x4418a
cmpq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rax
je 0x441c0
cmpq $0x0, -0x8(%rax,%rbx)
jne 0x441c8
addq $-0x8, %rbx
jne 0x441b2
movq %r12, %rdi
callq 0x9670
callq 0x90d0
nop