// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rsi, 0x30(%rsp)
movq 0x8(%rdi), %r8
movq %r8, %r9
shrq $0x30, %r9
testl %edx, %edx
movl $0x28, %ebx
cmovnsl %edx, %ebx
testq %r9, %r9
jne 0x2d4a8
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %edx, %edx
xorpd %xmm1, %xmm1
jmp 0x2d6df
movl %ebx, 0x2c(%rsp)
leaq (,%r9,4), %rcx
shrl $0x18, %r8d
addq 0x30(%rdi), %r8
leaq (%r8,%r9,4), %r10
movl 0x10(%rdi), %eax
incl %eax
sarl %eax
cltq
leaq (%r10,%rax), %rdx
movq %rdx, 0x40(%rsp)
movl 0x4(%rdi), %r11d
addq %rax, %rcx
addq %r8, %rcx
movq %rcx, 0x48(%rsp)
xorl %edi, %edi
xorl %r14d, %r14d
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
movl $0x0, 0x1c(%rsp)
movq $0x0, 0x20(%rsp)
movq %r8, 0x38(%rsp)
movq %r9, 0x50(%rsp)
movl (%r8,%rdi,4), %ecx
movl %ecx, %ebx
shrl $0x4, %ebx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x7, %edx
setne %al
testb $0x7, %cl
setne %sil
testb %al, %sil
jne 0x2d614
cmpl $0x10, %ecx
setae %sil
movl %r11d, 0x8(%rsp)
movq %r14, 0x10(%rsp)
jb 0x2d5f3
movslq %r14d, %r15
addq 0x48(%rsp), %r15
movslq %r11d, %r13
movl %ebx, %ebp
addq 0x30(%rsp), %r13
leal (,%r14,4), %r9d
movl %r14d, %r8d
xorl %r14d, %r14d
leal (%r8,%r14), %eax
sarl %eax
cltq
movzbl (%r10,%rax), %edx
movl %r9d, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edx
movsbq (%r13,%r14), %rcx
testq %rcx, %rcx
je 0x2d5d8
leaq 0x22850(%rip), %rax     # 0x4fde0
movzbl (%rax,%rcx), %r11d
cmpl $0xf, %r11d
je 0x2d5d8
andl $0xf, %edx
cmpl $0xf, %edx
je 0x2d5d8
movb (%r15,%r14), %al
cmpb $0xd, %al
jb 0x2d5d8
incl 0x4(%rsp)
testl %edx, %edx
je 0x2d5d8
cmpl %r11d, %edx
je 0x2d5d8
movq 0x20(%rsp), %rdx
incl %edx
movq %rdx, 0x20(%rsp)
movzbl %al, %edx
cmpb $0x21, %al
movl $0x21, %eax
cmovael %eax, %edx
movzbl %dl, %eax
addl %eax, 0x1c(%rsp)
testb %cl, %cl
je 0x2d5f3
incq %r14
cmpq %rbx, %r14
setb %sil
addl $0x4, %r9d
cmpq %r14, %rbp
jne 0x2d567
movb $0x1, %cl
testb $0x1, %sil
movq 0x50(%rsp), %r9
je 0x2d654
movq 0x38(%rsp), %r8
movl 0x8(%rsp), %r11d
movq 0x10(%rsp), %r14
jmp 0x2d6a6
decl %edx
cmpl $0x4, %edx
ja 0x2d67a
leaq 0x16026(%rip), %rsi     # 0x43648
movslq (%rsi,%rdx,4), %rax
addq %rsi, %rax
jmpq *%rax
cmpl $0x10, %ecx
jb 0x2d64a
movslq %r11d, %rcx
movl %ebx, %edx
addq 0x30(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx,%rax)
je 0x2d6b8
incq %rax
cmpq %rax, %rdx
jne 0x2d63c
addl %r11d, %ebx
xorl %ecx, %ecx
movl %ebx, %r11d
jmp 0x2d6a6
movl 0x8(%rsp), %r11d
addl %ebx, %r11d
movq 0x10(%rsp), %r14
addl %ebx, %r14d
addl %ebx, 0x4(%rsp)
xorl %ecx, %ecx
movq 0x38(%rsp), %r8
jmp 0x2d6a6
leal (%rbx,%rbx,2), %eax
leal (%rbx,%rax,4), %eax
addl %eax, %r12d
xorl %ecx, %ecx
jmp 0x2d6a6
cmpl $0x10, %ecx
jb 0x2d69e
movslq %r14d, %rcx
addq 0x40(%rsp), %rcx
movl %ebx, %edx
xorl %esi, %esi
movzbl (%rcx,%rsi), %eax
addl %eax, %r12d
incq %rsi
cmpq %rsi, %rdx
jne 0x2d68f
addl %r14d, %ebx
xorl %ecx, %ecx
movl %ebx, %r14d
testb %cl, %cl
jne 0x2d6bc
incq %rdi
cmpq %r9, %rdi
jne 0x2d50c
jmp 0x2d6bc
movb $0x1, %cl
jmp 0x2d6a6
cvtsi2sdl 0x4(%rsp), %xmm1
cvtsi2sdl 0x1c(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movl 0x2c(%rsp), %ebx
movq 0x20(%rsp), %rdx
testl %edx, %edx
movsd %xmm0, 0x8(%rsp)
jle 0x2d719
negl %edx
movsd 0x132c5(%rip), %xmm0    # 0x409b8
movl $0x1, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
leal (%rdx,%rcx), %eax
incl %eax
incl %ecx
cmpl $0x1, %eax
jne 0x2d6f8
jmp 0x2d721
movsd 0x13297(%rip), %xmm0    # 0x409b8
callq 0x3330
mulsd 0x1330a(%rip), %xmm0    # 0x40a38
addsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm3
divsd 0x15f7e(%rip), %xmm3    # 0x436c0
cvtsi2sd %ebx, %xmm4
addsd %xmm0, %xmm3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm4, %xmm3
ja 0x2d79a
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
maxsd %xmm3, %xmm2
movapd %xmm4, %xmm0
subsd %xmm2, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm1, %xmm0
jb 0x2d779
sqrtsd %xmm0, %xmm0
jmp 0x2d78a
movsd %xmm4, 0x8(%rsp)
callq 0x37a0
movsd 0x8(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0x1088a(%rip), %xmm0    # 0x3e020
cvttsd2si %xmm0, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq