// File: /workspace/github_cpp_makefile_star2_O3/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/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) {
			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 $0x38, %rsp
movq 0x8(%rdi), %r8
movq %r8, %r9
shrq $0x30, %r9
testl %edx, %edx
movl $0x28, %ebx
cmovnsl %edx, %ebx
movsd 0x9d05(%rip), %xmm0     # 0x234d0
xorl %eax, %eax
testq %r9, %r9
jne 0x197d9
xorl %ebp, %ebp
jmp 0x199fb
movl %ebx, 0xc(%rsp)
leaq (,%r9,4), %rdx
shrl $0x18, %r8d
addq 0x30(%rdi), %r8
leaq (%r8,%r9,4), %r10
movl 0x10(%rdi), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
incl %eax
sarl %eax
cltq
movq %r10, 0x20(%rsp)
leaq (%r10,%rax), %rcx
movq %rcx, 0x18(%rsp)
movl 0x4(%rdi), %r14d
addq %rax, %rdx
addq %r8, %rdx
movq %rdx, 0x28(%rsp)
xorl %r15d, %r15d
leaq 0xc6e6(%rip), %rbx      # 0x25f10
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r10d, %r10d
movl $0x0, 0x8(%rsp)
xorl %edx, %edx
movq %rsi, 0x30(%rsp)
movl (%r8,%r15,4), %eax
movl %eax, %ecx
andl $0xf, %ecx
cmpl $0x5, %ecx
ja 0x199a4
movl %eax, %r11d
shrl $0x4, %r11d
movslq (%rbx,%rcx,4), %rcx
addq %rbx, %rcx
jmpq *%rcx
movl %r10d, (%rsp)
cmpl $0x10, %eax
movl %r14d, 0x14(%rsp)
movl %r12d, 0x10(%rsp)
jb 0x1992a
movslq %r12d, %rbx
addq 0x28(%rsp), %rbx
movslq %r14d, %r13
movl %r11d, %eax
addq %rsi, %r13
movl %r12d, %r12d
xorl %edi, %edi
movq 0x20(%rsp), %rsi
leal (%r12,%rdi), %r10d
movl %r10d, %ecx
shrl $0x1f, %ecx
addl %r10d, %ecx
movl %ecx, %r14d
sarl %r14d
movslq %r14d, %r14
movzbl (%rsi,%r14), %r14d
andl $0x3e, %ecx
subl %ecx, %r10d
shll $0x2, %r10d
movb $0x4, %cl
subb %r10b, %cl
shrl %cl, %r14d
movsbq (%r13,%rdi), %rcx
testq %rcx, %rcx
je 0x199b2
leaq 0x15308(%rip), %r10     # 0x2ebe0
movzbl (%r10,%rcx), %ecx
cmpl $0xf, %ecx
je 0x1991e
andl $0xf, %r14d
cmpl $0xf, %r14d
je 0x1991e
movb (%rbx,%rdi), %r10b
cmpb $0xd, %r10b
jb 0x1991e
incl (%rsp)
testl %r14d, %r14d
je 0x1991e
cmpl %ecx, %r14d
je 0x1991e
incl %edx
movzbl %r10b, %ecx
cmpb $0x21, %r10b
movl $0x21, %r10d
cmovael %r10d, %ecx
movzbl %cl, %ecx
addl %ecx, 0x8(%rsp)
incq %rdi
cmpq %rdi, %rax
jne 0x19895
movl 0x14(%rsp), %r14d
addl %r11d, %r14d
movl 0x10(%rsp), %r12d
addl %r11d, %r12d
movl (%rsp), %r10d
addl %r11d, %r10d
movq 0x30(%rsp), %rsi
leaq 0xc5c3(%rip), %rbx      # 0x25f10
jmp 0x199a4
cmpl $0x10, %eax
jb 0x1996c
movslq %r12d, %rax
addq 0x18(%rsp), %rax
xorl %ecx, %ecx
movzbl (%rax,%rcx), %edi
addl %edi, %ebp
incq %rcx
cmpq %rcx, %r11
jne 0x1995e
addl %r12d, %r11d
movl %r11d, %r12d
jmp 0x199a4
cmpl $0x10, %eax
jb 0x19992
movslq %r14d, %rax
movl %r11d, %ecx
addq %rsi, %rax
xorl %edi, %edi
cmpb $0x0, (%rax,%rdi)
je 0x199b6
incq %rdi
cmpq %rdi, %rcx
jne 0x19984
addl %r14d, %r11d
movl %r11d, %r14d
jmp 0x199a4
leal (%r11,%r11,2), %eax
leal (%r11,%rax,4), %eax
addl %eax, %ebp
incq %r15
cmpq %r9, %r15
jne 0x19841
jmp 0x199b6
movl (%rsp), %r10d
testl %edx, %edx
jle 0x199f3
cvtsi2sd %r10d, %xmm1
negl %edx
movsd 0x9b07(%rip), %xmm0     # 0x234d0
movl $0x1, %eax
movl 0xc(%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 0x199d2
jmp 0x199f7
movl 0xc(%rsp), %ebx
movl 0x8(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, (%rsp)
callq 0x32a0
mulsd 0x9ad4(%rip), %xmm0     # 0x234e8
addsd (%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
divsd 0xc528(%rip), %xmm1     # 0x25f50
cvtsi2sd %ebx, %xmm4
addsd %xmm0, %xmm1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm4, %xmm1
ja 0x19a7e
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
maxsd %xmm1, %xmm3
movapd %xmm4, %xmm0
subsd %xmm3, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm2, %xmm0
jb 0x19a5f
sqrtsd %xmm0, %xmm0
jmp 0x19a6e
movsd %xmm4, (%rsp)
callq 0x3660
movsd (%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0x9a86(%rip), %xmm0     # 0x23500
cvttsd2si %xmm0, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq