// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bcftools/mut.c
// Function: bcf_trio_call
int bcf_trio_call(const uint32_t *prep, const bcf1_t *b, int *llr, int64_t *gt)
{
	int i, j, k;
	const bcf_ginfo_t *PL;
	uint8_t *gl10;
	int map[10];
	if (b->n_smpl != 3) return -1; // not a trio
	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
	gl10 = alloca(10 * b->n_smpl);
	if (bcf_gl10(b, gl10) < 0) {
		if (bcf_gl10_indel(b, gl10) < 0) return -1;
	}
	PL = b->gi + i;
	for (i = 0, k = 0; i < 4; ++i)
		for (j = i; j < 4; ++j)
			map[k++] = seq_nt16rev[1<<i|1<<j];
	for (j = 0; j < 3; ++j) // check if ref hom is the most probable in all members
		if (((uint8_t*)PL->data)[j * PL->len] != 0) break;
	if (j < 3) { // we need to go through the complex procedure
		uint8_t *g[3];
		int minc = 1<<30, minc_j = -1, minf = 0, gtf = 0, gtc = 0;
		g[0] = gl10;
		g[1] = gl10 + 10;
		g[2] = gl10 + 20;
		for (j = 1; j <= (int)prep[0]; ++j) { // compute LK with constraint
			int sum = g[0][prep[j]&0xff] + g[1][prep[j]>>8&0xff] + g[2][prep[j]>>16&0xff];
			if (sum < minc) minc = sum, minc_j = j;
		}
		gtc |= map[prep[minc_j]&0xff]; gtc |= map[prep[minc_j]>>8&0xff]<<8; gtc |= map[prep[minc_j]>>16]<<16;
		for (j = 0; j < 3; ++j) { // compute LK without constraint
			int min = 1<<30, min_k = -1;
			for (k = 0; k < 10; ++k)
				if (g[j][k] < min) min = g[j][k], min_k = k;
			gtf |= map[min_k]<<(j*8);
			minf += min;
		}
		*llr = minc - minf; *gt = (int64_t)gtc<<32 | gtf;
	} else *llr = 0, *gt = -1;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl 0x5c(%rsi), %eax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpq $0x3, %rax
jne 0x17fed
movq %rsi, %rbx
movq %rdi, %r15
movl 0x48(%rsi), %esi
testl %esi, %esi
jle 0x17ddf
movq 0x50(%rbx), %rdi
xorl %r14d, %r14d
cmpl $0x504c, (%rdi)         # imm = 0x504C
je 0x17de2
incq %r14
addq $0x10, %rdi
cmpq %r14, %rsi
jne 0x17dc6
movl %esi, %r14d
jmp 0x17de2
xorl %r14d, %r14d
cmpl %esi, %r14d
je 0x17fed
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
leaq (%rax,%rax,4), %rax
movq %rsp, %r12
leaq 0xf(,%rax,2), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf755
testl %eax, %eax
jns 0x17e2e
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf938
testl %eax, %eax
js 0x17fed
movq 0x50(%rbx), %rax
movl %r14d, %edx
xorl %ecx, %ecx
movl $0x4, %esi
movq 0x85dd(%rip), %rdi      # 0x20420
xorl %r8d, %r8d
movl $0x1, %r9d
shll %cl, %r9d
movslq %r8d, %r8
leaq -0x80(,%r8,4), %r10
addq %rbp, %r10
addl %esi, %r8d
movl %ecx, %r11d
movl %r9d, %ebx
btsl %r11d, %ebx
movsbl (%rdi,%rbx), %ebx
movl %ebx, (%r10)
incl %r11d
addq $0x4, %r10
cmpl $0x4, %r11d
jne 0x17e63
incl %ecx
decl %esi
cmpl $0x4, %ecx
jne 0x17e46
shlq $0x4, %rdx
movq 0x8(%rax,%rdx), %rcx
movslq 0x4(%rax,%rdx), %rax
movl $0x3, %edx
cmpb $0x0, (%rcx)
jne 0x17ec1
addq %rax, %rcx
decq %rdx
jne 0x17e9a
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movq -0x38(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x17fea
movq %r12, -0x50(%rbp)
leaq 0xa(%r12), %rax
movq %rax, -0x48(%rbp)
leaq 0x14(%r12), %rax
movq %rax, -0x40(%rbp)
movl (%r15), %ecx
testl %ecx, %ecx
jle 0x17f3d
incq %rcx
movl $0x40000000, %eax       # imm = 0x40000000
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x1, %esi
movl $0xff, %edi
movl %eax, %r8d
movl (%r15,%rsi,4), %eax
movzbl %al, %r9d
movzbl (%r12,%r9), %r9d
movl %eax, %r10d
shrl $0x8, %r10d
andl %edi, %r10d
movzbl 0xa(%r12,%r10), %r10d
addl %r9d, %r10d
shrl $0x10, %eax
andl %edi, %eax
movzbl 0x14(%r12,%rax), %eax
addl %r10d, %eax
cmpl %r8d, %eax
cmovgel %r8d, %eax
cmovll %esi, %edx
incq %rsi
cmpq %rsi, %rcx
jne 0x17ef5
movslq %edx, %rcx
jmp 0x17f49
movq $-0x1, %rcx
movl $0x40000000, %eax       # imm = 0x40000000
movl (%r15,%rcx,4), %ecx
movl $0xff, %edx
movl %ecx, %esi
andl %edx, %esi
movl -0x80(%rbp,%rsi,4), %esi
movl %ecx, %edi
shrl $0x8, %edi
andl %edx, %edi
movl -0x80(%rbp,%rdi,4), %edx
shll $0x8, %edx
shrl $0x10, %ecx
movl -0x80(%rbp,%rcx,4), %r8d
shll $0x10, %r8d
xorl %r10d, %r10d
xorl %edi, %edi
xorl %r9d, %r9d
movq -0x38(%rbp), %r12
movq -0x50(%rbp,%r10,8), %rcx
movl $0x40000000, %r11d      # imm = 0x40000000
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movl %r11d, %r15d
movzbl (%rcx,%r14), %r11d
cmpl %r11d, %r15d
cmovll %r15d, %r11d
cmovgl %r14d, %ebx
incq %r14
cmpq $0xa, %r14
jne 0x17f93
movslq %ebx, %rcx
movl -0x80(%rbp,%rcx,4), %ebx
leal (,%r10,8), %ecx
shll %cl, %ebx
orl %ebx, %edi
addl %r11d, %r9d
incq %r10
cmpq $0x3, %r10
jne 0x17f80
orl %esi, %edx
orl %r8d, %edx
subl %r9d, %eax
movq -0x30(%rbp), %rcx
movl %eax, (%rcx)
shlq $0x20, %rdx
movslq %edi, %rax
orq %rdx, %rax
movq %rax, (%r12)
xorl %r13d, %r13d
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq