// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bcftools/prob1.c
// Function: calc_hwe
double calc_hwe(int obs_hom1, int obs_hom2, int obs_hets)
{
    if (obs_hom1 + obs_hom2 + obs_hets == 0 ) return 1;

    assert(obs_hom1 >= 0 && obs_hom2 >= 0 && obs_hets >= 0);

    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*) calloc(rare_copies+1, sizeof(double));

    /* 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--;
    }
    int i;
    for (i = 0; i <= rare_copies; i++) het_probs[i] /= sum;

    /*  p-value calculation for p_hwe  */
    double p_hwe = 0.0;
    for (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);
    return p_hwe;

}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
leal (%rsi,%rdi), %eax
addl %edx, %eax
jne 0x10a56
movsd 0x8627(%rip), %xmm0     # 0x19078
jmp 0x10c6b
movl %edx, %r14d
movl %esi, %eax
orl %edi, %eax
orl %edx, %eax
js 0x10c77
cmpl %esi, %edi
movl %esi, %r12d
cmovgl %edi, %r12d
cmovll %edi, %esi
leal (%r14,%rsi,2), %r15d
addl %r14d, %r12d
addl %esi, %r12d
leal (%r14,%rsi,2), %ebx
incl %ebx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2260
movq %rax, %rdi
leal (%r12,%r12), %ecx
movl %ecx, %eax
subl %r15d, %eax
imull %r15d, %eax
cltd
idivl %ecx
movl %eax, %edx
xorl %r14d, %edx
andl $0x1, %edx
addl %eax, %edx
movl %r15d, %ecx
subl %edx, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
subl %edx, %r12d
subl %eax, %r12d
movslq %edx, %rcx
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
movq %rsi, (%rdi,%rcx,8)
cmpl $0x2, %ecx
jl 0x10b70
movl %edx, %esi
movd %r12d, %xmm0
movd %eax, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
addq $-0x2, %rsi
movsd 0x8584(%rip), %xmm0     # 0x19078
movsd 0x859c(%rip), %xmm2     # 0x19098
movapd 0x850c(%rip), %xmm3     # 0x19010
movsd 0xa99c(%rip), %xmm4     # 0x1b4a8
pcmpeqd %xmm5, %xmm5
movl %edx, %r8d
xorps %xmm6, %xmm6
cvtsi2sd %r8d, %xmm6
movsd 0x10(%rdi,%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, %r9d
movsd %xmm6, (%rdi,%r9,8)
addsd %xmm6, %xmm0
psubd %xmm5, %xmm1
leal -0x2(%r8), %r9d
addq $-0x2, %rsi
cmpl $0x3, %r8d
movl %r9d, %r8d
jg 0x10b13
jmp 0x10b78
movsd 0x8500(%rip), %xmm0     # 0x19078
leal -0x2(%r15), %esi
cmpl %esi, %edx
jg 0x10bee
movslq %esi, %rdx
movsd (%rdi,%rcx,8), %xmm2
movsd 0xa918(%rip), %xmm1     # 0x1b4a8
movsd 0xa918(%rip), %xmm3     # 0x1b4b0
movsd 0x84d8(%rip), %xmm4     # 0x19078
mulsd %xmm1, %xmm2
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm2, %xmm5
xorps %xmm2, %xmm2
cvtsi2sd %r12d, %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(%rdi,%rcx,8)
addq $0x2, %rcx
addsd %xmm2, %xmm0
decl %eax
decl %r12d
cmpq %rdx, %rcx
jle 0x10ba0
xorpd %xmm1, %xmm1
testl %r15d, %r15d
js 0x10c50
xorl %eax, %eax
movsd (%rdi,%rax,8), %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, (%rdi,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x10bf9
testl %r15d, %r15d
js 0x10c50
movl %r14d, %eax
movsd (%rdi,%rax,8), %xmm0
xorpd %xmm2, %xmm2
xorl %eax, %eax
movsd (%rdi,%rax,8), %xmm3
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
movapd %xmm1, %xmm4
andpd %xmm2, %xmm1
addsd %xmm3, %xmm2
andnpd %xmm2, %xmm4
orpd %xmm4, %xmm1
incq %rax
movapd %xmm1, %xmm2
cmpq %rax, %rbx
jne 0x10c22
movsd 0x8420(%rip), %xmm0     # 0x19078
minsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
callq 0x2060
movsd (%rsp), %xmm0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xa95d(%rip), %rdi      # 0x1b5db
leaq 0xa986(%rip), %rsi      # 0x1b60b
leaq 0xa987(%rip), %rcx      # 0x1b613
movl $0xd9, %edx
callq 0x21d0