// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x14(%rcx), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl $0x1, %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x12(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x23b6d
movl $0x28, -0x24(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movl -0x4c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jae 0x23ead
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, -0x80(%rbp)
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
je 0x23bfd
cmpl $0x7, -0x84(%rbp)
je 0x23bfd
cmpl $0x8, -0x84(%rbp)
jne 0x23d90
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x23d60
movl -0x50(%rbp), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x70(%rbp), %rax
movslq -0x7c(%rbp), %rcx
addq %rcx, %rax
cmpq -0x20(%rbp), %rax
jge 0x23c45
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0x7c(%rbp), %rdx
addq %rdx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x23c4a
jmp 0x23d60
movq -0x30(%rbp), %rax
movl -0x90(%rbp), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl -0x90(%rbp), %ecx
xorl $-0x1, %ecx
andl $0x1, %ecx
shll $0x2, %ecx
sarl %cl, %eax
andl $0xf, %eax
movl %eax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0x7c(%rbp), %rdx
addq %rdx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0xc627d(%rip), %rax     # 0xe9f10
movzbl (%rax,%rcx), %eax
movl %eax, -0x8c(%rbp)
cmpl $0xf, -0x8c(%rbp)
je 0x23d50
cmpl $0xf, -0x88(%rbp)
je 0x23d50
movq -0x38(%rbp), %rax
movslq -0x90(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jl 0x23d50
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x88(%rbp)
je 0x23d4e
movl -0x88(%rbp), %eax
cmpl -0x8c(%rbp), %eax
je 0x23d4e
movq -0x38(%rbp), %rax
movslq -0x90(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jl 0x23d4e
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x38(%rbp), %rax
movslq -0x90(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x21, %eax
jle 0x23d2d
movl $0x21, %eax
movl %eax, -0x94(%rbp)
jmp 0x23d42
movq -0x38(%rbp), %rax
movslq -0x90(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x23d50
jmp 0x23d52
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x23c04
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x23d6d
jmp 0x23ead
movslq -0x80(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x80(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x80(%rbp), %eax
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x23e9d
cmpl $0x2, -0x84(%rbp)
jne 0x23dfe
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x23de0
movq -0x70(%rbp), %rax
movslq -0x7c(%rbp), %rcx
addq %rcx, %rax
cmpq -0x20(%rbp), %rax
jge 0x23dd1
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
movslq -0x7c(%rbp), %rdx
addq %rdx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x23dd3
jmp 0x23de0
jmp 0x23dd5
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x23da0
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x23ded
jmp 0x23ead
movslq -0x80(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x23e9b
cmpl $0x4, -0x84(%rbp)
jne 0x23e4c
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x23e38
movq -0x38(%rbp), %rax
movl -0x50(%rbp), %ecx
addl -0x7c(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x23e0e
movl -0x80(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x80(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x23e99
cmpl $0x5, -0x84(%rbp)
jne 0x23e6a
imull $0xd, -0x80(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x80(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x23e97
cmpl $0x1, -0x84(%rbp)
jne 0x23e7e
movl -0x80(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x23e95
cmpl $0x3, -0x84(%rbp)
jne 0x23e93
movslq -0x80(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x23e95
jmp 0x23e97
jmp 0x23e99
jmp 0x23e9b
jmp 0x23e9d
jmp 0x23e9f
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x23ba9
movl $0x0, -0x4c(%rbp)
movsd 0xc73ec(%rip), %xmm0    # 0xeb2a8
movsd %xmm0, -0x78(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x23ef1
cvtsi2sdl -0x5c(%rbp), %xmm0
movl -0x4c(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x23ec1
cvtsi2sdl -0x58(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x78(%rbp), %xmm0
callq 0x4380
movsd -0xa0(%rbp), %xmm1
movsd 0xc7570(%rip), %xmm2    # 0xeb488
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsi2sdl -0x64(%rbp), %xmm1
movsd 0xc7553(%rip), %xmm2    # 0xeb480
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x78(%rbp), %xmm0
cvtsi2sdl -0x24(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x23f53
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x23f9f
xorps %xmm0, %xmm0
ucomisd -0x78(%rbp), %xmm0
jbe 0x23f65
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
subsd -0x78(%rbp), %xmm0
cvtsi2sdl -0x24(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x4360
cvtsi2sdl -0x24(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd 0xc74fd(%rip), %xmm0    # 0xeb490
addsd -0x78(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)