// File: /workspace/github_cpp_makefile_star2_O1/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 0x301e
leaq -0x48(%rbp), %rdi
leaq -0x350(%rbp), %rdx
movl $0x0, (%rdx)
movq %rbx, %rsi
callq 0x301e
movups -0xa0(%rbp), %xmm0
leaq -0x140(%rbp), %rdi
movaps %xmm0, -0x10(%rdi)
leaq -0x90(%rbp), %rsi
callq 0x32d8
leaq -0x150(%rbp), %rbx
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
movq %rbx, %r8
callq 0x2f5c
movss %xmm0, -0x94(%rbp)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x2a26
movq -0x130(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
leaq -0xc0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3058
movq -0xc0(%rbp), %rsi
xorl %r13d, %r13d
cmpq -0xb8(%rbp), %rsi
je 0x2c85
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 0x31f8
leaq -0xc0(%rbp), %rdi
callq 0x30fa
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
movss -0x94(%rbp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x2c6c
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 0x31f8
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 0x2eef
movl $0x1, -0x4(%rcx,%rax,4)
cmpl %r12d, -0x50(%rbp)
jg 0x2b22
movl -0x54(%rbp), %ebx
cmpl %r14d, %ebx
jle 0x2b22
leaq 0x362a(%rip), %rdi      # 0x6140
leaq -0x48(%rbp), %rsi
callq 0x31f8
movl %ebx, %r14d
movups -0x58(%rbp), %xmm0
movaps %xmm0, -0x120(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x32d8
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
leaq -0x120(%rbp), %r8
callq 0x2f5c
movss %xmm0, -0x4c(%rbp)
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x2b78
movq -0x100(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
xorps %xmm1, %xmm1
cvtsi2ss %r14d, %xmm1
movss -0x4c(%rbp), %xmm0
movss %xmm1, -0x74(%rbp)
ucomiss %xmm1, %xmm0
jbe 0x2ba7
cmpl %r12d, -0x50(%rbp)
jge 0x2ba7
leaq -0xc0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3058
jmp 0x2baa
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 0x2eef
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 0x32d8
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movl %r12d, %ecx
leaq -0xf0(%rbp), %r8
callq 0x2f5c
movss %xmm0, -0x4c(%rbp)
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x2c45
movq -0xd0(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
addl $0x2, -0x2c(%rbp)
movss -0x4c(%rbp), %xmm0
ucomiss -0x74(%rbp), %xmm0
jbe 0x2c6c
cmpl %r12d, -0x50(%rbp)
jge 0x2c6c
leaq -0xc0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3058
jmp 0x2c6f
incl %r15d
movq -0xc0(%rbp), %rsi
cmpq -0xb8(%rbp), %rsi
jne 0x2a5d
jmp 0x2c92
movl $0x1, -0x2c(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x34af(%rip), %rax      # 0x6148
movq 0x34a0(%rip), %rcx      # 0x6140
subq %rcx, %rax
je 0x2ccb
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 0x2cb6
leaq -0x350(%rbp), %rdi
callq 0x21e0
movq 0x347a(%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 0x12fb(%rip), %rsi      # 0x4004
movl $0x1, %edx
movq %rax, %rdi
callq 0x2150
movq %r12, %rdi
movl %r14d, %esi
callq 0x21f0
movq %rax, %rbx
leaq 0x12d9(%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 0x1282(%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 0x3375(%rip), %rax      # 0x6140
cmpq %rax, 0x3376(%rip)      # 0x6148
je 0x2e6e
xorl %r13d, %r13d
leaq -0x350(%rbp), %rbx
leaq 0x121b(%rip), %r15      # 0x4004
cmpl $0x1, (%rax,%r13,4)
jne 0x2e4d
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
incq %r13
movq 0x32f1(%rip), %rcx      # 0x6148
movq 0x32e2(%rip), %rax      # 0x6140
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r13, %rcx
ja 0x2de9
movq 0x3163(%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 0x2ebc
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2ed1
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
leaq -0xc0(%rbp), %rdi
callq 0x3384
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1141(%rip), %rdi      # 0x4037
xorl %eax, %eax
callq 0x2180
jmp 0x2f18
jmp 0x2f18
jmp 0x2f18
jmp 0x2f07
jmp 0x2f18
movq %rax, %rbx
leaq -0x350(%rbp), %rdi
callq 0x2170
jmp 0x2f1b
movq %rax, %rbx
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x2f33
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2f48
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2140
leaq -0xc0(%rbp), %rdi
callq 0x3384
movq %rbx, %rdi
callq 0x2200