// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rdx, 0x18(%rsp)
movq %rsi, 0x40(%rsp)
movl 0x14(%rdi), %r10d
testl %ecx, %ecx
movl $0x28, %ebx
cmovnsl %ecx, %ebx
testq %r10, %r10
je 0x1a46e
movl %ebx, 0x3c(%rsp)
movq 0x38(%rdi), %r8
movl 0x18(%rdi), %eax
leal (,%r10,4), %ecx
movzwl 0x12(%rdi), %esi
leaq (%r8,%rsi), %r11
leaq (%r11,%rcx), %r13
incl %eax
sarl %eax
cltq
leaq (%rax,%r13), %r9
movq %r9, 0x50(%rsp)
movq (%rdi), %rdx
addq %rax, %rsi
addq %rcx, %r8
addq %rsi, %r8
movq %r8, 0x58(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq $0x0, 0x30(%rsp)
xorl %r14d, %r14d
movl $0x0, 0xc(%rsp)
movl $0x0, 0x24(%rsp)
movq 0x18(%rsp), %rdi
movq %r10, 0x60(%rsp)
movq %r11, 0x48(%rsp)
movl (%r11,%r15,4), %eax
movl %eax, %ebp
shrl $0x4, %ebp
movl %eax, %ecx
andl $0xf, %ecx
cmpl $0x7, %ecx
setne %sil
testb $0x7, %al
setne %r8b
testb %sil, %r8b
jne 0x1a39f
cmpl $0x10, %eax
setae %r9b
movq %rbx, 0x10(%rsp)
jb 0x1a383
movslq %ebx, %rax
addq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
movl %ebp, %r12d
leal (,%rbx,4), %r10d
movl %ebx, %r8d
movq 0x40(%rsp), %rax
leaq (%rax,%rdx), %rbx
xorl %r11d, %r11d
leaq (%rdx,%r11), %rax
cmpq %rdi, %rax
jge 0x1a366
movzbl (%rbx,%r11), %esi
testq %rsi, %rsi
je 0x1a366
leal (%r8,%r11), %eax
sarl %eax
cltq
movzbl (%r13,%rax), %eax
movl %r10d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %eax
leaq 0x79b8f(%rip), %rcx     # 0x93e90
movzbl (%rcx,%rsi), %esi
movb $0x1, %cl
cmpl $0xf, %esi
je 0x1a368
andl $0xf, %eax
cmpl $0xf, %eax
je 0x1a368
movq %r8, %rdi
movq %r13, %r8
movq 0x28(%rsp), %r13
movb (%r13,%r11), %r13b
cmpb $0xd, %r13b
jb 0x1a359
incl 0xc(%rsp)
testl %eax, %eax
je 0x1a359
cmpl %esi, %eax
je 0x1a359
movq 0x30(%rsp), %rax
incl %eax
movq %rax, 0x30(%rsp)
movzbl %r13b, %eax
cmpb $0x21, %r13b
movl $0x21, %esi
cmovael %esi, %eax
movzbl %al, %eax
addl %eax, 0x24(%rsp)
movq %r8, %r13
movq %rdi, %r8
movq 0x18(%rsp), %rdi
jmp 0x1a368
xorl %ecx, %ecx
testb %cl, %cl
je 0x1a383
incq %r11
cmpq %rbp, %r11
setb %r9b
addl $0x4, %r10d
cmpq %r11, %r12
jne 0x1a2c7
movb $0x1, %cl
testb $0x1, %r9b
movq 0x60(%rsp), %r10
je 0x1a3ea
movq 0x48(%rsp), %r11
movq 0x10(%rsp), %rbx
jmp 0x1a439
decl %ecx
cmpl $0x4, %ecx
ja 0x1a40e
leaq 0x7b18b(%rip), %rsi     # 0x95538
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
cmpl $0x10, %eax
jb 0x1a3e3
movl %ebp, %eax
movq 0x40(%rsp), %rcx
leaq (%rcx,%rdx), %rsi
xorl %r8d, %r8d
leaq (%rdx,%r8), %r9
movb $0x1, %cl
cmpq %rdi, %r9
jge 0x1a439
cmpb $0x0, (%rsi,%r8)
je 0x1a439
incq %r8
cmpq %r8, %rax
jne 0x1a3c9
movl %ebp, %eax
addq %rax, %rdx
jmp 0x1a40e
movl %ebp, %eax
addq %rax, %rdx
movq 0x10(%rsp), %rbx
addl %ebp, %ebx
addl %ebp, 0xc(%rsp)
xorl %ecx, %ecx
movq 0x48(%rsp), %r11
jmp 0x1a439
leal (%rbp,%rbp,2), %eax
leal (%rbp,%rax,4), %eax
addl %eax, %r14d
xorl %ecx, %ecx
jmp 0x1a439
cmpl $0x10, %eax
jb 0x1a433
movslq %ebx, %rax
addq 0x50(%rsp), %rax
movl %ebp, %ecx
xorl %esi, %esi
movzbl (%rax,%rsi), %r8d
addl %r8d, %r14d
incq %rsi
cmpq %rsi, %rcx
jne 0x1a423
addl %ebx, %ebp
xorl %ecx, %ecx
movl %ebp, %ebx
testb %cl, %cl
jne 0x1a449
incq %r15
cmpq %r10, %r15
jne 0x1a26a
cvtsi2sdl 0xc(%rsp), %xmm1
cvtsi2sdl 0x24(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movl 0x3c(%rsp), %ebx
movq 0x30(%rsp), %rdx
jmp 0x1a47e
xorl %edx, %edx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorpd %xmm1, %xmm1
testl %edx, %edx
movsd %xmm0, 0x10(%rsp)
jle 0x1a4b8
negl %edx
movsd 0x7adfe(%rip), %xmm0    # 0x95290
movl $0x1, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
leal (%rdx,%rax), %ecx
incl %ecx
incl %eax
cmpl $0x1, %ecx
jne 0x1a497
jmp 0x1a4c0
movsd 0x7add0(%rip), %xmm0    # 0x95290
callq 0x4390
mulsd 0x7b053(%rip), %xmm0    # 0x95520
addsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm3
divsd 0x7b047(%rip), %xmm3    # 0x95528
cvtsi2sd %ebx, %xmm4
addsd %xmm0, %xmm3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm4, %xmm3
ja 0x1a539
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
maxsd %xmm3, %xmm2
movapd %xmm4, %xmm0
subsd %xmm2, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm1, %xmm0
jb 0x1a518
sqrtsd %xmm0, %xmm0
jmp 0x1a529
movsd %xmm4, 0x10(%rsp)
callq 0x4370
movsd 0x10(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0x7affb(%rip), %xmm0    # 0x95530
cvttsd2si %xmm0, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq