// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %r9, %rbx
movq %r8, 0x20(%rsp)
movl %ecx, %r13d
movq %rdi, 0x28(%rsp)
movslq 0xf8(%rsp), %rcx
movq %rsi, 0x70(%rsp)
movq 0xb0(%rsi), %rax
movq %rcx, 0x68(%rsp)
movq (%rax,%rcx,8), %rbp
movq %rdx, 0x10(%rsp)
movl 0x28(%rdx), %r14d
movq (%rbp), %r15
movq 0x8(%rbp), %r12
leaq 0x40(%rbp), %rdi
pushq $0x4
popq %rsi
movq %rdi, 0x8(%rsp)
callq 0xa11c
xorb $0x1, %al
subq $0x8, %rsp
leaq 0xf(%rsp), %r10
leaq 0xa0(%rsp), %r11
movzbl %al, %ecx
movq %r15, %rdi
movq %r12, %rsi
movl %r13d, %edx
movl %r14d, 0x20(%rsp)
movl %r14d, %r8d
movl %r13d, %r14d
movq %rbx, 0x38(%rsp)
movq %rbx, %r9
pushq %r10
pushq %r11
leaq 0xb8(%rsp), %rax
pushq %rax
movq 0x40(%rsp), %r13
callq 0x1dea1
addq $0x20, %rsp
cmpl $0x4, 0x78(%r13)
jl 0x248ee
movq 0x25781(%rip), %rdi     # 0x49fe8
leaq 0x12a4c(%rip), %rsi     # 0x372ba
callq 0x72b0
leaq 0xd806(%rip), %rcx      # 0x32080
leaq 0x12bb4(%rip), %rsi     # 0x37435
testb %r14b, %r14b
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x72b0
leaq 0x12a45(%rip), %rsi     # 0x372dc
movq %rax, %rdi
callq 0x72b0
movq %rax, %r15
pushq $0x4
popq %rsi
movq 0x8(%rsp), %rdi
callq 0xa11c
leaq 0x12a32(%rip), %rcx     # 0x372e8
leaq 0x12a2d(%rip), %rsi     # 0x372ea
testb %al, %al
cmovneq %rcx, %rsi
movq %r15, %rdi
callq 0x72b0
leaq 0x12a1a(%rip), %rsi     # 0x372ec
movq %rax, %rdi
callq 0x72b0
movq (%rbp), %rsi
movq %rax, %rdi
callq 0x72a0
movq %rax, %rdi
callq 0x7160
movq 0xa0(%rsp), %rbx
movq 0x98(%rsp), %r15
movb 0x7(%rsp), %al
movl %r14d, %r12d
movzbl %r14b, %edx
movb %al, 0x8(%rsp)
movzbl %al, %ebp
movq 0x10(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq %rbx, %rcx
movq %r15, %r8
movl %ebp, %r9d
callq 0x1dfa4
movl %eax, %r14d
cmpl $0x2, 0x78(%r13)
jl 0x24960
movq 0x256ac(%rip), %rdi     # 0x49fe8
leaq 0x12a37(%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
movl %r12d, 0x1c(%rsp)
js 0x24973
pushq $0x1
popq %r15
jmp 0x24b4c
movq 0xe0(%rsp), %rdi
cmpl $0x4, 0x78(%r13)
jl 0x249a5
movq 0x2565f(%rip), %rdi     # 0x49fe8
leaq 0x12eb9(%rip), %rsi     # 0x37849
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movq 0xe0(%rsp), %rdi
cvtsi2sdl 0x18(%rsp), %xmm1
movsd 0x30(%r13), %xmm0
movsd %xmm1, 0x90(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
incl %esi
movl %esi, 0x3c(%rsp)
movq %rbx, %rdx
movq %r15, %rcx
movl %ebp, %r8d
callq 0x24ded
testl %eax, %eax
movq %rbx, 0x60(%rsp)
movq %r15, 0x88(%rsp)
js 0x24afa
movl %eax, %ebp
movq %r14, 0x80(%rsp)
cmpl $0x4, 0x78(%r13)
jl 0x24a17
movq 0x255e5(%rip), %rdi     # 0x49fe8
leaq 0x12e66(%rip), %rsi     # 0x37870
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movl %ebp, %eax
movq 0xe0(%rsp), %r12
movq (%r12), %rcx
imulq $0x38, %rax, %r13
leaq (%rcx,%r13), %rbp
addq $0x20, %rbp
movq (%rbp), %rax
movq (%rax), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x22b12
movq 0x10(%rsp), %r14
movq 0xb8(%r14), %rbx
subq 0xb0(%r14), %rbx
sarq $0x3, %rbx
leaq 0x50(%r15), %rdi
movq (%r12), %rax
movzbl 0x4(%rax,%r13), %eax
movl %eax, %ecx
xorb $0x1, %cl
movzbl %cl, %ecx
shll $0x6, %ecx
leaq (%r14,%rcx), %rsi
addq $0x20, %rsi
movsd 0x90(%rsp), %xmm0
mulsd 0xde90(%rip), %xmm0     # 0x32928
cvttsd2si %xmm0, %r9d
subq $0x8, %rsp
movq %rbp, %rdx
movl 0x44(%rsp), %ecx
movl 0xf0(%rsp), %r8d
pushq %rax
pushq %r14
leaq 0x90(%rsp), %rax
pushq (%rax)
leaq 0x60(%rsp), %rax
pushq (%rax)
pushq 0x48(%rsp)
callq 0x1b714
addq $0x30, %rsp
movq 0xb8(%r14), %r15
subq 0xb0(%r14), %r15
sarq $0x3, %r15
cmpq %rbx, %r15
jbe 0x24b00
movq (%r12), %rax
movb $0x1, 0x18(%rax,%r13)
subl %ebx, %r15d
movl %ebx, %r14d
jmp 0x24b0c
pushq $0x1
popq %r15
jmp 0x24b11
pushq $0x1
popq %r15
movq 0x80(%rsp), %r14
movq 0x20(%rsp), %r13
cmpl $0x2, 0x78(%r13)
jl 0x24b43
movq 0x254c9(%rip), %rdi     # 0x49fe8
leaq 0x12897(%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 0x24c07
cmpl $0x4, 0x78(%r13)
jl 0x24b6e
movq 0x2548e(%rip), %rdi     # 0x49fe8
leaq 0x12d5b(%rip), %rsi     # 0x378bc
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movl 0x1c(%rsp), %eax
movl %eax, %r13d
xorb $0x1, %r13b
movl %r14d, %ebp
xorl %ebx, %ebx
testl %r15d, %r15d
cmovgl %r15d, %ebx
leaq 0x40(%rsp), %r12
pushq $0x1
popq %r15
movq 0x10(%rsp), %r14
subq $0x1, %rbx
jb 0x24dc3
movq 0xb0(%r14), %rax
movq (%rax,%rbp,8), %rax
movl 0x70(%rax), %eax
movb %r13b, 0x40(%rsp)
movl %ebp, 0x44(%rsp)
movl %eax, 0x48(%rsp)
movq 0xf0(%rsp), %rdi
movq %r12, %rsi
callq 0x279ba
cmpb $0x0, 0x1c(%rsp)
je 0x24be6
movq 0x28(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq %r14, %rdx
movq 0x68(%rsp), %rcx
movl %ebp, %r8d
jmp 0x24bfa
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq 0x70(%rsp), %rdx
movl %ebp, %ecx
movq 0x68(%rsp), %r8
movl %r15d, %r9d
callq 0x1dc1e
incq %rbp
jmp 0x24b93
movq 0x30(%rsp), %rax
cmpb $0x1, 0x14(%rax)
jne 0x24dc3
cmpl $0x4, 0x78(%r13)
jl 0x24c38
movq 0x253c4(%rip), %rdi     # 0x49fe8
leaq 0x12c63(%rip), %rsi     # 0x3788e
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movq 0x60(%rsp), %r12
movq 0x88(%rsp), %rbx
subl %r12d, %ebx
incl %ebx
movslq %ebx, %rdi
movq %rdi, 0x30(%rsp)
callq 0x7430
movq %rax, %rbp
movq 0x28(%rsp), %rax
movq 0x50(%rax), %rax
addq %r12, %rax
xorl %r8d, %r8d
cmpl $0x0, 0x30(%rsp)
movl $0x0, %edx
movq %rbx, %rcx
cmovgl %ebx, %edx
leaq 0x11f70(%rip), %rsi     # 0x36bf0
cmpq %r8, %rdx
je 0x24c98
movsbq (%rax,%r8), %rdi
movb (%rdi,%rsi), %dil
movb %dil, (%rbp,%r8)
incq %r8
jmp 0x24c80
movl (%r13), %eax
movl 0x18(%rsp), %esi
imull %esi, %eax
xorl %r10d, %r10d
cmpl $0xfa, %eax
setl %r10b
orl $0x8, %r10d
shll $0x10, %r10d
movb 0x8(%rsp), %al
xorb $0x1, %al
movzbl %al, %edx
movq 0x10(%rsp), %rax
movq %rdx, 0x8(%rsp)
movq 0xa0(%rax,%rdx,8), %rdx
leaq 0x10(%r13), %rax
movl 0x8(%r13), %r11d
movl 0xc(%r13), %ebx
subq $0x8, %rsp
leaq 0x48(%rsp), %r12
pushq $0x5
popq %r9
movq %r12, %rdi
movq %rbp, %r8
pushq $0x0
pushq %r10
pushq %rbx
pushq %r11
pushq %rax
callq 0x30bcd
addq $0x30, %rsp
movq %rbp, %rdi
callq 0x7360
movl 0xe8(%rsp), %eax
cmpl %eax, (%r12)
jle 0x24db5
movl $0x100, %edi            # imm = 0x100
callq 0x72d0
movq %rax, %rbp
movq %rax, %rdi
callq 0x229e0
leaq 0x78(%rsp), %r12
movq %rbp, (%r12)
movl 0x40(%rsp), %eax
movl %eax, 0x70(%rbp)
movl 0x58(%rsp), %eax
movl %eax, 0x10(%rbp)
movl 0x48(%rsp), %eax
movl %eax, 0x14(%rbp)
movslq 0x54(%rsp), %rax
movq %rax, %rcx
movq 0x60(%rsp), %rdx
addq %rdx, %rcx
movq %rcx, (%rbp)
movslq 0x44(%rsp), %rcx
addq %rcx, %rdx
movq %rdx, 0x8(%rbp)
subl %eax, %ecx
incl %ecx
movl %ecx, 0x98(%rbp)
addq $0x40, %rbp
pushq $0x4
popq %rsi
movq %rbp, %rdi
movq 0x8(%rsp), %rdx
callq 0xa1f6
movq 0x10(%rsp), %rbx
leaq 0xb0(%rbx), %rdi
movq %r12, %rsi
callq 0x22ac6
movq 0xb8(%rbx), %r14
subq 0xb0(%rbx), %r14
shrq $0x3, %r14
decl %r14d
testl %r14d, %r14d
movq 0x20(%rsp), %r13
jns 0x24b4c
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdi
callq 0x72e0
movq %rbx, %rdi
callq 0x75e0