// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/realn.c
// Function: sam_cap_mapq
int sam_cap_mapq(bam1_t *b, const char *ref, hts_pos_t ref_len, int thres)
{
    uint8_t *seq = bam_get_seq(b), *qual = bam_get_qual(b);
    uint32_t *cigar = bam_get_cigar(b);
    bam1_core_t *c = &b->core;
    int i, y, mm, q, len, clip_l, clip_q;
    hts_pos_t x;
    double t;
    if (thres < 0) thres = 40; // set the default
    mm = q = len = clip_l = clip_q = 0;
    for (i = y = 0, x = c->pos; i < c->n_cigar; ++i) {
        int j, l = cigar[i]>>4, op = cigar[i]&0xf;
        if (op == BAM_CMATCH || op == BAM_CEQUAL || op == BAM_CDIFF) {
            for (j = 0; j < l; ++j) {
                int c1, c2, z = y + j;
                if (x+j >= ref_len || ref[x+j] == '\0') break; // out of bounds
                c1 = bam_seqi(seq, z), c2 = seq_nt16_table[(unsigned char)ref[x+j]];
                if (c2 != 15 && c1 != 15 && qual[z] >= 13) { // not ambiguous
                    ++len;
                    if (c1 && c1 != c2 && qual[z] >= 13) { // mismatch
                        ++mm;
                        q += qual[z] > 33? 33 : qual[z];
                    }
                }
            }
            if (j < l) break;
            x += l; y += l; len += l;
        } else if (op == BAM_CDEL) {
            for (j = 0; j < l; ++j)
                if (x+j >= ref_len || ref[x+j] == '\0') break;
            if (j < l) break;
            x += l;
        } else if (op == BAM_CSOFT_CLIP) {
            for (j = 0; j < l; ++j) clip_q += qual[y+j];
            clip_l += l;
            y += l;
        } else if (op == BAM_CHARD_CLIP) {
            clip_q += 13 * l;
            clip_l += l;
        } else if (op == BAM_CINS) y += l;
        else if (op == BAM_CREF_SKIP) x += l;
    }
    for (i = 0, t = 1; i < mm; ++i)
        t *= (double)len / (i+1);
    t = q - 4.343 * log(t) + clip_q / 5.;
    if (t > thres) return -1;
    if (t < 0) t = 0;
    t = sqrt((thres - t) / thres) * thres;
    //fprintf(stderr, "%s %lf %d\n", bam_get_qname(b), t, q);
    return (int)(t + .499);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x38(%rdi), %rax
movl 0x14(%rdi), %r11d
leal (,%r11,4), %r10d
movzwl 0x12(%rdi), %ebx
leaq (%rax,%rbx), %r15
leaq (%r15,%r10), %r14
movl 0x18(%rdi), %r8d
incl %r8d
sarl %r8d
movslq %r8d, %r8
leaq (%r14,%r8), %r9
movq %r9, 0x18(%rsp)
testl %ecx, %ecx
pushq $0x28
popq %r9
cmovnsl %ecx, %r9d
movl %r9d, 0x14(%rsp)
movq (%rdi), %r12
addq %r10, %rbx
addq %rax, %rbx
addq %r8, %rbx
movq %rbx, 0x40(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x10(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %rsi, 0x30(%rsp)
movq %r11, 0x28(%rsp)
movq %r15, 0x20(%rsp)
cmpq %r11, %r13
je 0x165d9
movl (%r15,%r13,4), %ecx
movl %ecx, %r8d
shrl $0x4, %r8d
movl %ecx, %eax
andl $0xf, %eax
cmpl $0x7, %eax
setne %dil
testb $0x7, %cl
setne %cl
movl %r10d, 0x8(%rsp)
movslq %r10d, %r10
testb %dil, %cl
jne 0x16539
movq %r13, 0x38(%rsp)
movl %ebx, %r13d
leaq (%rsi,%r12), %r11
movl %r10d, %eax
leal (,%r10,4), %ebx
addq 0x40(%rsp), %r10
xorl %edi, %edi
movl %r9d, %esi
cmpq %rdi, %r8
je 0x1655d
leaq (%r12,%rdi), %rcx
cmpq %rdx, %rcx
jge 0x165d4
movzbl (%r11,%rdi), %r9d
testq %r9, %r9
je 0x165d4
leal (%rax,%rdi), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%r14,%rcx), %r15d
movl %ebx, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r15d
leaq 0x5da16(%rip), %rcx     # 0x73f00
movzbl (%rcx,%r9), %ecx
cmpl $0xf, %ecx
je 0x1652e
andl $0xf, %r15d
cmpl $0xf, %r15d
je 0x1652e
movb (%r10,%rdi), %r9b
cmpb $0xd, %r9b
jb 0x1652e
incl %r13d
testl %r15d, %r15d
je 0x1652e
cmpl %ecx, %r15d
je 0x1652e
incl %esi
movzbl %r9b, %ecx
cmpb $0x21, %r9b
pushq $0x21
popq %r9
cmovael %r9d, %ecx
movzbl %cl, %ecx
addl %ecx, 0x10(%rsp)
incq %rdi
addl $0x4, %ebx
jmp 0x164a8
decl %eax
cmpl $0x4, %eax
ja 0x165af
leaq 0x5f081(%rip), %rdi     # 0x755c8
movslq (%rdi,%rax,4), %rcx
addq %rdi, %rcx
movl %r8d, %eax
jmpq *%rcx
addl 0x8(%rsp), %r8d
movl %r8d, %r10d
jmp 0x165b4
movl %esi, %r9d
addq %r8, %r12
movl 0x8(%rsp), %r10d
addl %r8d, %r10d
movl %r13d, %ebx
addl %r8d, %ebx
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %r11
movq 0x20(%rsp), %r15
movq 0x38(%rsp), %r13
jmp 0x165b4
imull $0xd, %r8d, %eax
addl %eax, %ebp
jmp 0x165af
movq %r12, %rcx
movq %rax, %rdi
subq $0x1, %rdi
jb 0x165ac
cmpq %rdx, %rcx
jge 0x165d9
cmpb $0x0, (%rsi,%rcx)
leaq 0x1(%rcx), %rcx
jne 0x16595
jmp 0x165d9
addq %rax, %r12
movl 0x8(%rsp), %r10d
incq %r13
jmp 0x16450
addq 0x18(%rsp), %r10
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x16553
movzbl (%r10,%rcx), %edi
addl %edi, %ebp
incq %rcx
jmp 0x165c3
movl %r13d, %ebx
jmp 0x165dc
movl %r9d, %esi
cvtsi2sd %ebx, %xmm1
xorl %eax, %eax
testl %esi, %esi
cmovgl %esi, %eax
negl %eax
movsd 0x5ed3f(%rip), %xmm0    # 0x75330
pushq $0x1
popq %rcx
leal (%rax,%rcx), %edx
cmpl $0x1, %edx
je 0x16610
cvtsi2sd %ecx, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
incl %ecx
jmp 0x165f4
cvtsi2sdl 0x10(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
callq 0x4390
mulsd 0x5ef87(%rip), %xmm0    # 0x755b0
cvtsi2sd %ebp, %xmm1
addsd 0x8(%rsp), %xmm0
divsd 0x5ef7d(%rip), %xmm1    # 0x755b8
addsd %xmm0, %xmm1
cvtsi2sdl 0x14(%rsp), %xmm4
ucomisd %xmm4, %xmm1
jbe 0x16650
pushq $-0x1
popq %rax
jmp 0x16695
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
maxsd %xmm1, %xmm3
movapd %xmm4, %xmm0
subsd %xmm3, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm2, %xmm0
jb 0x16674
sqrtsd %xmm0, %xmm0
jmp 0x16685
movsd %xmm4, 0x8(%rsp)
callq 0x4370
movsd 0x8(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0x5ef2f(%rip), %xmm0    # 0x755c0
cvttsd2si %xmm0, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq