// File: readmapping[P]alfalfa/src/mapper.cpp
// Function: pairedMatch1(sparseSA const&, read_t&, read_t&, align_opt const&, paired_opt const&)
void pairedMatch1(const sparseSA& sa, read_t & mate1, read_t & mate2, const align_opt & alnOptions, const paired_opt & pairedOpt){
    //Calculate the mappings for read 1 and 2
    if(alnOptions.print >= 3) cerr << "matching first mate ..." << endl;
    unpairedMatch(sa, mate1, alnOptions);
    if(alnOptions.print >= 3) cerr << "first mate resulted in " << mate1.alignments.size() << " alignments" << endl;
    if(alnOptions.print >= 3) cerr << "matching second mate ..." << endl;
    unpairedMatch(sa, mate2, alnOptions);
    if(alnOptions.print >= 3) cerr << "second mate resulted in " << mate2.alignments.size() << " alignments" << endl;
    int alnCount1 = mate1.alignmentCount();
    int alnCount2 = mate2.alignmentCount();
    int alnCount = 0;
    int maxPairedAlignments = alnOptions.alignmentCount+1;
    //ORDER ALIGNMENTS ACCORDING TO SCORE
    vector<alnScoreInfo_t> pairedOrder;
    pairedOrder.reserve(alnCount1+alnCount2);
    for(size_t i = 0; i < mate1.alignments.size(); i++)
        pairedOrder.push_back(alnScoreInfo_t(true, i,mate1.alignments[i]->alignmentScore ));
    for(size_t i = 0; i < mate2.alignments.size(); i++)
        pairedOrder.push_back(alnScoreInfo_t(false, i,mate2.alignments[i]->alignmentScore ));
    sort(pairedOrder.begin(),pairedOrder.end(),compAlnScorePaired);
    //TRY AND PAIR THE ALIGNMENTS
    if(alnCount1>0 && alnCount2>0){
        if(alnOptions.print >= 3) cerr << "search concordant alignments of " << pairedOrder.size() << " alignments" << endl;
        pairAlignmentsOrdered(sa, mate1, mate2, alnOptions, pairedOpt, 
        pairedOrder, alnCount, maxPairedAlignments);
        if(alnOptions.print >= 3) cerr << "found " << alnCount << " concordant alignments" << endl;
    }
    //RESCUE PROCEDURE IF NO CONCORDANT ALIGNMENTS WERE FOUND
    if(pairedOpt.pairedRescue && alnCount < maxPairedAlignments && max(alnCount1,alnCount2)>0){
        if(alnOptions.print >= 2) cerr << "#PAIREDRESCUE" << endl;
        if(alnOptions.print >= 3) cerr << "try to recover concordant alignments using windows " << endl;
        vector<alnScoreInfo_t> newAlnFromWindow;
        pairAlignmentToWindow(sa, mate1, mate2, alnOptions, pairedOpt, pairedOrder, 
        newAlnFromWindow, alnCount, maxPairedAlignments);
        if(alnOptions.print >= 3) cerr << "rescue procedure resulted in " << alnCount << " concordant alignments" << endl;
    }
    //FIND DISCORDANT ALIGNMENTS AMONGST HIGHEST SCORING UNPAIRED ALIGNMENTS
    int pairedAlignments = mate1.pairedAlignmentCount;
    if(alnOptions.print >= 2) cerr << "#CONCORDANTCOUNT " << pairedAlignments << endl;
    if(pairedOpt.discordant && alnCount < maxPairedAlignments)
        calculateDiscordantAlignments(sa, mate1, mate2);
    if(alnOptions.print >= 2) cerr << "#DISCORDANTCOUNT " << mate1.pairedAlignmentCount-pairedAlignments << endl;
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x1a0(%rsp)
movq %rsi, 0x198(%rsp)
movq %rdx, 0x190(%rsp)
movq %rcx, 0x188(%rsp)
movq %r8, 0x180(%rsp)
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x453df
movq 0x51c2c(%rip), %rdi     # 0x96ff0
leaq 0x38f0c(%rip), %rsi     # 0x7e2d7
callq 0x8360
movq %rax, %rdi
movq 0x51bd6(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
movq 0x188(%rsp), %rdx
callq 0x42e50
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x45467
movq 0x51bdf(%rip), %rdi     # 0x96ff0
leaq 0x38ed7(%rip), %rsi     # 0x7e2ef
callq 0x8360
movq %rax, 0xe0(%rsp)
movq 0x198(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdac0
movq 0xe0(%rsp), %rdi
movq %rax, %rsi
callq 0x8100
movq %rax, %rdi
leaq 0x39094(%rip), %rsi     # 0x7e4e7
callq 0x8360
movq %rax, %rdi
movq 0x51b4e(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x45497
movq 0x51b74(%rip), %rdi     # 0x96ff0
leaq 0x38e84(%rip), %rsi     # 0x7e307
callq 0x8360
movq %rax, %rdi
movq 0x51b1e(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x1a0(%rsp), %rdi
movq 0x190(%rsp), %rsi
movq 0x188(%rsp), %rdx
callq 0x42e50
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x4551f
movq 0x51b27(%rip), %rdi     # 0x96ff0
leaq 0x38e50(%rip), %rsi     # 0x7e320
callq 0x8360
movq %rax, 0xd8(%rsp)
movq 0x190(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdac0
movq 0xd8(%rsp), %rdi
movq %rax, %rsi
callq 0x8100
movq %rax, %rdi
leaq 0x38fdc(%rip), %rsi     # 0x7e4e7
callq 0x8360
movq %rax, %rdi
movq 0x51a96(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x198(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x17c(%rsp)
movq 0x190(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x178(%rsp)
movl $0x0, 0x174(%rsp)
movq 0x188(%rsp), %rax
movl 0x50(%rax), %eax
incl %eax
movl %eax, 0x170(%rsp)
leaq 0x158(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
callq 0x4e6a0
movq 0xd0(%rsp), %rdi
movl 0x17c(%rsp), %eax
movl 0x178(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x4e6b0
jmp 0x4559d
movq $0x0, 0x140(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x198(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdac0
movq %rax, %rcx
movq 0xc8(%rsp), %rax
cmpq %rcx, %rax
jae 0x45672
movq 0x140(%rsp), %rsi
movl %esi, %eax
movl %eax, 0xc4(%rsp)
movq 0x198(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdae0
movl 0xc4(%rsp), %edx
movq (%rax), %rax
movl 0x70(%rax), %ecx
leaq 0x134(%rsp), %rdi
movl $0x1, %esi
callq 0x4e660
jmp 0x45627
leaq 0x158(%rsp), %rdi
leaq 0x134(%rsp), %rsi
callq 0x4e630
jmp 0x4563e
jmp 0x45640
movq 0x140(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x140(%rsp)
jmp 0x455a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0x45be4
movq $0x0, 0x128(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x190(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdac0
movq %rax, %rcx
movq 0xb8(%rsp), %rax
cmpq %rcx, %rax
jae 0x45727
movq 0x128(%rsp), %rsi
movl %esi, %eax
movl %eax, 0xb4(%rsp)
movq 0x190(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdae0
movl 0xb4(%rsp), %edx
movq (%rax), %rax
movl 0x70(%rax), %ecx
leaq 0x11c(%rsp), %rdi
xorl %esi, %esi
callq 0x4e660
jmp 0x456f5
leaq 0x158(%rsp), %rdi
leaq 0x11c(%rsp), %rsi
callq 0x4e630
jmp 0x4570c
jmp 0x4570e
movq 0x128(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x128(%rsp)
jmp 0x4567e
leaq 0x158(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x4e810
movq 0xa8(%rsp), %rdi
movq %rax, 0x110(%rsp)
callq 0x4e840
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rdi
movq 0x108(%rsp), %rsi
leaq -0x15a0(%rip), %rdx     # 0x441d0
callq 0x4e7c0
jmp 0x45777
cmpl $0x0, 0x17c(%rsp)
jle 0x458e7
cmpl $0x0, 0x178(%rsp)
jle 0x458e7
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x4581b
movq 0x51848(%rip), %rdi     # 0x96ff0
leaq 0x38b8a(%rip), %rsi     # 0x7e339
callq 0x8360
movq %rax, 0xa0(%rsp)
jmp 0x457be
leaq 0x158(%rsp), %rdi
callq 0x4e5b0
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x8100
movq %rax, 0x98(%rsp)
jmp 0x457e5
movq 0x98(%rsp), %rdi
leaq 0x38cf3(%rip), %rsi     # 0x7e4e7
callq 0x8360
movq %rax, 0x90(%rsp)
jmp 0x45803
movq 0x90(%rsp), %rdi
movq 0x5179e(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x45819
jmp 0x4581b
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
movq 0x190(%rsp), %rdx
movq 0x188(%rsp), %rcx
movq 0x180(%rsp), %r8
movl 0x170(%rsp), %r9d
movq %rsp, %rax
movl %r9d, 0x8(%rax)
leaq 0x174(%rsp), %r9
movq %r9, (%rax)
leaq 0x158(%rsp), %r9
callq 0x44f10
jmp 0x4586c
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x458e5
movq 0x5176f(%rip), %rdi     # 0x96ff0
leaq 0x38ad2(%rip), %rsi     # 0x7e35a
callq 0x8360
movq %rax, 0x88(%rsp)
jmp 0x45897
movq 0x88(%rsp), %rdi
movl 0x174(%rsp), %esi
callq 0x8710
movq %rax, 0x80(%rsp)
jmp 0x458b5
movq 0x80(%rsp), %rdi
leaq 0x38c18(%rip), %rsi     # 0x7e4dc
callq 0x8360
movq %rax, 0x78(%rsp)
jmp 0x458d0
movq 0x78(%rsp), %rdi
movq 0x516d4(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x458e3
jmp 0x458e5
jmp 0x458e7
movq 0x180(%rsp), %rax
testb $0x1, 0x14(%rax)
je 0x45ac1
movl 0x174(%rsp), %eax
cmpl 0x170(%rsp), %eax
jge 0x45ac1
leaq 0x17c(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x14d40
movq %rax, 0x70(%rsp)
jmp 0x45929
movq 0x70(%rsp), %rax
cmpl $0x0, (%rax)
jle 0x45ac1
movq 0x188(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x45974
movq 0x516a4(%rip), %rdi     # 0x96ff0
leaq 0x38a0e(%rip), %rsi     # 0x7e361
callq 0x8360
movq %rax, 0x68(%rsp)
jmp 0x4595f
movq 0x68(%rsp), %rdi
movq 0x51645(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x45972
jmp 0x45974
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x459b1
movq 0x51667(%rip), %rdi     # 0x96ff0
leaq 0x389df(%rip), %rsi     # 0x7e36f
callq 0x8360
movq %rax, 0x60(%rsp)
jmp 0x4599c
movq 0x60(%rsp), %rdi
movq 0x51608(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x459af
jmp 0x459b1
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x4e6a0
movq 0x58(%rsp), %r9
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
movq 0x190(%rsp), %rdx
movq 0x188(%rsp), %rcx
movq 0x180(%rsp), %r8
movl 0x170(%rsp), %r10d
movq %rsp, %rax
movl %r10d, 0x10(%rax)
leaq 0x174(%rsp), %r10
movq %r10, 0x8(%rax)
movq %r9, (%rax)
leaq 0x158(%rsp), %r9
callq 0x44370
jmp 0x45a1d
movq 0x188(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x45ab4
movq 0x515ba(%rip), %rdi     # 0x96ff0
leaq 0x38966(%rip), %rsi     # 0x7e3a3
callq 0x8360
movq %rax, 0x50(%rsp)
jmp 0x45a49
movq 0x50(%rsp), %rdi
movl 0x174(%rsp), %esi
callq 0x8710
movq %rax, 0x48(%rsp)
jmp 0x45a61
movq 0x48(%rsp), %rdi
leaq 0x38a6f(%rip), %rsi     # 0x7e4dc
callq 0x8360
movq %rax, 0x40(%rsp)
jmp 0x45a79
movq 0x40(%rsp), %rdi
movq 0x5152b(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x45a8c
jmp 0x45ab4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x4e870
jmp 0x45be4
leaq 0xf0(%rsp), %rdi
callq 0x4e870
movq 0x198(%rsp), %rax
movl 0xc8(%rax), %eax
movl %eax, 0xec(%rsp)
movq 0x188(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x45b2b
movq 0x51505(%rip), %rdi     # 0x96ff0
leaq 0x388cf(%rip), %rsi     # 0x7e3c1
callq 0x8360
movq %rax, 0x38(%rsp)
jmp 0x45afe
movq 0x38(%rsp), %rdi
movl 0xec(%rsp), %esi
callq 0x8710
movq %rax, 0x30(%rsp)
jmp 0x45b16
movq 0x30(%rsp), %rdi
movq 0x5148e(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x45b29
jmp 0x45b2b
movq 0x180(%rsp), %rax
testb $0x1, 0x9(%rax)
je 0x45b6a
movl 0x174(%rsp), %eax
cmpl 0x170(%rsp), %eax
jge 0x45b6a
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
movq 0x190(%rsp), %rdx
callq 0x43d30
jmp 0x45b68
jmp 0x45b6a
movq 0x188(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x45bcf
movq 0x51471(%rip), %rdi     # 0x96ff0
leaq 0x3884d(%rip), %rsi     # 0x7e3d3
callq 0x8360
movq %rax, 0x28(%rsp)
jmp 0x45b92
movq 0x28(%rsp), %rdi
movq 0x198(%rsp), %rax
movl 0xc8(%rax), %esi
movl 0xec(%rsp), %eax
subl %eax, %esi
callq 0x8710
movq %rax, 0x20(%rsp)
jmp 0x45bba
movq 0x20(%rsp), %rdi
movq 0x513ea(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x45bcd
jmp 0x45bcf
leaq 0x158(%rsp), %rdi
callq 0x4e870
addq $0x1a8, %rsp            # imm = 0x1A8
retq
leaq 0x158(%rsp), %rdi
callq 0x4e870
movq 0x150(%rsp), %rdi
callq 0x8730
nop