// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movl %edx, %r13d
movl %esi, %r15d
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%rcx)
movq %rax, (%r8)
movq %rax, (%r9)
leal (%r15,%r13), %eax
addl %edi, %eax
je 0x1fa03
movq %rcx, %rbx
movl %edi, %r12d
movq %r9, (%rsp)
movq %r8, 0x8(%rsp)
movl %r15d, %eax
orl %edi, %eax
orl %r13d, %eax
jns 0x1f727
leaq 0x48(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x19c18(%rip), %rsi     # 0x39308
leaq 0x19c3b(%rip), %rdx     # 0x39332
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0xc85e
leaq 0x29d9d(%rip), %rdi     # 0x494a8
movq %r14, %rsi
movl $0x5b, %edx
callq 0x36ec8
movq 0x38(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1f727
callq 0x5410
cmpl %r13d, %r15d
movl %r13d, %ebp
cmovgl %r15d, %ebp
cmovll %r15d, %r13d
leal (%r12,%r13,2), %r14d
incl %r14d
movslq %r14d, %rdi
shlq $0x3, %rdi
callq 0x55c0
movq %rax, %r15
testq %rax, %rax
jne 0x1f79b
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x19bd3(%rip), %rsi     # 0x39333
leaq 0x19bfd(%rip), %rdx     # 0x39364
leaq 0x18(%rsp), %rdi
callq 0xc85e
leaq 0x29d30(%rip), %rdi     # 0x494a8
leaq 0x18(%rsp), %rsi
movl $0x5a, %edx
callq 0x36ec8
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x1f79b
callq 0x5410
leal (%r12,%r13,2), %r10d
addl %r12d, %ebp
addl %r13d, %ebp
movl %r14d, %r14d
testl %r10d, %r10d
js 0x1f7db
leaq (,%r14,8), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %r13, 0x10(%rsp)
movq %rbx, %r13
movq %r12, %rbx
movq %r10, %r12
callq 0x5200
movq %r12, %r10
movq %rbx, %r12
movq %r13, %rbx
movq 0x10(%rsp), %r13
leal (,%rbp,2), %ecx
movl %ecx, %eax
subl %r10d, %eax
imull %r10d, %eax
cltd
idivl %ecx
movl %eax, %edx
xorl %r12d, %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, (%r15,%rcx,8)
cmpl $0x2, %ecx
jl 0x1f8bc
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 0x187da(%rip), %xmm0    # 0x38018
movsd 0x197fa(%rip), %xmm2    # 0x39040
movapd 0x19a42(%rip), %xmm3    # 0x39290
movsd 0x19a1a(%rip), %xmm4    # 0x39270
pcmpeqd %xmm5, %xmm5
movl %edx, %edi
movq (%rsp), %r9
xorps %xmm6, %xmm6
cvtsi2sd %edi, %xmm6
movsd 0x10(%r15,%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, (%r15,%r8,8)
addsd %xmm6, %xmm0
psubd %xmm5, %xmm1
leal -0x2(%rdi), %r8d
addq $-0x2, %rsi
cmpl $0x3, %edi
movl %r8d, %edi
jg 0x1f860
jmp 0x1f8c8
movsd 0x18754(%rip), %xmm0    # 0x38018
movq (%rsp), %r9
leal -0x2(%r10), %esi
cmpl %esi, %edx
jle 0x1f8d7
movq 0x8(%rsp), %rsi
jmp 0x1f94a
movslq %esi, %rdx
movsd (%r15,%rcx,8), %xmm2
movsd 0x19988(%rip), %xmm1    # 0x39270
movsd 0x19988(%rip), %xmm3    # 0x39278
movsd 0x18720(%rip), %xmm4    # 0x38018
movq 0x8(%rsp), %rsi
mulsd %xmm1, %xmm2
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm2, %xmm5
xorps %xmm2, %xmm2
cvtsi2sd %ebp, %xmm2
xorps %xmm6, %xmm6
cvtsi2sd %ecx, %xmm6
mulsd %xmm5, %xmm2
movapd %xmm6, %xmm5
addsd %xmm3, %xmm5
addsd %xmm4, %xmm6
mulsd %xmm5, %xmm6
divsd %xmm6, %xmm2
movsd %xmm2, 0x10(%r15,%rcx,8)
addq $0x2, %rcx
addsd %xmm2, %xmm0
decl %eax
decl %ebp
cmpq %rdx, %rcx
jle 0x1f8fd
testl %r10d, %r10d
js 0x1f969
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r15,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x1f951
movslq %r12d, %rax
movsd (%r15,%rax,8), %xmm0
movapd %xmm0, %xmm1
testl %r13d, %r13d
jle 0x1f991
movslq %r10d, %rcx
movapd %xmm0, %xmm1
addsd 0x8(%r15,%rax,8), %xmm1
incq %rax
cmpq %rcx, %rax
jl 0x1f982
movsd %xmm1, (%r9)
movapd %xmm0, %xmm1
testl %r12d, %r12d
jle 0x1f9b9
movl %r12d, %eax
incq %rax
movapd %xmm0, %xmm1
addsd -0x10(%r15,%rax,8), %xmm1
decq %rax
cmpq $0x1, %rax
ja 0x1f9a9
movsd %xmm1, (%rsi)
movq $0x0, (%rbx)
xorpd %xmm1, %xmm1
testl %r10d, %r10d
js 0x1f9eb
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm2
ucomisd %xmm0, %xmm2
ja 0x1f9e3
addsd %xmm2, %xmm1
movsd %xmm1, (%rbx)
incq %rax
cmpq %rax, %r14
jne 0x1f9cf
movsd 0x18625(%rip), %xmm0    # 0x38018
minsd %xmm1, %xmm0
movsd %xmm0, (%rbx)
movq %r15, %rdi
callq 0x5530
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
jne 0x1fa33
jmp 0x1fa38
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1fa38
callq 0x5410
movq %rbx, %rdi
callq 0x57d0