// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam_md.c
// Function: bam_cap_mapQ
int bam_cap_mapQ(bam1_t *b, char *ref, int thres)
{
	uint8_t *seq = bam1_seq(b), *qual = bam1_qual(b);
	uint32_t *cigar = bam1_cigar(b);
	bam1_core_t *c = &b->core;
	int i, x, y, mm, q, len, clip_l, clip_q;
	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 z = y + j;
				int c1 = bam1_seqi(seq, z), c2 = bam_nt16_table[(int)ref[x+j]];
				if (ref[x+j] == 0) break; // out of boundary
				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 (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", bam1_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 0x8(%rdi), %r8
movq %r8, %r10
shrq $0x30, %r10
testl %edx, %edx
movl $0x28, %ebx
cmovnsl %edx, %ebx
movq %rsi, 0x20(%rsp)
movsd 0x13bb2(%rip), %xmm0    # 0x419f8
xorl %eax, %eax
testq %r10, %r10
jne 0x2de54
xorl %ebp, %ebp
jmp 0x2e056
movl %ebx, 0x1c(%rsp)
leaq (,%r10,4), %rcx
shrl $0x18, %r8d
addq 0x30(%rdi), %r8
leaq (%r8,%r10,4), %r11
movl 0x10(%rdi), %eax
incl %eax
sarl %eax
cltq
leaq (%r11,%rax), %rdx
movq %rdx, 0x28(%rsp)
movl 0x4(%rdi), %ebx
addq %rax, %rcx
addq %r8, %rcx
movq %rcx, 0x30(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %edi, %edi
movl $0x0, 0xc(%rsp)
xorl %edx, %edx
movq %r10, 0x38(%rsp)
movl (%r8,%r15,4), %eax
movl %eax, %r14d
shrl $0x4, %r14d
movl %eax, %ecx
andl $0xf, %ecx
cmpl $0x7, %ecx
setne %sil
testb $0x7, %al
setne %r9b
testb %sil, %r9b
jne 0x2df93
cmpl $0x10, %eax
movl %ebx, 0x10(%rsp)
movq %r12, 0x40(%rsp)
jb 0x2df7a
movslq %r12d, %rsi
addq 0x30(%rsp), %rsi
movslq %ebx, %rbx
addq 0x20(%rsp), %rbx
leal (,%r12,4), %r13d
movl %r12d, %eax
xorl %r12d, %r12d
leal (%rax,%r12), %ecx
sarl %ecx
movslq %ecx, %rcx
movzbl (%r11,%rcx), %r10d
movl %r13d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %r10d
movsbq (%rbx,%r12), %rcx
testq %rcx, %rcx
je 0x2e012
leaq 0x22eb7(%rip), %r9      # 0x50de0
movzbl (%r9,%rcx), %ecx
cmpl $0xf, %ecx
je 0x2df6e
andl $0xf, %r10d
cmpl $0xf, %r10d
je 0x2df6e
movb (%rsi,%r12), %r9b
cmpb $0xd, %r9b
jb 0x2df6e
incl %edi
testl %r10d, %r10d
je 0x2df6e
cmpl %ecx, %r10d
je 0x2df6e
incl %edx
movzbl %r9b, %ecx
cmpb $0x21, %r9b
movl $0x21, %r9d
cmovael %r9d, %ecx
movzbl %cl, %ecx
addl %ecx, 0xc(%rsp)
incq %r12
addl $0x4, %r13d
cmpq %r12, %r14
jne 0x2defb
movl 0x10(%rsp), %ebx
addl %r14d, %ebx
movq 0x40(%rsp), %r12
addl %r14d, %r12d
addl %r14d, %edi
movq 0x38(%rsp), %r10
jmp 0x2e006
decl %ecx
cmpl $0x4, %ecx
ja 0x2e006
leaq 0x16793(%rip), %rsi     # 0x44734
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
cmpl $0x10, %eax
jb 0x2dfca
movslq %ebx, %rax
movl %r14d, %ecx
addq 0x20(%rsp), %rax
xorl %esi, %esi
cmpb $0x0, (%rax,%rsi)
je 0x2e012
incq %rsi
cmpq %rsi, %rcx
jne 0x2dfbc
addl %ebx, %r14d
movl %r14d, %ebx
jmp 0x2e006
leal (%r14,%r14,2), %eax
leal (%r14,%rax,4), %eax
addl %eax, %ebp
jmp 0x2e006
cmpl $0x10, %eax
jb 0x2e000
movslq %r12d, %rax
addq 0x28(%rsp), %rax
movl %r14d, %ecx
xorl %esi, %esi
movzbl (%rax,%rsi), %r9d
addl %r9d, %ebp
incq %rsi
cmpq %rsi, %rcx
jne 0x2dff0
addl %r12d, %r14d
movl %r14d, %r12d
incq %r15
cmpq %r10, %r15
jne 0x2dea5
testl %edx, %edx
jle 0x2e04e
cvtsi2sd %edi, %xmm1
negl %edx
movsd 0x139d4(%rip), %xmm0    # 0x419f8
movl $0x1, %eax
movl 0x1c(%rsp), %ebx
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 0x2e02d
jmp 0x2e052
movl 0x1c(%rsp), %ebx
movl 0xc(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x3330
mulsd 0x13a10(%rip), %xmm0    # 0x41a80
addsd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
divsd 0x16723(%rip), %xmm1    # 0x447a8
cvtsi2sd %ebx, %xmm4
addsd %xmm0, %xmm1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm4, %xmm1
ja 0x2e0dd
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
maxsd %xmm1, %xmm3
movapd %xmm4, %xmm0
subsd %xmm3, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm2, %xmm0
jb 0x2e0bc
sqrtsd %xmm0, %xmm0
jmp 0x2e0cd
movsd %xmm4, 0x10(%rsp)
callq 0x37a0
movsd 0x10(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0x10f57(%rip), %xmm0    # 0x3f030
cvttsd2si %xmm0, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq