// File: /workspace/github_cpp_makefile_star2_O3/ahnafraihan[P]BestFirstSearch/main.cpp
// Function: knapsack3(int, int*, int*, int, int)
void knapsack3(int n, int p[], int w[], int W, int maxprofit) {
    int optSize = 0; //initialize current optimal size 0
    int numNodes = 1; //initalize current number of nodes visited to 1, to account for root node
    int numLeaves = 0; //initalize current number of leaf nodes visited to 0
    
    //initialize priority queue of nodes Q
    priority_queue<node>Q;
    
    //initialize nodes u and v, with v as root
    node u, v;
    
    //initialize Q to be empty
    v.level = 0; v.profit = 0; v.weight = 0; v.k.resize(n+1, 0); u.k.resize(n+1,0);
    maxprofit = 0; //set current maximum profit to 0
    
    v.bound = bound(n, p, w, W, v);
    Q.push(v); //enqueue v
    
    //remove node with best bound
    while(!Q.empty()){
        v= Q.top();
        Q.pop();
        //expand v to see if it is promising
        if(v.bound > maxprofit) {
            numNodes += 2; //increment number of total nodes visited
            u.level = v.level + 1; // child of v
            //"yes" child
            
            u.weight = v.weight + w[u.level-1];
            u.profit = v.profit + p[u.level-1];
            u.k = v.k; //pass down optimization history to child
            u.k.at(u.level-1) = 1; //designate part of personal solution
            if(u.weight <= W && u.profit > maxprofit) {
                maxprofit = u.profit;
                optimalSolution = u.k; //add to global optimal solution
            }
            
            //find the upper bound of our child u
            u.bound = bound(n, p, w, W, u);
            if(u.bound > maxprofit  && u.weight < W) {
                Q.push(u);
            }
            else
                numLeaves++;
            //"no" child
            
            u.weight = v.weight;
            u.profit = v.profit;
            u.k.at(u.level - 1) = 0;
            u.bound = bound(n, p, w, W, u);
            //node chosen to be expanded
            if(u.bound > maxprofit  && u.weight < W) {
                Q.push(u);
            }
            else
                numLeaves++;
            //node not expanded
        }//queue is empty
        else
            numLeaves++; //increment number of leaf nodes visited
    }
    
    //calculate size of our optimal solution from optimalSolution global variable
    for (int i = 0; i < optimalSolution.size(); i++) {
        if (optimalSolution.at(i) == 1)
            optSize++;
    }
    
    //create output stream to record results onto output text file
    ofstream myfile;
    myfile.open (output.c_str());
    myfile << n << "," << maxprofit << "," << optSize << endl << numNodes << "," << numLeaves << endl;
    for (int i = 0; i < optimalSolution.size(); i++) {
        if (optimalSolution.at(i) == 1)
            myfile << w[i] << "," << p[i] << endl;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movl %ecx, %r12d
movq %rdx, -0x68(%rbp)
movq %rsi, -0x60(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
xorl %eax, %eax
movq %rax, -0xb0(%rbp)
movq %rax, -0x38(%rbp)
movups %xmm0, -0x48(%rbp)
leaq -0x90(%rbp), %rsi
movq %rax, 0x10(%rsi)
movups %xmm0, (%rsi)
movq %rax, -0x10(%rsi)
movl %eax, -0x8(%rsi)
movq %rdi, -0x70(%rbp)
leal 0x1(%rdi), %ecx
movslq %ecx, %rbx
leaq -0x350(%rbp), %rdx
movl %eax, (%rdx)
movq %rsi, %rdi
movq %rbx, %rsi
callq 0x3078
leaq -0x48(%rbp), %rdi
leaq -0x350(%rbp), %rdx
movl $0x0, (%rdx)
movq %rbx, %rsi
callq 0x3078
movups -0xa0(%rbp), %xmm0
leaq -0x140(%rbp), %rdi
movaps %xmm0, -0x10(%rdi)
leaq -0x90(%rbp), %rsi
callq 0x3342
leaq -0x150(%rbp), %rbx
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
movq %rbx, %r8
callq 0x2fb6
movss %xmm0, -0x94(%rbp)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x2a7a
movq -0x130(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
leaq -0xc0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x30b2
movq -0xc0(%rbp), %rsi
xorl %r13d, %r13d
cmpq -0xb8(%rbp), %rsi
je 0x2cd9
movl $0x1, -0x2c(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movups (%rsi), %xmm0
movaps %xmm0, -0xa0(%rbp)
addq $0x10, %rsi
leaq -0x90(%rbp), %rdi
callq 0x3254
leaq -0xc0(%rbp), %rdi
callq 0x3154
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
movss -0x94(%rbp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x2cc0
movslq -0xa0(%rbp), %rax
leal 0x1(%rax), %ecx
movl %ecx, -0x58(%rbp)
movq -0x68(%rbp), %rcx
movl (%rcx,%rax,4), %ecx
addl -0x98(%rbp), %ecx
movl %ecx, -0x50(%rbp)
movq -0x60(%rbp), %rcx
movl (%rcx,%rax,4), %eax
addl -0x9c(%rbp), %eax
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3254
movslq -0x58(%rbp), %rax
leaq -0x1(%rax), %rsi
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0x2f49
movl $0x1, -0x4(%rcx,%rax,4)
cmpl %r12d, -0x50(%rbp)
jg 0x2b76
movl -0x54(%rbp), %ebx
cmpl %r14d, %ebx
jle 0x2b76
leaq 0x35d6(%rip), %rdi      # 0x6140
leaq -0x48(%rbp), %rsi
callq 0x3254
movl %ebx, %r14d
movups -0x58(%rbp), %xmm0
movaps %xmm0, -0x120(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3342
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
leaq -0x120(%rbp), %r8
callq 0x2fb6
movss %xmm0, -0x4c(%rbp)
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x2bd1
movq -0x100(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
movss -0x4c(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r14d, %xmm1
movss %xmm1, -0x74(%rbp)
ucomiss %xmm1, %xmm0
jbe 0x2bfb
cmpl %r12d, -0x50(%rbp)
jge 0x2bfb
leaq -0xc0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x30b2
jmp 0x2bfe
incl %r15d
movq -0x9c(%rbp), %rax
movq %rax, -0x54(%rbp)
movslq -0x58(%rbp), %rax
leaq -0x1(%rax), %rsi
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0x2f49
movl $0x0, -0x4(%rcx,%rax,4)
movl -0x58(%rbp), %eax
movl %eax, -0xf0(%rbp)
movq -0x54(%rbp), %rax
movq %rax, -0xec(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0xe4(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3342
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
leaq -0xf0(%rbp), %r8
callq 0x2fb6
movss %xmm0, -0x4c(%rbp)
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x2c9e
movq -0xd0(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
movss -0x4c(%rbp), %xmm0
addl $0x2, -0x2c(%rbp)
ucomiss -0x74(%rbp), %xmm0
jbe 0x2cc0
cmpl %r12d, -0x50(%rbp)
jge 0x2cc0
leaq -0xc0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x30b2
jmp 0x2cc3
incl %r15d
movq -0xc0(%rbp), %rsi
cmpq -0xb8(%rbp), %rsi
jne 0x2ab1
jmp 0x2ce6
movl $0x1, -0x2c(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x345b(%rip), %rax      # 0x6148
movq 0x344c(%rip), %rcx      # 0x6140
subq %rcx, %rax
je 0x2d1f
sarq $0x2, %rax
cmpq $0x1, %rax
adcq $0x0, %rax
xorl %edx, %edx
xorl %r13d, %r13d
xorl %esi, %esi
cmpl $0x1, (%rcx,%rdx,4)
sete %sil
addl %esi, %r13d
incq %rdx
cmpq %rdx, %rax
jne 0x2d0a
leaq -0x350(%rbp), %rdi
callq 0x21e0
movq 0x3426(%rip), %rsi      # 0x6158
leaq -0x350(%rbp), %rdi
movl $0x10, %edx
callq 0x2040
leaq -0x350(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x21f0
movq %rax, %r12
leaq 0x12a7(%rip), %rsi      # 0x4004
movl $0x1, %edx
movq %rax, %rdi
callq 0x2150
movq %r12, %rdi
movl %r14d, %esi
callq 0x21f0
movq %rax, %rbx
leaq 0x1285(%rip), %rsi      # 0x4004
movl $0x1, %edx
movq %rax, %rdi
callq 0x2150
movq %rbx, %rdi
movl %r13d, %esi
callq 0x21f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x21f0
movq %rax, %rbx
leaq 0x122e(%rip), %rsi      # 0x4004
movl $0x1, %edx
movq %rax, %rdi
callq 0x2150
movq %rbx, %rdi
movl %r15d, %esi
callq 0x21f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
movq 0x3329(%rip), %rax      # 0x6148
movq 0x331a(%rip), %rcx      # 0x6140
cmpq %rcx, %rax
je 0x2ec8
xorl %r13d, %r13d
leaq -0x350(%rbp), %rbx
leaq 0x11c4(%rip), %r15      # 0x4004
cmpl $0x1, (%rcx,%r13,4)
jne 0x2eb2
movq -0x68(%rbp), %rax
movl (%rax,%r13,4), %esi
movq %rbx, %rdi
callq 0x21f0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2150
movq -0x60(%rbp), %rax
movl (%rax,%r13,4), %esi
movq %r12, %rdi
callq 0x21f0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2120
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20d0
movq 0x329d(%rip), %rax      # 0x6148
movq 0x328e(%rip), %rcx      # 0x6140
incq %r13
movq %rax, %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r13, %rdx
ja 0x2e40
movq 0x3109(%rip), %rax      # 0x5fd8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x348(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x350(%rbp,%rcx)
callq 0x2100
leaq -0x258(%rbp), %rdi
callq 0x2070
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x2f16
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2f2b
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
leaq -0xc0(%rbp), %rdi
callq 0x33ee
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10e7(%rip), %rdi      # 0x4037
xorl %eax, %eax
callq 0x2180
jmp 0x2f72
jmp 0x2f72
jmp 0x2f72
jmp 0x2f61
jmp 0x2f72
movq %rax, %rbx
leaq -0x350(%rbp), %rdi
callq 0x2170
jmp 0x2f75
movq %rax, %rbx
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x2f8d
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2fa2
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
leaq -0xc0(%rbp), %rdi
callq 0x33ee
movq %rbx, %rdi
callq 0x2200