// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl (%rsi), %r11d
testl %r11d, %r11d
jle 0x1e1ca
movq %rdx, %rbx
movq %rsi, %r14
movq 0x40(%rsp), %rdx
movq (%rcx), %rax
movq (%r9), %rsi
leaq -0x1(%rsi,%rax), %rax
movq (%rdx), %rdx
movq (%rdi), %rsi
movq 0x8(%rdi), %r9
movq %r9, (%rsp)
movq 0x38(%rdi), %r10
movq 0x10(%rdi), %rdi
movslq (%rbx), %r15
testq %r15, %r15
jle 0x1e1ca
movl %r11d, %r12d
movq -0x8(%rsi,%r12,8), %r13
movq (%rsi,%r12,8), %rbp
movl (%r10), %r9d
addl -0x4(%r13,%r15,4), %r9d
cmpl %r9d, (%rbp,%r15,4)
jne 0x1e163
movslq 0x10(%rcx), %r9
movslq %r11d, %r13
addq %r9, %r13
movb -0x1(%rdx,%r13), %r9b
cmpb %r9b, (%rax,%r15)
jne 0x1e163
decl %r11d
movl %r11d, (%r14)
jmp 0x1e198
movq -0x8(%rsi,%r12,8), %r9
movq (%rsi,%r12,8), %r11
movl 0x4(%r10), %ebp
addl -0x4(%r9,%r15,4), %ebp
cmpl %ebp, (%r11,%r15,4)
jne 0x1e183
incl (%r8)
decl (%r14)
jmp 0x1e198
movq (%rsi,%r12,8), %r9
movl (%r9,%r15,4), %r9d
movq (%rdi,%r12,8), %r11
cmpl (%r11,%r15,4), %r9d
jne 0x1e1ac
incl (%r8)
movq %rbx, %r11
decl (%r11)
movl (%r14), %r11d
testl %r11d, %r11d
jg 0x1e11f
jmp 0x1e1ca
movq (%rsi,%r12,8), %r9
movl (%r9,%r15,4), %r9d
movq (%rsp), %r11
movq (%r11,%r12,8), %r11
cmpl (%r11,%r15,4), %r9d
jne 0x1e1db
incl (%r8)
movq %r14, %r11
jmp 0x1e19b
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x35e06(%rip), %r15     # 0x53fe8
leaq 0x266df(%rip), %rsi     # 0x448c8
movq %r15, %rdi
callq 0x72c0
movq %rax, %rdi
callq 0x7150
leaq 0x266e5(%rip), %rsi     # 0x448e5
movq %r15, %rdi
callq 0x72c0
movq %rax, %rdi
callq 0x7150
leaq 0x266ec(%rip), %rsi     # 0x44903
movq %r15, %rdi
callq 0x72c0
movl (%r14), %esi
movq %rax, %rdi
callq 0x75d0
leaq 0x266dc(%rip), %rsi     # 0x4490d
movq %rax, %rdi
callq 0x72c0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x75d0
movq %rax, %rdi
callq 0x7150
movl $0x1, %edi
callq 0x73b0
nop