// File: /workspace/github_cpp_makefile_star2_O1/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 %r8, 0xa8(%rsp)
movq %rdi, 0x20(%rsp)
movq (%r9), %rax
cmpq %rax, 0x8(%r9)
je 0x268d8
movq %rdx, %rbx
movq %rsi, %r12
movl 0x100(%rsp), %edx
movq 0xf8(%rsp), %rsi
cmpl %edx, (%rsi)
jge 0x268d8
movq %rcx, %r15
cmpl $0x0, 0x54(%rcx)
jle 0x268d8
movq %r9, %rbp
leaq 0x10(%r15), %rcx
movq %rcx, 0x90(%rsp)
xorl %r13d, %r13d
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
xorl %r8d, %r8d
movq %r15, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq %r9, 0x98(%rsp)
movq %r12, 0x18(%rsp)
leaq (,%r13,2), %rcx
addq %r13, %rcx
cmpb $0x0, (%rax,%rcx,4)
movq %rcx, 0x10(%rsp)
movslq 0x4(%rax,%rcx,4), %rax
movq %rbx, %rcx
cmovneq %r12, %rcx
movq 0xb0(%rcx), %rcx
movq (%rcx,%rax,8), %r14
testb $0x1, 0x40(%r14)
jne 0x268af
movl %r8d, 0x48(%rsp)
cmpl $0x4, 0x78(%r15)
jl 0x262aa
movl $0x21, %edx
movq 0x2ddff(%rip), %rbx     # 0x53fe8
movq %rbx, %rdi
leaq 0x1f107(%rip), %rsi     # 0x452fa
callq 0x7340
movq (%rbp), %rax
movq 0x10(%rsp), %rcx
cmpb $0x0, (%rax,%rcx,4)
leaq 0x1b91b(%rip), %rsi     # 0x41b27
leaq 0x19e6d(%rip), %rax     # 0x40080
cmovneq %rax, %rsi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x7340
movl $0xb, %edx
movq %rbx, %rdi
leaq 0x1f0e9(%rip), %rsi     # 0x4531c
callq 0x7340
testb $0x10, 0x40(%r14)
leaq 0x1f0e4(%rip), %rsi     # 0x45328
leaq 0x1f0df(%rip), %rax     # 0x4532a
cmoveq %rax, %rsi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x7340
movl $0xe, %edx
movq %rbx, %rdi
leaq 0x1f0c1(%rip), %rsi     # 0x4532c
callq 0x7340
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x72a0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %rbx, %rdi
movq 0x30(%rsp), %rbx
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq (%rbp), %rax
movq 0x10(%rsp), %rcx
movzbl (%rax,%rcx,4), %edx
testl %edx, %edx
cmovneq %rbx, %r12
movl 0x28(%r12), %r8d
movq (%r14), %rdi
movq 0x8(%r14), %rsi
xorl %ecx, %ecx
testb $0x10, 0x40(%r14)
sete %cl
subq $0x8, %rsp
movl %r8d, 0x4c(%rsp)
movq 0xb0(%rsp), %r9
leaq 0x17(%rsp), %rax
pushq %rax
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0x25f96
addq $0x20, %rsp
cmpl $0x4, 0x78(%r15)
jl 0x263bb
movl $0x15, %edx
movq 0x2dccf(%rip), %rbx     # 0x53fe8
movq %rbx, %rdi
leaq 0x1f018(%rip), %rsi     # 0x4533b
callq 0x7340
cmpb $0x0, 0xf(%rsp)
leaq 0x1eff4(%rip), %rsi     # 0x45328
leaq 0x1efef(%rip), %rax     # 0x4532a
cmovneq %rax, %rsi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x7340
movl $0x22, %edx
movq %rbx, %rdi
leaq 0x1eff6(%rip), %rsi     # 0x45351
callq 0x7340
movq 0x68(%rsp), %rsi
movq %rbx, %rdi
callq 0x72a0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x1eb11(%rip), %rsi     # 0x44e90
callq 0x7340
movq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0x72a0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq (%rbp), %rax
movq 0x10(%rsp), %rcx
movzbl (%rax,%rcx,4), %edx
movq 0x68(%rsp), %rcx
movq 0x60(%rsp), %rbx
movb 0xf(%rsp), %r14b
movzbl %r14b, %r9d
movq %r12, 0x58(%rsp)
movq %r12, %rdi
movq 0xf0(%rsp), %rsi
movq %rcx, 0x38(%rsp)
movq %rbx, %r8
callq 0x26080
movl %eax, %r12d
cmpl $0x4, 0x78(%r15)
jl 0x2646b
movl $0x2a, %edx
movq 0x2dbda(%rip), %r15     # 0x53fe8
movq %r15, %rdi
leaq 0x1ef5c(%rip), %rsi     # 0x45374
callq 0x7340
movq %r15, %rdi
movl %r12d, %esi
callq 0x75d0
movq %rax, %r15
movl $0x1a, %edx
movq %rax, %rdi
leaq 0x1ef65(%rip), %rsi     # 0x4539f
callq 0x7340
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r15, %rdi
movq 0x28(%rsp), %r15
callq 0x7040
movq %rax, %rdi
callq 0x71d0
cmpl $0x2, 0x78(%r15)
jl 0x264cc
movl $0xc, %edx
movq 0x2db6a(%rip), %r15     # 0x53fe8
movq %r15, %rdi
leaq 0x1ef32(%rip), %rsi     # 0x453ba
callq 0x7340
movl %r12d, %esi
notl %esi
shrl $0x1f, %esi
movq %r15, %rdi
callq 0x75a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r15, %rdi
movq 0x28(%rsp), %r15
callq 0x7040
movq %rax, %rdi
callq 0x71d0
testl %r12d, %r12d
jns 0x267d7
movq %r12, 0xa0(%rsp)
subl 0x38(%rsp), %ebx
leal 0x1(%rbx), %r15d
movslq %r15d, %rdi
callq 0x7430
movq %rax, %rbp
cmpl $0x7ffffffe, %ebx       # imm = 0x7FFFFFFE
leaq 0x1e703(%rip), %rdi     # 0x44c00
ja 0x26532
movq 0x20(%rsp), %rax
movq 0x50(%rax), %rax
addq 0x38(%rsp), %rax
cmpl $0x2, %r15d
movl $0x1, %ecx
cmovgel %r15d, %ecx
xorl %edx, %edx
movsbq (%rax,%rdx), %rsi
movb (%rsi,%rdi), %sil
movb %sil, (%rbp,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x2651c
movq 0x28(%rsp), %r8
movl (%r8), %eax
movl 0x44(%rsp), %r12d
imull %r12d, %eax
xorl %r10d, %r10d
cmpl $0xfa, %eax
setl %r10b
orl $0x8, %r10d
shll $0x10, %r10d
xorb $0x1, %r14b
movzbl %r14b, %r14d
movq 0x58(%rsp), %rax
movq 0xa0(%rax,%r14,8), %rdx
movl 0x8(%r8), %eax
movl 0xc(%r8), %r11d
subq $0x8, %rsp
leaq 0x7c(%rsp), %rdi
movl %r12d, %esi
movl %r15d, %ecx
movq %r8, %r15
movq %rbp, %r8
movl $0x5, %r9d
pushq $0x0
pushq %r10
pushq %r11
pushq %rax
pushq 0xb8(%rsp)
callq 0x3f742
addq $0x30, %rsp
movq %rbp, %rdi
callq 0x7380
cvtsi2sd %r12d, %xmm0
mulsd 0x30(%r15), %xmm0
cvttsd2si %xmm0, %eax
incl %eax
movl (%r15), %ecx
movl 0x4(%r15), %ebx
imull %ecx, %r12d
subl %ecx, %ebx
imull %eax, %ebx
addl %r12d, %ebx
cmpl $0x3, 0x78(%r15)
jle 0x26651
movl $0x25, %edx
movq %r14, %r12
movq 0x2da00(%rip), %r14     # 0x53fe8
movq %r14, %rdi
leaq 0x1edd5(%rip), %rsi     # 0x453c7
callq 0x7340
movl 0x74(%rsp), %esi
movq %r14, %rdi
callq 0x75d0
movq %rax, %r14
movl $0xf, %edx
movq %rax, %rdi
leaq 0x1edd8(%rip), %rsi     # 0x453ed
callq 0x7340
movq %r14, %rdi
movl %ebx, %esi
callq 0x75d0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r14, %rdi
movq %r12, %r14
callq 0x7040
movq %rax, %rdi
callq 0x71d0
cmpl %ebx, 0x74(%rsp)
movq 0x98(%rsp), %rbp
movq 0xa0(%rsp), %r12
jle 0x2677b
movl $0x100, %edi            # imm = 0x100
callq 0x72e0
movq %rax, %rbx
movq %rax, %rdi
callq 0x2c5b6
movq %rbx, 0xb0(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x70(%rbx)
movl 0x8c(%rsp), %eax
movl %eax, 0x10(%rbx)
movl 0x7c(%rsp), %eax
movl %eax, 0x14(%rbx)
movslq 0x88(%rsp), %rax
movq %rax, %rcx
movq 0x38(%rsp), %rdx
addq %rdx, %rcx
movq %rcx, (%rbx)
movslq 0x78(%rsp), %rcx
addq %rcx, %rdx
movq %rdx, 0x8(%rbx)
subl %eax, %ecx
incl %ecx
movl %ecx, 0x98(%rbx)
movq 0x40(%rbx), %rax
andq $-0x11, %rax
shll $0x4, %r14d
orq %rax, %r14
movq %r14, 0x40(%rbx)
movq 0x58(%rsp), %r14
movq 0xb8(%r14), %rsi
cmpq 0xc0(%r14), %rsi
je 0x26701
movq %rbx, (%rsi)
addq $0x8, 0xb8(%r14)
jmp 0x26715
leaq 0xb0(%r14), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x309b8
movq 0xb8(%r14), %r12
subq 0xb0(%r14), %r12
shrq $0x3, %r12
decl %r12d
movq (%rbp), %rax
movq 0x10(%rsp), %rcx
movb (%rax,%rcx,4), %al
xorb $0x1, %al
movl 0x74(%rsp), %ecx
movb %al, 0x4c(%rsp)
movl %r12d, 0x50(%rsp)
movl %ecx, 0x54(%rsp)
movq 0xf0(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x26771
movl 0x54(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x4c(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rdi)
jmp 0x2677b
leaq 0x4c(%rsp), %rdx
callq 0x319e2
cmpl $0x2, 0x78(%r15)
jl 0x267d7
movl $0xd, %edx
movq 0x2d85a(%rip), %rbx     # 0x53fe8
movq %rbx, %rdi
leaq 0x1ec65(%rip), %rsi     # 0x453fd
callq 0x7340
movl %r12d, %esi
notl %esi
shrl $0x1f, %esi
movq %rbx, %rdi
callq 0x75a0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq 0x30(%rsp), %rbx
testl %r12d, %r12d
movl 0x48(%rsp), %r8d
js 0x26858
cmpl $0x4, 0x78(%r15)
jl 0x2682f
movl $0x21, %edx
movq 0x2d7ef(%rip), %r14     # 0x53fe8
movq %r14, %rdi
leaq 0x1ec08(%rip), %rsi     # 0x4540b
callq 0x7340
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r14, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq (%rbp), %rax
movq 0x10(%rsp), %rcx
movl 0x4(%rax,%rcx,4), %r8d
cmpb $0x1, (%rax,%rcx,4)
jne 0x26871
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
movl %r8d, %ecx
movl %r12d, %r8d
jmp 0x26881
incl %r8d
movq 0x18(%rsp), %r12
movl 0x100(%rsp), %edx
movq 0xf8(%rsp), %rsi
jmp 0x268a5
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
movl %r12d, %ecx
movl $0x1, %r9d
callq 0x25a0b
movq 0xf8(%rsp), %rsi
incl (%rsi)
xorl %r8d, %r8d
movq 0x18(%rsp), %r12
movl 0x100(%rsp), %edx
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
incq %r13
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
imulq %rdi, %rcx
cmpq %rcx, %r13
jae 0x268d8
cmpl %edx, (%rsi)
jge 0x268d8
cmpl 0x54(%r15), %r8d
jl 0x26197
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 0x72f0
movq %r14, %rdi
callq 0x7600