// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bcftools/bcf2qcall.c
// Function: bcf_2qcall
int bcf_2qcall(bcf_hdr_t *h, bcf1_t *b)
{
	int a[4], k, g[10], l, map[4], k1, j, i, i0, anno[16], dp, mq, d_rest;
	char *s;
	if (b->ref[1] != 0 || b->n_alleles > 4) return -1; // ref is not a single base
	for (i = 0; i < b->n_gi; ++i)
		if (b->gi[i].fmt == bcf_str2int("PL", 2)) break;
	if (i == b->n_gi) return -1; // no PL
	if (read_I16(b, anno) != 0) return -1; // no I16; FIXME: can be improved
	d_rest = dp = anno[0] + anno[1] + anno[2] + anno[3];
	if (dp == 0) return -1; // depth is zero
	mq = (int)(sqrt((double)(anno[9] + anno[11]) / dp) + .499);
	i0 = i;
	a[0] = nt4_table[(int)b->ref[0]];
	if (a[0] > 3) return -1; // ref is not A/C/G/T
	a[1] = a[2] = a[3] = -2; // -1 has a special meaning
	if (b->alt[0] == 0) return -1; // no alternate allele
	map[0] = map[1] = map[2] = map[3] = -2;
	map[a[0]] = 0;
	for (k = 0, s = b->alt, k1 = -1; k < 3 && *s; ++k, s += 2) {
		if (s[1] != ',' && s[1] != 0) return -1; // ALT is not single base
		a[k+1] = nt4_table[(int)*s];
		if (a[k+1] >= 0) map[a[k+1]] = k+1;
		else k1 = k+1;
		if (s[1] == 0) break;
	}
	for (k = 0; k < 4; ++k)
		if (map[k] < 0) map[k] = k1;
	for (i = 0; i < h->n_smpl; ++i) {
		int d;
		uint8_t *p = b->gi[i0].data + i * b->gi[i0].len;
		for (j = 0; j < b->gi[i0].len; ++j)
			if (p[j]) break;
		d = (int)((double)d_rest / (h->n_smpl - i) + .499);
		if (d == 0) d = 1;
		if (j == b->gi[i0].len) d = 0;
		d_rest -= d;
		for (k = j = 0; k < 4; ++k) {
			for (l = k; l < 4; ++l) {
				int t, x = map[k], y = map[l];
				if (x > y) t = x, x = y, y = t; // swap
				g[j++] = p[y * (y+1) / 2 + x];
			}
		}
		printf("%s\t%d\t%c", h->ns[b->tid], b->pos+1, *b->ref);
		printf("\t%d\t%d\t0", d, mq);
		for (j = 0; j < 10; ++j)
			printf("\t%d", g[j]);
		printf("\t%s\n", h->sns[i]);
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq 0x20(%rsi), %rax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpb $0x0, 0x1(%rax)
jne 0x184f0
movq %rsi, %rbx
cmpl $0x4, 0x58(%rsi)
jg 0x184f0
movq %rdi, %r14
movl 0x48(%rbx), %eax
testl %eax, %eax
jle 0x181ee
movq 0x50(%rbx), %rcx
xorl %ebp, %ebp
cmpl $0x504c, (%rcx)         # imm = 0x504C
je 0x181f0
incq %rbp
addq $0x10, %rcx
cmpq %rbp, %rax
jne 0x181d6
movl %eax, %ebp
jmp 0x181f0
xorl %ebp, %ebp
cmpl %eax, %ebp
je 0x184f0
movq 0x38(%rbx), %rdi
leaq 0xf9d(%rip), %rsi       # 0x191a0
callq 0x2550
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x184f0
addq $0x4, %rax
leaq 0x20(%rsp), %r12
movq %rax, (%r12)
xorl %r13d, %r13d
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x22d0
movl %eax, 0x50(%rsp,%r13,4)
testl %eax, %eax
jne 0x1825a
callq 0x2070
movl (%rax), %eax
cmpl $0x16, %eax
je 0x184f0
cmpl $0x22, %eax
je 0x184f0
incq 0x20(%rsp)
incq %r13
cmpq $0x10, %r13
jne 0x18226
movl 0x54(%rsp), %r13d
addl 0x50(%rsp), %r13d
addl 0x58(%rsp), %r13d
addl 0x5c(%rsp), %r13d
je 0x184f0
movl 0x7c(%rsp), %eax
addl 0x74(%rsp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %r13d, %xmm1
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x182a7
sqrtsd %xmm0, %xmm0
jmp 0x182ac
callq 0x24e0
movq 0x20(%rbx), %rax
movsbq (%rax), %rax
leaq 0x3735(%rip), %rcx      # 0x1b9f0
movsbq (%rax,%rcx), %rax
cmpq $0x3, %rax
jg 0x184f0
movq 0x28(%rbx), %rdx
cmpb $0x0, (%rdx)
je 0x184f0
addsd 0xd29(%rip), %xmm0      # 0x19008
cvttsd2si %xmm0, %esi
movl %esi, 0x10(%rsp)
movabsq $-0x100000002, %rsi     # imm = 0xFFFFFFFEFFFFFFFE
movq %rsi, 0x8(%rsp)
movq %rsi, (%rsp)
movl $0x0, (%rsp,%rax,4)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %esi, %esi
movsbq (%rdx,%rsi,2), %r9
testq %r9, %r9
je 0x18353
movzbl 0x1(%rdx,%rsi,2), %edi
cmpl $0x2c, %edi
je 0x18324
testl %edi, %edi
jne 0x184ea
leaq 0x1(%rsi), %r8
movl %r9d, %r10d
andb $-0x21, %r10b
cmpb $0x58, %r10b
jne 0x1833a
movl %r8d, %eax
jmp 0x18346
movsbq (%r9,%rcx), %r9
movl %r9d, %r9d
movl %r8d, (%rsp,%r9,4)
testb %dil, %dil
je 0x18353
cmpl $0x2, %esi
movq %r8, %rsi
jne 0x18308
xorl %ecx, %ecx
movl (%rsp,%rcx,4), %edx
testl %edx, %edx
cmovsl %eax, %edx
movl %edx, (%rsp,%rcx,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x18355
movl 0x4(%r14), %ecx
testl %ecx, %ecx
jle 0x184e5
movl %ebp, %eax
shlq $0x4, %rax
movq %rax, 0x18(%rsp)
leaq 0x377b(%rip), %r12      # 0x1bb02
xorl %ebp, %ebp
movq 0x50(%rbx), %rsi
movq 0x18(%rsp), %rdi
movl 0x4(%rsi,%rdi), %edx
movl %edx, %eax
imull %ebp, %eax
cltq
addq 0x8(%rsi,%rdi), %rax
movl $0x0, %esi
testl %edx, %edx
jle 0x183bd
xorl %esi, %esi
cmpb $0x0, (%rax,%rsi)
jne 0x183bd
incq %rsi
cmpq %rsi, %rdx
jne 0x183ad
movl %edx, %esi
movl %r13d, 0x14(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
subl %ebp, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
addsd 0xc29(%rip), %xmm0      # 0x19008
cvttsd2si %xmm0, %r15d
cmpl $0x1, %r15d
adcl $0x0, %r15d
xorl %ecx, %ecx
cmpl %edx, %esi
cmovel %ecx, %r15d
movq %rsp, %rdx
xorl %esi, %esi
movl (%rsp,%rcx,4), %edi
movslq %esi, %r8
leaq (%rsp,%r8,4), %r9
addq $0x20, %r9
xorl %r8d, %r8d
movl (%rdx,%r8,4), %r10d
cmpl %r10d, %edi
movl %r10d, %r11d
cmovll %edi, %r11d
cmovgl %edi, %r10d
leal 0x1(%r10), %r13d
imull %r10d, %r13d
movl %r13d, %r10d
shrl $0x1f, %r10d
addl %r13d, %r10d
sarl %r10d
addl %r11d, %r10d
movslq %r10d, %r10
movzbl (%rax,%r10), %r10d
movl %r10d, (%r9,%r8,4)
incq %r8
leal (%r8,%rcx), %r10d
cmpl $0x4, %r10d
jne 0x1840a
addl %r8d, %esi
incq %rcx
addq $0x4, %rdx
cmpq $0x4, %rcx
jne 0x183f9
movq 0x30(%r14), %rax
movslq (%rbx), %rcx
movq (%rax,%rcx,8), %rsi
movl 0x4(%rbx), %edx
incl %edx
movq 0x20(%rbx), %rax
movsbl (%rax), %ecx
leaq 0x3675(%rip), %rdi      # 0x1baf0
xorl %eax, %eax
callq 0x21b0
leaq 0x3670(%rip), %rdi      # 0x1baf9
movl %r15d, %esi
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x21b0
xorl %r13d, %r13d
movl 0x20(%rsp,%r13,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x21b0
incq %r13
cmpq $0xa, %r13
jne 0x1849a
movl 0x14(%rsp), %r13d
subl %r15d, %r13d
movq 0x38(%r14), %rax
movq (%rax,%rbp,8), %rsi
xorl %r15d, %r15d
leaq 0x363a(%rip), %rdi      # 0x1bb06
xorl %eax, %eax
callq 0x21b0
incq %rbp
movslq 0x4(%r14), %rcx
cmpq %rcx, %rbp
jl 0x18389
jmp 0x184f0
xorl %r15d, %r15d
jmp 0x184f0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq