// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %ebp
movq %rdx, %rbx
movq %rsi, 0x20(%rsp)
movq %rdi, %r14
cmpl $0x3, 0x78(%rdx)
jl 0x1a478
movq 0x2fb84(%rip), %rdi     # 0x49fe8
leaq 0x1c90f(%rip), %rsi     # 0x36d7a
callq 0x72b0
movq %rax, %rdi
callq 0x7160
movq 0xb0(%r14), %rdi
movq 0xb8(%r14), %rsi
cmpq %rsi, %rdi
je 0x1a89d
leaq -0x20b(%rip), %rax      # 0x1a28b
leaq -0x21c(%rip), %rdx      # 0x1a281
movl %ebp, 0xc(%rsp)
testb %bpl, %bpl
cmovneq %rax, %rdx
callq 0x255ff
movq 0xb8(%r14), %rax
subq 0xb0(%r14), %rax
shrq $0x3, %rax
movl 0x50(%rbx), %ecx
cmpl %eax, %ecx
movl %eax, %r12d
cmovll %ecx, %r12d
cmpb $0x1, 0x73(%rbx)
jne 0x1a4e3
cmpl $0x5, %ecx
pushq $0x4
popq %r12
cmovgel %ecx, %r12d
cmpl %eax, %r12d
cmovgel %eax, %r12d
movq 0x20(%rsp), %rax
addq $0x50, %rax
movq %rax, 0x18(%rsp)
leaq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
testl %r12d, %r12d
cmovlel %ebp, %r12d
movl $0x0, 0x8(%rsp)
cmpq %rbp, %r12
je 0x1a656
movq 0xb0(%r14), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rsp), %rsi
callq 0x21f20
movq 0xb0(%r14), %rax
movq (%rax,%rbp,8), %r13
movq 0xe8(%r13), %rax
cmpq 0xf0(%r13), %rax
je 0x1a54a
cmpb $0x0, 0x73(%rbx)
je 0x1a5a0
addq $0x40, %r13
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0xa11c
movzbl %al, %eax
movq 0xa0(%r14,%rax,8), %rsi
movl 0x28(%r14), %edx
movq 0xb0(%r14), %rax
movq (%rax,%rbp,8), %rcx
movl 0x90(%rbx), %r8d
movl (%rbx), %eax
movl 0x8(%rbx), %r10d
movl 0xc(%rbx), %r11d
movl 0x78(%rbx), %r15d
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r9
pushq %r15
pushq %r11
pushq %r10
pushq %rax
callq 0x1a2a3
jmp 0x1a5ed
incl 0x8(%rsp)
leaq 0x40(%r13), %rdi
pushq $0x4
popq %rsi
callq 0xa11c
movzbl %al, %eax
movq 0xa0(%r14,%rax,8), %rdx
movl 0x28(%r14), %ecx
movl 0x90(%rbx), %r8d
movl (%rbx), %eax
movl 0x8(%rbx), %r10d
movl 0xc(%rbx), %r11d
movl 0x78(%rbx), %r15d
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r9
pushq %r15
pushq %r11
pushq %r10
pushq %rax
callq 0x1961c
addq $0x20, %rsp
cmpl $0x3, 0x78(%rbx)
jl 0x1a64e
movq 0x2f9ea(%rip), %rdi     # 0x49fe8
leaq 0x1c78e(%rip), %rsi     # 0x36d93
callq 0x72b0
movq 0xb0(%r14), %rcx
movq (%rcx,%rbp,8), %rcx
movl 0x6c(%rcx), %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
leaq 0x1c775(%rip), %rsi     # 0x36d9f
callq 0x72b0
movq 0xb0(%r14), %rcx
movq (%rcx,%rbp,8), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x7280
movq %rax, %rdi
callq 0x7160
incq %rbp
jmp 0x1a50b
cmpl $0x1, 0x78(%rbx)
jle 0x1a6c3
movq 0x2f985(%rip), %r15     # 0x49fe8
leaq 0x1c741(%rip), %rsi     # 0x36dab
movq %r15, %rdi
callq 0x72b0
movq %rax, %rdi
movl 0x8(%rsp), %ebp
movl %ebp, %esi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
leaq 0x1c72c(%rip), %rsi     # 0x36dbb
movq %r15, %rdi
callq 0x72b0
movq 0xb8(%r14), %rsi
subq 0xb0(%r14), %rsi
shrq $0x3, %rsi
movl 0x50(%rbx), %ecx
cmpl %esi, %ecx
cmovll %ecx, %esi
subl %ebp, %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
movq 0xb0(%r14), %rdi
movq 0xb8(%r14), %rsi
cmpb $0x0, 0xc(%rsp)
leaq -0x45c(%rip), %rdx      # 0x1a281
leaq -0x459(%rip), %rax      # 0x1a28b
cmovneq %rax, %rdx
callq 0x255ff
movq 0xb0(%r14), %rax
movq (%rax), %rcx
movl 0xe0(%rcx), %r13d
addl 0x70(%rcx), %r13d
movq 0xb8(%r14), %rcx
subq %rax, %rcx
pushq $0x3c
popq %rbp
cmpq $0x8, %rcx
jbe 0x1a756
movq 0x8(%rax), %rcx
movl 0xe0(%rcx), %eax
addl 0x70(%rcx), %eax
movl 0x4(%rbx), %ecx
imull 0x28(%r14), %ecx
cmpl %ecx, %eax
je 0x1a75e
movl %r13d, %ecx
subl %eax, %ecx
cvtsi2sd %ecx, %xmm0
cvtsi2sd %r13d, %xmm1
divsd %xmm1, %xmm0
mulsd 0x1c428(%rip), %xmm0    # 0x36b70
addsd 0x1c428(%rip), %xmm0    # 0x36b78
cvttsd2si %xmm0, %ecx
jmp 0x1a760
movl 0x4(%rbx), %eax
imull 0x28(%r14), %eax
movl %ebp, %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpl $0x3c, %ecx
cmovll %ecx, %ebp
cmpl %eax, %r13d
cmovel %edx, %ebp
cmpl $0x2, 0x78(%rbx)
jl 0x1a7da
movq 0x2f868(%rip), %r15     # 0x49fe8
leaq 0x1c643(%rip), %rsi     # 0x36dca
movq %r15, %rdi
callq 0x72b0
movq %rax, %rdi
movl %ebp, %esi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
leaq 0x1c629(%rip), %rsi     # 0x36dd1
movq %r15, %rdi
callq 0x72b0
movq 0xb8(%r14), %rsi
subq 0xb0(%r14), %rsi
shrq $0x3, %rsi
movl 0x50(%rbx), %ecx
cmpl %esi, %ecx
cmovll %ecx, %esi
movq %rax, %rdi
callq 0x75c0
movq %rax, %rdi
callq 0x7160
xorl %r15d, %r15d
pushq $0x1
popq %r12
movq 0xb0(%r14), %rax
movq 0xb8(%r14), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movl 0x50(%rbx), %edx
cmpl %ecx, %edx
cmovll %edx, %ecx
movslq %ecx, %rcx
cmpq %rcx, %r15
jge 0x1a89d
cmpl $0x3, 0x78(%rbx)
jl 0x1a86f
movq 0x2f7d1(%rip), %rdi     # 0x49fe8
leaq 0x1c5be(%rip), %rsi     # 0x36ddc
callq 0x72b0
movq 0xb0(%r14), %rcx
movq (%rcx,%r15,8), %rsi
addq $0x48, %rsi
movq %rax, %rdi
callq 0x7280
movq %rax, %rdi
leaq 0x1c5bd(%rip), %rsi     # 0x36e01
callq 0x72b0
movq 0xb0(%r14), %rcx
movq (%rcx,%r15,8), %rcx
movq 0x18(%rcx), %rsi
movq %rax, %rdi
callq 0x72a0
movq %rax, %rdi
callq 0x7160
movq 0xb0(%r14), %rax
movq (%rax,%r15,8), %rdi
movl 0xe0(%rdi), %eax
addl 0x70(%rdi), %eax
cmpl %r13d, %eax
jne 0x1a886
movl %ebp, 0x68(%rdi)
jmp 0x1a895
addq $0x40, %rdi
pushq $0x8
popq %rsi
movl %r12d, %edx
callq 0xa1f6
incq %r15
jmp 0x1a7e1
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq