// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x2, 0x58(%rdi)
jl 0x15323
movq %rcx, %rbx
movl %esi, %ebp
testl %esi, %esi
js 0x14f67
cmpl %ebp, 0x5c(%rdi)
jge 0x14f69
xorl %ebp, %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
movl 0x5c(%rdi), %r15d
callq 0x15338
testq %rax, %rax
je 0x15323
movq %rax, %r14
xorl %eax, %eax
movabsq $-0x4010000000000000, %rcx # imm = 0xBFF0000000000000
movq %rcx, (%rbx,%rax,8)
incq %rax
cmpq $0xa, %rax
jne 0x14fa3
movl %r15d, %edi
movq %r14, %rsi
callq 0x15445
movsd %xmm0, (%rbx)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x1531b
xorl %edi, %edi
movl %r15d, %esi
movq %r14, %rdx
callq 0x154f8
movsd %xmm0, (%rbx)
testl $0x31e, %r13d           # imm = 0x31E
je 0x15138
movl %r13d, 0x4(%rsp)
leaq 0x8(%rbx), %r12
movsd 0x400e(%rip), %xmm1     # 0x19010
subsd %xmm0, %xmm1
movapd %xmm0, %xmm2
addsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
mulsd %xmm1, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm1, 0x8(%rbx)
movsd %xmm2, 0x10(%rsp)
movsd %xmm2, 0x10(%rbx)
mulsd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, 0x18(%rbx)
movl $0x31, %r13d
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %r15d, %ecx
callq 0x1564f
subl $0x1, %r13d
setb %al
movsd 0x6648(%rip), %xmm1     # 0x1b6a8
ucomisd %xmm0, %xmm1
ja 0x1506a
testb %al, %al
je 0x15041
testl %r15d, %r15d
movsd 0x18(%rsp), %xmm9
movsd 0x3f94(%rip), %xmm0     # 0x19010
movsd 0x10(%rsp), %xmm11
movsd 0x8(%rsp), %xmm12
jle 0x15118
movsd 0x8(%rbx), %xmm10
movsd 0x10(%rbx), %xmm1
movsd 0x18(%rbx), %xmm2
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rax
movsd 0x3f5c(%rip), %xmm0     # 0x19010
xorl %ecx, %ecx
movsd (%r14,%rcx), %xmm3
movsd 0x8(%r14,%rcx), %xmm4
movapd %xmm4, %xmm5
mulsd %xmm1, %xmm5
movapd %xmm3, %xmm6
movsd 0x10(%r14,%rcx), %xmm7
movapd %xmm7, %xmm8
mulsd %xmm10, %xmm6
mulsd %xmm2, %xmm8
mulsd %xmm11, %xmm4
mulsd %xmm12, %xmm3
addsd %xmm5, %xmm6
addsd %xmm4, %xmm3
mulsd %xmm9, %xmm7
addsd %xmm3, %xmm7
addsd %xmm6, %xmm8
divsd %xmm7, %xmm8
mulsd %xmm8, %xmm0
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0x150b6
callq 0x2210
movapd %xmm0, %xmm1
movsd 0x3eef(%rip), %xmm0     # 0x19018
callq 0x1623a
movsd %xmm0, 0x20(%rbx)
movl 0x4(%rsp), %r13d
testb $-0x20, %r13b
setne %al
testl %ebp, %ebp
setg %cl
andb %al, %cl
cmpl %r15d, %ebp
setl %al
andb %cl, %al
cmpb $0x1, %al
jne 0x1517d
movsd (%rbx), %xmm0
xorl %edi, %edi
movl %ebp, %esi
movq %r14, %rdx
callq 0x154f8
movsd %xmm0, 0x28(%rbx)
movsd (%rbx), %xmm0
movl %ebp, %edi
movl %r15d, %esi
movq %r14, %rdx
callq 0x154f8
movsd %xmm0, 0x30(%rbx)
cmpl %r15d, %ebp
setl %al
testl %ebp, %ebp
setg %cl
testb %r13b, %r13b
sets %dl
andb %al, %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x15238
movsd (%rbx), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x30(%rbx), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x50(%rsp), %rax
xorl %ecx, %ecx
movsd 0x3e46(%rip), %xmm0     # 0x19010
movsd 0x20(%rsp,%rcx,8), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movapd %xmm1, %xmm3
addsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm2, %xmm2
movsd %xmm2, -0x10(%rax)
movsd %xmm3, -0x8(%rax)
mulsd %xmm1, %xmm1
movsd %xmm1, (%rax)
incq %rcx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0x151ca
leaq 0x40(%rsp), %rcx
movl %r15d, %edi
movl %ebp, %esi
movq %r14, %rdx
callq 0x15725
callq 0x2210
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0x3dea(%rip), %xmm0     # 0x19018
callq 0x1623a
movsd %xmm0, 0x38(%rbx)
cmpl %r15d, %ebp
setl %al
testl %ebp, %ebp
setg %cl
testl $0x300, %r13d           # imm = 0x300
setne %dl
andb %cl, %dl
andb %al, %dl
xorl %r12d, %r12d
cmpb $0x1, %dl
jne 0x1531b
leaq 0x8(%rbx), %rax
xorl %ecx, %ecx
movq 0x10(%rax), %rdx
movq %rdx, 0x50(%rsp,%rcx)
movupd (%rax), %xmm0
movupd %xmm0, 0x40(%rsp,%rcx)
addq $0x18, %rcx
cmpq $0x48, %rcx
jne 0x15263
movl $0x31, %r13d
leaq 0x58(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %ebp, %ecx
callq 0x1564f
subl $0x1, %r13d
setb %al
movsd 0x63ff(%rip), %xmm1     # 0x1b6a8
ucomisd %xmm0, %xmm1
ja 0x152b3
testb %al, %al
je 0x1528b
movl $0x31, %r13d
leaq 0x70(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x1564f
subl $0x1, %r13d
setb %al
movsd 0x63cb(%rip), %xmm1     # 0x1b6a8
ucomisd %xmm0, %xmm1
ja 0x152e7
testb %al, %al
je 0x152be
leaq 0x40(%rsp), %rcx
movl %r15d, %edi
movl %ebp, %esi
movq %r14, %rdx
callq 0x15725
callq 0x2210
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0x3d02(%rip), %xmm0     # 0x19010
callq 0x1623a
movsd %xmm0, 0x40(%rbx)
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x2060
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq