// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movl %edx, 0x8c(%rsp)
movq 0x98(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x98(%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq 0x98(%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x18, %rcx
andq $0xff, %rcx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x80(%rsp)
movq 0x98(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x98(%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq 0x98(%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x18, %rcx
andq $0xff, %rcx
movslq %ecx, %rcx
addq %rcx, %rax
movq 0x98(%rsp), %rcx
movl 0x10(%rcx), %ecx
addl $0x1, %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x78(%rsp)
movq 0x98(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x98(%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x18, %rcx
andq $0xff, %rcx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x68(%rsp)
cmpl $0x0, 0x8c(%rsp)
jge 0x479a05
movl $0x28, 0x8c(%rsp)
movl $0x0, 0x48(%rsp)
movl $0x0, 0x4c(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x54(%rsp)
movl $0x0, 0x58(%rsp)
movl $0x0, 0x5c(%rsp)
movl $0x0, 0x64(%rsp)
movq 0x68(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x60(%rsp)
movl 0x64(%rsp), %eax
movq 0x68(%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
cmpl %ecx, %eax
jge 0x479d70
movq 0x70(%rsp), %rax
movslq 0x64(%rsp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
movl %eax, 0x38(%rsp)
movq 0x70(%rsp), %rax
movslq 0x64(%rsp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xf, %eax
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x479aa3
cmpl $0x7, 0x34(%rsp)
je 0x479aa3
cmpl $0x8, 0x34(%rsp)
jne 0x479c48
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0x479c10
movl 0x5c(%rsp), %eax
addl 0x3c(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0x30(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq 0x18(%rsp), %rax
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x24(%rsp)
movl 0x30(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl 0x24(%rsp), %eax
movl $0x1, %ecx
subl %edx, %ecx
shll $0x2, %ecx
sarl %cl, %eax
andl $0xf, %eax
movl %eax, 0x2c(%rsp)
movq 0x90(%rsp), %rax
movl 0x60(%rsp), %ecx
addl 0x3c(%rsp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
movslq %eax, %rcx
leaq 0x189d5c(%rip), %rax    # 0x603890
movzbl (%rax,%rcx), %eax
movl %eax, 0x28(%rsp)
movq 0x90(%rsp), %rax
movl 0x60(%rsp), %ecx
addl 0x3c(%rsp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x479b5d
jmp 0x479c10
cmpl $0xf, 0x28(%rsp)
je 0x479bfe
cmpl $0xf, 0x2c(%rsp)
je 0x479bfe
movq 0x78(%rsp), %rax
movslq 0x30(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jl 0x479bfe
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0x479bfc
movl 0x2c(%rsp), %eax
cmpl 0x28(%rsp), %eax
je 0x479bfc
movq 0x78(%rsp), %rax
movslq 0x30(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jl 0x479bfc
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
movq 0x78(%rsp), %rax
movslq 0x30(%rsp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x21, %eax
jle 0x479bde
movl $0x21, %eax
movl %eax, 0x14(%rsp)
jmp 0x479bf0
movq 0x78(%rsp), %rax
movslq 0x30(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
addl 0x54(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x479bfe
jmp 0x479c00
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x479aab
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0x479c1f
jmp 0x479d70
movl 0x38(%rsp), %eax
addl 0x60(%rsp), %eax
movl %eax, 0x60(%rsp)
movl 0x38(%rsp), %eax
addl 0x5c(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x38(%rsp), %eax
addl 0x50(%rsp), %eax
movl %eax, 0x50(%rsp)
jmp 0x479d5e
cmpl $0x2, 0x34(%rsp)
jne 0x479cae
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0x479c8e
movq 0x90(%rsp), %rax
movl 0x60(%rsp), %ecx
addl 0x3c(%rsp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x479c7f
jmp 0x479c8e
jmp 0x479c81
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x479c57
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0x479c9d
jmp 0x479d70
movl 0x38(%rsp), %eax
addl 0x60(%rsp), %eax
movl %eax, 0x60(%rsp)
jmp 0x479d5c
cmpl $0x4, 0x34(%rsp)
jne 0x479d0a
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0x479cf0
movq 0x78(%rsp), %rax
movl 0x5c(%rsp), %ecx
addl 0x3c(%rsp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
addl 0x48(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x479cbd
movl 0x38(%rsp), %eax
addl 0x4c(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0x38(%rsp), %eax
addl 0x5c(%rsp), %eax
movl %eax, 0x5c(%rsp)
jmp 0x479d5a
cmpl $0x5, 0x34(%rsp)
jne 0x479d2c
imull $0xd, 0x38(%rsp), %eax
addl 0x48(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x38(%rsp), %eax
addl 0x4c(%rsp), %eax
movl %eax, 0x4c(%rsp)
jmp 0x479d58
cmpl $0x1, 0x34(%rsp)
jne 0x479d41
movl 0x38(%rsp), %eax
addl 0x5c(%rsp), %eax
movl %eax, 0x5c(%rsp)
jmp 0x479d56
cmpl $0x3, 0x34(%rsp)
jne 0x479d54
movl 0x38(%rsp), %eax
addl 0x60(%rsp), %eax
movl %eax, 0x60(%rsp)
jmp 0x479d56
jmp 0x479d58
jmp 0x479d5a
jmp 0x479d5c
jmp 0x479d5e
jmp 0x479d60
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x479a49
movl $0x0, 0x64(%rsp)
movsd 0x7c8e8(%rip), %xmm0    # 0x4f6668
movsd %xmm0, 0x40(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x58(%rsp), %eax
jge 0x479dbe
cvtsi2sdl 0x50(%rsp), %xmm0
movl 0x64(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
mulsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x479d86
cvtsi2sdl 0x54(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x40(%rsp), %xmm0
callq 0x31530
movsd 0x8(%rsp), %xmm1
movsd 0x1009bd(%rip), %xmm2   # 0x57a7a0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsi2sdl 0x48(%rsp), %xmm1
movsd 0xad81f(%rip), %xmm2    # 0x527618
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
cvtsi2sdl 0x8c(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x479e29
movl $0xffffffff, 0xa4(%rsp) # imm = 0xFFFFFFFF
jmp 0x479e8a
xorps %xmm0, %xmm0
ucomisd 0x40(%rsp), %xmm0
jbe 0x479e3d
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
cvtsi2sdl 0x8c(%rsp), %xmm0
subsd 0x40(%rsp), %xmm0
cvtsi2sdl 0x8c(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x2f440
cvtsi2sdl 0x8c(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x10092f(%rip), %xmm0   # 0x57a7a8
addsd 0x40(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopl (%rax)