// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movl %ecx, %r15d
movq %rdx, %r14
movq %rdi, 0x30(%rsp)
movslq 0xe8(%rsp), %rcx
movq %rsi, 0x68(%rsp)
movq 0xb0(%rsi), %rax
movq %rcx, 0x60(%rsp)
movq (%rax,%rcx,8), %r12
movl 0x28(%rdx), %r13d
movq (%r12), %rdi
movq 0x8(%r12), %rsi
xorl %ecx, %ecx
testb $0x10, 0x40(%r12)
sete %cl
subq $0x8, %rsp
leaq 0xb(%rsp), %rax
leaq 0x90(%rsp), %r10
leaq 0x98(%rsp), %r11
movl %r15d, %edx
movl %r13d, %r8d
movq %r9, 0x88(%rsp)
pushq %rax
pushq %r10
pushq %r11
callq 0x25f96
addq $0x20, %rsp
movq %rbx, 0x8(%rsp)
cmpl $0x4, 0x78(%rbx)
jl 0x2ea14
movl %r15d, %ebx
movq 0x2568f(%rip), %r15     # 0x53fe8
leaq 0x1699a(%rip), %rsi     # 0x452fa
movl $0x21, %edx
movq %r15, %rdi
callq 0x7340
leaq 0x1170c(%rip), %rax     # 0x40080
leaq 0x16afa(%rip), %rsi     # 0x45475
testb %bl, %bl
cmovneq %rax, %rsi
movl $0x1, %edx
movq %r15, %rdi
callq 0x7340
leaq 0x16987(%rip), %rsi     # 0x4531c
movl $0xb, %edx
movq %r15, %rdi
callq 0x7340
testb $0x10, 0x40(%r12)
leaq 0x1697b(%rip), %rax     # 0x4532a
leaq 0x16972(%rip), %rsi     # 0x45328
cmoveq %rax, %rsi
movl $0x1, %edx
movq %r15, %rdi
callq 0x7340
leaq 0x1695e(%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
movl %ebx, %r15d
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq 0x90(%rsp), %r12
movq 0x88(%rsp), %rbx
movb 0x3(%rsp), %bpl
movzbl %r15b, %edx
movzbl %bpl, %r9d
movq %r14, %rdi
movq 0xe0(%rsp), %rsi
movq %r12, %rcx
movq %rbx, %r8
callq 0x26080
movq 0x8(%rsp), %rcx
cmpl $0x2, 0x78(%rcx)
movl %r13d, 0x24(%rsp)
jl 0x2eaca
movl %r15d, %r13d
movq 0x25587(%rip), %r15     # 0x53fe8
leaq 0x16952(%rip), %rsi     # 0x453ba
movl $0xc, %edx
movq %r15, %rdi
movq %rbx, 0x28(%rsp)
movl %ebp, %ebx
movq %rax, %rbp
callq 0x7340
movl %ebp, %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
movl %r13d, %r15d
movl 0x24(%rsp), %r13d
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq %rbp, %rax
movl %ebx, %ebp
movq 0x28(%rsp), %rbx
movl $0x1, %esi
testl %eax, %eax
jns 0x2edc1
movl %r15d, 0x14(%rsp)
movq %rax, 0x18(%rsp)
movq 0xd0(%rsp), %r9
movq 0x8(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x2eb3e
movq 0x254ed(%rip), %r15     # 0x53fe8
leaq 0x16d87(%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), %r9
cvtsi2sd %r13d, %xmm1
movq 0x8(%rsp), %rax
movsd 0x30(%rax), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r10d
incl %r10d
movq (%r9), %rax
movq 0x8(%r9), %rdx
subq %rax, %rdx
je 0x2ebcd
sarq $0x3, %rdx
movabsq $0x6db6db6db6db6db7, %rcx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rdx, %rcx
movslq %r10d, %rdx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x10, %rax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffd8f0, %esi       # imm = 0xFFFFD8F0
xorl %edi, %edi
movq -0x8(%rax), %r8
addq %rdx, %r8
cmpq %r12, %r8
jl 0x2ebbf
movq (%rax), %r8
subq %rdx, %r8
cmpq %rbx, %r8
jg 0x2ebbf
cmpb %bpl, -0xc(%rax)
jne 0x2ebbf
movl -0x10(%rax), %r8d
cmpl %esi, %r8d
cmovgl %edi, %r15d
cmovgl %r8d, %esi
incq %rdi
addq $0x38, %rax
cmpq %rdi, %rcx
jne 0x2eb93
jmp 0x2ebd3
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %r15d, %r15d
movq %rbx, 0x28(%rsp)
js 0x2ed34
movsd %xmm1, 0x78(%rsp)
movl %r10d, 0x4(%rsp)
movb %bpl, 0x2(%rsp)
movq 0x8(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x2ec45
movq 0x253e5(%rip), %r13     # 0x53fe8
leaq 0x16ca6(%rip), %rsi     # 0x458b0
movl $0x1d, %edx
movq %r13, %rdi
movq %r9, %rbx
callq 0x7340
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x72b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movq %rbx, %r9
movl %r15d, %eax
movq (%r9), %rcx
imulq $0x38, %rax, %r15
leaq (%rcx,%r15), %rax
addq $0x20, %rax
movq %rax, 0x70(%rsp)
movq (%rax), %rax
movq (%rax), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x58(%rsp), %rcx
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r9, %rbx
callq 0x2c6f4
movq 0xb8(%r14), %r13
subq 0xb0(%r14), %r13
sarq $0x3, %r13
leaq 0x50(%rbp), %rdi
movq (%rbx), %rax
movzbl 0x4(%rax,%r15), %eax
movl %eax, %ecx
xorb $0x1, %cl
movzbl %cl, %ecx
shll $0x6, %ecx
leaq (%r14,%rcx), %rsi
addq $0x20, %rsi
movsd 0x78(%rsp), %xmm0
mulsd 0x11be4(%rip), %xmm0    # 0x408a0
cvttsd2si %xmm0, %r9d
subq $0x8, %rsp
movq 0x78(%rsp), %rdx
movl 0xc(%rsp), %ecx
movl 0xe0(%rsp), %r8d
pushq %rax
pushq %r14
leaq 0x70(%rsp), %rax
pushq (%rax)
leaq 0x58(%rsp), %rax
pushq (%rax)
pushq 0x30(%rsp)
callq 0x22a63
addq $0x30, %rsp
movq 0xb8(%r14), %rax
subq 0xb0(%r14), %rax
sarq $0x3, %rax
movl $0x1, %esi
cmpq %r13, %rax
jbe 0x2ed23
movq (%rbx), %rcx
movb $0x1, 0x18(%rcx,%r15)
subl %r13d, %eax
movl %eax, %esi
movq %r13, 0x18(%rsp)
movl 0x14(%rsp), %r15d
movb 0x2(%rsp), %bpl
movq 0x28(%rsp), %rbx
jmp 0x2ed3e
movl $0x1, %esi
movl 0x14(%rsp), %r15d
movq 0x8(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x2f0df
movq 0x25294(%rip), %r15     # 0x53fe8
movl %esi, 0x4(%rsp)
leaq 0x1669e(%rip), %rsi     # 0x453fd
movl $0xd, %edx
movq %r15, %rdi
callq 0x7340
movl %ebp, %r13d
movq 0x18(%rsp), %rbp
movl %ebp, %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
movl 0x14(%rsp), %r15d
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movl 0x4(%rsp), %esi
movq %rbp, %rax
movl %r13d, %ebp
movq 0x28(%rsp), %rbx
movq 0xe0(%rsp), %rcx
testl %eax, %eax
jns 0x2efac
movq 0x80(%rsp), %rdx
cmpb $0x1, 0x14(%rdx)
jne 0x2efac
movl %esi, 0x4(%rsp)
movb %bpl, 0x2(%rsp)
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x2ee44
movl %r15d, %ebp
movq 0x251e2(%rip), %r15     # 0x53fe8
leaq 0x16ac1(%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
movl %ebp, %r15d
callq 0x7040
movq %rax, %rdi
callq 0x71d0
movl %r15d, %ebp
subl %r12d, %ebx
leal 0x1(%rbx), %r15d
movslq %r15d, %rdi
callq 0x7430
movl %r15d, %ecx
movq %rax, %r15
cmpl $0x7ffffffe, %ebx       # imm = 0x7FFFFFFE
ja 0x2ee9b
movq 0x30(%rsp), %rax
movq 0x50(%rax), %rax
addq %r12, %rax
cmpl $0x2, %ecx
movl $0x1, %r8d
cmovgel %ecx, %r8d
xorl %edx, %edx
leaq 0x15d7a(%rip), %rsi     # 0x44c00
movsbq (%rax,%rdx), %rdi
movb (%rdi,%rsi), %dil
movb %dil, (%r15,%rdx)
incq %rdx
cmpq %rdx, %r8
jne 0x2ee86
movq 0x8(%rsp), %rdi
movl (%rdi), %eax
movl 0x24(%rsp), %esi
imull %esi, %eax
xorl %r10d, %r10d
cmpl $0xfa, %eax
setl %r10b
orl $0x8, %r10d
shll $0x10, %r10d
movb 0x2(%rsp), %al
xorb $0x1, %al
movzbl %al, %r13d
movq 0xa0(%r14,%r13,8), %rdx
leaq 0x10(%rdi), %rax
movl 0x8(%rdi), %r11d
movl 0xc(%rdi), %ebx
subq $0x8, %rsp
leaq 0x40(%rsp), %rdi
movq %r15, %r8
movl $0x5, %r9d
pushq $0x0
pushq %r10
pushq %rbx
pushq %r11
pushq %rax
callq 0x3f742
addq $0x30, %rsp
movq %r15, %rdi
callq 0x7380
movl 0xd8(%rsp), %eax
leaq 0x38(%rsp), %rcx
cmpl %eax, (%rcx)
movq 0x18(%rsp), %rax
jle 0x2f116
movl $0x100, %edi            # imm = 0x100
callq 0x72e0
movq %rax, %r15
movq %rax, %rdi
callq 0x2c5b6
movq %r15, 0x58(%rsp)
movl 0x38(%rsp), %eax
movl %eax, 0x70(%r15)
movl 0x50(%rsp), %eax
movl %eax, 0x10(%r15)
movl 0x40(%rsp), %eax
movl %eax, 0x14(%r15)
movslq 0x4c(%rsp), %rax
movq %rax, %rcx
addq %r12, %rcx
movq %rcx, (%r15)
movslq 0x3c(%rsp), %rcx
addq %rcx, %r12
movq %r12, 0x8(%r15)
subl %eax, %ecx
incl %ecx
movl %ecx, 0x98(%r15)
movq 0x40(%r15), %rax
andq $-0x11, %rax
shll $0x4, %r13d
orq %rax, %r13
movq %r13, 0x40(%r15)
movq 0xb8(%r14), %rsi
cmpq 0xc0(%r14), %rsi
je 0x2f0f1
movq %r15, (%rsi)
addq $0x8, 0xb8(%r14)
jmp 0x2f102
movl %r15d, %ebp
testl %eax, %eax
js 0x2f0cd
movq 0x8(%rsp), %rdx
cmpl $0x4, 0x78(%rdx)
jl 0x2f018
movq 0x2501f(%rip), %r15     # 0x53fe8
movl %esi, %r12d
leaq 0x16929(%rip), %rsi     # 0x458fc
movl $0x1a, %edx
movq %r15, %rdi
movq %rax, %rbx
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, %esi
movq %rbx, %rax
movq 0xe0(%rsp), %rcx
testl %esi, %esi
jle 0x2f0cd
movl %ebp, %r13d
xorb $0x1, %r13b
movl %eax, %r15d
movl %esi, %ebx
leaq 0x38(%rsp), %r12
movq 0xb0(%r14), %rax
movq (%rax,%r15,8), %rax
movl 0x70(%rax), %eax
movb %r13b, 0x38(%rsp)
movl %r15d, 0x3c(%rsp)
movl %eax, 0x40(%rsp)
movq 0x8(%rcx), %rsi
cmpq 0x10(%rcx), %rsi
je 0x2f06d
movl 0x40(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x38(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rcx)
jmp 0x2f07d
movq 0xe0(%rsp), %rdi
movq %r12, %rdx
callq 0x319e2
testb %bpl, %bpl
je 0x2f099
movq 0x30(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq %r14, %rdx
movq 0x60(%rsp), %rcx
movl %r15d, %r8d
jmp 0x2f0ae
movq 0x30(%rsp), %rdi
movq %r14, %rsi
movq 0x68(%rsp), %rdx
movl %r15d, %ecx
movq 0x60(%rsp), %r8
movl $0x1, %r9d
callq 0x25a0b
incq %r15
decq %rbx
movq 0xe0(%rsp), %rcx
jne 0x2f031
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe0(%rsp), %rcx
movq 0x18(%rsp), %rax
jmp 0x2edc9
leaq 0xb0(%r14), %rdi
leaq 0x58(%rsp), %rdx
callq 0x309b8
movq 0xb8(%r14), %rax
subq 0xb0(%r14), %rax
shrq $0x3, %rax
decl %eax
movq 0xe0(%rsp), %rcx
movl 0x4(%rsp), %esi
jmp 0x2efaf
movq %rax, %rbx
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
callq 0x72f0
movq %rbx, %rdi
callq 0x7600
nop