// File: 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:
subq $0x88, %rsp
movq 0xa8(%rsp), %rax
movq 0xa0(%rsp), %rax
movb 0x98(%rsp), %al
movq 0x90(%rsp), %r10
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movq %r9, 0x58(%rsp)
andb $0x1, %al
movb %al, 0x57(%rsp)
movq 0x80(%rsp), %rsi
movq %rsi, 0x30(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movq %rsi, %rdi
addq $0x2c, %rdi
addq $0x28, %rsi
callq 0x14d40
movq 0x30(%rsp), %rsi
movl (%rax), %eax
movl %eax, 0x48(%rsp)
movq %rsi, %rdi
addq $0x30, %rdi
addq $0x28, %rsi
callq 0x14d40
movl (%rax), %eax
movl %eax, 0x44(%rsp)
movq 0x68(%rsp), %rcx
movb $0x1, %al
testb $0x1, 0x4(%rcx)
movb %al, 0x3a(%rsp)
jne 0x35210
movq 0x68(%rsp), %rcx
xorl %eax, %eax
testb $0x1, 0x2(%rcx)
movb %al, 0x2f(%rsp)
je 0x35208
movq 0x68(%rsp), %rax
movb (%rax), %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
movb %al, 0x3a(%rsp)
movb 0x3a(%rsp), %al
andb $0x1, %al
movb %al, 0x43(%rsp)
movl $0x0, 0x3c(%rsp)
movq 0x78(%rsp), %rax
movl (%rax), %eax
cmpl 0x50(%rsp), %eax
jle 0x35242
movq 0x70(%rsp), %rax
movl (%rax), %ecx
movb $0x1, %al
cmpl 0x4c(%rsp), %ecx
movb %al, 0x2e(%rsp)
jg 0x3524a
movb 0x43(%rsp), %al
movb %al, 0x2e(%rsp)
movb 0x2e(%rsp), %al
testb $0x1, %al
jne 0x35257
jmp 0x35900
movb $0x0, 0x3b(%rsp)
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x35358
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x35358
testb $0x1, 0x57(%rsp)
je 0x352e7
movq 0xa0(%rsp), %rdi
movq 0x90(%rsp), %rax
movq (%rax), %rsi
movq 0x70(%rsp), %rax
movslq (%rax), %rax
addq %rax, %rsi
subq $0x1, %rsi
callq 0x8410
movsbl (%rax), %eax
movl %eax, 0x28(%rsp)
movq 0xa8(%rsp), %rdi
movq 0x90(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x78(%rsp), %rcx
addl (%rcx), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8410
movq %rax, %rcx
movl 0x28(%rsp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
sete %al
movb %al, 0x2d(%rsp)
jmp 0x3534e
movq 0xa0(%rsp), %rdi
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x70(%rsp), %rax
movslq (%rax), %rax
subq %rax, %rsi
addq $0x1, %rsi
callq 0x8410
movsbl (%rax), %eax
movl %eax, 0x24(%rsp)
movq 0xa8(%rsp), %rdi
movq 0x90(%rsp), %rax
movl 0x14(%rax), %eax
movq 0x78(%rsp), %rcx
subl (%rcx), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x8410
movq %rax, %rcx
movl 0x24(%rsp), %eax
movsbl (%rcx), %ecx
cmpl %ecx, %eax
sete %al
movb %al, 0x2d(%rsp)
movb 0x2d(%rsp), %al
andb $0x1, %al
movb %al, 0x3b(%rsp)
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x353f8
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x353f8
movq 0x30(%rsp), %rdx
movq (%rdx), %rax
movq 0x78(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq (%rdx), %rcx
movq 0x78(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movq (%rcx,%rsi,8), %rcx
movq 0x70(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movl (%rcx,%rsi,4), %ecx
movq 0x38(%rdx), %rdx
addl (%rdx), %ecx
cmpl %ecx, %eax
jne 0x353f8
testb $0x1, 0x3b(%rsp)
je 0x353f8
movq 0x58(%rsp), %rdi
addq $0x10, %rdi
movl $0x3d, %esi
callq 0x8440
movq 0x78(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq 0x70(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x358fb
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x3549e
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x3549e
movq 0x30(%rsp), %rdx
movq (%rdx), %rax
movq 0x78(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq (%rdx), %rcx
movq 0x78(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movq (%rcx,%rsi,8), %rcx
movq 0x70(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movl (%rcx,%rsi,4), %ecx
movq 0x38(%rdx), %rdx
addl 0x4(%rdx), %ecx
cmpl %ecx, %eax
jne 0x3549e
movq 0x58(%rsp), %rdi
addq $0x10, %rdi
movl $0x58, %esi
callq 0x8440
movq 0x60(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x78(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq 0x70(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x358f9
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x3554d
movq 0x70(%rsp), %rax
movl (%rax), %eax
movq 0x78(%rsp), %rcx
movl (%rcx), %ecx
addl 0x3c(%rsp), %ecx
subl 0x48(%rsp), %ecx
cmpl %ecx, %eax
jle 0x3554d
movq 0x30(%rsp), %rdx
movq (%rdx), %rax
movq 0x78(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq (%rdx), %rcx
movq 0x78(%rsp), %rsi
movslq (%rsi), %rsi
movq (%rcx,%rsi,8), %rcx
movq 0x70(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movl (%rcx,%rsi,4), %ecx
movq 0x38(%rdx), %rdx
addl 0xc(%rdx), %ecx
cmpl %ecx, %eax
je 0x3551d
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x3554d
movq 0x58(%rsp), %rdi
addq $0x10, %rdi
movl $0x44, %esi
callq 0x8440
movq 0x60(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x70(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x358f7
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x355fc
movq 0x70(%rsp), %rax
movl (%rax), %eax
movq 0x78(%rsp), %rcx
movl (%rcx), %ecx
addl 0x3c(%rsp), %ecx
addl 0x44(%rsp), %ecx
cmpl %ecx, %eax
jge 0x355fc
movq 0x30(%rsp), %rdx
movq (%rdx), %rax
movq 0x78(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
movq (%rdx), %rcx
movq 0x78(%rsp), %rsi
movl (%rsi), %esi
subl $0x1, %esi
movslq %esi, %rsi
movq (%rcx,%rsi,8), %rcx
movq 0x70(%rsp), %rsi
movslq (%rsi), %rsi
movl (%rcx,%rsi,4), %ecx
movq 0x38(%rdx), %rdx
addl 0xc(%rdx), %ecx
cmpl %ecx, %eax
je 0x355cc
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x355fc
movq 0x58(%rsp), %rdi
addq $0x10, %rdi
movl $0x49, %esi
callq 0x8440
movq 0x60(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x78(%rsp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0x358f5
movq 0x619ed(%rip), %rdi     # 0x96ff0
leaq 0x4828e(%rip), %rsi     # 0x7d898
callq 0x8360
movq %rax, %rdi
movq 0x61997(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x619cb(%rip), %rdi     # 0x96ff0
leaq 0x48289(%rip), %rsi     # 0x7d8b5
callq 0x8360
movq %rax, %rdi
movq 0x61975(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x619a9(%rip), %rdi     # 0x96ff0
leaq 0x48285(%rip), %rsi     # 0x7d8d3
callq 0x8360
movq %rax, %rdi
movq 0x78(%rsp), %rax
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x48271(%rip), %rsi     # 0x7d8dd
callq 0x8360
movq %rax, %rdi
movq 0x70(%rsp), %rax
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
movq 0x61926(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x6195a(%rip), %rdi     # 0x96ff0
leaq 0x48243(%rip), %rsi     # 0x7d8e0
callq 0x8360
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq 0x78(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %esi
callq 0x8710
movq %rax, %rdi
movq 0x618dd(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x357e8
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x357e8
movq 0x618f5(%rip), %rdi     # 0x96ff0
leaq 0x481ef(%rip), %rsi     # 0x7d8f1
callq 0x8360
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq 0x78(%rsp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %esi
callq 0x8710
movq %rax, %rdi
movq 0x6186e(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x618a2(%rip), %rdi     # 0x96ff0
leaq 0x481aa(%rip), %rsi     # 0x7d8ff
callq 0x8360
movq %rax, 0x10(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x90(%rsp), %rax
movq (%rax), %rsi
movq 0x70(%rsp), %rax
movslq (%rax), %rax
addq %rax, %rsi
subq $0x1, %rsi
callq 0x8410
movq 0x10(%rsp), %rdi
movsbl (%rax), %esi
callq 0x8440
movq %rax, %rdi
leaq 0x4819c(%rip), %rsi     # 0x7d939
callq 0x8360
movq %rax, 0x18(%rsp)
movq 0xa8(%rsp), %rdi
movq 0x90(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x78(%rsp), %rcx
addl (%rcx), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8410
movq 0x18(%rsp), %rdi
movsbl (%rax), %esi
callq 0x8440
movq %rax, %rdi
movq 0x617cd(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x61801(%rip), %rdi     # 0x96ff0
leaq 0x48115(%rip), %rsi     # 0x7d90b
callq 0x8360
movq 0x30(%rsp), %rdi
movq %rax, 0x8(%rsp)
addq $0x28, %rdi
leaq 0x48(%rsp), %rsi
callq 0x14d40
movq %rax, %rdi
leaq 0x44(%rsp), %rsi
callq 0x14d40
movq 0x8(%rsp), %rdi
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
movq 0x6177a(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x35893
movq 0x617a4(%rip), %rdi     # 0x96ff0
leaq 0x480c2(%rip), %rsi     # 0x7d915
callq 0x8360
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq 0x78(%rsp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %esi
callq 0x8710
movq %rax, %rdi
movq 0x61722(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x358eb
movq 0x6174c(%rip), %rdi     # 0x96ff0
leaq 0x48078(%rip), %rsi     # 0x7d923
callq 0x8360
movq %rax, %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq 0x78(%rsp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq 0x70(%rsp), %rcx
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %esi
callq 0x8710
movq %rax, %rdi
movq 0x616ca(%rip), %rsi     # 0x96fb0
callq 0x83b0
movl $0x1, %edi
callq 0x84a0
jmp 0x358f7
jmp 0x358f9
jmp 0x358fb
jmp 0x35222
xorl %eax, %eax
addq $0x88, %rsp
retq
nopw (%rax,%rax)