// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/dp.cpp
// Function: dynProg::dpTraceBackFull(int&, int&, dp_output&, std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char>>&, boundaries const&, bool, 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::dpTraceBackFull(int& i, int& j, dp_output& output, std::stringstream & ss, 
        const boundaries& offset, bool forward, 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;
    int bandL = max(bandLeft, bandSize);
    int bandR = max(bandRight, bandSize);
    int bandCorrection = 0;
    while( (i > qBound && j > rBound)){
        bool equal = false;
        if(j > 0 && i > 0)
            equal = forward ? ref[offset.refB+j-1] == query[offset.queryB+i-1] : ref[offset.refE-j+1] == query[offset.queryE-i+1];
        if( j > 0 && i > 0 && M[ i ][ j ] == M[ i-1 ][ j-1 ] + scores.match && equal){
            ss << '=';
            i-- ;  j-- ;
        }
        else if(j > 0 && i > 0 && M[ i ][ j ] == M[ i-1 ][ j-1 ] + scores.mismatch){
            ss << 'X';
            output.editDist++;
            i-- ;  j-- ;
        }
        else if(j > 0 && j > i+bandCorrection - bandL && (M[ i ][ j ] == LEFT[ i ][ j ] || i==0)){
            ss << 'D';
            output.editDist++;
            j-- ;
        }
        else if(i > 0 && j < i+bandCorrection + bandR && (M[ i ][ j ] == UP[ i ][ j ] || j==0)){
            ss << 'I';
            output.editDist++;
            i-- ;
        }
        else{
            cerr << "Error in dynamic programming" << endl;
            cerr << "No valid trace position found" << endl;
            cerr << "Position " << i << ", " << j << endl;
            cerr << "Scores: Current " << M[ i ][ j ] << endl;
            if(j > 0 && i > 0){
                cerr << "Scores: Diag "    << M[ i-1 ][ j-1 ] << endl;
                cerr << "Characters "    << ref[offset.refB+j-1] << "  " << query[offset.queryB+i-1] << endl; 
            }
            cerr << "BandSize " << max(max(bandSize, bandL), bandRight) << endl;
            if(j > 0)
                cerr << "Scores: Left "    << M[ i ][ j-1 ] << endl;
            if(i > 0)
                cerr << "Scores: Up "    << M[ i-1 ][ j ] << endl;
            exit(1);
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r12
movq %r8, %r13
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl 0x28(%rdi), %ebp
movl 0x2c(%rdi), %eax
cmpl %ebp, %eax
cmovlel %ebp, %eax
movl %eax, 0xc(%rsp)
movl 0x30(%rdi), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
addq $0x10, %r13
movslq (%r14), %rax
testq %rax, %rax
jle 0x168bd
movslq (%rbx), %rcx
testq %rcx, %rcx
jle 0x168bd
cmpb $0x0, 0x50(%rsp)
je 0x167da
movq 0x58(%rsp), %rdx
movq (%rdx), %rdx
addq (%r12), %rdx
leaq (%rcx,%rdx), %rsi
decq %rsi
movl 0x10(%r12), %edi
addl %eax, %edi
pushq $-0x1
jmp 0x167f9
movq 0x8(%r12), %rdx
subq %rcx, %rdx
movq 0x58(%rsp), %rsi
movq (%rsi), %rsi
addq %rdx, %rsi
incq %rsi
movl 0x14(%r12), %edi
subl %eax, %edi
pushq $0x1
popq %r8
movq (%r15), %rdx
movq 0x38(%r15), %r9
movq -0x8(%rdx,%rax,8), %r10
movq (%rdx,%rax,8), %rdx
movl (%rdx,%rcx,4), %edx
movl -0x4(%r10,%rcx,4), %r10d
movl (%r9), %r11d
addl %r10d, %r11d
cmpl %r11d, %edx
jne 0x1683f
movslq %edi, %rdi
movq 0x60(%rsp), %r11
addq (%r11), %rdi
movb (%r8,%rdi), %dil
cmpb %dil, (%rsi)
jne 0x1683f
movq %r13, %rdi
pushq $0x3d
popq %rsi
callq 0x7330
jmp 0x1685a
addl 0x4(%r9), %r10d
cmpl %r10d, %edx
jne 0x16867
movq %r13, %rdi
pushq $0x58
popq %rsi
callq 0x7330
movq 0x10(%rsp), %rax
incl (%rax)
decl (%r14)
movq %rbx, %rax
decl (%rax)
jmp 0x1679d
movl %eax, %esi
subl 0xc(%rsp), %esi
cmpl %esi, %ecx
jle 0x16892
movq 0x10(%r15), %rsi
movq (%rsi,%rax,8), %rsi
cmpl (%rsi,%rcx,4), %edx
jne 0x16892
movq %r13, %rdi
pushq $0x44
popq %rsi
callq 0x7330
movq 0x10(%rsp), %rax
incl (%rax)
jmp 0x1685d
leal (%rax,%rbp), %esi
cmpl %esi, %ecx
jge 0x168ce
movq 0x8(%r15), %rsi
movq (%rsi,%rax,8), %rax
cmpl (%rax,%rcx,4), %edx
jne 0x168ce
movq %r13, %rdi
pushq $0x49
popq %rsi
callq 0x7330
movq 0x10(%rsp), %rax
incl (%rax)
movq %r14, %rax
jmp 0x16860
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x33713(%rip), %r13     # 0x49fe8
leaq 0x1ffec(%rip), %rsi     # 0x368c8
movq %r13, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
leaq 0x1fff2(%rip), %rsi     # 0x368e5
movq %r13, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
leaq 0x1fff9(%rip), %rsi     # 0x36903
movq %r13, %rdi
callq 0x72b0
movl (%r14), %esi
movq %rax, %rdi
callq 0x75c0
leaq 0x1ffe9(%rip), %rsi     # 0x3690d
movq %rax, %rdi
callq 0x72b0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
leaq 0x1ffcb(%rip), %rsi     # 0x36910
movq %r13, %rdi
callq 0x72b0
movq (%r15), %rcx
movslq (%r14), %rdx
movq (%rcx,%rdx,8), %rcx
movslq (%rbx), %rdx
movl (%rcx,%rdx,4), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
cmpl $0x0, (%rbx)
jle 0x16a1a
cmpl $0x0, (%r14)
jle 0x16a1a
movq 0x33661(%rip), %r13     # 0x49fe8
leaq 0x1ff93(%rip), %rsi     # 0x36921
movq %r13, %rdi
callq 0x72b0
movq (%r15), %rcx
movslq (%r14), %rdx
movq -0x8(%rcx,%rdx,8), %rcx
movslq (%rbx), %rdx
movl -0x4(%rcx,%rdx,4), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
leaq 0x1ff70(%rip), %rsi     # 0x3692f
movq %r13, %rdi
callq 0x72b0
movslq (%rbx), %rcx
movq 0x58(%rsp), %rdx
movq (%rdx), %rdx
addq (%r12), %rdx
movsbl -0x1(%rcx,%rdx), %esi
movq %rax, %rdi
callq 0x7330
leaq 0x1ff7f(%rip), %rsi     # 0x36969
movq %rax, %rdi
callq 0x72b0
movslq 0x10(%r12), %rcx
movslq (%r14), %rdx
addq %rcx, %rdx
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movsbl -0x1(%rcx,%rdx), %esi
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
callq 0x7160
movq 0x335c7(%rip), %rdi     # 0x49fe8
leaq 0x1ff13(%rip), %rsi     # 0x3693b
callq 0x72b0
movl 0x28(%r15), %ecx
movl 0xc(%rsp), %esi
cmpl %esi, %ecx
cmovgl %ecx, %esi
movl 0x30(%r15), %ecx
cmpl %ecx, %esi
cmovlel %ecx, %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
cmpl $0x0, (%rbx)
jle 0x16a8c
movq 0x33589(%rip), %rdi     # 0x49fe8
leaq 0x1fedf(%rip), %rsi     # 0x36945
callq 0x72b0
movq (%r15), %rcx
movslq (%r14), %rdx
movq (%rcx,%rdx,8), %rcx
movslq (%rbx), %rdx
movl -0x4(%rcx,%rdx,4), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
cmpl $0x0, (%r14)
jle 0x16ac6
movq 0x3354f(%rip), %rdi     # 0x49fe8
leaq 0x1feb3(%rip), %rsi     # 0x36953
callq 0x72b0
movq (%r15), %rcx
movslq (%r14), %rdx
movq -0x8(%rcx,%rdx,8), %rcx
movslq (%rbx), %rdx
movl (%rcx,%rdx,4), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
pushq $0x1
popq %rdi
callq 0x7390