// File: /workspace/github_c_makefile_star2_O3/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 $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $0x3, 0x5c(%rsi)
jne 0x183bd
movq %rsi, %rbx
movq %rdi, %r15
movslq 0x48(%rsi), %rax
testq %rax, %rax
jle 0x181bf
movq 0x50(%rbx), %rsi
xorl %r14d, %r14d
cmpl $0x504c, (%rsi)         # imm = 0x504C
je 0x181c2
incq %r14
addq $0x10, %rsi
cmpq %r14, %rax
jne 0x181a6
jmp 0x183bd
xorl %r14d, %r14d
cmpl %eax, %r14d
je 0x183bd
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %rsp, %r12
addq $-0x20, %r12
movq %r12, %rsp
movq %rbx, %rdi
movq %r12, %rsi
callq 0xfa26
testl %eax, %eax
jns 0x181ff
movq %rbx, %rdi
movq %r12, %rsi
callq 0xfc16
testl %eax, %eax
js 0x183bd
movq 0x50(%rbx), %rax
movl %r14d, %edx
movl $0x4, %esi
xorl %edi, %edi
movq 0x820c(%rip), %r8       # 0x20420
xorl %ecx, %ecx
movslq %edi, %r9
leaq -0x80(,%r9,4), %r9
addq %rbp, %r9
movl $0x1, %r10d
shll %cl, %r10d
movl %ecx, %r11d
movl %r10d, %ebx
btsl %r11d, %ebx
movsbl (%r8,%rbx), %ebx
movl %ebx, (%r9)
incl %r11d
addq $0x4, %r9
cmpl $0x4, %r11d
jne 0x18230
incl %ecx
addl %esi, %edi
decl %esi
cmpl $0x4, %ecx
jne 0x18216
shlq $0x4, %rdx
movq 0x8(%rax,%rdx), %rcx
movslq 0x4(%rax,%rdx), %rax
movl $0x3, %edx
cmpb $0x0, (%rcx)
jne 0x18291
addq %rax, %rcx
decq %rdx
jne 0x1826a
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movq -0x38(%rbp), %rax
movq $-0x1, (%rax)
jmp 0x183ba
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 0x1830d
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 0x182c5
movslq %edx, %rcx
jmp 0x18319
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 0x18363
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 0x18350
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