// File: readmapping[P]alfalfa/src/dp.cpp
// Function: dynProg::dpTraceBackScore(int&, int&, boundaries const&, dp_output&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int dynProg::dpTraceBackScore(int& i, int& j, const boundaries& offset, dp_output& output, const string& ref, const string& query){
    //TODO: after mallocs: change this traversal to row-order
    //change stringstream to char array of length dimensions array
    int qBound = 0; int rBound = 0;
    while( (i > qBound && j > rBound)){
        bool equal = false;
        if(j > 0 && i > 0)
            equal = ref[offset.refB+j-1] == query[offset.queryB+i-1];
        if( j > 0 && i > 0 && M[ i ][ j ] == M[ i-1 ][ j-1 ] + scores.match && equal){
            i-- ;  j-- ;
        }
        else if(j > 0 && i > 0 && M[ i ][ j ] == M[ i-1 ][ j-1 ] + scores.mismatch){
            output.editDist++;
            i-- ;  j-- ;
        }
        else if(j > 0 && (M[ i ][ j ] == LEFT[ i ][ j ] || i==0)){
            output.editDist++;
            j-- ;
        }
        else if(i > 0 && (M[ i ][ j ] == UP[ i ][ j ] || j==0)){
            output.editDist++;
            i-- ;
        }
        else{
            cerr << "Error in dynamic programming" << endl;
            cerr << "No valid trace position found" << endl;
            cerr << "Position " << i << ", " << j << endl;
            exit(1);
        }
    }
    return 0;
}

// Assembly:
subq $0x58, %rsp
movq 0x60(%rsp), %rax
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
movq 0x48(%rsp), %rax
movl (%rax), %ecx
xorl %eax, %eax
cmpl 0x24(%rsp), %ecx
movb %al, 0xf(%rsp)
jle 0x37a86
movq 0x40(%rsp), %rax
movl (%rax), %eax
cmpl 0x20(%rsp), %eax
setg %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x37a93
jmp 0x37d9e
movb $0x0, 0x1f(%rsp)
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37b08
movq 0x48(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37b08
movq 0x28(%rsp), %rdi
movq 0x38(%rsp), %rax
movq (%rax), %rsi
movq 0x40(%rsp), %rax
movslq (%rax), %rax
addq %rax, %rsi
subq $0x1, %rsi
callq 0x8410
movsbl (%rax), %eax
movl %eax, 0x8(%rsp)
movq 0x60(%rsp), %rdi
movq 0x38(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x48(%rsp), %rcx
addl (%rcx), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8410
movq %rax, %rcx
movl 0x8(%rsp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movb %al, 0x1f(%rsp)
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37b8d
movq 0x48(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37b8d
movq 0x10(%rsp), %rdx
movq (%rdx), %rax
movq 0x48(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq (%rdx), %rcx
movq 0x48(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movq (%rcx,%rsi,8), %rcx
movq 0x40(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movl (%rcx,%rsi,4), %ecx
movq 0x38(%rdx), %rdx
addl (%rdx), %ecx
cmpl %ecx, %eax
jne 0x37b8d
testb $0x1, 0x1f(%rsp)
je 0x37b8d
movq 0x48(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x37d99
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37c1c
movq 0x48(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37c1c
movq 0x10(%rsp), %rdx
movq (%rdx), %rax
movq 0x48(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq (%rdx), %rcx
movq 0x48(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movq (%rcx,%rsi,8), %rcx
movq 0x40(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movl (%rcx,%rsi,4), %ecx
movq 0x38(%rdx), %rdx
addl 0x4(%rdx), %ecx
cmpl %ecx, %eax
jne 0x37c1c
movq 0x30(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x48(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x37d97
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37c89
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq 0x48(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x40(%rsp), %rdx
movslq (%rdx), %rdx
movl (%rax,%rdx,4), %eax
movq 0x10(%rcx), %rcx
movq 0x48(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x40(%rsp), %rdx
movslq (%rdx), %rdx
cmpl (%rcx,%rdx,4), %eax
je 0x37c6c
movq 0x48(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x37c89
movq 0x30(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x37d95
movq 0x48(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x37cf6
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq 0x48(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x40(%rsp), %rdx
movslq (%rdx), %rdx
movl (%rax,%rdx,4), %eax
movq 0x8(%rcx), %rcx
movq 0x48(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x40(%rsp), %rdx
movslq (%rdx), %rdx
cmpl (%rcx,%rdx,4), %eax
je 0x37cd9
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x37cf6
movq 0x30(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x48(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x37d93
movq 0x5f2f3(%rip), %rdi     # 0x96ff0
leaq 0x45b94(%rip), %rsi     # 0x7d898
callq 0x8360
movq %rax, %rdi
movq 0x5f29d(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x5f2d1(%rip), %rdi     # 0x96ff0
leaq 0x45b8f(%rip), %rsi     # 0x7d8b5
callq 0x8360
movq %rax, %rdi
movq 0x5f27b(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x5f2af(%rip), %rdi     # 0x96ff0
leaq 0x45b8b(%rip), %rsi     # 0x7d8d3
callq 0x8360
movq %rax, %rdi
movq 0x48(%rsp), %rax
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x45b77(%rip), %rsi     # 0x7d8dd
callq 0x8360
movq %rax, %rdi
movq 0x40(%rsp), %rax
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
movq 0x5f22c(%rip), %rsi     # 0x96fb0
callq 0x83b0
movl $0x1, %edi
callq 0x84a0
jmp 0x37d95
jmp 0x37d97
jmp 0x37d99
jmp 0x37a61
xorl %eax, %eax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)