// File: readmapping[P]alfalfa/src/mapper.cpp
// Function: pairAlignmentToWindow(sparseSA const&, read_t&, read_t&, align_opt const&, paired_opt const&, std::vector<alnScoreInfo_t, std::allocator<alnScoreInfo_t>>&, std::vector<alnScoreInfo_t, std::allocator<alnScoreInfo_t>>&, int&, int)
void pairAlignmentToWindow(const sparseSA& sa, read_t & mate1, read_t & mate2, 
        const align_opt & alnOptions, const paired_opt & pairedOpt, vector<alnScoreInfo_t> & alnMateAlns, 
        vector<alnScoreInfo_t> & windowMateAlns, int & alnCount, int maxAlnCount){
    size_t i = 0;
    int trial = 0;
    while(i < alnMateAlns.size() && alnCount < maxAlnCount && trial < alnOptions.maxTrial){
        alignment_t * aln = alnMateAlns[i].firstMate ? mate1.alignments[alnMateAlns[i].position] : mate2.alignments[alnMateAlns[i].position]; 
        if(!aln->paired()){
            if(alnOptions.print >= 4) cerr << "find window for aligment of mate " << (alnMateAlns[i].firstMate ? "1" : "0") << " on strand " << (aln->flag.test(4) ? "R" : "F") << " and position " << aln->refB << endl;
            trial++;
            //FIND WINDOW
            long refB, refE;
            bool fw;
            read_t & windowMate = alnMateAlns[i].firstMate ? mate2 : mate1;
            int Plength = windowMate.sequence.length();
            dpWindow(aln->refB, aln->refE, alnMateAlns[i].firstMate, !aln->flag.test(4), 
                    Plength, pairedOpt, refB, refE, fw);
            if(alnOptions.print >= 4) cerr << "window is located on " << (fw ? "F" : "R") << " strand and position has interval " << refB << "," << refE << endl;
            //CHECK IF AN ALIGNMENT ALREADY EXISTS IN THIS WINDOW
            int windowMatePos = bestAlignmentInWindow(windowMate, windowMateAlns, alnMateAlns[i].firstMate, refB, refE, fw);
            if(alnOptions.print >= 4) cerr << "an alignment of the mate was found as the " << windowMatePos << " 'th alignment of the mate" << endl;
            if(alnOptions.print >= 2) cerr << "#WINDOWLIST " << (windowMatePos>=0) << endl;
            if(windowMatePos < 0){
                //TRY TO ALIGN IN WINDOW    
                int tLen = refE-refB+1;
                uint8_t * target = (uint8_t *)malloc(tLen);
                for (int j = 0; j < tLen; ++j) // convert to the nt4 encoding
                    target[j] = Utils::ORDVALUE[(size_t)sa.S[refB+j]];
                int xtra = KSW_XSTART | (Plength * alnOptions.match < 250? KSW_XBYTE : 0);
                kswr_t alnResult = ksw_align(Plength, fw ? windowMate.readP : windowMate.rcReadP, tLen, 
                        target, 5, alnOptions.mat, alnOptions.openGap, alnOptions.extendGap, xtra, 0);
                free(target);
                //if succes, set paired, trial back to 0
                int editDist = (int)(alnOptions.errorPercent*Plength)+1;
                int minScore = (int) (Plength*alnOptions.match + (alnOptions.mismatch-alnOptions.match)*editDist);
                if(alnOptions.print >= 4) cerr << "an alignment was produced with score " << alnResult.score << ". Min score is " << minScore << endl;
                if(alnResult.score > minScore){
                    alignment_t * newAln = new alignment_t();
                    newAln->alignmentScore = alnResult.score;
                    newAln->qB = alnResult.qb;
                    newAln->qE = alnResult.qe;
                    newAln->refB = refB + alnResult.tb;
                    newAln->refE = refB + alnResult.te;
                    newAln->refLength = alnResult.te - alnResult.tb + 1;
                    newAln->flag.set(4, !fw);
                    windowMate.alignments.push_back(newAln);
                    windowMatePos = windowMate.alignments.size()-1;
                    windowMateAlns.push_back(alnScoreInfo_t(!alnMateAlns[i].firstMate,windowMatePos,alnResult.score));
                }
                if(alnOptions.print >= 2) cerr << "#WINDOWALIGN " << (windowMatePos >= 0) << endl;
            }
            if(windowMatePos >= 0){
                //PAIR ALIGNMENTS, SUCCES
                if(alnOptions.print >= 4) cerr << "found concordancy between 2 alns." << endl;
                if(alnMateAlns[i].firstMate)
                    setPaired(sa, mate1, mate2, alnMateAlns[i].position,windowMatePos, true);
                else
                    setPaired(sa, mate1, mate2,windowMatePos, alnMateAlns[i].position, true);
                trial = 0;
                alnCount++;
            }
        }
        i++;
    }
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x1e0, %rsp            # imm = 0x1E0
movl 0x210(%rsp), %eax
movq 0x208(%rsp), %rax
movq 0x200(%rsp), %rax
movq %rdi, 0x1d8(%rsp)
movq %rsi, 0x1d0(%rsp)
movq %rdx, 0x1c8(%rsp)
movq %rcx, 0x1c0(%rsp)
movq %r8, 0x1b8(%rsp)
movq %r9, 0x1b0(%rsp)
movq $0x0, 0x1a8(%rsp)
movl $0x0, 0x1a4(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x4e5b0
movq 0x100(%rsp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, 0x10f(%rsp)
jae 0x44447
movq 0x208(%rsp), %rax
movl (%rax), %ecx
xorl %eax, %eax
cmpl 0x210(%rsp), %ecx
movb %al, 0x10f(%rsp)
jge 0x44447
movl 0x1a4(%rsp), %eax
movq 0x1c0(%rsp), %rcx
cmpl 0x54(%rcx), %eax
setl %al
movb %al, 0x10f(%rsp)
movb 0x10f(%rsp), %al
testb $0x1, %al
jne 0x44457
jmp 0x44eee
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
testb $0x1, (%rax)
je 0x444ba
movq 0x1d0(%rsp), %rax
addq $0xb0, %rax
movq %rax, 0xf0(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
movq 0xf0(%rsp), %rdi
movslq 0x4(%rax), %rsi
callq 0xdae0
movq (%rax), %rax
movq %rax, 0xf8(%rsp)
jmp 0x44501
movq 0x1c8(%rsp), %rax
addq $0xb0, %rax
movq %rax, 0xe8(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
movq 0xe8(%rsp), %rdi
movslq 0x4(%rax), %rsi
callq 0xdae0
movq (%rax), %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %rdi
callq 0xe470
testb $0x1, %al
jne 0x44ed5
movq 0x1c0(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x4463f
movq 0x52ab1(%rip), %rdi     # 0x96ff0
leaq 0x39c14(%rip), %rsi     # 0x7e15a
callq 0x8360
movq %rax, 0xe0(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
testb $0x1, (%rax)
je 0x4457e
leaq 0x34adc(%rip), %rax     # 0x79050
movq %rax, 0xd8(%rsp)
jmp 0x4458f
leaq 0x365e2(%rip), %rax     # 0x7ab67
movq %rax, 0xd8(%rsp)
jmp 0x4458f
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x8360
movq %rax, %rdi
leaq 0x39bce(%rip), %rsi     # 0x7e17c
callq 0x8360
movq %rax, 0xd0(%rsp)
movq 0x198(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
testb $0x1, %al
jne 0x445d7
jmp 0x445e8
leaq 0x39baa(%rip), %rax     # 0x7e188
movq %rax, 0xc8(%rsp)
jmp 0x445f9
leaq 0x39b9b(%rip), %rax     # 0x7e18a
movq %rax, 0xc8(%rsp)
jmp 0x445f9
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x8360
movq %rax, %rdi
leaq 0x39b74(%rip), %rsi     # 0x7e18c
callq 0x8360
movq %rax, %rdi
movq 0x198(%rsp), %rax
movq (%rax), %rsi
callq 0x8790
movq %rax, %rdi
movq 0x52976(%rip), %rsi     # 0x96fb0
callq 0x83b0
movl 0x1a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a4(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
testb $0x1, (%rax)
je 0x4467c
movq 0x1c8(%rsp), %rax
movq %rax, 0xc0(%rsp)
jmp 0x4468c
movq 0x1d0(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rdi
addq $0x20, %rdi
callq 0x8630
movl %eax, 0x174(%rsp)
movq 0x198(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0x198(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
movb (%rax), %al
movb %al, 0xaf(%rsp)
movq 0x198(%rsp), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
movb 0xaf(%rsp), %cl
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
xorb $-0x1, %al
movl 0x174(%rsp), %r8d
movq 0x1b8(%rsp), %r9
movq %rsp, %rdx
leaq 0x187(%rsp), %r10
movq %r10, 0x10(%rdx)
leaq 0x188(%rsp), %r10
movq %r10, 0x8(%rdx)
leaq 0x190(%rsp), %r10
movq %r10, (%rdx)
movzbl %cl, %edx
andl $0x1, %edx
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x43fa0
movq 0x1c0(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x4482a
movq 0x52868(%rip), %rdi     # 0x96ff0
leaq 0x39a0c(%rip), %rsi     # 0x7e19b
callq 0x8360
movq %rax, 0xa0(%rsp)
testb $0x1, 0x187(%rsp)
je 0x447b7
leaq 0x399dd(%rip), %rax     # 0x7e18a
movq %rax, 0x98(%rsp)
jmp 0x447c8
leaq 0x399ca(%rip), %rax     # 0x7e188
movq %rax, 0x98(%rsp)
jmp 0x447c8
movq 0xa0(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x8360
movq %rax, %rdi
leaq 0x399ca(%rip), %rsi     # 0x7e1b1
callq 0x8360
movq %rax, %rdi
movq 0x190(%rsp), %rsi
callq 0x8790
movq %rax, %rdi
leaq 0x394ea(%rip), %rsi     # 0x7dcf0
callq 0x8360
movq %rax, %rdi
movq 0x188(%rsp), %rsi
callq 0x8790
movq %rax, %rdi
movq 0x5278b(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x178(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x200(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
movq 0x88(%rsp), %rdi
movq 0x90(%rsp), %rsi
movb (%rax), %dl
movq 0x190(%rsp), %rcx
movq 0x188(%rsp), %r8
movb 0x187(%rsp), %al
andb $0x1, %dl
andb $0x1, %al
movzbl %dl, %edx
movzbl %al, %r9d
callq 0x441f0
movl %eax, 0x170(%rsp)
movq 0x1c0(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x448ee
movq 0x5273b(%rip), %rdi     # 0x96ff0
leaq 0x39918(%rip), %rsi     # 0x7e1d4
callq 0x8360
movq %rax, %rdi
movl 0x170(%rsp), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x39925(%rip), %rsi     # 0x7e1ff
callq 0x8360
movq %rax, %rdi
movq 0x526c7(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x1c0(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x44937
movq 0x526ed(%rip), %rdi     # 0x96ff0
leaq 0x39910(%rip), %rsi     # 0x7e21a
callq 0x8360
movq %rax, %rdi
cmpl $0x0, 0x170(%rsp)
setge %al
movzbl %al, %esi
andl $0x1, %esi
callq 0x8760
movq %rax, %rdi
movq 0x5267e(%rip), %rsi     # 0x96fb0
callq 0x83b0
cmpl $0x0, 0x170(%rsp)
jge 0x44d95
movq 0x188(%rsp), %rax
subq 0x190(%rsp), %rax
addq $0x1, %rax
movl %eax, 0x16c(%rsp)
movslq 0x16c(%rsp), %rdi
callq 0x8560
movq %rax, 0x160(%rsp)
movl $0x0, 0x15c(%rsp)
movl 0x15c(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x449e8
movq 0x1d8(%rsp), %rdi
addq $0x50, %rdi
movq 0x190(%rsp), %rsi
movslq 0x15c(%rsp), %rax
addq %rax, %rsi
callq 0x8410
movsbq (%rax), %rcx
leaq 0x39de1(%rip), %rax     # 0x7e7a0
movb (%rax,%rcx), %dl
movq 0x160(%rsp), %rax
movslq 0x15c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x15c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x15c(%rsp)
jmp 0x44980
movl 0x174(%rsp), %edx
movq 0x1c0(%rsp), %rax
imull (%rax), %edx
xorl %eax, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmpl $0xfa, %edx
cmovll %ecx, %eax
orl $0x80000, %eax          # imm = 0x80000
movl %eax, 0x158(%rsp)
movl 0x174(%rsp), %eax
movl %eax, 0x84(%rsp)
testb $0x1, 0x187(%rsp)
je 0x44a44
movq 0x178(%rsp), %rax
movq 0xa0(%rax), %rax
movq %rax, 0x78(%rsp)
jmp 0x44a58
movq 0x178(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rax, 0x78(%rsp)
movl 0x84(%rsp), %esi
movq 0x78(%rsp), %rdx
movl 0x16c(%rsp), %ecx
movq 0x160(%rsp), %r8
movq 0x1c0(%rsp), %rbx
addq $0x10, %rbx
movq 0x1c0(%rsp), %rax
movl 0x8(%rax), %r11d
movq 0x1c0(%rsp), %rax
movl 0xc(%rax), %r10d
movl 0x158(%rsp), %eax
leaq 0x13c(%rsp), %rdi
movl $0x5, %r9d
xorl %ebp, %ebp
movq %rbx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
callq 0x76270
movq 0x160(%rsp), %rdi
callq 0x8470
movq 0x1c0(%rsp), %rax
movsd 0x30(%rax), %xmm0
cvtsi2sdl 0x174(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
addl $0x1, %eax
movl %eax, 0x138(%rsp)
movl 0x174(%rsp), %eax
movq 0x1c0(%rsp), %rcx
imull (%rcx), %eax
movq 0x1c0(%rsp), %rcx
movl 0x4(%rcx), %ecx
movq 0x1c0(%rsp), %rdx
subl (%rdx), %ecx
imull 0x138(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x134(%rsp)
movq 0x1c0(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x44b98
movq 0x524a0(%rip), %rdi     # 0x96ff0
leaq 0x396d0(%rip), %rsi     # 0x7e227
callq 0x8360
movq %rax, %rdi
movl 0x13c(%rsp), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x396d8(%rip), %rsi     # 0x7e24d
callq 0x8360
movq %rax, %rdi
movl 0x134(%rsp), %esi
callq 0x8710
movq %rax, %rdi
movq 0x5241d(%rip), %rsi     # 0x96fb0
callq 0x83b0
movl 0x13c(%rsp), %eax
cmpl 0x134(%rsp), %eax
jle 0x44d4a
movl $0x100, %edi            # imm = 0x100
callq 0x8380
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x70(%rsp)
callq 0x4dd90
jmp 0x44bc8
movq 0x70(%rsp), %rax
movq %rax, 0x128(%rsp)
movl 0x13c(%rsp), %ecx
movq 0x128(%rsp), %rax
movl %ecx, 0x70(%rax)
movl 0x154(%rsp), %ecx
movq 0x128(%rsp), %rax
movl %ecx, 0x10(%rax)
movl 0x144(%rsp), %ecx
movq 0x128(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x190(%rsp), %rcx
movslq 0x150(%rsp), %rax
addq %rax, %rcx
movq 0x128(%rsp), %rax
movq %rcx, (%rax)
movq 0x190(%rsp), %rcx
movslq 0x140(%rsp), %rax
addq %rax, %rcx
movq 0x128(%rsp), %rax
movq %rcx, 0x8(%rax)
movl 0x140(%rsp), %ecx
subl 0x150(%rsp), %ecx
addl $0x1, %ecx
movq 0x128(%rsp), %rax
movl %ecx, 0x98(%rax)
movq 0x128(%rsp), %rdi
addq $0x40, %rdi
movb 0x187(%rsp), %al
xorb $-0x1, %al
movzbl %al, %edx
andl $0x1, %edx
movl $0x4, %esi
callq 0xe640
movq 0x178(%rsp), %rdi
addq $0xb0, %rdi
leaq 0x128(%rsp), %rsi
callq 0x4dfc0
movq 0x178(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdac0
subq $0x1, %rax
movl %eax, 0x170(%rsp)
movq 0x200(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
movb (%rax), %al
xorb $-0x1, %al
movl 0x170(%rsp), %edx
movl 0x13c(%rsp), %ecx
movzbl %al, %esi
andl $0x1, %esi
leaq 0x110(%rsp), %rdi
callq 0x4e660
movq 0x68(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x4e630
jmp 0x44d4a
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
movl $0x100, %esi            # imm = 0x100
callq 0x8390
jmp 0x44efa
movq 0x1c0(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x44d93
movq 0x52291(%rip), %rdi     # 0x96ff0
leaq 0x394f7(%rip), %rsi     # 0x7e25d
callq 0x8360
movq %rax, %rdi
cmpl $0x0, 0x170(%rsp)
setge %al
movzbl %al, %esi
andl $0x1, %esi
callq 0x8760
movq %rax, %rdi
movq 0x52222(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x44d95
cmpl $0x0, 0x170(%rsp)
jl 0x44ed3
movq 0x1c0(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x44dd3
movq 0x52238(%rip), %rdi     # 0x96ff0
leaq 0x394ac(%rip), %rsi     # 0x7e26b
callq 0x8360
movq %rax, %rdi
movq 0x521e2(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
testb $0x1, (%rax)
je 0x44e50
movq 0x1d8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movl 0x4(%rax), %ecx
movl 0x170(%rsp), %r8d
movl $0x1, %r9d
callq 0x43b50
jmp 0x44eb9
movq 0x1d8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x170(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x4e610
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movl 0x4c(%rsp), %ecx
movl 0x4(%rax), %r8d
movl $0x1, %r9d
callq 0x43b50
movl $0x0, 0x1a4(%rsp)
movq 0x208(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x44ed5
movq 0x1a8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1a8(%rsp)
jmp 0x443d9
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbx
popq %r14
popq %rbp
retq
movq 0x120(%rsp), %rdi
callq 0x8730
nopw (%rax,%rax)