// File: /workspace/github_cpp_makefile_star2_O1/auton1[P]viterbi/entry.cpp
// Function: entry::SNPHWE(int, int, int, double&, double&, double&)
void entry::SNPHWE(int obs_hets, int obs_hom1, int obs_hom2, double &p_hwe, double &p_lo, double &p_hi)
{
	p_hwe = 1.0; p_lo = 1.0; p_hi = 1.0; //p_hi_lo = 1.0;
	if (obs_hom1 + obs_hom2 + obs_hets == 0 ) return;

	if (obs_hom1 < 0 || obs_hom2 < 0 || obs_hets < 0)
		LOG.error("Internal error: negative count in HWE test", 91);

	int obs_homc = obs_hom1 < obs_hom2 ? obs_hom2 : obs_hom1;
	int obs_homr = obs_hom1 < obs_hom2 ? obs_hom1 : obs_hom2;

	int rare_copies = 2 * obs_homr + obs_hets;
	int genotypes   = obs_hets + obs_homc + obs_homr;

	double * het_probs = (double *) malloc((size_t) (rare_copies + 1) * sizeof(double));
	if (het_probs == NULL)
		LOG.error("Internal error: SNP-HWE: Unable to allocate array", 90);

	for (int i = 0; i <= rare_copies; i++)
		het_probs[i] = 0.0;

	/* start at midpoint */
	int mid = rare_copies * (2 * genotypes - rare_copies) / (2 * genotypes);

	/* check to ensure that midpoint and rare alleles have same parity */
	if ((rare_copies & 1) ^ (mid & 1))
		mid++;

	int curr_hets = mid;
	int curr_homr = (rare_copies - mid) / 2;
	int curr_homc = genotypes - curr_hets - curr_homr;

	het_probs[mid] = 1.0;
	double sum = het_probs[mid];
	for (curr_hets = mid; curr_hets > 1; curr_hets -= 2)
	{
	  het_probs[curr_hets - 2] = het_probs[curr_hets] * curr_hets * (curr_hets - 1.0) / (4.0 * (curr_homr + 1.0) * (curr_homc + 1.0));
	  sum += het_probs[curr_hets - 2];

	  /* 2 fewer heterozygotes for next iteration -> add one rare, one common homozygote */
	  curr_homr++;
	  curr_homc++;
	}

	curr_hets = mid;
	curr_homr = (rare_copies - mid) / 2;
	curr_homc = genotypes - curr_hets - curr_homr;
	for (curr_hets = mid; curr_hets <= rare_copies - 2; curr_hets += 2)
	{
	  het_probs[curr_hets + 2] = het_probs[curr_hets] * 4.0 * curr_homr * curr_homc	/((curr_hets + 2.0) * (curr_hets + 1.0));
	  sum += het_probs[curr_hets + 2];

	  /* add 2 heterozygotes for next iteration -> subtract one rare, one common homozygote */
	  curr_homr--;
	  curr_homc--;
	}

	for (int i = 0; i <= rare_copies; i++)
		het_probs[i] /= sum;

	// alternate p-value calculation for p_hi/p_lo
	p_hi = het_probs[obs_hets];
	for (int i = obs_hets + 1; i <= rare_copies; i++)
	 p_hi += het_probs[i];

	p_lo = het_probs[obs_hets];
	for (int i = obs_hets - 1; i >= 0; i--)
	  p_lo += het_probs[i];

	//p_hi_lo = p_hi < p_lo ? 2.0 * p_hi : 2.0 * p_lo;

	p_hwe = 0.0;
	/*  p-value calculation for p_hwe  */
	for (int i = 0; i <= rare_copies; i++)
	{
		if (het_probs[i] > het_probs[obs_hets])
			continue;
		p_hwe += het_probs[i];
	}

	p_hwe = p_hwe > 1.0 ? 1.0 : p_hwe;

	free(het_probs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r13d
movl %esi, %r12d
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%rcx)
movq %rax, (%r8)
movq %rax, (%r9)
leal (%r12,%r13), %eax
addl %edi, %eax
je 0x1fb9e
movq %rcx, %rbx
movl %edi, %r14d
movq %r9, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movl %r12d, %eax
orl %edi, %eax
orl %r13d, %eax
jns 0x1f8a4
leaq 0x58(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x1aabf(%rip), %rsi     # 0x3a32c
leaq 0x1aae2(%rip), %rdx     # 0x3a356
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0xc868
leaq 0x2ac20(%rip), %rdi     # 0x4a4a8
movq %r15, %rsi
movl $0x5b, %edx
callq 0x3721e
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1f8a4
callq 0x5410
cmpl %r13d, %r12d
movl %r13d, %ebp
cmovgl %r12d, %ebp
cmovll %r12d, %r13d
leal (%r14,%r13,2), %eax
movq %rax, 0x18(%rsp)
movslq %eax, %r15
leaq 0x8(,%r15,8), %rdi
callq 0x55c0
movq %rax, %r12
testq %rax, %rax
jne 0x1f91e
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1aa74(%rip), %rsi     # 0x3a357
leaq 0x1aa9e(%rip), %rdx     # 0x3a388
leaq 0x28(%rsp), %rdi
callq 0xc868
leaq 0x2abad(%rip), %rdi     # 0x4a4a8
leaq 0x28(%rsp), %rsi
movl $0x5a, %edx
callq 0x3721e
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x1f91e
callq 0x5410
leal (,%r13,2), %r11d
addl %r14d, %ebp
addl %r13d, %ebp
movq 0x18(%rsp), %r10
testl %r10d, %r10d
js 0x1f96b
leal (%r11,%r14), %edx
incl %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, 0x20(%rsp)
movq %r13, %rbx
movq %r14, %r13
movq %r11, %r14
callq 0x5200
movq %r14, %r11
movq %r13, %r14
movq %rbx, %r13
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r10
leal (,%rbp,2), %ecx
movl %ecx, %eax
subl %r10d, %eax
imull %r10d, %eax
cltd
idivl %ecx
movl %eax, %edx
xorl %r14d, %edx
andl $0x1, %edx
addl %eax, %edx
movl %r10d, %ecx
subl %edx, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
subl %edx, %ebp
subl %eax, %ebp
movslq %edx, %rcx
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
movq %rsi, (%r12,%rcx,8)
cmpl $0x2, %ecx
jl 0x1fa4d
movl %edx, %esi
movd %ebp, %xmm0
movd %eax, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
addq $-0x2, %rsi
movsd 0x1964a(%rip), %xmm0    # 0x39018
movsd 0x1a68a(%rip), %xmm2    # 0x3a060
movapd 0x1a8d2(%rip), %xmm3    # 0x3a2b0
movsd 0x1a8aa(%rip), %xmm4    # 0x3a290
pcmpeqd %xmm5, %xmm5
movl %edx, %edi
movq 0x8(%rsp), %r9
xorps %xmm6, %xmm6
cvtsi2sd %edi, %xmm6
movsd 0x10(%r12,%rsi,8), %xmm7
mulsd %xmm6, %xmm7
addsd %xmm2, %xmm6
mulsd %xmm7, %xmm6
cvtdq2pd %xmm1, %xmm7
addpd %xmm3, %xmm7
movapd %xmm7, %xmm8
mulsd %xmm4, %xmm8
unpckhpd %xmm7, %xmm7            # xmm7 = xmm7[1,1]
mulsd %xmm8, %xmm7
divsd %xmm7, %xmm6
movl %esi, %r8d
movsd %xmm6, (%r12,%r8,8)
addsd %xmm6, %xmm0
psubd %xmm5, %xmm1
leal -0x2(%rdi), %r8d
addq $-0x2, %rsi
cmpl $0x3, %edi
movl %r8d, %edi
jg 0x1f9f1
jmp 0x1fa5a
movsd 0x195c3(%rip), %xmm0    # 0x39018
movq 0x8(%rsp), %r9
leal -0x2(%r10), %esi
cmpl %esi, %edx
jle 0x1fa69
movq 0x10(%rsp), %rsi
jmp 0x1fadc
movslq %esi, %rdx
movsd 0x1a81c(%rip), %xmm1    # 0x3a290
movsd 0x1a81c(%rip), %xmm2    # 0x3a298
movsd 0x19594(%rip), %xmm3    # 0x39018
movq 0x10(%rsp), %rsi
movsd (%r12,%rcx,8), %xmm4
mulsd %xmm1, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm4, %xmm5
xorps %xmm4, %xmm4
cvtsi2sd %ebp, %xmm4
xorps %xmm6, %xmm6
cvtsi2sd %ecx, %xmm6
mulsd %xmm5, %xmm4
movapd %xmm6, %xmm5
addsd %xmm2, %xmm5
addsd %xmm3, %xmm6
mulsd %xmm5, %xmm6
divsd %xmm6, %xmm4
movsd %xmm4, 0x10(%r12,%rcx,8)
addq $0x2, %rcx
addsd %xmm4, %xmm0
decl %eax
decl %ebp
cmpq %rdx, %rcx
jle 0x1fa89
testl %r10d, %r10d
js 0x1fb01
leal (%r11,%r14), %eax
incl %eax
xorl %ecx, %ecx
movsd (%r12,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x1fae9
movslq %r14d, %rax
movsd (%r12,%rax,8), %xmm0
movapd %xmm0, %xmm1
testl %r13d, %r13d
jle 0x1fb26
movapd %xmm0, %xmm1
addsd 0x8(%r12,%rax,8), %xmm1
incq %rax
cmpq %r15, %rax
jl 0x1fb17
movsd %xmm1, (%r9)
movapd %xmm0, %xmm1
testl %r14d, %r14d
jle 0x1fb4e
movl %r14d, %eax
incq %rax
movapd %xmm0, %xmm1
addsd -0x10(%r12,%rax,8), %xmm1
decq %rax
cmpq $0x1, %rax
jg 0x1fb3e
movsd %xmm1, (%rsi)
movq $0x0, (%rbx)
testl %r10d, %r10d
js 0x1fb86
leal (%r11,%r14), %eax
incl %eax
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movsd (%r12,%rcx,8), %xmm2
ucomisd %xmm0, %xmm2
ja 0x1fb7e
addsd %xmm2, %xmm1
movsd %xmm1, (%rbx)
incq %rcx
cmpq %rcx, %rax
jne 0x1fb6a
movsd 0x1948a(%rip), %xmm0    # 0x39018
minsd (%rbx), %xmm0
movsd %xmm0, (%rbx)
movq %r12, %rdi
callq 0x5530
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
jne 0x1fbce
jmp 0x1fbd3
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1fbd3
callq 0x5410
movq %rbx, %rdi
callq 0x57d0
nop