// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movl %esi, %r13d
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%rcx)
movq %rax, (%r8)
movq %rax, (%r9)
leal (%r15,%r13), %eax
addl %edi, %eax
je 0x1a0f8
movq %r9, %r12
movq %r8, %r14
movl %edi, %ebx
movq %rcx, 0x20(%rsp)
movl %r13d, %eax
orl %edi, %eax
orl %r15d, %eax
jns 0x19ea6
leaq 0x143cf(%rip), %rsi     # 0x2e248
leaq 0x48(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x5160
leaq 0x27641(%rip), %rdi     # 0x414d0
leaq 0x48(%rsp), %rsi
pushq $0x5b
popq %rdx
callq 0x2bf18
leaq 0x48(%rsp), %rdi
callq 0x5908
movq %r12, 0x10(%rsp)
movq %r14, 0x18(%rsp)
cmpl %r15d, %r13d
movl %r15d, %ebp
cmovgl %r13d, %ebp
cmovll %r13d, %r15d
leal (%rbx,%r15,2), %r12d
movslq %r12d, %r14
leaq 0x8(,%r14,8), %rdi
callq 0x5600
movq %rax, %r13
testq %rax, %rax
jne 0x19f18
movq %rbx, 0x8(%rsp)
leaq 0x1438d(%rip), %rsi     # 0x2e273
leaq 0x28(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x5160
leaq 0x275d4(%rip), %rdi     # 0x414d0
leaq 0x28(%rsp), %rsi
pushq $0x5a
popq %rdx
callq 0x2bf18
leaq 0x28(%rsp), %rdi
callq 0x5908
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
addl %ebx, %ebp
addl %r15d, %ebp
xorl %eax, %eax
movq 0x20(%rsp), %r8
movabsq $0x3ff0000000000000, %r9 # imm = 0x3FF0000000000000
cmpq %r14, %rax
jg 0x19f41
andq $0x0, (%r13,%rax,8)
incq %rax
jmp 0x19f31
leal (,%rbp,2), %ecx
movl %ecx, %eax
subl %r12d, %eax
imull %r12d, %eax
cltd
idivl %ecx
movl %eax, %esi
xorl %edi, %esi
andl $0x1, %esi
addl %eax, %esi
movl %r12d, %eax
subl %esi, %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
subl %esi, %ebp
subl %eax, %ebp
movslq %esi, %rcx
movq %r9, (%r13,%rcx,8)
movd %ebp, %xmm1
movd %eax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movsd 0x13090(%rip), %xmm1    # 0x2d018
movsd 0x13ff0(%rip), %xmm3    # 0x2df80
movapd 0x14238(%rip), %xmm4    # 0x2e1d0
movsd 0x14210(%rip), %xmm2    # 0x2e1b0
pcmpeqd %xmm5, %xmm5
cmpl $0x1, %esi
jle 0x19ff4
cvtsi2sd %esi, %xmm6
movsd (%r13,%rsi,8), %xmm7
mulsd %xmm6, %xmm7
addsd %xmm3, %xmm6
mulsd %xmm7, %xmm6
cvtdq2pd %xmm0, %xmm7
addpd %xmm4, %xmm7
movapd %xmm7, %xmm8
mulsd %xmm2, %xmm8
unpckhpd %xmm7, %xmm7            # xmm7 = xmm7[1,1]
mulsd %xmm8, %xmm7
divsd %xmm7, %xmm6
movsd %xmm6, -0x10(%r13,%rsi,8)
addq $-0x2, %rsi
addsd %xmm6, %xmm1
psubd %xmm5, %xmm0
jmp 0x19fa4
addl $-0x2, %r12d
movslq %r12d, %rdx
movsd 0x141b5(%rip), %xmm3    # 0x2e1b8
movsd 0x1300d(%rip), %xmm0    # 0x2d018
movq 0x10(%rsp), %rsi
cmpq %rdx, %rcx
jg 0x1a05d
movsd (%r13,%rcx,8), %xmm4
mulsd %xmm2, %xmm4
cvtsi2sd %eax, %xmm5
mulsd %xmm4, %xmm5
cvtsi2sd %ebp, %xmm4
mulsd %xmm5, %xmm4
cvtsi2sd %ecx, %xmm5
movapd %xmm5, %xmm6
addsd %xmm3, %xmm6
addsd %xmm0, %xmm5
mulsd %xmm6, %xmm5
divsd %xmm5, %xmm4
movsd %xmm4, 0x10(%r13,%rcx,8)
addq $0x2, %rcx
addsd %xmm4, %xmm1
decl %eax
decl %ebp
jmp 0x1a010
xorl %eax, %eax
cmpq %r14, %rax
jg 0x1a07b
movsd (%r13,%rax,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%r13,%rax,8)
incq %rax
jmp 0x1a05f
movslq %edi, %rax
movsd (%r13,%rax,8), %xmm1
movapd %xmm1, %xmm2
cmpq %r14, %rax
jge 0x1a09a
addsd 0x8(%r13,%rax,8), %xmm2
incq %rax
jmp 0x1a089
movsd %xmm2, (%rsi)
movl %edi, %eax
movapd %xmm1, %xmm2
testl %eax, %eax
jle 0x1a0b4
addsd -0x8(%r13,%rax,8), %xmm2
decq %rax
jmp 0x1a0a4
movq 0x18(%rsp), %rax
movsd %xmm2, (%rax)
andq $0x0, (%r8)
xorpd %xmm2, %xmm2
xorl %eax, %eax
cmpq %r14, %rax
jg 0x1a0e7
movsd (%r13,%rax,8), %xmm3
ucomisd %xmm1, %xmm3
ja 0x1a0e2
addsd %xmm3, %xmm2
movsd %xmm2, (%r8)
incq %rax
jmp 0x1a0c7
minsd %xmm2, %xmm0
movsd %xmm0, (%r8)
movq %r13, %rdi
callq 0x5560
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x1a119
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5908
jmp 0x1a125
jmp 0x1a122
movq %rax, %rbx
movq %rbx, %rdi
callq 0x5800
nop