// File: readmapping[P]alfalfa/src/mapper.cpp
// Function: read_t::postprocess(sparseSA const&, align_opt const&, bool)
void read_t::postprocess(const sparseSA& sa, const align_opt & alnOptions, bool paired){
    //mapq independent from match/mismatch
    if(alnOptions.print >= 3) cerr << "postprocessing read ... " << endl;
    if(!alignments.empty()){
        //FIRST SORT
        if(!paired)
            sort(alignments.begin(),alignments.end(), compAlignments);
        else
            sort(alignments.begin(),alignments.end(), compPairedAlignments);
        int chainCount = 0;
        int maxDP = min((int)alignments.size(),alnOptions.alignmentCount);
        if(alnOptions.fullDP)
            maxDP = min((int)alignments.size(),max(4,alnOptions.alignmentCount));//TODO set parameter
        for(int j = 0; j < maxDP; j++){
            alignments[j]->setLocalPos(sa);
            if(!alignments[j]->chain.empty() && !alnOptions.fullDP){
                chainCount++;
                alignments[j]->constructCIGAR(sa.S, alignments[j]->flag.test(4) ? rcReadP : readP, 
                    sequence.length(), alnOptions.fixedBandSize, alnOptions.mat, 
                    alnOptions.match, alnOptions.openGap, alnOptions.extendGap, alnOptions.print);
            }
            else{
                //full dp version
                fullDP(sa.S, alignments[j]->flag.test(4) ? rcReadP : readP, sequence.size(), 
             alignments[j], alnOptions.fixedBandSize, alnOptions.mat, 
             alnOptions.match, alnOptions.openGap, alnOptions.extendGap, alnOptions.print);
            }
            if(alnOptions.print >= 3) cerr << "edit dist: " << alignments[j]->editDist << " and cigar " << alignments[j]->cigar << endl;
        }
        if(alnOptions.print >= 2){ 
            cerr << "#POSTPROCCHAIN " << chainCount << endl;
            cerr << "#POSTPROCFULL " << (min((int)alignments.size(),alnOptions.alignmentCount)-chainCount) << endl;
        }
        //SECOND SORT
        if(!paired)
            sort(alignments.begin(),alignments.end(), compAlignments);
        else
            sort(alignments.begin(),alignments.end(), compPairedAlignments);
        int maxScore = alignments[0]->alignmentScore + alignments[0]->mate.alignmentScore;
        int secBestScore = alignments.size() > 1 ? alignments[1]->alignmentScore + alignments[1]->mate.alignmentScore : alnOptions.mismatch*sequence.length();
        int mapq = (secBestScore == (int)(alnOptions.mismatch*sequence.length())) ? MAX_MAPQ :
            (int)(250*((double)(maxScore-secBestScore) / maxScore) + 0.499);
        mapq = maxScore == secBestScore ? 0 : mapq;
        mapq = min(max(0,mapq),MAX_MAPQ);
        if(alnOptions.print >= 2){ 
            cerr << "#MAPQ " << mapq << endl;
            cerr << "#POSTPROC " << min((int)alignments.size(),alnOptions.alignmentCount) << endl;
        }
        for(int j = 0; j < min((int)alignments.size(),alnOptions.alignmentCount); j++){
            if(alnOptions.print >= 3) cerr << "constructing cigar for alignment on " << alignments[j]->rname << " and pos " << alignments[j]->pos << endl;
            if(alignments[j]->alignmentScore + alignments[j]->mate.alignmentScore == maxScore)
                alignments[j]->mapq = mapq;
            else
                alignments[j]->flag.set(8,true);
        }
        
    }
}

// Assembly:
pushq %rbx
subq $0x180, %rsp            # imm = 0x180
movb %cl, %al
movq %rdi, 0x178(%rsp)
movq %rsi, 0x170(%rsp)
movq %rdx, 0x168(%rsp)
andb $0x1, %al
movb %al, 0x167(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x168(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x3c89b
movq 0x5a770(%rip), %rdi     # 0x96ff0
leaq 0x41393(%rip), %rsi     # 0x7dc1a
callq 0x8360
movq %rax, %rdi
movq 0x5a71a(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
callq 0xbe60
testb $0x1, %al
jne 0x3d3dd
testb $0x1, 0x167(%rsp)
jne 0x3c917
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
callq 0x4c270
movq 0xd8(%rsp), %rdi
movq %rax, 0x158(%rsp)
addq $0xb0, %rdi
callq 0x4c2a0
movq %rax, 0x150(%rsp)
movq 0x158(%rsp), %rdi
movq 0x150(%rsp), %rsi
leaq -0x370(%rip), %rdx      # 0x3c5a0
callq 0x4c220
jmp 0x3c96b
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
callq 0x4c270
movq 0xd8(%rsp), %rdi
movq %rax, 0x148(%rsp)
addq $0xb0, %rdi
callq 0x4c2a0
movq %rax, 0x140(%rsp)
movq 0x148(%rsp), %rdi
movq 0x140(%rsp), %rsi
leaq -0x3a6(%rip), %rdx      # 0x3c5c0
callq 0x4c220
movq 0xd8(%rsp), %rdi
movl $0x0, 0x13c(%rsp)
addq $0xb0, %rdi
callq 0xdac0
movl %eax, 0x134(%rsp)
movq 0x168(%rsp), %rsi
addq $0x50, %rsi
leaq 0x134(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
movl %eax, 0x138(%rsp)
movq 0x168(%rsp), %rax
testb $0x1, 0x73(%rax)
je 0x3ca19
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
callq 0xdac0
movl %eax, 0x130(%rsp)
movl $0x4, 0x12c(%rsp)
movq 0x168(%rsp), %rsi
addq $0x50, %rsi
leaq 0x12c(%rsp), %rdi
callq 0x14d40
movq %rax, %rsi
leaq 0x130(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
movl %eax, 0x138(%rsp)
movl $0x0, 0x128(%rsp)
movl 0x128(%rsp), %eax
cmpl 0x138(%rsp), %eax
jge 0x3cdf3
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq (%rax), %rdi
movq 0x170(%rsp), %rsi
callq 0x4c2d0
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq (%rax), %rdi
addq $0xe8, %rdi
callq 0x4c370
testb $0x1, %al
jne 0x3cbf6
movq 0x168(%rsp), %rax
testb $0x1, 0x73(%rax)
jne 0x3cbf6
movq 0xd8(%rsp), %rdi
movl 0x13c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x13c(%rsp)
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq 0xd8(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xc8(%rsp)
movq 0x170(%rsp), %rax
addq $0x50, %rax
movq %rax, 0xd0(%rsp)
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq (%rax), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
testb $0x1, %al
jne 0x3cb28
jmp 0x3cb41
movq 0xd8(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rax, 0xc0(%rsp)
jmp 0x3cb58
movq 0xd8(%rsp), %rax
movq 0xa0(%rax), %rax
movq %rax, 0xc0(%rsp)
movq 0xd8(%rsp), %rdi
movq 0xc0(%rsp), %rax
movq %rax, 0xb8(%rsp)
addq $0x20, %rdi
callq 0x8630
movq 0xc8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movl %eax, %ecx
movq 0x168(%rsp), %rax
movl 0x90(%rax), %r8d
movq 0x168(%rsp), %r9
addq $0x10, %r9
movq 0x168(%rsp), %rax
movl (%rax), %ebx
movq 0x168(%rsp), %rax
movl 0x8(%rax), %r11d
movq 0x168(%rsp), %rax
movl 0xc(%rax), %r10d
movq 0x168(%rsp), %rax
movl 0x78(%rax), %eax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x3a8d0
jmp 0x3cd29
movq 0xd8(%rsp), %rdi
movq 0x170(%rsp), %rax
addq $0x50, %rax
movq %rax, 0xb0(%rsp)
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq (%rax), %rdi
addq $0x40, %rdi
movl $0x4, %esi
callq 0xe340
testb $0x1, %al
jne 0x3cc3d
jmp 0x3cc56
movq 0xd8(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rax, 0xa8(%rsp)
jmp 0x3cc6d
movq 0xd8(%rsp), %rax
movq 0xa0(%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0xd8(%rsp), %rdi
movq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
addq $0x20, %rdi
callq 0x8250
movq 0xd8(%rsp), %rdi
movl %eax, 0xa4(%rsp)
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq 0xb0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl 0xa4(%rsp), %edx
movq (%rax), %rcx
movq 0x168(%rsp), %rax
movl 0x90(%rax), %r8d
movq 0x168(%rsp), %r9
addq $0x10, %r9
movq 0x168(%rsp), %rax
movl (%rax), %ebx
movq 0x168(%rsp), %rax
movl 0x8(%rax), %r11d
movq 0x168(%rsp), %rax
movl 0xc(%rax), %r10d
movq 0x168(%rsp), %rax
movl 0x78(%rax), %eax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x3c600
movq 0x168(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x3cddb
movq 0x5a2ae(%rip), %rdi     # 0x96ff0
leaq 0x40eea(%rip), %rsi     # 0x7dc33
callq 0x8360
movq 0xd8(%rsp), %rdi
movq %rax, 0x88(%rsp)
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq 0x88(%rsp), %rdi
movq (%rax), %rax
movl 0x6c(%rax), %esi
callq 0x8710
movq %rax, %rdi
leaq 0x40eb0(%rip), %rsi     # 0x7dc3f
callq 0x8360
movq 0xd8(%rsp), %rdi
movq %rax, 0x90(%rsp)
addq $0xb0, %rdi
movslq 0x128(%rsp), %rsi
callq 0xdae0
movq 0x90(%rsp), %rdi
movq (%rax), %rsi
addq $0x20, %rsi
callq 0x8330
movq %rax, %rdi
movq 0x5a1da(%rip), %rsi     # 0x96fb0
callq 0x83b0
jmp 0x3cddd
movl 0x128(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x128(%rsp)
jmp 0x3ca24
movq 0x168(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x3ceaa
movq 0x5a1e4(%rip), %rdi     # 0x96ff0
leaq 0x40e38(%rip), %rsi     # 0x7dc4b
callq 0x8360
movq %rax, %rdi
movl 0x13c(%rsp), %esi
callq 0x8710
movq %rax, %rdi
movq 0x5a17f(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x5a1b3(%rip), %rdi     # 0x96ff0
leaq 0x40e17(%rip), %rsi     # 0x7dc5b
callq 0x8360
movq 0xd8(%rsp), %rdi
movq %rax, 0x80(%rsp)
addq $0xb0, %rdi
callq 0xdac0
movl %eax, 0x124(%rsp)
movq 0x168(%rsp), %rsi
addq $0x50, %rsi
leaq 0x124(%rsp), %rdi
callq 0xc080
movq 0x80(%rsp), %rdi
movl (%rax), %esi
subl 0x13c(%rsp), %esi
callq 0x8710
movq %rax, %rdi
movq 0x5a10b(%rip), %rsi     # 0x96fb0
callq 0x83b0
testb $0x1, 0x167(%rsp)
jne 0x3cf0a
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
callq 0x4c270
movq 0xd8(%rsp), %rdi
movq %rax, 0x118(%rsp)
addq $0xb0, %rdi
callq 0x4c2a0
movq %rax, 0x110(%rsp)
movq 0x118(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq -0x963(%rip), %rdx      # 0x3c5a0
callq 0x4c220
jmp 0x3cf5e
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
callq 0x4c270
movq 0xd8(%rsp), %rdi
movq %rax, 0x108(%rsp)
addq $0xb0, %rdi
callq 0x4c2a0
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq -0x999(%rip), %rdx      # 0x3c5c0
callq 0x4c220
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xdae0
movq 0xd8(%rsp), %rdi
movq (%rax), %rax
movl 0x70(%rax), %eax
movl %eax, 0x7c(%rsp)
addq $0xb0, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xdae0
movq 0xd8(%rsp), %rdi
movq %rax, %rcx
movl 0x7c(%rsp), %eax
movq (%rcx), %rcx
addl 0xe0(%rcx), %eax
movl %eax, 0xfc(%rsp)
addq $0xb0, %rdi
callq 0xdac0
cmpq $0x1, %rax
jbe 0x3d01e
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
movl $0x1, %esi
callq 0xdae0
movq 0xd8(%rsp), %rdi
movq (%rax), %rax
movl 0x70(%rax), %eax
movl %eax, 0x6c(%rsp)
addq $0xb0, %rdi
movl $0x1, %esi
callq 0xdae0
movq %rax, %rcx
movl 0x6c(%rsp), %eax
movq (%rcx), %rcx
addl 0xe0(%rcx), %eax
cltq
movq %rax, 0x70(%rsp)
jmp 0x3d051
movq 0xd8(%rsp), %rdi
movq 0x168(%rsp), %rax
movslq 0x4(%rax), %rax
movq %rax, 0x60(%rsp)
addq $0x20, %rdi
callq 0x8630
movq %rax, %rcx
movq 0x60(%rsp), %rax
imulq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0xd8(%rsp), %rdi
movq 0x70(%rsp), %rax
movl %eax, 0xf8(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x168(%rsp), %rax
movslq 0x4(%rax), %rax
movq %rax, 0x50(%rsp)
addq $0x20, %rdi
callq 0x8630
movq 0x50(%rsp), %rcx
movq %rax, %rdx
movl 0x5c(%rsp), %eax
imulq %rdx, %rcx
cmpl %ecx, %eax
jne 0x3d0a9
movl $0x3c, %eax
movl %eax, 0x4c(%rsp)
jmp 0x3d0e8
movl 0xfc(%rsp), %eax
subl 0xf8(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0xfc(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x40a70(%rip), %xmm1    # 0x7db40
mulsd %xmm1, %xmm0
movsd 0x40a6c(%rip), %xmm1    # 0x7db48
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0xf4(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jne 0x3d10b
xorl %eax, %eax
movl %eax, 0x48(%rsp)
jmp 0x3d116
movl 0xf4(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0xf4(%rsp)
movl $0x0, 0xf0(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xf4(%rsp), %rsi
callq 0x14d40
movq %rax, %rdi
leaq 0x41755(%rip), %rsi     # 0x7e8a0
callq 0xc080
movl (%rax), %eax
movl %eax, 0xf4(%rsp)
movq 0x168(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jl 0x3d203
movq 0x59e7e(%rip), %rdi     # 0x96ff0
leaq 0x40af1(%rip), %rsi     # 0x7dc6a
callq 0x8360
movq %rax, %rdi
movl 0xf4(%rsp), %esi
callq 0x8710
movq %rax, %rdi
movq 0x59e19(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0x59e4d(%rip), %rdi     # 0x96ff0
leaq 0x40ac7(%rip), %rsi     # 0x7dc71
callq 0x8360
movq 0xd8(%rsp), %rdi
movq %rax, 0x40(%rsp)
addq $0xb0, %rdi
callq 0xdac0
movl %eax, 0xec(%rsp)
movq 0x168(%rsp), %rsi
addq $0x50, %rsi
leaq 0xec(%rsp), %rdi
callq 0xc080
movq 0x40(%rsp), %rdi
movl (%rax), %esi
callq 0x8710
movq %rax, %rdi
movq 0x59db2(%rip), %rsi     # 0x96fb0
callq 0x83b0
movl $0x0, 0xe8(%rsp)
movq 0xd8(%rsp), %rdi
movl 0xe8(%rsp), %eax
movl %eax, 0x3c(%rsp)
addq $0xb0, %rdi
callq 0xdac0
movl %eax, 0xe4(%rsp)
movq 0x168(%rsp), %rsi
addq $0x50, %rsi
leaq 0xe4(%rsp), %rdi
callq 0xc080
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cmpl (%rcx), %eax
jge 0x3d3db
movq 0x168(%rsp), %rax
cmpl $0x3, 0x78(%rax)
jl 0x3d303
movq 0x59d7b(%rip), %rdi     # 0x96ff0
leaq 0x40a00(%rip), %rsi     # 0x7dc7c
callq 0x8360
movq 0xd8(%rsp), %rdi
movq %rax, 0x28(%rsp)
addq $0xb0, %rdi
movslq 0xe8(%rsp), %rsi
callq 0xdae0
movq 0x28(%rsp), %rdi
movq (%rax), %rsi
addq $0x48, %rsi
callq 0x8330
movq %rax, %rdi
leaq 0x409e4(%rip), %rsi     # 0x7dca1
callq 0x8360
movq 0xd8(%rsp), %rdi
movq %rax, 0x30(%rsp)
addq $0xb0, %rdi
movslq 0xe8(%rsp), %rsi
callq 0xdae0
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movq 0x18(%rax), %rsi
callq 0x8790
movq %rax, %rdi
movq 0x59cb2(%rip), %rsi     # 0x96fb0
callq 0x83b0
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
movslq 0xe8(%rsp), %rsi
callq 0xdae0
movq 0xd8(%rsp), %rdi
movq (%rax), %rax
movl 0x70(%rax), %eax
movl %eax, 0x24(%rsp)
addq $0xb0, %rdi
movslq 0xe8(%rsp), %rsi
callq 0xdae0
movq %rax, %rcx
movl 0x24(%rsp), %eax
movq (%rcx), %rcx
addl 0xe0(%rcx), %eax
cmpl 0xfc(%rsp), %eax
jne 0x3d391
movq 0xd8(%rsp), %rdi
movl 0xf4(%rsp), %eax
movl %eax, 0x20(%rsp)
addq $0xb0, %rdi
movslq 0xe8(%rsp), %rsi
callq 0xdae0
movl 0x20(%rsp), %ecx
movq (%rax), %rax
movl %ecx, 0x68(%rax)
jmp 0x3d3c3
movq 0xd8(%rsp), %rdi
addq $0xb0, %rdi
movslq 0xe8(%rsp), %rsi
callq 0xdae0
movq (%rax), %rdi
addq $0x40, %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0xe640
jmp 0x3d3c5
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x3d20e
jmp 0x3d3dd
addq $0x180, %rsp            # imm = 0x180
popq %rbx
retq
nopw %cs:(%rax,%rax)