// File: /workspace/github_cpp_makefile_star2_O3/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 %rsi, %r14
movq %rdi, %r15
movl 0x28(%rdi), %edx
movl 0x2c(%rdi), %eax
cmpl %edx, %eax
cmovlel %edx, %eax
movl %eax, 0x10(%rsp)
movl 0x30(%rdi), %eax
cmpl %edx, %eax
cmovgl %eax, %edx
movl %edx, 0x14(%rsp)
cmpb $0x1, 0x4(%rcx)
jne 0x1c0bb
movl (%r14), %ecx
testl %ecx, %ecx
setg %dl
movl (%rbx), %eax
testl %eax, %eax
setg %dil
movb $0x1, %bpl
jmp 0x1c0e4
movb (%rcx), %bpl
andb 0x2(%rcx), %bpl
movl (%r14), %ecx
testl %ecx, %ecx
setg %dl
movl (%rbx), %eax
testl %eax, %eax
setg %dil
movl %edx, %esi
andb %dil, %sil
orb %bpl, %sil
testb $0x1, %sil
je 0x1c2af
addq $0x10, %r12
movl %ecx, %esi
testb $0x1, %dl
je 0x1c12c
testb $0x1, %dil
je 0x1c12c
movl %eax, %edi
movq 0x68(%rsp), %r8
movq (%r8), %r8
cmpb $0x0, 0x58(%rsp)
je 0x1c146
movq 0x60(%rsp), %r9
movq (%r9), %r9
movq 0x50(%rsp), %r10
addq (%r10), %r9
movb -0x1(%rdi,%r9), %r9b
movslq 0x10(%r10), %r10
movslq %ecx, %r11
addq %r10, %r11
cmpb -0x1(%r8,%r11), %r9b
jmp 0x1c16e
testb $0x1, %dil
je 0x1c218
testb $0x1, %dl
je 0x1c1e6
movl %eax, %edi
xorl %r8d, %r8d
jmp 0x1c172
movq 0x50(%rsp), %r11
movq 0x8(%r11), %r9
subq %rdi, %r9
movq 0x60(%rsp), %r10
movq (%r10), %r10
movb 0x1(%r10,%r9), %r9b
movslq 0x14(%r11), %r10
movslq %ecx, %r11
subq %r11, %r10
cmpb 0x1(%r8,%r10), %r9b
sete %r8b
movq (%r15), %r10
movq 0x38(%r15), %r9
movq -0x8(%r10,%rsi,8), %r11
movq (%r10,%rsi,8), %r10
movl (%r10,%rdi,4), %r10d
movl -0x4(%r11,%rdi,4), %edi
movl (%r9), %r11d
addl %edi, %r11d
cmpl %r11d, %r10d
jne 0x1c1b4
testb %r8b, %r8b
je 0x1c1b4
movb $0x3d, 0xa(%rsp)
movl $0x1, %edx
movq %r12, %rdi
leaq 0xa(%rsp), %rsi
callq 0x7340
jmp 0x1c1d8
addl 0x4(%r9), %edi
cmpl %edi, %r10d
jne 0x1c1e6
movb $0x58, 0xb(%rsp)
movl $0x1, %edx
movq %r12, %rdi
leaq 0xb(%rsp), %rsi
callq 0x7340
incl (%r13)
decl (%r14)
movl (%rbx), %eax
decl %eax
movl %eax, (%rbx)
jmp 0x1c26d
movl %ecx, %edi
subl 0x10(%rsp), %edi
cmpl %edi, %eax
jle 0x1c218
testl %ecx, %ecx
je 0x1c28f
movslq %ecx, %rdi
movq (%r15), %r8
movq 0x38(%r15), %r9
movq (%r8,%rdi,8), %rdi
movl %eax, %r8d
movl 0xc(%r9), %r9d
addl -0x4(%rdi,%r8,4), %r9d
cmpl %r9d, (%rdi,%r8,4)
je 0x1c28f
addl 0x14(%rsp), %ecx
cmpl %ecx, %eax
setl %cl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1c2c0
testl %eax, %eax
je 0x1c24d
movq (%r15), %rcx
movq 0x38(%r15), %rdx
movq -0x8(%rcx,%rsi,8), %rdi
movq (%rcx,%rsi,8), %rcx
cltq
movl 0xc(%rdx), %edx
addl (%rdi,%rax,4), %edx
cmpl %edx, (%rcx,%rax,4)
jne 0x1c2c0
movb $0x49, 0xd(%rsp)
movl $0x1, %edx
movq %r12, %rdi
leaq 0xd(%rsp), %rsi
callq 0x7340
incl (%r13)
decl (%r14)
movl (%rbx), %eax
movl (%r14), %ecx
testl %ecx, %ecx
setg %dl
testl %eax, %eax
setg %dil
movl %edx, %esi
andb %dil, %sil
orb %bpl, %sil
testb $0x1, %sil
jne 0x1c0e8
jmp 0x1c2af
movb $0x44, 0xc(%rsp)
movl $0x1, %edx
movq %r12, %rdi
leaq 0xc(%rsp), %rsi
callq 0x7340
incl (%r13)
jmp 0x1c1db
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x37d21(%rip), %r12     # 0x53fe8
leaq 0x285fa(%rip), %rsi     # 0x448c8
movl $0x1c, %edx
movq %r12, %rdi
callq 0x7340
movq %r12, %rdi
callq 0x7150
leaq 0x285fb(%rip), %rsi     # 0x448e5
movl $0x1d, %edx
movq %r12, %rdi
callq 0x7340
movq %r12, %rdi
callq 0x7150
leaq 0x285fd(%rip), %rsi     # 0x44903
movl $0x9, %edx
movq %r12, %rdi
callq 0x7340
movl (%r14), %esi
movq %r12, %rdi
callq 0x75d0
movq %rax, %r13
leaq 0x285e5(%rip), %rsi     # 0x4490d
movl $0x2, %edx
movq %rax, %rdi
callq 0x7340
movl (%rbx), %esi
movq %r13, %rdi
callq 0x75d0
movq %rax, %rdi
callq 0x7150
leaq 0x285c2(%rip), %rsi     # 0x44910
movl $0x10, %edx
movq %r12, %rdi
callq 0x7340
movq (%r15), %rax
movslq (%r14), %rcx
movq (%rax,%rcx,8), %rax
movslq (%rbx), %rcx
movl (%rax,%rcx,4), %esi
movq %r12, %rdi
callq 0x75d0
movq %rax, %rdi
callq 0x7150
cmpl $0x0, (%rbx)
jle 0x1c454
cmpl $0x0, (%r14)
jle 0x1c454
movq 0x37c53(%rip), %r12     # 0x53fe8
leaq 0x28585(%rip), %rsi     # 0x44921
movl $0xd, %edx
movq %r12, %rdi
callq 0x7340
movq (%r15), %rax
movslq (%r14), %rcx
movq -0x8(%rax,%rcx,8), %rax
movslq (%rbx), %rcx
movl -0x4(%rax,%rcx,4), %esi
movq %r12, %rdi
callq 0x75d0
movq %rax, %rdi
callq 0x7150
leaq 0x2855d(%rip), %rsi     # 0x4492f
movl $0xb, %edx
movq %r12, %rdi
callq 0x7340
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq 0x50(%rsp), %r13
addq (%r13), %rax
movslq (%rbx), %rcx
movb -0x1(%rcx,%rax), %al
leaq 0xe(%rsp), %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %r12, %rdi
callq 0x7340
movq %rax, %r12
leaq 0x28554(%rip), %rsi     # 0x44969
movl $0x2, %edx
movq %rax, %rdi
callq 0x7340
movslq 0x10(%r13), %rax
movslq (%r14), %rcx
addq %rax, %rcx
movq 0x68(%rsp), %rax
movq (%rax), %rax
movb -0x1(%rax,%rcx), %al
leaq 0xf(%rsp), %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %r12, %rdi
callq 0x7340
movq %rax, %rdi
callq 0x7150
movq 0x37b8d(%rip), %r12     # 0x53fe8
leaq 0x284d9(%rip), %rsi     # 0x4493b
movl $0x9, %edx
movq %r12, %rdi
callq 0x7340
movl 0x28(%r15), %eax
movl 0x10(%rsp), %esi
cmpl %esi, %eax
cmovgl %eax, %esi
movl 0x14(%rsp), %eax
cmpl %eax, %esi
cmovlel %eax, %esi
movq %r12, %rdi
callq 0x75d0
movq %rax, %rdi
callq 0x7150
cmpl $0x0, (%rbx)
jle 0x1c4d6
movq 0x37b47(%rip), %r12     # 0x53fe8
leaq 0x2849d(%rip), %rsi     # 0x44945
movl $0xd, %edx
movq %r12, %rdi
callq 0x7340
movq (%r15), %rax
movslq (%r14), %rcx
movq (%rax,%rcx,8), %rax
movslq (%rbx), %rcx
movl -0x4(%rax,%rcx,4), %esi
movq %r12, %rdi
callq 0x75d0
movq %rax, %rdi
callq 0x7150
cmpl $0x0, (%r14)
jle 0x1c518
movq 0x37b05(%rip), %r12     # 0x53fe8
leaq 0x28469(%rip), %rsi     # 0x44953
movl $0xb, %edx
movq %r12, %rdi
callq 0x7340
movq (%r15), %rax
movslq (%r14), %rcx
movq -0x8(%rax,%rcx,8), %rax
movslq (%rbx), %rcx
movl (%rax,%rcx,4), %esi
movq %r12, %rdi
callq 0x75d0
movq %rax, %rdi
callq 0x7150
movl $0x1, %edi
callq 0x73b0