// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, %r12
movq %r8, 0xa8(%rsp)
movq %rcx, %rbp
movq %rdx, %r13
movq %rsi, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movl 0x100(%rsp), %r14d
leaq 0x10(%rcx), %rax
movq %rax, 0xa0(%rsp)
movq $0x0, 0x10(%rsp)
xorl %edi, %edi
movq %r9, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq (%r12), %rcx
movq 0x8(%r12), %rax
subq %rcx, %rax
cqto
pushq $0xc
popq %rsi
idivq %rsi
cmpq %rax, %rdi
jae 0x1e6b0
movq 0xf8(%rsp), %rax
cmpl %r14d, (%rax)
jge 0x1e6b0
movq 0x10(%rsp), %rax
cmpl 0x54(%rbp), %eax
jge 0x1e6b0
movq %rdi, 0x60(%rsp)
imulq $0xc, %rdi, %r15
movslq 0x4(%rcx,%r15), %rax
cmpb $0x0, (%rcx,%r15)
movq %r13, %rcx
cmovneq 0x18(%rsp), %rcx
movq 0xb0(%rcx), %rcx
movq (%rcx,%rax,8), %rbx
movq %rbx, %rdi
callq 0xa166
testb %al, %al
jne 0x1e6a3
cmpl $0x4, 0x78(%rbp)
jl 0x1e1ed
movq 0x2be86(%rip), %rdi     # 0x49fe8
leaq 0x19151(%rip), %rsi     # 0x372ba
callq 0x72b0
movq (%r12), %rcx
cmpb $0x0, (%rcx,%r15)
leaq 0x15a29(%rip), %rsi     # 0x33ba7
leaq 0x13efb(%rip), %rcx     # 0x32080
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x72b0
movq %rax, %rdi
leaq 0x19141(%rip), %rsi     # 0x372dc
callq 0x72b0
movq %rax, %r14
leaq 0x40(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xa11c
testb %al, %al
leaq 0x19132(%rip), %rsi     # 0x372ea
leaq 0x19129(%rip), %rax     # 0x372e8
cmovneq %rax, %rsi
movq %r14, %rdi
callq 0x72b0
movq %rax, %rdi
leaq 0x19117(%rip), %rsi     # 0x372ec
callq 0x72b0
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x72a0
movq %rax, %rdi
callq 0x7160
movq (%r12), %rax
movzbl (%rax,%r15), %r14d
testl %r14d, %r14d
movq 0x18(%rsp), %rax
cmovneq %r13, %rax
movq %rax, 0x30(%rsp)
movl 0x28(%rax), %r13d
movq (%rbx), %r12
movq 0x8(%rbx), %rbp
addq $0x40, %rbx
movq %rbx, %rdi
pushq $0x4
popq %rsi
callq 0xa11c
xorb $0x1, %al
subq $0x8, %rsp
movzbl %al, %ecx
movq %r12, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl %r13d, %r8d
movq 0xb0(%rsp), %r9
leaq 0x17(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
callq 0x1dea1
addq $0x20, %rsp
movq 0x48(%rsp), %rbp
cmpl $0x3, 0x78(%rbp)
jle 0x1e2eb
movq 0x2bd76(%rip), %rdi     # 0x49fe8
leaq 0x19082(%rip), %rsi     # 0x372fb
callq 0x72b0
movb 0xf(%rsp), %r12b
testb %r12b, %r12b
leaq 0x1905b(%rip), %rsi     # 0x372e8
leaq 0x19056(%rip), %rcx     # 0x372ea
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x72b0
movq %rax, %rdi
leaq 0x19067(%rip), %rsi     # 0x37311
callq 0x72b0
movq 0x70(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x72a0
movq %rax, %rdi
leaq 0x18b87(%rip), %rsi     # 0x36e50
callq 0x72b0
movq 0x68(%rsp), %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x72a0
movq %rax, %rdi
callq 0x7160
movl %r12d, %ecx
jmp 0x1e2f9
movq 0x70(%rsp), %r14
movq 0x68(%rsp), %rbx
movb 0xf(%rsp), %cl
movq 0x50(%rsp), %r12
movq (%r12), %rax
movzbl (%rax,%r15), %edx
movb %cl, 0x28(%rsp)
movzbl %cl, %r9d
andl $0x1, %r9d
movq 0x30(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq %r14, 0x58(%rsp)
movq %r14, %rcx
movq %rbx, %r8
callq 0x1dfa4
movl %eax, %r14d
movl 0x78(%rbp), %eax
cmpl $0x4, %eax
jl 0x1e373
movq 0x2bca6(%rip), %rdi     # 0x49fe8
leaq 0x18feb(%rip), %rsi     # 0x37334
callq 0x72b0
movq %rax, %rdi
movl %r14d, %esi
callq 0x75c0
movq %rax, %rdi
leaq 0x18ffc(%rip), %rsi     # 0x3735f
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movl 0x78(%rbp), %eax
cmpl $0x2, %eax
jl 0x1e3a3
movq 0x2bc69(%rip), %rdi     # 0x49fe8
leaq 0x18ff4(%rip), %rsi     # 0x3737a
callq 0x72b0
movl %r14d, %esi
notl %esi
shrl $0x1f, %esi
movq %rax, %rdi
callq 0x75a0
movq %rax, %rdi
callq 0x7160
testl %r14d, %r14d
js 0x1e3b2
movq 0x40(%rsp), %r13
jmp 0x1e629
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movl %r13d, 0x3c(%rsp)
movq 0x58(%rsp), %r13
subl %r13d, %ebx
incl %ebx
movslq %ebx, %rbp
movq %rbp, %rdi
callq 0x7430
movq %rax, %r12
movq 0x20(%rsp), %rax
movq 0x50(%rax), %rax
addq %r13, %rax
movl 0x3c(%rsp), %r13d
testl %ebp, %ebp
movq 0x48(%rsp), %rbp
movl $0x0, %ecx
cmovgl %ebx, %ecx
xorl %edx, %edx
leaq 0x187ec(%rip), %rdi     # 0x36bf0
cmpq %rdx, %rcx
je 0x1e41b
movsbq (%rax,%rdx), %rsi
movb (%rsi,%rdi), %sil
movb %sil, (%r12,%rdx)
incq %rdx
jmp 0x1e404
movl (%rbp), %eax
imull %r13d, %eax
xorl %r10d, %r10d
cmpl $0xfa, %eax
setl %r10b
orl $0x8, %r10d
shll $0x10, %r10d
movb 0x28(%rsp), %al
notb %al
movzbl %al, %ecx
andl $0x1, %ecx
movq 0x30(%rsp), %rax
movq %rcx, 0x28(%rsp)
movq 0xa0(%rax,%rcx,8), %rdx
movl 0x8(%rbp), %eax
movl 0xc(%rbp), %r11d
subq $0x8, %rsp
leaq 0x8c(%rsp), %rdi
movl %r13d, %esi
movl %ebx, %ecx
movq %r12, %r8
pushq $0x5
popq %r9
pushq $0x0
pushq %r10
pushq %r11
pushq %rax
pushq 0xc8(%rsp)
callq 0x30bcd
addq $0x30, %rsp
movq %r12, %rdi
callq 0x7360
cvtsi2sd %r13d, %xmm0
mulsd 0x30(%rbp), %xmm0
cvttsd2si %xmm0, %eax
incl %eax
movl (%rbp), %ecx
movl 0x4(%rbp), %ebx
imull %ecx, %r13d
subl %ecx, %ebx
imull %eax, %ebx
addl %r13d, %ebx
cmpl $0x3, 0x78(%rbp)
jle 0x1e4fd
movq 0x2bb27(%rip), %rdi     # 0x49fe8
leaq 0x18ebf(%rip), %rsi     # 0x37387
callq 0x72b0
movl 0x84(%rsp), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
leaq 0x18ec7(%rip), %rsi     # 0x373ad
callq 0x72b0
movq %rax, %rdi
movl %ebx, %esi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
cmpl %ebx, 0x84(%rsp)
movq 0x50(%rsp), %r12
movq 0x40(%rsp), %r13
jle 0x1e5f3
movl $0x100, %edi            # imm = 0x100
callq 0x72d0
movq %rax, %rbx
movq %rax, %rdi
callq 0x229e0
movq %rbx, 0xb0(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x70(%rbx)
movl 0x9c(%rsp), %eax
movl %eax, 0x10(%rbx)
movl 0x8c(%rsp), %eax
movl %eax, 0x14(%rbx)
movslq 0x98(%rsp), %rax
movq %rax, %rcx
movq 0x58(%rsp), %rdx
addq %rdx, %rcx
movq %rcx, (%rbx)
movslq 0x88(%rsp), %rcx
addq %rcx, %rdx
movq %rdx, 0x8(%rbx)
subl %eax, %ecx
incl %ecx
movl %ecx, 0x98(%rbx)
addq $0x40, %rbx
movq %rbx, %rdi
pushq $0x4
popq %rsi
movq 0x28(%rsp), %rdx
callq 0xa1f6
movq 0x30(%rsp), %rbx
leaq 0xb0(%rbx), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x22ac6
movq 0xb8(%rbx), %r14
subq 0xb0(%rbx), %r14
shrq $0x3, %r14
decl %r14d
movq (%r12), %rax
movb (%rax,%r15), %al
xorb $0x1, %al
movl 0x84(%rsp), %ecx
movb %al, 0x78(%rsp)
movl %r14d, 0x7c(%rsp)
movl %ecx, 0x80(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x279ba
cmpl $0x2, 0x78(%rbp)
jl 0x1e624
movq 0x2b9e8(%rip), %rdi     # 0x49fe8
leaq 0x18db6(%rip), %rsi     # 0x373bd
callq 0x72b0
movl %r14d, %esi
notl %esi
shrl $0x1f, %esi
movq %rax, %rdi
callq 0x75a0
movq %rax, %rdi
callq 0x7160
testl %r14d, %r14d
js 0x1e69b
cmpl $0x4, 0x78(%rbp)
jl 0x1e64a
movq 0x2b9b2(%rip), %rdi     # 0x49fe8
leaq 0x18d8e(%rip), %rsi     # 0x373cb
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movq (%r12), %rax
movl 0x4(%rax,%r15), %r8d
cmpb $0x1, (%rax,%r15)
jne 0x1e66f
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movl %r8d, %ecx
movl %r14d, %r8d
jmp 0x1e67f
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movl %r14d, %ecx
pushq $0x1
popq %r9
callq 0x1dc1e
movq 0xf8(%rsp), %rax
incl (%rax)
movq $0x0, 0x10(%rsp)
movl 0x100(%rsp), %r14d
movq 0x60(%rsp), %rdi
incq %rdi
jmp 0x1e0de
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
callq 0x72e0
movq %r14, %rdi
callq 0x75e0