// File: /workspace/github_cpp_makefile_star2_O2/readmapping[P]alfalfa/src/dp.cpp
// Function: dynProg::dpTraceBackOptFull(int&, int&, dp_type const&, 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::dpTraceBackOptFull(int& i, int& j, const dp_type& type, 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);
    bool local = type.local || (type.freeQueryB && type.freeRefB);
    int bandCorrection = 0;
    while( (i > qBound && j > rBound) || local){
        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 ] == M[ i ][ j-1 ] + scores.extendGap || i==0)){
            ss << 'D';
            output.editDist++;
            j-- ;
        }
        else if(i > 0 && j < i+bandCorrection + bandR && (M[ i ][ j ] == M[ i-1 ][ j ] + scores.extendGap || 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), bandR) << 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 %rdx, %rbx
movq %rdi, %r14
movl 0x28(%rdi), %edx
movl 0x2c(%rdi), %eax
cmpl %edx, %eax
cmovlel %edx, %eax
movl %eax, 0xc(%rsp)
movl 0x30(%rdi), %eax
cmpl %edx, %eax
cmovgl %eax, %edx
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
movb $0x1, %bpl
cmpb $0x0, 0x4(%rcx)
jne 0x16ed3
cmpb $0x1, 0x2(%rcx)
jne 0x16ed1
movb (%rcx), %bpl
jmp 0x16ed3
xorl %ebp, %ebp
addq $0x10, %r12
movslq (%r15), %rax
testq %rax, %rax
setg %dl
movl (%rbx), %ecx
testl %ecx, %ecx
setg %sil
andb %dl, %sil
orb %bpl, %sil
testb $0x1, %sil
je 0x17053
movl %ecx, %edx
testl %edx, %edx
setle %sil
testl %eax, %eax
setle %dil
orb %sil, %dil
jne 0x16f3d
movq 0x68(%rsp), %rsi
movq (%rsi), %rsi
cmpb $0x0, 0x58(%rsp)
je 0x16f51
movq 0x60(%rsp), %rdi
movq (%rdi), %rdi
movq 0x50(%rsp), %r8
addq (%r8), %rdi
movb -0x1(%rdx,%rdi), %dil
movslq 0x10(%r8), %r8
addq %rax, %r8
cmpb -0x1(%rsi,%r8), %dil
jmp 0x16f76
testl %edx, %edx
jle 0x16ff5
testl %eax, %eax
jle 0x16fd0
xorl %esi, %esi
jmp 0x16f7a
movq 0x50(%rsp), %r9
movq 0x8(%r9), %rdi
subq %rdx, %rdi
movq 0x60(%rsp), %r8
movq (%r8), %r8
movb 0x1(%r8,%rdi), %dil
movslq 0x14(%r9), %r8
subq %rax, %r8
cmpb 0x1(%rsi,%r8), %dil
sete %sil
movq (%r14), %r8
movq 0x38(%r14), %rdi
movq -0x8(%r8,%rax,8), %r9
movq (%r8,%rax,8), %r8
movl (%r8,%rdx,4), %r8d
movl -0x4(%r9,%rdx,4), %r9d
movl (%rdi), %r10d
addl %r9d, %r10d
cmpl %r10d, %r8d
jne 0x16fb0
testb %sil, %sil
je 0x16fb0
movq %r12, %rdi
pushq $0x3d
popq %rsi
callq 0x7330
jmp 0x16fc8
addl 0x4(%rdi), %r9d
cmpl %r9d, %r8d
jne 0x16fd0
movq %r12, %rdi
pushq $0x58
popq %rsi
callq 0x7330
incl (%r13)
decl (%r15)
movq %rbx, %rax
jmp 0x17038
movl %eax, %esi
subl 0xc(%rsp), %esi
cmpl %esi, %ecx
jle 0x16ff5
testl %eax, %eax
je 0x1703f
movq (%r14), %rsi
movq 0x38(%r14), %rdi
movq (%rsi,%rax,8), %rsi
movl 0xc(%rdi), %edi
addl -0x4(%rsi,%rdx,4), %edi
cmpl %edi, (%rsi,%rdx,4)
je 0x1703f
testl %eax, %eax
jle 0x17064
movq 0x10(%rsp), %rsi
addl %eax, %esi
cmpl %esi, %edx
jge 0x17064
testl %ecx, %ecx
je 0x17026
movslq %ecx, %rcx
movq (%r14), %rdx
movq 0x38(%r14), %rsi
movq -0x8(%rdx,%rax,8), %rdi
movq (%rdx,%rax,8), %rax
movl 0xc(%rsi), %edx
addl (%rdi,%rcx,4), %edx
cmpl %edx, (%rax,%rcx,4)
jne 0x17064
movq %r12, %rdi
pushq $0x49
popq %rsi
callq 0x7330
incl (%r13)
movq %r15, %rax
decl (%rax)
jmp 0x16ed7
movq %r12, %rdi
pushq $0x44
popq %rsi
callq 0x7330
incl (%r13)
jmp 0x16fcb
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x32f7d(%rip), %r12     # 0x49fe8
leaq 0x1f856(%rip), %rsi     # 0x368c8
movq %r12, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
leaq 0x1f85c(%rip), %rsi     # 0x368e5
movq %r12, %rdi
callq 0x72b0
movq %rax, %rdi
callq 0x7160
leaq 0x1f863(%rip), %rsi     # 0x36903
movq %r12, %rdi
callq 0x72b0
movl (%r15), %esi
movq %rax, %rdi
callq 0x75c0
leaq 0x1f853(%rip), %rsi     # 0x3690d
movq %rax, %rdi
callq 0x72b0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
leaq 0x1f835(%rip), %rsi     # 0x36910
movq %r12, %rdi
callq 0x72b0
movq (%r14), %rcx
movslq (%r15), %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 0x171b5
cmpl $0x0, (%r15)
jle 0x171b5
movq 0x32ecb(%rip), %r12     # 0x49fe8
leaq 0x1f7fd(%rip), %rsi     # 0x36921
movq %r12, %rdi
callq 0x72b0
movq (%r14), %rcx
movslq (%r15), %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 0x1f7da(%rip), %rsi     # 0x3692f
movq %r12, %rdi
callq 0x72b0
movslq (%rbx), %rcx
movq 0x60(%rsp), %rdx
movq (%rdx), %rdx
movq 0x50(%rsp), %r12
addq (%r12), %rdx
movsbl -0x1(%rcx,%rdx), %esi
movq %rax, %rdi
callq 0x7330
leaq 0x1f7e4(%rip), %rsi     # 0x36969
movq %rax, %rdi
callq 0x72b0
movslq 0x10(%r12), %rcx
movslq (%r15), %rdx
addq %rcx, %rdx
movq 0x68(%rsp), %rcx
movq (%rcx), %rcx
movsbl -0x1(%rcx,%rdx), %esi
movq %rax, %rdi
callq 0x7330
movq %rax, %rdi
callq 0x7160
movq 0x32e2c(%rip), %rdi     # 0x49fe8
leaq 0x1f778(%rip), %rsi     # 0x3693b
callq 0x72b0
movl 0x28(%r14), %ecx
movl 0xc(%rsp), %esi
cmpl %esi, %ecx
cmovgl %ecx, %esi
movq 0x10(%rsp), %rcx
cmpl %ecx, %esi
cmovlel %ecx, %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
cmpl $0x0, (%rbx)
jle 0x17228
movq 0x32ded(%rip), %rdi     # 0x49fe8
leaq 0x1f743(%rip), %rsi     # 0x36945
callq 0x72b0
movq (%r14), %rcx
movslq (%r15), %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, (%r15)
jle 0x17262
movq 0x32db3(%rip), %rdi     # 0x49fe8
leaq 0x1f717(%rip), %rsi     # 0x36953
callq 0x72b0
movq (%r14), %rcx
movslq (%r15), %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