// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rdx, 0x10(%rsp)
movl 0x14(%rdi), %r11d
testl %ecx, %ecx
movl $0x28, %ebx
cmovnsl %ecx, %ebx
movsd 0x7883f(%rip), %xmm0    # 0x922f0
testq %r11, %r11
je 0x19d0b
movl %ebx, 0x1c(%rsp)
movq 0x38(%rdi), %r14
movl 0x18(%rdi), %eax
leal (,%r11,4), %ecx
movzwl 0x12(%rdi), %r8d
leaq (%r14,%r8), %rdx
leaq (%rdx,%rcx), %r9
incl %eax
sarl %eax
cltq
movq %r9, 0x50(%rsp)
addq %rax, %r9
movq %r9, 0x20(%rsp)
movq (%rdi), %r12
addq %rax, %r8
addq %rcx, %r14
addq %r8, %r14
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %edi, %edi
xorl %ebp, %ebp
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movq %rsi, 0x40(%rsp)
movq %r11, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movl (%rdx,%r13,4), %ecx
movl %ecx, %eax
shrl $0x4, %eax
movl %ecx, %r10d
andl $0xf, %r10d
cmpl $0x7, %r10d
setne %r8b
testb $0x7, %cl
setne %r9b
testb %r8b, %r9b
jne 0x19b57
cmpl $0xf, %ecx
movq %rbx, 0x48(%rsp)
ja 0x19ba9
movq %rax, %rbx
jmp 0x19c76
decl %r10d
cmpl $0x4, %r10d
ja 0x19cbd
leaq 0x78a3d(%rip), %r9      # 0x925a8
movslq (%r9,%r10,4), %r8
addq %r9, %r8
jmpq *%r8
movl %eax, %eax
movq %rax, %r9
movq %r12, %r8
cmpl $0x10, %ecx
jb 0x19c91
cmpq 0x10(%rsp), %r8
jge 0x19cce
cmpb $0x0, (%rsi,%r8)
je 0x19cce
incq %r8
decq %r9
jne 0x19b86
jmp 0x19c91
movl %r15d, %edx
movslq %ebx, %r11
addq %r14, %r11
movq %rbx, %rcx
movl %eax, %ebx
leal (,%rcx,4), %r15d
movl %ecx, %r9d
leaq (%rsi,%r12), %r8
xorl %esi, %esi
leaq (%r12,%rsi), %rcx
cmpq 0x10(%rsp), %rcx
jge 0x19ccb
movzbl (%r8,%rsi), %r14d
testq %r14, %r14
je 0x19ccb
leal (%r9,%rsi), %ecx
sarl %ecx
movslq %ecx, %rcx
movq 0x50(%rsp), %r10
movzbl (%r10,%rcx), %r10d
movl %r15d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r10d
leaq 0x772f6(%rip), %rcx     # 0x90f00
movzbl (%rcx,%r14), %ecx
cmpl $0xf, %ecx
je 0x19c4f
andl $0xf, %r10d
cmpl $0xf, %r10d
je 0x19c4f
movb (%r11,%rsi), %r14b
cmpb $0xd, %r14b
jb 0x19c4f
incl %edx
testl %r10d, %r10d
je 0x19c4f
cmpl %ecx, %r10d
je 0x19c4f
incl %edi
movzbl %r14b, %ecx
cmpb $0x21, %r14b
movl $0x21, %r10d
cmovael %r10d, %ecx
movzbl %cl, %ecx
addl %ecx, 0xc(%rsp)
incq %rsi
addl $0x4, %r15d
cmpq %rsi, %rbx
jne 0x19bc8
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %r11
movq 0x30(%rsp), %r14
movl %edx, %r15d
movq 0x28(%rsp), %rdx
addq %rbx, %r12
movq 0x48(%rsp), %rbx
addl %eax, %ebx
addl %eax, %r15d
jmp 0x19cbd
leal (%rax,%rax,2), %ecx
leal (%rax,%rcx,4), %eax
addl %eax, %ebp
jmp 0x19cbd
movl %eax, %eax
addq %rax, %r12
jmp 0x19cbd
cmpl $0x10, %ecx
jb 0x19cb9
movslq %ebx, %rcx
addq 0x20(%rsp), %rcx
movl %eax, %r10d
xorl %r8d, %r8d
movzbl (%rcx,%r8), %r9d
addl %r9d, %ebp
incq %r8
cmpq %r8, %r10
jne 0x19ca9
addl %ebx, %eax
movl %eax, %ebx
incq %r13
cmpq %r11, %r13
jne 0x19b21
jmp 0x19cce
movl %edx, %r15d
testl %edi, %edi
jle 0x19d11
cvtsi2sd %r15d, %xmm1
negl %edi
movsd 0x7860f(%rip), %xmm0    # 0x922f0
movl $0x1, %eax
movl 0x1c(%rsp), %ebx
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
leal (%rdi,%rax), %ecx
incl %ecx
incl %eax
cmpl $0x1, %ecx
jne 0x19cea
jmp 0x19d15
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0x19d19
movl 0x1c(%rsp), %ebx
movl 0xc(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x4390
mulsd 0x7885d(%rip), %xmm0    # 0x92590
addsd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
divsd 0x78850(%rip), %xmm1    # 0x92598
cvtsi2sd %ebx, %xmm4
addsd %xmm0, %xmm1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm4, %xmm1
ja 0x19da0
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
maxsd %xmm1, %xmm3
movapd %xmm4, %xmm0
subsd %xmm3, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm2, %xmm0
jb 0x19d7f
sqrtsd %xmm0, %xmm0
jmp 0x19d90
movsd %xmm4, 0x10(%rsp)
callq 0x4370
movsd 0x10(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0x78804(%rip), %xmm0    # 0x925a0
cvttsd2si %xmm0, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq