// File: readmapping[P]alfalfa/src/mapper.cpp
// Function: pairAlignmentsOrdered(sparseSA const&, read_t&, read_t&, align_opt const&, paired_opt const&, std::vector<alnScoreInfo_t, std::allocator<alnScoreInfo_t>> const&, int&, int)
void pairAlignmentsOrdered(const sparseSA& sa, read_t & mate1, read_t & mate2, 
        const align_opt & alnOptions, const paired_opt & pairedOpt, 
        const vector<alnScoreInfo_t> & pairedOrder, int & alnCount, int maxPairedAlignments){
    if(alnOptions.print >= 2) cerr << "#COUPLEALN " << pairedOrder.size() << endl;
    size_t i = 0;
    //TRY AND PAIR THE ALIGNMENTS
    while(i < pairedOrder.size() && alnCount < maxPairedAlignments){
        size_t j = i+1;
        bool paired = pairedOrder[i].firstMate ? mate1.alignments[pairedOrder[i].position]->paired() : mate2.alignments[pairedOrder[i].position]->paired();
        if(!paired){
            while(j < pairedOrder.size() && alnCount < maxPairedAlignments){
                if(pairedOrder[i].firstMate != pairedOrder[j].firstMate){
                    int mate1Index = pairedOrder[i].firstMate ? pairedOrder[i].position : pairedOrder[j].position;
                    int mate2Index = pairedOrder[i].firstMate ? pairedOrder[j].position : pairedOrder[i].position;
                    alignment_t * aln1 = mate1.alignments[mate1Index];
                    aln1->setLocalPos(sa);
                    alignment_t * aln2 = mate2.alignments[mate2Index];
                    aln2->setLocalPos(sa);
                    if(isConcordant(aln1, aln2, pairedOpt, alnOptions.print)){
                        if(alnOptions.print >= 4) cerr << "found concordancy between aln " << mate1Index << " of mate 1 and aln " << mate2Index << " of mate 2" << endl;
                        setPaired(sa, mate1, mate2,mate1Index,mate2Index, true);
                        alnCount++;
                    }
                }
                j++;
            }
        }
        i++;
    }
}

// Assembly:
subq $0xb8, %rsp
movl 0xc8(%rsp), %eax
movq 0xc0(%rsp), %rax
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
movq %r8, 0x90(%rsp)
movq %r9, 0x88(%rsp)
movq 0x98(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x44fa5
movq 0x52085(%rip), %rdi     # 0x96ff0
leaq 0x3931b(%rip), %rsi     # 0x7e28d
callq 0x8360
movq %rax, 0x50(%rsp)
movq 0x88(%rsp), %rdi
callq 0x4e5b0
movq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x8100
movq %rax, %rdi
movq 0x52010(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq $0x0, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x88(%rsp), %rdi
callq 0x4e5b0
movq 0x40(%rsp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, 0x4f(%rsp)
jae 0x44ff6
movq 0xc0(%rsp), %rax
movl (%rax), %eax
cmpl 0xc8(%rsp), %eax
setl %al
movb %al, 0x4f(%rsp)
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x45003
jmp 0x45377
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
testb $0x1, (%rax)
je 0x45072
movq 0xa8(%rsp), %rax
addq $0xb0, %rax
movq %rax, 0x30(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
movq 0x30(%rsp), %rdi
movslq 0x4(%rax), %rsi
callq 0xdae0
movq (%rax), %rdi
callq 0xe470
movb %al, 0x3f(%rsp)
jmp 0x450b4
movq 0xa0(%rsp), %rax
addq $0xb0, %rax
movq %rax, 0x28(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
movq 0x28(%rsp), %rdi
movslq 0x4(%rax), %rsi
callq 0xdae0
movq (%rax), %rdi
callq 0xe470
movb %al, 0x3f(%rsp)
movb 0x3f(%rsp), %al
andb $0x1, %al
movb %al, 0x77(%rsp)
testb $0x1, 0x77(%rsp)
jne 0x4535e
jmp 0x450cb
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x88(%rsp), %rdi
callq 0x4e5b0
movq 0x18(%rsp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, 0x27(%rsp)
jae 0x4510d
movq 0xc0(%rsp), %rax
movl (%rax), %eax
cmpl 0xc8(%rsp), %eax
setl %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x4511a
jmp 0x4535c
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
movb (%rax), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x14(%rsp)
movq 0x88(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x4e5d0
movq %rax, %rcx
movl 0x14(%rsp), %eax
movb (%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x45349
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
testb $0x1, (%rax)
je 0x4519b
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
movl 0x4(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x451b4
movq 0x88(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x4e5d0
movl 0x4(%rax), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x70(%rsp)
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
testb $0x1, (%rax)
je 0x451f1
movq 0x88(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x4e5d0
movl 0x4(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x4520d
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x4e5d0
movl 0x4(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0xa8(%rsp), %rdi
addq $0xb0, %rdi
movslq 0x70(%rsp), %rsi
callq 0xdae0
movq (%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x4c2d0
movq 0xa0(%rsp), %rdi
addq $0xb0, %rdi
movslq 0x6c(%rsp), %rsi
callq 0xdae0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x4c2d0
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x90(%rsp), %rdx
movq 0x98(%rsp), %rax
movl 0x78(%rax), %ecx
callq 0x43290
testb $0x1, %al
jne 0x452a6
jmp 0x45347
movq 0x98(%rsp), %rax
cmpl $0x4, 0x78(%rax)
jl 0x4530c
movq 0x51d35(%rip), %rdi     # 0x96ff0
leaq 0x38fd7(%rip), %rsi     # 0x7e299
callq 0x8360
movq %rax, %rdi
movl 0x70(%rsp), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x38fdb(%rip), %rsi     # 0x7e2b8
callq 0x8360
movq %rax, %rdi
movl 0x6c(%rsp), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x38fd4(%rip), %rsi     # 0x7e2cc
callq 0x8360
movq %rax, %rdi
movq 0x51ca9(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0xb0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movl 0x70(%rsp), %ecx
movl 0x6c(%rsp), %r8d
movl $0x1, %r9d
callq 0x43b50
movq 0xc0(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x45349
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x450cb
jmp 0x4535e
movq 0x80(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x80(%rsp)
jmp 0x44fb1
addq $0xb8, %rsp
retq
nop