// File: 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:
subq $0xe8, %rsp
movl %edi, 0xe4(%rsp)
movl %esi, 0xe0(%rsp)
movl %edx, 0xdc(%rsp)
movq %rcx, 0xd0(%rsp)
movq %r8, 0xc8(%rsp)
movq %r9, 0xc0(%rsp)
movq 0xd0(%rsp), %rax
movsd 0x2edb4(%rip), %xmm0    # 0x74018
movsd %xmm0, (%rax)
movq 0xc8(%rsp), %rax
movsd 0x2eda0(%rip), %xmm0    # 0x74018
movsd %xmm0, (%rax)
movq 0xc0(%rsp), %rax
movsd 0x2ed8c(%rip), %xmm0    # 0x74018
movsd %xmm0, (%rax)
movl 0xe0(%rsp), %eax
addl 0xdc(%rsp), %eax
addl 0xe4(%rsp), %eax
cmpl $0x0, %eax
jne 0x452af
jmp 0x45949
cmpl $0x0, 0xe0(%rsp)
jl 0x452d1
cmpl $0x0, 0xdc(%rsp)
jl 0x452d1
cmpl $0x0, 0xe4(%rsp)
jge 0x4537e
leaq 0x9f(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x69f0
movq 0x18(%rsp), %rdx
leaq 0x2febd(%rip), %rsi     # 0x751ac
leaq 0xa0(%rsp), %rdi
callq 0x67c0
jmp 0x452fe
leaq 0x4e2fb(%rip), %rdi     # 0x93600
leaq 0xa0(%rsp), %rsi
movl $0x5b, %edx
callq 0x712f0
jmp 0x45319
leaq 0xa0(%rsp), %rdi
callq 0x6b50
leaq 0x9f(%rsp), %rdi
callq 0x65f0
jmp 0x4537e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
jmp 0x4536c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x6b50
leaq 0x9f(%rsp), %rdi
callq 0x65f0
jmp 0x45951
movl 0xe0(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x4539b
movl 0xdc(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x453a6
movl 0xe0(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0xe0(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jge 0x453ce
movl 0xe0(%rsp), %eax
movl %eax, 0x10(%rsp)
jmp 0x453d9
movl 0xdc(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
shll %eax
addl 0xe4(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0xe4(%rsp), %eax
addl 0x88(%rsp), %eax
addl 0x84(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x80(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x6780
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x454d3
leaq 0x4f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x69f0
movq 0x8(%rsp), %rdx
leaq 0x2fd81(%rip), %rsi     # 0x751d7
leaq 0x50(%rsp), %rdi
callq 0x67c0
jmp 0x45462
leaq 0x4e197(%rip), %rdi     # 0x93600
leaq 0x50(%rsp), %rsi
movl $0x5a, %edx
callq 0x712f0
jmp 0x4547a
leaq 0x50(%rsp), %rdi
callq 0x6b50
leaq 0x4f(%rsp), %rdi
callq 0x65f0
jmp 0x454d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
jmp 0x454c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x6b50
leaq 0x4f(%rsp), %rdi
callq 0x65f0
jmp 0x45951
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x80(%rsp), %eax
jg 0x45507
movq 0x70(%rsp), %rax
movslq 0x48(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x454db
movl 0x80(%rsp), %eax
movl 0x7c(%rsp), %ecx
shll %ecx
subl 0x80(%rsp), %ecx
imull %ecx, %eax
movl 0x7c(%rsp), %ecx
shll %ecx
cltd
idivl %ecx
movl %eax, 0x44(%rsp)
movl 0x80(%rsp), %eax
andl $0x1, %eax
movl 0x44(%rsp), %ecx
andl $0x1, %ecx
xorl %ecx, %eax
cmpl $0x0, %eax
je 0x4554e
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x80(%rsp), %eax
subl 0x44(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x3c(%rsp)
movl 0x7c(%rsp), %eax
subl 0x40(%rsp), %eax
subl 0x3c(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x70(%rsp), %rax
movslq 0x44(%rsp), %rcx
movsd 0x2ea89(%rip), %xmm0    # 0x74018
movsd %xmm0, (%rax,%rcx,8)
movq 0x70(%rsp), %rax
movslq 0x44(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x40(%rsp)
cmpl $0x1, 0x40(%rsp)
jle 0x4567d
movq 0x70(%rsp), %rax
movslq 0x40(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl 0x40(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl 0x40(%rsp), %xmm1
movsd 0x2ea35(%rip), %xmm2    # 0x74018
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl 0x3c(%rsp), %xmm2
movsd 0x2ea1f(%rip), %xmm1    # 0x74018
addsd %xmm1, %xmm2
movsd 0x2ea23(%rip), %xmm1    # 0x74028
mulsd %xmm2, %xmm1
cvtsi2sdl 0x38(%rsp), %xmm2
movsd 0x2ea01(%rip), %xmm3    # 0x74018
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x70(%rsp), %rax
movl 0x40(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x70(%rsp), %rax
movl 0x40(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
movl 0x40(%rsp), %eax
subl $0x2, %eax
movl %eax, 0x40(%rsp)
jmp 0x455b1
movl 0x44(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x80(%rsp), %eax
subl 0x44(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x3c(%rsp)
movl 0x7c(%rsp), %eax
subl 0x40(%rsp), %eax
subl 0x3c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
movl 0x80(%rsp), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jg 0x4577b
movq 0x70(%rsp), %rax
movslq 0x40(%rsp), %rcx
movsd 0x2e94c(%rip), %xmm0    # 0x74028
mulsd (%rax,%rcx,8), %xmm0
cvtsi2sdl 0x3c(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl 0x40(%rsp), %xmm1
movsd 0x2fa8d(%rip), %xmm2    # 0x75190
addsd %xmm2, %xmm1
cvtsi2sdl 0x40(%rsp), %xmm2
movsd 0x2e903(%rip), %xmm3    # 0x74018
addsd %xmm3, %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x70(%rsp), %rax
movl 0x40(%rsp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x70(%rsp), %rax
movl 0x40(%rsp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x38(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x38(%rsp)
movl 0x40(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x40(%rsp)
jmp 0x456b4
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x80(%rsp), %eax
jg 0x457bb
movsd 0x30(%rsp), %xmm1
movq 0x70(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x45783
movq 0x70(%rsp), %rax
movslq 0xe4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xc0(%rsp), %rax
movsd %xmm0, (%rax)
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x80(%rsp), %eax
jg 0x45820
movq 0x70(%rsp), %rax
movslq 0x28(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xc0(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x457e7
movq 0x70(%rsp), %rax
movslq 0xe4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xc8(%rsp), %rax
movsd %xmm0, (%rax)
movl 0xe4(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jl 0x4587f
movq 0x70(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xc8(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0x24(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x4584c
movq 0xd0(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x80(%rsp), %eax
jg 0x458f4
movq 0x70(%rsp), %rax
movslq 0x20(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x70(%rsp), %rax
movslq 0xe4(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x458c8
jmp 0x458e7
movq 0x70(%rsp), %rax
movslq 0x20(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xd0(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x45896
movq 0xd0(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x2e710(%rip), %xmm1    # 0x74018
ucomisd %xmm1, %xmm0
jbe 0x4591d
movsd 0x2e702(%rip), %xmm0    # 0x74018
movsd %xmm0, (%rsp)
jmp 0x4592e
movq 0xd0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movsd (%rsp), %xmm0
movq 0xd0(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x70(%rsp), %rdi
callq 0x66a0
addq $0xe8, %rsp
retq
movq 0x90(%rsp), %rdi
callq 0x69e0
nop