// File: /workspace/github_cpp_makefile_star2_O2/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
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 0x38(%rdi), %r10
movq 0x10(%rdi), %rdi
movslq (%r14), %r11
testq %r11, %r11
jle 0x184af
movslq (%rbx), %r15
testq %r15, %r15
jle 0x184af
movq -0x8(%rsi,%r11,8), %r12
movq (%rsi,%r11,8), %r13
movl (%r13,%r15,4), %ebp
movl -0x4(%r12,%r15,4), %r12d
movl (%r10), %r13d
addl %r12d, %r13d
cmpl %r13d, %ebp
jne 0x18477
movslq 0x10(%rcx), %r13
addq %r11, %r13
movb -0x1(%rdx,%r13), %r13b
cmpb %r13b, (%rax,%r15)
jne 0x18477
decl %r11d
movl %r11d, (%r14)
jmp 0x18495
addl 0x4(%r10), %r12d
cmpl %r12d, %ebp
jne 0x18488
incl (%r8)
decl (%r14)
jmp 0x18495
movq (%rdi,%r11,8), %r12
cmpl (%r12,%r15,4), %ebp
jne 0x1849d
incl (%r8)
movq %rbx, %r11
decl (%r11)
jmp 0x1842f
movq (%r9,%r11,8), %r11
cmpl (%r11,%r15,4), %ebp
jne 0x184c0
incl (%r8)
movq %r14, %r11
jmp 0x18498
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x31b21(%rip), %r15     # 0x49fe8
leaq 0x1e3fa(%rip), %rsi     # 0x368c8
movq %r15, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
leaq 0x1e400(%rip), %rsi     # 0x368e5
movq %r15, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
leaq 0x1e407(%rip), %rsi     # 0x36903
movq %r15, %rdi
callq 0x72b0
movl (%r14), %esi
movq %rax, %rdi
callq 0x75c0
leaq 0x1e3f7(%rip), %rsi     # 0x3690d
movq %rax, %rdi
callq 0x72b0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
pushq $0x1
popq %rdi
callq 0x7390