// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bcftools/em.c
// Function: bcf_em1
int bcf_em1(const bcf1_t *b, int n1, int flag, double x[10])
{
	double *pdg;
	int i, n, n2;
	if (b->n_alleles < 2) return -1; // one allele only
	// initialization
	if (n1 < 0 || n1 > b->n_smpl) n1 = 0;
	if (flag & 1<<7) flag |= 7<<5; // compute group freq if LRT is required
	if (flag & 0xf<<1) flag |= 0xf<<1;
	n = b->n_smpl; n2 = n - n1;
	pdg = get_pdg3(b);
	if (pdg == 0) return -1;
	for (i = 0; i < 10; ++i) x[i] = -1.; // set to negative
	{
		if ((x[0] = est_freq(n, pdg)) < 0.) {
			free(pdg);
			return -1; // no data
		}
		x[0] = freqml(x[0], 0, n, pdg);
	}
	if (flag & (0xf<<1|3<<8)) { // estimate the genotype frequency and test HWE
		double *g = x + 1, f3[3], r;
		f3[0] = g[0] = (1 - x[0]) * (1 - x[0]);
		f3[1] = g[1] = 2 * x[0] * (1 - x[0]);
		f3[2] = g[2] = x[0] * x[0];
		for (i = 0; i < ITER_MAX; ++i)
			if (g3_iter(g, pdg, 0, n) < EPS) break;
		// Hardy-Weinberg equilibrium (HWE)
		for (i = 0, r = 1.; i < n; ++i) {
			double *p = pdg + i * 3;
			r *= (p[0] * g[0] + p[1] * g[1] + p[2] * g[2]) / (p[0] * f3[0] + p[1] * f3[1] + p[2] * f3[2]);
		}
		x[4] = kf_gammaq(.5, log(r));
	}
	if ((flag & 7<<5) && n1 > 0 && n1 < n) { // group frequency
		x[5] = freqml(x[0], 0, n1, pdg);
		x[6] = freqml(x[0], n1, n, pdg);
	}
	if ((flag & 1<<7) && n1 > 0 && n1 < n) { // 1-degree P-value
		double f[3], f3[3][3], tmp;
		f[0] = x[0]; f[1] = x[5]; f[2] = x[6];
		for (i = 0; i < 3; ++i)
			f3[i][0] = (1-f[i])*(1-f[i]), f3[i][1] = 2*f[i]*(1-f[i]), f3[i][2] = f[i]*f[i];
		tmp = log(lk_ratio_test(n, n1, pdg, f3));
		if (tmp < 0) tmp = 0;
		x[7] = kf_gammaq(.5, tmp);
	}
	if ((flag & 3<<8) && n1 > 0 && n1 < n) { // 2-degree P-value
		double g[3][3], tmp;
		for (i = 0; i < 3; ++i) memcpy(g[i], x + 1, 3 * sizeof(double));
		for (i = 0; i < ITER_MAX; ++i)
			if (g3_iter(g[1], pdg, 0, n1) < EPS) break;
		for (i = 0; i < ITER_MAX; ++i)
			if (g3_iter(g[2], pdg, n1, n) < EPS) break;
		tmp = log(lk_ratio_test(n, n1, pdg, g));
		if (tmp < 0) tmp = 0;
		x[8] = kf_gammaq(1., tmp);
	}
	// free
	free(pdg);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x2, 0x58(%rdi)
jl 0x15728
movq %rcx, %rbx
movl 0x5c(%rdi), %r15d
xorl %eax, %eax
cmpl %esi, %r15d
movl %esi, %ebp
cmovll %eax, %ebp
testl %esi, %esi
cmovsl %eax, %ebp
movl %edx, %eax
orl $0xe0, %eax
testb %dl, %dl
cmovnsl %edx, %eax
movl %eax, %r13d
orl $0x1e, %r13d
testb $0x1e, %al
cmovel %eax, %r13d
callq 0x1573d
testq %rax, %rax
je 0x15728
movq %rax, %r14
movl %r15d, %r15d
xorl %eax, %eax
movapd 0x613e(%rip), %xmm0     # 0x1b520
movupd %xmm0, (%rbx,%rax,8)
addq $0x2, %rax
cmpq $0xa, %rax
jne 0x153e2
movl %r15d, %edi
movq %r14, %rsi
callq 0x15849
movsd %xmm0, (%rbx)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x15720
xorl %edi, %edi
movl %r15d, %esi
movq %r14, %rdx
callq 0x15907
movsd %xmm0, (%rbx)
testl $0x31e, %r13d           # imm = 0x31E
je 0x15557
movl %r13d, 0xc(%rsp)
leaq 0x8(%rbx), %r12
movsd 0x3c35(%rip), %xmm2     # 0x19078
subsd %xmm0, %xmm2
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
movapd %xmm1, 0x10(%rsp)
movsd %xmm1, 0x8(%rbx)
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
movapd %xmm2, 0x20(%rsp)
movupd %xmm2, 0x10(%rbx)
movl $0x31, %r13d
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x15a75
subl $0x1, %r13d
setb %al
movsd 0x62f2(%rip), %xmm1     # 0x1b790
ucomisd %xmm0, %xmm1
ja 0x154a8
testb %al, %al
je 0x1547f
testl %r15d, %r15d
movapd 0x20(%rsp), %xmm5
movsd 0x3bbf(%rip), %xmm0     # 0x19078
jle 0x15537
movsd 0x8(%rbx), %xmm6
movsd 0x10(%rbx), %xmm1
unpcklpd 0x10(%rsp), %xmm6       # xmm6 = xmm6[0],mem[0]
unpcklpd %xmm5, %xmm1            # xmm1 = xmm1[0],xmm5[0]
movlpd 0x18(%rbx), %xmm5       # xmm5 = mem[0],xmm5[1]
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rax
movsd 0x3b90(%rip), %xmm0     # 0x19078
xorl %ecx, %ecx
movsd (%r14,%rcx), %xmm2
movsd 0x8(%r14,%rcx), %xmm3
movsd 0x10(%r14,%rcx), %xmm4
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
mulpd %xmm1, %xmm3
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm6, %xmm2
addpd %xmm3, %xmm2
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
mulpd %xmm5, %xmm4
addpd %xmm2, %xmm4
movapd %xmm4, %xmm2
unpckhpd %xmm4, %xmm2            # xmm2 = xmm2[1],xmm4[1]
divsd %xmm2, %xmm4
mulsd %xmm4, %xmm0
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x154ea
callq 0x2210
movapd %xmm0, %xmm1
movsd 0x3b38(%rip), %xmm0     # 0x19080
callq 0x165df
movsd %xmm0, 0x20(%rbx)
movl 0xc(%rsp), %r13d
testb $-0x20, %r13b
je 0x15591
testl %ebp, %ebp
je 0x15591
cmpl %r15d, %ebp
jge 0x15591
movsd (%rbx), %xmm0
xorl %edi, %edi
movl %ebp, %esi
movq %r14, %rdx
callq 0x15907
movsd %xmm0, 0x28(%rbx)
movsd (%rbx), %xmm0
movl %ebp, %edi
movl %r15d, %esi
movq %r14, %rdx
callq 0x15907
movsd %xmm0, 0x30(%rbx)
testb %r13b, %r13b
jns 0x1563e
testl %ebp, %ebp
je 0x1563e
cmpl %r15d, %ebp
jge 0x1563e
movsd (%rbx), %xmm0
movsd %xmm0, 0x30(%rsp)
movups 0x28(%rbx), %xmm0
movups %xmm0, 0x38(%rsp)
leaq 0x58(%rsp), %rax
xorl %ecx, %ecx
movsd 0x3aab(%rip), %xmm0     # 0x19078
movsd 0x30(%rsp,%rcx,8), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
movsd %xmm3, -0x8(%rax)
movapd %xmm1, %xmm3
addsd %xmm1, %xmm3
unpcklpd %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
mulpd %xmm3, %xmm2
movupd %xmm2, (%rax)
incq %rcx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0x155cd
leaq 0x50(%rsp), %rcx
movl %r15d, %edi
movl %ebp, %esi
movq %r14, %rdx
callq 0x15b28
callq 0x2210
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0x3a4c(%rip), %xmm0     # 0x19080
callq 0x165df
movsd %xmm0, 0x38(%rbx)
xorl %r12d, %r12d
testl $0x300, %r13d           # imm = 0x300
je 0x15720
testl %ebp, %ebp
je 0x15720
cmpl %r15d, %ebp
jge 0x15720
leaq 0x8(%rbx), %rax
xorl %ecx, %ecx
movq 0x10(%rax), %rdx
movq %rdx, 0x60(%rsp,%rcx)
movupd (%rax), %xmm0
movupd %xmm0, 0x50(%rsp,%rcx)
addq $0x18, %rcx
cmpq $0x48, %rcx
jne 0x15665
movl $0x31, %r13d
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %ebp, %ecx
callq 0x15a75
subl $0x1, %r13d
setb %al
movsd 0x60e5(%rip), %xmm1     # 0x1b790
ucomisd %xmm0, %xmm1
ja 0x156b5
testb %al, %al
je 0x1568d
movl $0x31, %r13d
leaq 0x80(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x15a75
subl $0x1, %r13d
setb %al
movsd 0x60ae(%rip), %xmm1     # 0x1b790
ucomisd %xmm0, %xmm1
ja 0x156ec
testb %al, %al
je 0x156c3
leaq 0x50(%rsp), %rcx
movl %r15d, %edi
movl %ebp, %esi
movq %r14, %rdx
callq 0x15b28
callq 0x2210
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0x3965(%rip), %xmm0     # 0x19078
callq 0x165df
movsd %xmm0, 0x40(%rbx)
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x2060
movl %r12d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq