// File: 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:
subq $0x88, %rsp
movq 0xa0(%rsp), %rax
movq 0x98(%rsp), %rax
movb 0x90(%rsp), %al
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)
movl $0x0, 0x40(%rsp)
movq 0x78(%rsp), %rax
movl (%rax), %ecx
xorl %eax, %eax
cmpl 0x50(%rsp), %ecx
movb %al, 0x2f(%rsp)
jle 0x34360
movq 0x70(%rsp), %rax
movl (%rax), %eax
cmpl 0x4c(%rsp), %eax
setg %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0x3436d
jmp 0x349e6
movb $0x0, 0x3f(%rsp)
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x34462
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x34462
testb $0x1, 0x57(%rsp)
je 0x343f7
movq 0x98(%rsp), %rdi
movq 0x58(%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 0xa0(%rsp), %rdi
movq 0x58(%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, 0x2e(%rsp)
jmp 0x34458
movq 0x98(%rsp), %rdi
movq 0x58(%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 0xa0(%rsp), %rdi
movq 0x58(%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, 0x2e(%rsp)
movb 0x2e(%rsp), %al
andb $0x1, %al
movb %al, 0x3f(%rsp)
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x34502
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x34502
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 0x34502
testb $0x1, 0x3f(%rsp)
je 0x34502
movq 0x60(%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 0x349e1
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x345a8
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x345a8
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 0x345a8
movq 0x60(%rsp), %rdi
addq $0x10, %rdi
movl $0x58, %esi
callq 0x8440
movq 0x68(%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 0x349df
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x34646
movq 0x70(%rsp), %rax
movl (%rax), %eax
movq 0x78(%rsp), %rcx
movl (%rcx), %ecx
addl 0x40(%rsp), %ecx
subl 0x48(%rsp), %ecx
cmpl %ecx, %eax
jle 0x34646
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x78(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x70(%rsp), %rdx
movslq (%rdx), %rdx
movl (%rax,%rdx,4), %eax
movq 0x10(%rcx), %rcx
movq 0x78(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x70(%rsp), %rdx
movslq (%rdx), %rdx
cmpl (%rcx,%rdx,4), %eax
je 0x34616
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x34646
movq 0x60(%rsp), %rdi
addq $0x10, %rdi
movl $0x44, %esi
callq 0x8440
movq 0x68(%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 0x349dd
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x346e4
movq 0x70(%rsp), %rax
movl (%rax), %eax
movq 0x78(%rsp), %rcx
movl (%rcx), %ecx
addl 0x40(%rsp), %ecx
addl 0x44(%rsp), %ecx
cmpl %ecx, %eax
jge 0x346e4
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x78(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rax,%rdx,8), %rax
movq 0x70(%rsp), %rdx
movslq (%rdx), %rdx
movl (%rax,%rdx,4), %eax
movq 0x8(%rcx), %rcx
movq 0x78(%rsp), %rdx
movslq (%rdx), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x70(%rsp), %rdx
movslq (%rdx), %rdx
cmpl (%rcx,%rdx,4), %eax
je 0x346b4
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x346e4
movq 0x60(%rsp), %rdi
addq $0x10, %rdi
movl $0x49, %esi
callq 0x8440
movq 0x68(%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 0x349db
movq 0x62905(%rip), %rdi     # 0x96ff0
leaq 0x491a6(%rip), %rsi     # 0x7d898
callq 0x8360
movq %rax, %rdi
movq 0x628af(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x628e3(%rip), %rdi     # 0x96ff0
leaq 0x491a1(%rip), %rsi     # 0x7d8b5
callq 0x8360
movq %rax, %rdi
movq 0x6288d(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x628c1(%rip), %rdi     # 0x96ff0
leaq 0x4919d(%rip), %rsi     # 0x7d8d3
callq 0x8360
movq %rax, %rdi
movq 0x78(%rsp), %rax
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x49189(%rip), %rsi     # 0x7d8dd
callq 0x8360
movq %rax, %rdi
movq 0x70(%rsp), %rax
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
movq 0x6283e(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x62872(%rip), %rdi     # 0x96ff0
leaq 0x4915b(%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 0x627f5(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x348ca
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x348ca
movq 0x6280d(%rip), %rdi     # 0x96ff0
leaq 0x49107(%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 0x62786(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x627ba(%rip), %rdi     # 0x96ff0
leaq 0x490c2(%rip), %rsi     # 0x7d8ff
callq 0x8360
movq %rax, 0x10(%rsp)
movq 0x98(%rsp), %rdi
movq 0x58(%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 0x490b7(%rip), %rsi     # 0x7d939
callq 0x8360
movq %rax, 0x18(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x58(%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 0x626eb(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x6271f(%rip), %rdi     # 0x96ff0
leaq 0x49033(%rip), %rsi     # 0x7d90b
callq 0x8360
movq 0x30(%rsp), %rdi
movq %rax, 0x8(%rsp)
addq $0x28, %rdi
leaq 0x48(%rsp), %rsi
callq 0x14d40
movq 0x30(%rsp), %rsi
movq %rax, %rdi
addq $0x30, %rsi
callq 0x14d40
movq 0x8(%rsp), %rdi
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
movq 0x62694(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x34979
movq 0x626be(%rip), %rdi     # 0x96ff0
leaq 0x48fdc(%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 0x6263c(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x349d1
movq 0x62666(%rip), %rdi     # 0x96ff0
leaq 0x48f92(%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 0x625e4(%rip), %rsi     # 0x96fb0
callq 0x83b0
movl $0x1, %edi
callq 0x84a0
jmp 0x349dd
jmp 0x349df
jmp 0x349e1
jmp 0x3433b
xorl %eax, %eax
addq $0x88, %rsp
retq