// File: ahnafraihan[P]BestFirstSearch/main.cpp
// Function: main
int main(int argc, const char * argv[]) {
    output = argv[2]; //save name of desired output file
    int n = 0; //initialize number of items n
    int W = 0; //initialize max weight W
    int maxprofit = 0; //initialize maximum profit to 0
    ifstream infile (argv[1]); //create an ifstream to parse our input file
    string line; //string to hold each line
    string buf; //string to hold each number
    
    if (getline(infile, line)) { //parse input file by commas
        stringstream s (line);
        if (getline(s, buf, ',')) { //identify number of items n
            stringstream ss (buf);
            ss >> n;
        }
        if (getline(s, buf, ',')) { //identify maximum weight W
            stringstream ss (buf);
            ss >> W;
        }
    }
    
    int p[n]; //initialize array of profits of size n
    int w[n]; //initialize array of weights of size n
    
    int count = 0; // count variable used to identify current line of input
    
    while (getline (infile, line)) { //parse input again, this time to identify individual items
        stringstream s (line);
        if (getline(s,buf, ',')) { //identify weights of items
            int in;
            stringstream ss (buf);
            ss >> in;
            w[count] = in;
        }
        if (getline(s, buf, ',')) { //identify profits of items
            int in;
            stringstream ss (buf);
            ss >> in;
            p[count] = in;
        }
        count ++;
    }
    
    sorts s[n]; //create array of sorts of size n items
    
    for (int i = 0; i < n; i++) { //populate array of sorts with respective price/weight's and index's
        s[i].pw = (float)p[i]/(float)w[i];
        s[i].index = i;
    }
    
    sort (s, s + n, comparePW); //sort array of sorts by pw
    
    int pSorted[n]; //create array to hold sorted profits
    int wSorted[n]; //create array to hold sorted weights
    
    for (int i = 0; i < n; i++) { //populate sorted profit and weight arrays with knowedge of p/w order from sorts
        pSorted[i] = p[s[i].index];
        wSorted[i] = w[s[i].index];
    }
    
    //call our knapsack 3 function
    knapsack3(n, pSorted, wSorted, W, maxprofit);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xca0, %rsp            # imm = 0xCA0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0xad30(%rip), %rdi      # 0xd158
callq 0x2150
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x228(%rbp), %rdi
movq %rdi, -0xc18(%rbp)
movl $0x8, %edx
callq 0x2210
leaq -0x248(%rbp), %rdi
movq %rdi, -0xc10(%rbp)
callq 0x2160
leaq -0x268(%rbp), %rdi
callq 0x2160
movq -0xc18(%rbp), %rdi
movq -0xc10(%rbp), %rsi
callq 0x2040
movq %rax, -0xc08(%rbp)
jmp 0x249d
movq -0xc08(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2140
movb %al, -0xc19(%rbp)
jmp 0x24bb
movb -0xc19(%rbp), %al
testb $0x1, %al
jne 0x24ca
jmp 0x26c9
movl $0x10, %edi
movl $0x8, %esi
callq 0x34f0
movl %eax, -0xc20(%rbp)
jmp 0x24e1
movl -0xc20(%rbp), %edx
leaq -0x400(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x21b0
jmp 0x24fc
leaq -0x400(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x2c, %edx
callq 0x2220
movq %rax, -0xc28(%rbp)
jmp 0x251d
movq -0xc28(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2140
movb %al, -0xc29(%rbp)
jmp 0x253b
movb -0xc29(%rbp), %al
testb $0x1, %al
jne 0x254a
jmp 0x25ed
movl $0x10, %edi
movl $0x8, %esi
callq 0x34f0
movl %eax, -0xc30(%rbp)
jmp 0x2561
movl -0xc30(%rbp), %edx
leaq -0x588(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x21b0
jmp 0x257c
leaq -0x588(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x20c0
jmp 0x258e
leaq -0x588(%rbp), %rdi
callq 0x20d0
jmp 0x25ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x2b78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x26b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x588(%rbp), %rdi
callq 0x20d0
jmp 0x26b8
leaq -0x400(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x2c, %edx
callq 0x2220
movq %rax, -0xc38(%rbp)
jmp 0x260e
movq -0xc38(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2140
movb %al, -0xc39(%rbp)
jmp 0x262c
movb -0xc39(%rbp), %al
testb $0x1, %al
jne 0x2638
jmp 0x26aa
movl $0x10, %edi
movl $0x8, %esi
callq 0x34f0
movl %eax, -0xc40(%rbp)
jmp 0x264f
movl -0xc40(%rbp), %edx
leaq -0x710(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x21b0
jmp 0x266a
leaq -0x710(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x20c0
jmp 0x267c
leaq -0x710(%rbp), %rdi
callq 0x20d0
jmp 0x26aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x710(%rbp), %rdi
callq 0x20d0
jmp 0x26b8
leaq -0x400(%rbp), %rdi
callq 0x20d0
jmp 0x26c9
leaq -0x400(%rbp), %rdi
callq 0x20d0
jmp 0x2b78
movl -0x14(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x718(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc50(%rbp)
movq %rcx, %rsp
movq %rax, -0x720(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc48(%rbp)
movq %rcx, %rsp
movq %rax, -0x728(%rbp)
movl $0x0, -0x72c(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x2040
movq %rax, -0xc58(%rbp)
jmp 0x2745
movq -0xc58(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2140
movb %al, -0xc59(%rbp)
jmp 0x2763
movb -0xc59(%rbp), %al
testb $0x1, %al
jne 0x2772
jmp 0x29a3
movl $0x10, %edi
movl $0x8, %esi
callq 0x34f0
movl %eax, -0xc60(%rbp)
jmp 0x2789
movl -0xc60(%rbp), %edx
leaq -0x8b8(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x21b0
jmp 0x27a4
leaq -0x8b8(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x2c, %edx
callq 0x2220
movq %rax, -0xc68(%rbp)
jmp 0x27c5
movq -0xc68(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2140
movb %al, -0xc69(%rbp)
jmp 0x27e3
movb -0xc69(%rbp), %al
testb $0x1, %al
jne 0x27f2
jmp 0x2898
movl $0x10, %edi
movl $0x8, %esi
callq 0x34f0
movl %eax, -0xc70(%rbp)
jmp 0x2809
movl -0xc70(%rbp), %edx
leaq -0xa48(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x21b0
jmp 0x2824
leaq -0xa48(%rbp), %rdi
leaq -0x8bc(%rbp), %rsi
callq 0x20c0
jmp 0x2839
movq -0xc48(%rbp), %rax
movl -0x8bc(%rbp), %edx
movslq -0x72c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
leaq -0xa48(%rbp), %rdi
callq 0x20d0
jmp 0x2898
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x2992
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0xa48(%rbp), %rdi
callq 0x20d0
jmp 0x2992
leaq -0x8b8(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x2c, %edx
callq 0x2220
movq %rax, -0xc78(%rbp)
jmp 0x28b9
movq -0xc78(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2140
movb %al, -0xc79(%rbp)
jmp 0x28d7
movb -0xc79(%rbp), %al
testb $0x1, %al
jne 0x28e6
jmp 0x2972
movl $0x10, %edi
movl $0x8, %esi
callq 0x34f0
movl %eax, -0xc80(%rbp)
jmp 0x28fd
movl -0xc80(%rbp), %edx
leaq -0xbd8(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x21b0
jmp 0x2918
leaq -0xbd8(%rbp), %rdi
leaq -0xa4c(%rbp), %rsi
callq 0x20c0
jmp 0x292d
movq -0xc50(%rbp), %rax
movl -0xa4c(%rbp), %edx
movslq -0x72c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
leaq -0xbd8(%rbp), %rdi
callq 0x20d0
jmp 0x2972
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0xbd8(%rbp), %rdi
callq 0x20d0
jmp 0x2992
movl -0x72c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x72c(%rbp)
leaq -0x8b8(%rbp), %rdi
callq 0x20d0
jmp 0x2729
leaq -0x8b8(%rbp), %rdi
callq 0x20d0
jmp 0x2b78
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc88(%rbp)
movq %rcx, %rsp
movq %rax, -0xbe0(%rbp)
movl $0x0, -0xbe4(%rbp)
movl -0xbe4(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2a3d
movq -0xc88(%rbp), %rax
movq -0xc48(%rbp), %rcx
movq -0xc50(%rbp), %rdx
movslq -0xbe4(%rbp), %rsi
cvtsi2ssl (%rdx,%rsi,4), %xmm0
movslq -0xbe4(%rbp), %rdx
cvtsi2ssl (%rcx,%rdx,4), %xmm1
divss %xmm1, %xmm0
movslq -0xbe4(%rbp), %rcx
movss %xmm0, (%rax,%rcx,8)
movl -0xbe4(%rbp), %edx
movslq -0xbe4(%rbp), %rcx
movl %edx, 0x4(%rax,%rcx,8)
movl -0xbe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbe4(%rbp)
jmp 0x29d3
movq -0xc88(%rbp), %rdi
movslq -0x14(%rbp), %rax
leaq (%rdi,%rax,8), %rsi
leaq -0x673(%rip), %rdx      # 0x23e0
callq 0x3510
jmp 0x2a5a
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc98(%rbp)
movq %rcx, %rsp
movq %rax, -0xbf0(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc90(%rbp)
movq %rcx, %rsp
movq %rax, -0xbf8(%rbp)
movl $0x0, -0xbfc(%rbp)
movl -0xbfc(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2b23
movq -0xc90(%rbp), %rax
movq -0xc48(%rbp), %rcx
movq -0xc88(%rbp), %rdx
movq -0xc98(%rbp), %rsi
movq -0xc50(%rbp), %rdi
movslq -0xbfc(%rbp), %r8
movslq 0x4(%rdx,%r8,8), %r8
movl (%rdi,%r8,4), %r8d
movslq -0xbfc(%rbp), %rdi
movl %r8d, (%rsi,%rdi,4)
movslq -0xbfc(%rbp), %rsi
movslq 0x4(%rdx,%rsi,8), %rdx
movl (%rcx,%rdx,4), %edx
movslq -0xbfc(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0xbfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbfc(%rbp)
jmp 0x2ab0
movq -0xc90(%rbp), %rdx
movq -0xc98(%rbp), %rsi
movl -0x14(%rbp), %edi
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %r8d
callq 0x2bb0
jmp 0x2b42
movq -0x718(%rbp), %rax
movq %rax, %rsp
leaq -0x268(%rbp), %rdi
callq 0x2238
leaq -0x248(%rbp), %rdi
callq 0x2238
leaq -0x228(%rbp), %rdi
callq 0x2050
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
leaq -0x268(%rbp), %rdi
callq 0x2238
leaq -0x248(%rbp), %rdi
callq 0x2238
leaq -0x228(%rbp), %rdi
callq 0x2050
movq -0x270(%rbp), %rdi
callq 0x2200
nopl (%rax,%rax)