// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/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
movl %r8d, %r11d
shrl $0x18, %r11d
addq 0x30(%rdi), %r11
shrq $0x30, %r8
leaq (%r11,%r8,4), %r10
movl 0x10(%rdi), %eax
incl %eax
sarl %eax
cltq
leaq (%r10,%rax), %rcx
movq %rcx, 0x20(%rsp)
testl %edx, %edx
pushq $0x28
popq %rcx
cmovnsl %edx, %ecx
movl %ecx, 0x1c(%rsp)
movl 0x4(%rdi), %edi
leaq (%rax,%r8,4), %rax
addq %r11, %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
pushq $0x2
popq %r15
leaq 0x1741eb(%rip), %rbx    # 0x411060
movl $0x0, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movl $0x0, 0x8(%rsp)
movq %rsi, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movq %r11, 0x28(%rsp)
cmpq %r8, %r13
je 0x29cfee
movl (%r11,%r13,4), %ecx
movl %ecx, %r12d
shrl $0x4, %r12d
movl %ecx, %eax
andl $0xf, %eax
cmpl $0x7, %eax
setne %dl
testb $0x7, %cl
setne %cl
movl %edi, 0x4(%rsp)
movslq %edi, %r9
movslq 0x8(%rsp), %rdi
testb %dl, %cl
jne 0x29cf5a
addq %rsi, %r9
movl %edi, %r11d
addq 0x40(%rsp), %rdi
xorl %r8d, %r8d
movl 0x14(%rsp), %esi
cmpq %r8, %r12
je 0x29cf80
leal (%r11,%r8), %eax
cltd
idivl %r15d
cltq
movzbl (%r10,%rax), %eax
shll $0x2, %edx
movb $0x4, %cl
subb %dl, %cl
shrl %cl, %eax
movsbq (%r9,%r8), %rcx
testq %rcx, %rcx
je 0x29cff2
movzbl (%rbx,%rcx), %ecx
cmpl $0xf, %ecx
je 0x29cf55
andl $0xf, %eax
cmpl $0xf, %eax
je 0x29cf55
movb (%rdi,%r8), %dl
cmpb $0xd, %dl
jb 0x29cf55
incl %r14d
testl %eax, %eax
je 0x29cf55
cmpl %ecx, %eax
je 0x29cf55
incl %esi
movzbl %dl, %eax
cmpb $0x21, %dl
pushq $0x21
popq %rcx
cmovael %ecx, %eax
movzbl %al, %eax
addl %eax, 0x18(%rsp)
incq %r8
jmp 0x29ceec
decl %eax
cmpl $0x4, %eax
ja 0x29cfaa
leaq 0xbe798(%rip), %rdx     # 0x35b700
movslq (%rdx,%rax,4), %rcx
addq %rdx, %rcx
movl %r12d, %eax
jmpq *%rcx
addl 0x8(%rsp), %r12d
movl %r12d, 0x8(%rsp)
jmp 0x29cfaa
movl %esi, 0x14(%rsp)
movl 0x4(%rsp), %edi
addl %r12d, %edi
addl %r12d, 0x8(%rsp)
addl %r12d, %r14d
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r11
jmp 0x29cfcf
imull $0xd, %r12d, %eax
addl %eax, %ebp
movl 0x4(%rsp), %edi
jmp 0x29cfcf
addq %rsi, %r9
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x29cfc7
cmpb $0x0, (%r9,%rcx)
leaq 0x1(%rcx), %rcx
jne 0x29cfb5
jmp 0x29cfee
addl 0x4(%rsp), %r12d
movl %r12d, %edi
incq %r13
jmp 0x29cea1
addq 0x20(%rsp), %rdi
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x29cf74
movzbl (%rdi,%rcx), %edx
addl %edx, %ebp
incq %rcx
jmp 0x29cfde
movl 0x14(%rsp), %esi
cvtsi2sd %r14d, %xmm1
xorl %eax, %eax
testl %esi, %esi
cmovgl %esi, %eax
negl %eax
movsd 0x3c850(%rip), %xmm0    # 0x2d9858
pushq $0x1
popq %rcx
leal (%rax,%rcx), %edx
cmpl $0x1, %edx
je 0x29d027
cvtsi2sd %ecx, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
incl %ecx
jmp 0x29d00b
cvtsi2sdl 0x18(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
callq 0x30520
mulsd 0xbe738(%rip), %xmm0    # 0x35b778
cvtsi2sd %ebp, %xmm1
addsd 0x8(%rsp), %xmm0
divsd 0x6c606(%rip), %xmm1    # 0x309658
addsd %xmm0, %xmm1
cvtsi2sdl 0x1c(%rsp), %xmm4
ucomisd %xmm4, %xmm1
jbe 0x29d067
pushq $-0x1
popq %rax
jmp 0x29d0ac
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
maxsd %xmm1, %xmm3
movapd %xmm4, %xmm0
subsd %xmm3, %xmm0
divsd %xmm4, %xmm0
ucomisd %xmm2, %xmm0
jb 0x29d08b
sqrtsd %xmm0, %xmm0
jmp 0x29d09c
movsd %xmm4, 0x8(%rsp)
callq 0x2e430
movsd 0x8(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd 0xbe6d8(%rip), %xmm0    # 0x35b780
cvttsd2si %xmm0, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq