// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movq 0x20(%rsi), %rax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpb $0x0, 0x1(%rax)
jne 0x1891c
movq %rsi, %rbx
cmpl $0x4, 0x58(%rsi)
jg 0x1891c
movq %rdi, %r14
movslq 0x48(%rbx), %rax
testq %rax, %rax
jle 0x185ce
movq 0x50(%rbx), %rcx
xorl %r13d, %r13d
cmpl $0x504c, (%rcx)         # imm = 0x504C
je 0x185d1
incq %r13
addq $0x10, %rcx
cmpq %r13, %rax
jne 0x185b5
jmp 0x1891c
xorl %r13d, %r13d
cmpl %eax, %r13d
je 0x1891c
movq 0x38(%rbx), %rdi
leaq 0xc13(%rip), %rsi       # 0x191f8
callq 0x2550
testq %rax, %rax
je 0x1891c
movq %rax, %rdi
addq $0x4, %rdi
leaq 0x30(%rsp), %r15
movq %rdi, (%r15)
xorl %ebp, %ebp
movq %r15, %rsi
movl $0xa, %edx
callq 0x22d0
movl %eax, 0x60(%rsp,%rbp,4)
testl %eax, %eax
jne 0x18632
callq 0x2070
movl (%rax), %eax
cmpl $0x16, %eax
je 0x1891c
cmpl $0x22, %eax
je 0x1891c
movq 0x30(%rsp), %rdi
incq %rdi
movq %rdi, 0x30(%rsp)
incq %rbp
cmpq $0x10, %rbp
jne 0x18604
movdqa 0x60(%rsp), %xmm0
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %r15d
testl %r15d, %r15d
je 0x1891c
movl 0x8c(%rsp), %eax
addl 0x84(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1869f
sqrtsd %xmm0, %xmm0
jmp 0x186a4
callq 0x24e0
movq 0x20(%rbx), %rax
movsbq (%rax), %rcx
leaq 0x347d(%rip), %rax      # 0x1bb30
movsbq (%rcx,%rax), %rdx
cmpq $0x3, %rdx
jg 0x1891c
movq 0x28(%rbx), %rcx
cmpb $0x0, (%rcx)
je 0x1891c
addsd 0x999(%rip), %xmm0      # 0x19070
cvttsd2si %xmm0, %esi
movl %esi, 0x8(%rsp)
movapd 0x2bb9(%rip), %xmm0     # 0x1b2a0
movapd %xmm0, 0x10(%rsp)
movl $0x0, 0x10(%rsp,%rdx,4)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %esi, %esi
movsbq (%rcx,%rsi,2), %r9
testq %r9, %r9
je 0x18748
movzbl 0x1(%rcx,%rsi,2), %edi
cmpl $0x2c, %edi
je 0x18718
testl %edi, %edi
jne 0x18916
leaq 0x1(%rsi), %r8
movl %r9d, %r10d
andb $-0x21, %r10b
cmpb $0x58, %r10b
jne 0x1872e
movl %r8d, %edx
jmp 0x1873b
movsbq (%r9,%rax), %r9
movl %r9d, %r9d
movl %r8d, 0x10(%rsp,%r9,4)
testb %dil, %dil
je 0x18748
cmpl $0x2, %esi
movq %r8, %rsi
jne 0x186fc
movd %edx, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movdqa 0x10(%rsp), %xmm1
pxor %xmm2, %xmm2
pcmpgtd %xmm1, %xmm2
pand %xmm2, %xmm0
pandn %xmm1, %xmm2
por %xmm0, %xmm2
movdqa %xmm2, 0x10(%rsp)
movl 0x4(%r14), %ecx
testl %ecx, %ecx
jle 0x18911
movl %r13d, %eax
shlq $0x4, %rax
movq %rax, 0x20(%rsp)
leaq 0x34b2(%rip), %r13      # 0x1bc42
xorl %ebp, %ebp
movq %r14, 0x28(%rsp)
movq 0x50(%rbx), %rdx
movq 0x20(%rsp), %rsi
movq 0x8(%rdx,%rsi), %rax
movl 0x4(%rdx,%rsi), %edx
movl $0x0, %esi
testl %edx, %edx
jle 0x187cf
movl %edx, %esi
imull %ebp, %esi
movslq %esi, %rdi
addq %rax, %rdi
xorl %esi, %esi
cmpb $0x0, (%rdi,%rsi)
jne 0x187cf
incq %rsi
cmpq %rsi, %rdx
jne 0x187bf
movl %edx, %esi
movl %edx, %edi
imull %ebp, %edi
movl %r15d, 0xc(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movslq %edi, %rdi
subl %ebp, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
divsd %xmm1, %xmm0
addsd 0x877(%rip), %xmm0      # 0x19070
cvttsd2si %xmm0, %r12d
addq %rdi, %rax
cmpl $0x1, %r12d
adcl $0x0, %r12d
xorl %ecx, %ecx
cmpl %edx, %esi
cmovel %ecx, %r12d
movl $0x4, %edx
leaq 0x10(%rsp), %rsi
xorl %edi, %edi
movslq %ecx, %r8
leaq (%rsp,%r8,4), %r8
addq $0x30, %r8
movl 0x10(%rsp,%rdi,4), %r9d
xorl %r10d, %r10d
movl (%rsi,%r10,4), %r11d
cmpl %r11d, %r9d
movl %r11d, %r15d
cmovll %r9d, %r15d
cmovgl %r9d, %r11d
leal 0x1(%r11), %r14d
imull %r11d, %r14d
movl %r14d, %r11d
shrl $0x1f, %r11d
addl %r14d, %r11d
sarl %r11d
addl %r15d, %r11d
movslq %r11d, %r11
movzbl (%rax,%r11), %r11d
movl %r11d, (%r8,%r10,4)
incq %r10
leal (%r10,%rdi), %r11d
cmpl $0x4, %r11d
jne 0x18830
incq %rdi
addl %edx, %ecx
decl %edx
addq $0x4, %rsi
cmpq $0x4, %rdi
jne 0x1881d
movq 0x28(%rsp), %r14
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 0x3389(%rip), %rdi      # 0x1bc30
xorl %eax, %eax
callq 0x21b0
leaq 0x3384(%rip), %rdi      # 0x1bc39
movl %r12d, %esi
movl 0x8(%rsp), %edx
xorl %eax, %eax
callq 0x21b0
xorl %r15d, %r15d
movl 0x30(%rsp,%r15,4), %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x21b0
incq %r15
cmpq $0xa, %r15
jne 0x188c6
movl 0xc(%rsp), %r15d
subl %r12d, %r15d
movq 0x38(%r14), %rax
movq (%rax,%rbp,8), %rsi
xorl %r12d, %r12d
leaq 0x334e(%rip), %rdi      # 0x1bc46
xorl %eax, %eax
callq 0x21b0
incq %rbp
movslq 0x4(%r14), %rcx
cmpq %rcx, %rbp
jl 0x18797
jmp 0x1891c
xorl %r12d, %r12d
jmp 0x1891c
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq