// File: /workspace/github_cpp_makefile_star2_O3/readmapping[P]alfalfa/src/mapper.cpp
// Function: findOrAlignMateToAlignment(sparseSA const&, read_t&, read_t&, bool, align_opt const&, paired_opt const&, std::vector<lis_t, std::allocator<lis_t>>&, int, std::vector<alnScoreInfo_t, std::allocator<alnScoreInfo_t>>&, int)
inline void findOrAlignMateToAlignment(const sparseSA& sa, read_t & alignmentMate, read_t & windowMate, 
        bool alnMateFirst, const align_opt & alnOptions, const paired_opt & pairedOpt, 
        vector<lis_t> & lisIntervalsWindow, int minScore, vector<alnScoreInfo_t> & windowAlns,
        int alignmentIndex){
    alignment_t * aln = alignmentMate.alignments[alignmentIndex];
    //FIND WINDOW
    long refB, refE;
    bool fw;
    int Plength = windowMate.sequence.length();
    dpWindow(aln->refB, aln->refE, alnMateFirst, !aln->flag.test(4), Plength, pairedOpt, refB, refE, fw);
    //CHECK IF AN ALIGNMENT ALREADY EXISTS IN THIS WINDOW
    if(alnOptions.print >= 4) cerr << "find window for aligment of mate " << (alnMateFirst ? "1" : "2") << " on strand " << (aln->flag.test(4) ? "R" : "F") << " and position " << aln->refB << endl;
    int windowMatePos = bestAlignmentInWindow(windowMate, windowAlns, alnMateFirst, refB, refE, fw);
    if(alnOptions.print >= 2) cerr << "#WINDOWLIST " << (windowMatePos>=0) << endl;
    int windowCount = 1;
    if(windowMatePos < 0){
        if(alnOptions.print >= 4) cerr << "found no existing concordant alignment" << endl;
        //TRY TO FIND LIS THAT IS CONCORDANT AND EXTEND THAT
        int editDist = (int)(alnOptions.errorPercent*Plength)+1;
        int lisPos = bestLISInWindow(lisIntervalsWindow, editDist, refB, refE, fw);
        if(lisPos >= 0){
            if(alnOptions.print >= 4) cerr << "found existing concordant LIS" << endl;
            //TRY TO EXTEND THIS LIS
            const vector<match_t> & matchVector = lisIntervalsWindow[lisPos].matches;
            long chrStart, chrEnd;
            sa.getChromBounds(matchVector[0].ref, chrStart, chrEnd);
            size_t extendCount = windowMate.alignments.size();
            extendAlignmentChain(sa.S, lisIntervalsWindow[lisPos].fw ? windowMate.sequence : windowMate.rcSequence, 
                    matchVector, editDist, minScore, Plength*0.01, alnOptions, chrStart, chrEnd, windowMate, lisIntervalsWindow[lisPos].fw);
            if(windowMate.alignments.size() > extendCount){
                lisIntervalsWindow[lisPos].extended = true;
                windowMatePos = extendCount;
                windowCount = windowMate.alignments.size() - extendCount;
            }
        }
        if(alnOptions.print >= 2) cerr << "#WINDOWALIGN " << (windowMatePos >= 0) << endl;
    }
    if(windowMatePos < 0 && pairedOpt.pairedRescue){
        if(alnOptions.print >= 4) cerr << "found no existing concordant LIS or alignment" << endl;
        //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(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;
        }
    }
    if(windowMatePos >= 0){
        if(alnOptions.print >= 4) cerr << "found concordant alignment" << endl;
        for(int i = 0; i < windowCount; i++){
            windowAlns.push_back(alnScoreInfo_t(!alnMateFirst,windowMatePos+i,windowMate.alignments[windowMatePos+i]->alignmentScore));
            //PAIR ALIGNMENTS, SUCCES
            if(alnMateFirst)
                setPaired(sa, alignmentMate, windowMate, alignmentIndex, windowMatePos+i, true);
            else
                setPaired(sa, windowMate, alignmentMate, windowMatePos+i, alignmentIndex, true);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %rbp
movl %ecx, %r13d
movq %rdx, %r14
movq %rdi, 0x30(%rsp)
movslq 0xe8(%rsp), %rcx
movq %rsi, 0x78(%rsp)
movq 0xb0(%rsi), %rax
movq %rcx, 0x70(%rsp)
movq (%rax,%rcx,8), %r12
movl 0x28(%rdx), %r8d
movq (%r12), %rdi
movq 0x8(%r12), %rsi
xorl %ecx, %ecx
testb $0x10, 0x40(%r12)
sete %cl
subq $0x8, %rsp
leaq 0x17(%rsp), %rax
leaq 0x90(%rsp), %r10
leaq 0x98(%rsp), %r11
movl %r13d, %edx
movl %r8d, 0x24(%rsp)
movq %r9, 0x40(%rsp)
pushq %rax
pushq %r10
pushq %r11
callq 0x25e92
addq $0x20, %rsp
movl 0x78(%rbp), %ebx
cmpl $0x4, %ebx
jl 0x2e922
movq 0x25782(%rip), %r15     # 0x53fe8
leaq 0x16a8d(%rip), %rsi     # 0x452fa
movl $0x21, %edx
movq %r15, %rdi
callq 0x7340
leaq 0x117ff(%rip), %rax     # 0x40080
leaq 0x16bed(%rip), %rsi     # 0x45475
testb %r13b, %r13b
cmovneq %rax, %rsi
movl $0x1, %edx
movq %r15, %rdi
callq 0x7340
leaq 0x16a79(%rip), %rsi     # 0x4531c
movl $0xb, %edx
movq %r15, %rdi
callq 0x7340
testb $0x10, 0x40(%r12)
leaq 0x16a6d(%rip), %rax     # 0x4532a
leaq 0x16a64(%rip), %rsi     # 0x45328
cmoveq %rax, %rsi
movl $0x1, %edx
movq %r15, %rdi
callq 0x7340
leaq 0x16a50(%rip), %rsi     # 0x4532c
movl $0xe, %edx
movq %r15, %rdi
callq 0x7340
movq (%r12), %rsi
movq %r15, %rdi
callq 0x72a0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movl 0x78(%rbp), %ebx
movq 0x90(%rsp), %rcx
movq 0x88(%rsp), %r8
movb 0xf(%rsp), %al
movzbl %r13b, %edx
movb %al, 0xe(%rsp)
movzbl %al, %r9d
movq %r14, %rdi
movq 0xe0(%rsp), %rsi
movq %rcx, 0x10(%rsp)
movq %r8, %r12
callq 0x25fe6
cmpl $0x2, %ebx
movq %rax, %rbx
jl 0x2e9b6
movq 0x2567f(%rip), %r15     # 0x53fe8
leaq 0x16a4a(%rip), %rsi     # 0x453ba
movl $0xc, %edx
movq %r15, %rdi
callq 0x7340
movl %ebx, %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
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movl $0x1, %r11d
testl %ebx, %ebx
movq %rbp, 0x28(%rsp)
js 0x2e9d5
movl %r13d, %ebp
movq 0xe0(%rsp), %rdi
jmp 0x2eca9
movq 0xd0(%rsp), %rcx
cmpl $0x4, 0x78(%rbp)
jl 0x2ea2d
movq 0x255fe(%rip), %r15     # 0x53fe8
leaq 0x16e98(%rip), %rsi     # 0x45889
movl $0x26, %edx
movq %r15, %rdi
callq 0x7340
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq 0xd0(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
movl $0x1, %r11d
subq %rax, %rdx
je 0x2ec2c
cvtsi2sdl 0x1c(%rsp), %xmm1
movsd 0x30(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
incl %esi
sarq $0x3, %rdx
movabsq $0x6db6db6db6db6db7, %rcx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rdx, %rcx
movl %esi, 0x6c(%rsp)
movslq %esi, %rdx
cmpq $0x1, %rcx
adcq $0x0, %rcx
leaq 0x10(%rax), %rsi
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffd8f0, %edi       # imm = 0xFFFFD8F0
xorl %r8d, %r8d
movq 0x10(%rsp), %r10
movq -0x8(%rsi), %r9
addq %rdx, %r9
cmpq %r10, %r9
jl 0x2eac1
movq (%rsi), %r9
subq %rdx, %r9
cmpq %r12, %r9
jg 0x2eac1
movb 0xe(%rsp), %r9b
cmpb %r9b, -0xc(%rsi)
jne 0x2eac1
movl -0x10(%rsi), %r9d
cmpl %edi, %r9d
cmovgl %r8d, %r15d
cmovgl %r9d, %edi
incq %r8
addq $0x38, %rsi
cmpq %r8, %rcx
jne 0x2ea90
testl %r15d, %r15d
js 0x2ec31
movsd %xmm1, 0x80(%rsp)
movq %rbx, 0x20(%rsp)
movl %r13d, 0x68(%rsp)
cmpl $0x4, 0x78(%rbp)
jl 0x2eb3c
movq 0x254f2(%rip), %rbx     # 0x53fe8
leaq 0x16db3(%rip), %rsi     # 0x458b0
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x7340
movq (%rbx), %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 0xd0(%rsp), %rax
movq (%rax), %rax
movl %r15d, %ecx
imulq $0x38, %rcx, %rbx
leaq (%rax,%rbx), %r15
addq $0x20, %r15
movq (%r15), %rax
movq (%rax), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
callq 0x2c5ce
movq 0xb8(%r14), %r13
subq 0xb0(%r14), %r13
sarq $0x3, %r13
leaq 0x50(%rbp), %rdi
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movzbl 0x4(%rax,%rbx), %eax
movl %eax, %ecx
xorb $0x1, %cl
movzbl %cl, %ecx
shll $0x6, %ecx
leaq (%r14,%rcx), %rsi
addq $0x20, %rsi
movsd 0x80(%rsp), %xmm0
mulsd 0x11cee(%rip), %xmm0    # 0x408a0
cvttsd2si %xmm0, %r9d
subq $0x8, %rsp
movq %r15, %rdx
movl 0x74(%rsp), %ecx
movl 0xe0(%rsp), %r8d
pushq %rax
pushq %r14
leaq 0x78(%rsp), %rax
pushq (%rax)
leaq 0x60(%rsp), %rax
pushq (%rax)
movq 0x50(%rsp), %rbp
pushq %rbp
callq 0x22769
addq $0x30, %rsp
movq 0xb8(%r14), %rax
subq 0xb0(%r14), %rax
sarq $0x3, %rax
movl $0x1, %r11d
cmpq %r13, %rax
jbe 0x2ec22
movq 0xd0(%rsp), %rcx
movq (%rcx), %rcx
movb $0x1, 0x18(%rcx,%rbx)
subl %r13d, %eax
movl %eax, %r11d
movq %r13, 0x20(%rsp)
movl 0x68(%rsp), %r13d
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r10
cmpl $0x2, 0x78(%rbp)
jl 0x2ec96
movq 0x253aa(%rip), %r15     # 0x53fe8
leaq 0x167b8(%rip), %rsi     # 0x453fd
movl $0xd, %edx
movq %r15, %rdi
movl %r11d, %ebp
callq 0x7340
movl %ebx, %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
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movl %ebp, %r11d
movq 0x10(%rsp), %r10
movl %r13d, %ebp
testl %ebx, %ebx
movq 0xe0(%rsp), %rdi
js 0x2edc5
movq 0x28(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x2ed04
movq 0x2532d(%rip), %r15     # 0x53fe8
leaq 0x16c3a(%rip), %rsi     # 0x458fc
movl $0x1a, %edx
movq %r15, %rdi
movl %r11d, %r12d
callq 0x7340
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movl %r12d, %r11d
movq 0xe0(%rsp), %rdi
testl %r11d, %r11d
jle 0x2edb3
movl %ebp, %r13d
xorb $0x1, %r13b
movl %ebx, %r12d
movl %r11d, %ebx
leaq 0x40(%rsp), %r15
movq 0xb0(%r14), %rax
movq (%rax,%r12,8), %rax
movl 0x70(%rax), %eax
movb %r13b, 0x40(%rsp)
movl %r12d, 0x44(%rsp)
movl %eax, 0x48(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2ed5b
movl 0x48(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x40(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rdi)
jmp 0x2ed63
movq %r15, %rdx
callq 0x3185c
testb %bpl, %bpl
je 0x2ed7f
movq 0x30(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq %r14, %rdx
movq 0x70(%rsp), %rcx
movl %r12d, %r8d
jmp 0x2ed94
movq 0x30(%rsp), %rdi
movq %r14, %rsi
movq 0x78(%rsp), %rdx
movl %r12d, %ecx
movq 0x70(%rsp), %r8
movl $0x1, %r9d
callq 0x25907
incq %r12
decq %rbx
movq 0xe0(%rsp), %rdi
jne 0x2ed1f
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %rax
cmpb $0x1, 0x14(%rax)
jne 0x2edb3
movl %r11d, 0x38(%rsp)
movq 0x28(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x2ee27
movq 0x25201(%rip), %r15     # 0x53fe8
leaq 0x16ae0(%rip), %rsi     # 0x458ce
movl $0x2d, %edx
movq %r15, %rdi
callq 0x7340
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq 0x10(%rsp), %r10
movq %rbx, 0x20(%rsp)
subl %r10d, %r12d
leal 0x1(%r12), %r15d
movslq %r15d, %rdi
callq 0x7430
movq %rax, %r8
cmpl $0x7ffffffe, %r12d      # imm = 0x7FFFFFFE
ja 0x2ee77
movq 0x30(%rsp), %rax
movq 0x50(%rax), %rax
addq 0x10(%rsp), %rax
movl %r15d, %ecx
xorl %edx, %edx
leaq 0x15d9e(%rip), %rsi     # 0x44c00
movsbq (%rax,%rdx), %rdi
movb (%rdi,%rsi), %dil
movb %dil, (%r8,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x2ee62
movq 0x28(%rsp), %rcx
movl (%rcx), %eax
movl 0x1c(%rsp), %esi
imull %esi, %eax
xorl %r10d, %r10d
cmpl $0xfa, %eax
setl %r10b
orl $0x8, %r10d
shll $0x10, %r10d
movb 0xe(%rsp), %al
xorb $0x1, %al
movzbl %al, %r12d
movq 0xa0(%r14,%r12,8), %rdx
leaq 0x10(%rcx), %rax
movl 0x8(%rcx), %r11d
movl 0xc(%rcx), %ebx
subq $0x8, %rsp
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movl %r15d, %ecx
movq %r8, %r15
movl $0x5, %r9d
pushq $0x0
pushq %r10
pushq %rbx
pushq %r11
pushq %rax
callq 0x3f651
addq $0x30, %rsp
movq %r15, %rdi
callq 0x7380
movl 0xd8(%rsp), %eax
cmpl %eax, (%r13)
movq 0x20(%rsp), %rbx
jle 0x2efb8
movl $0x100, %edi            # imm = 0x100
callq 0x72e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2c490
movq %r15, 0x60(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x70(%r15)
movl 0x58(%rsp), %eax
movl %eax, 0x10(%r15)
movl 0x48(%rsp), %eax
movl %eax, 0x14(%r15)
movslq 0x54(%rsp), %rax
movq %rax, %rcx
movq 0x10(%rsp), %rdx
addq %rdx, %rcx
movq %rcx, (%r15)
movslq 0x44(%rsp), %rcx
addq %rcx, %rdx
movq %rdx, 0x8(%r15)
subl %eax, %ecx
incl %ecx
movl %ecx, 0x98(%r15)
movq 0x40(%r15), %rax
andq $-0x11, %rax
shll $0x4, %r12d
orq %rax, %r12
movq %r12, 0x40(%r15)
leaq 0xb0(%r14), %r12
movq 0xb8(%r14), %rsi
cmpq 0xc0(%r14), %rsi
je 0x2ef9a
movq %r15, (%rsi)
movq 0xb8(%r14), %rbx
addq $0x8, %rbx
movq %rbx, 0xb8(%r14)
jmp 0x2efae
leaq 0x60(%rsp), %rdx
movq %r12, %rdi
callq 0x30830
movq 0xb8(%r14), %rbx
subq (%r12), %rbx
shrq $0x3, %rbx
decl %ebx
testl %ebx, %ebx
movq 0xe0(%rsp), %rdi
movl 0x38(%rsp), %r11d
jns 0x2eca9
jmp 0x2edb3
movq %rax, %rbx
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
callq 0x72f0
movq %rbx, %rdi
callq 0x7600