// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, 0x30(%rsp)
movq 0x8(%rdi), %r8
movq %r8, %r10
shrq $0x30, %r10
testl %edx, %edx
movl $0x28, %ebx
cmovnsl %edx, %ebx
testq %r10, %r10
jne 0x190ec
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %eax, %eax
xorpd %xmm1, %xmm1
jmp 0x1931a
movl %ebx, 0x24(%rsp)
leaq (,%r10,4), %rdx
shrl $0x18, %r8d
addq 0x30(%rdi), %r8
leaq (%r8,%r10,4), %rsi
movl 0x10(%rdi), %eax
movl %eax, %ecx
incl %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
incl %eax
sarl %eax
cltq
movq %rsi, 0x10(%rsp)
leaq (%rsi,%rax), %rcx
movq %rcx, 0x38(%rsp)
movl 0x4(%rdi), %ebx
addq %rax, %rdx
addq %r8, %rdx
movq %rdx, 0x40(%rsp)
xorl %edi, %edi
leaq 0xdcd5(%rip), %r11      # 0x26e10
xorl %r14d, %r14d
xorl %r12d, %r12d
movl $0x0, 0x8(%rsp)
movl $0x0, 0x18(%rsp)
xorl %eax, %eax
movq %r10, 0x28(%rsp)
movl (%r8,%rdi,4), %ecx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x5, %esi
ja 0x192c2
movl %ecx, %edx
shrl $0x4, %edx
movslq (%r11,%rsi,4), %rsi
addq %r11, %rsi
jmpq *%rsi
cmpl $0x10, %ecx
setae %sil
movl %ebx, 0x20(%rsp)
movl %r14d, 0x1c(%rsp)
jb 0x19244
movslq %r14d, %r13
addq 0x40(%rsp), %r13
movslq %ebx, %r15
movl %edx, %ebp
addq 0x30(%rsp), %r15
movl %r14d, %r11d
xorl %r14d, %r14d
leal (%r11,%r14), %r9d
movl %r9d, %ecx
shrl $0x1f, %ecx
addl %r9d, %ecx
movl %ecx, %r10d
sarl %r10d
movslq %r10d, %r10
movq 0x10(%rsp), %rbx
movzbl (%rbx,%r10), %ebx
andl $0x3e, %ecx
subl %ecx, %r9d
shll $0x2, %r9d
movb $0x4, %cl
subb %r9b, %cl
shrl %cl, %ebx
movsbq (%r15,%r14), %rcx
testq %rcx, %rcx
je 0x19230
leaq 0x169f8(%rip), %r9      # 0x2fbe0
movzbl (%r9,%rcx), %r10d
cmpl $0xf, %r10d
je 0x19230
andl $0xf, %ebx
cmpl $0xf, %ebx
je 0x19230
movb (%r13,%r14), %r9b
cmpb $0xd, %r9b
jb 0x19230
incl 0x8(%rsp)
testl %ebx, %ebx
je 0x19230
cmpl %r10d, %ebx
je 0x19230
incl %eax
movzbl %r9b, %r10d
cmpb $0x21, %r9b
movl $0x21, %r9d
cmovael %r9d, %r10d
movzbl %r10b, %r9d
addl %r9d, 0x18(%rsp)
testb %cl, %cl
je 0x19244
incq %r14
cmpq %rbp, %r14
setb %sil
jne 0x191a6
movb $0x1, %cl
testb $0x1, %sil
je 0x192d6
movq 0x28(%rsp), %r10
leaq 0xdbb4(%rip), %r11      # 0x26e10
movl 0x20(%rsp), %ebx
movl 0x1c(%rsp), %r14d
jmp 0x192c4
cmpl $0x10, %ecx
jb 0x19286
movslq %r14d, %rcx
addq 0x38(%rsp), %rcx
xorl %esi, %esi
movzbl (%rcx,%rsi), %r9d
addl %r9d, %r12d
incq %rsi
cmpq %rsi, %rdx
jne 0x19276
addl %r14d, %edx
xorl %ecx, %ecx
movl %edx, %r14d
jmp 0x192c4
cmpl $0x10, %ecx
jb 0x192b1
movslq %ebx, %rcx
movl %edx, %esi
addq 0x30(%rsp), %rcx
xorl %r9d, %r9d
cmpb $0x0, (%rcx,%r9)
je 0x192f8
incq %r9
cmpq %r9, %rsi
jne 0x192a2
addl %ebx, %edx
xorl %ecx, %ecx
movl %edx, %ebx
jmp 0x192c4
leal (%rdx,%rdx,2), %ecx
leal (%rdx,%rcx,4), %ecx
addl %ecx, %r12d
xorl %ecx, %ecx
testb %cl, %cl
jne 0x192fc
incq %rdi
cmpq %r10, %rdi
jne 0x19158
jmp 0x192fc
movl 0x20(%rsp), %ebx
addl %edx, %ebx
movl 0x1c(%rsp), %r14d
addl %edx, %r14d
addl %edx, 0x8(%rsp)
xorl %ecx, %ecx
movq 0x28(%rsp), %r10
leaq 0xdb1a(%rip), %r11      # 0x26e10
jmp 0x192c4
movb $0x1, %cl
jmp 0x192c4
cvtsi2sdl 0x8(%rsp), %xmm1
cvtsi2sdl 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movl 0x24(%rsp), %ebx
testl %eax, %eax
movsd %xmm0, 0x10(%rsp)
jle 0x19354
negl %eax
movsd 0xb142(%rip), %xmm0     # 0x24470
movl $0x1, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
leal (%rax,%rcx), %edx
incl %edx
incl %ecx
cmpl $0x1, %edx
jne 0x19333
jmp 0x1935c
movsd 0xb114(%rip), %xmm0     # 0x24470
callq 0x32a0
mulsd 0xb11f(%rip), %xmm0     # 0x24488
addsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm3
divsd 0xdad3(%rip), %xmm3     # 0x26e50
cvtsi2sd %ebx, %xmm4
addsd %xmm0, %xmm3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm4, %xmm3
ja 0x193d5
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
maxsd %xmm3, %xmm2
movapd %xmm4, %xmm0
subsd %xmm2, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm1, %xmm0
jb 0x193b4
sqrtsd %xmm0, %xmm0
jmp 0x193c5
movsd %xmm4, 0x10(%rsp)
callq 0x3660
movsd 0x10(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0xb0cf(%rip), %xmm0     # 0x244a0
cvttsd2si %xmm0, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq