// File: /workspace/github_c_makefile_star2_O1/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
movl %esi, %r14d
leal (%r14,%rdi), %eax
addl %edx, %eax
jne 0x1066d
movsd 0x89a8(%rip), %xmm0     # 0x19010
jmp 0x10888
movl %edx, %ebx
movl %r14d, %eax
orl %edi, %eax
orl %edx, %eax
js 0x10894
cmpl %r14d, %edi
movl %r14d, %r12d
cmovgl %edi, %r12d
cmovll %edi, %r14d
leal (%rbx,%r14,2), %r15d
addl %ebx, %r12d
addl %r14d, %r12d
leal (%rbx,%r14,2), %edi
incl %edi
movl $0x8, %esi
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 %ebx, %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 0x1077d
movl %edx, %esi
addq $-0x2, %rsi
movsd 0x8914(%rip), %xmm1     # 0x19010
movsd 0x892c(%rip), %xmm2     # 0x19030
movsd 0xad0c(%rip), %xmm3     # 0x1b418
movl %edx, %r10d
movapd %xmm1, %xmm0
movl %r12d, %r8d
movl %eax, %r9d
xorps %xmm4, %xmm4
cvtsi2sd %r10d, %xmm4
movsd 0x10(%rdi,%rsi,8), %xmm5
mulsd %xmm4, %xmm5
addsd %xmm2, %xmm4
mulsd %xmm5, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %r9d, %xmm5
addsd %xmm1, %xmm5
mulsd %xmm3, %xmm5
xorps %xmm6, %xmm6
cvtsi2sd %r8d, %xmm6
addsd %xmm1, %xmm6
mulsd %xmm5, %xmm6
divsd %xmm6, %xmm4
movl %esi, %r11d
movsd %xmm4, (%rdi,%r11,8)
addsd %xmm4, %xmm0
incl %r9d
incl %r8d
leal -0x2(%r10), %r11d
addq $-0x2, %rsi
cmpl $0x3, %r10d
movl %r11d, %r10d
jg 0x10719
jmp 0x10785
movsd 0x888b(%rip), %xmm0     # 0x19010
leal -0x2(%r15), %esi
cmpl %esi, %edx
jg 0x107fb
movslq %esi, %rdx
movsd 0xac80(%rip), %xmm1     # 0x1b418
movsd 0xac80(%rip), %xmm2     # 0x1b420
movsd 0x8868(%rip), %xmm3     # 0x19010
movsd (%rdi,%rcx,8), %xmm4
mulsd %xmm1, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm4, %xmm5
xorps %xmm4, %xmm4
cvtsi2sd %r12d, %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(%rdi,%rcx,8)
addq $0x2, %rcx
addsd %xmm4, %xmm0
decl %eax
decl %r12d
cmpq %rdx, %rcx
jle 0x107a8
addl %r14d, %r14d
testl %r15d, %r15d
js 0x10821
leal (%r14,%rbx), %eax
incl %eax
xorl %ecx, %ecx
movsd (%rdi,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x1080b
testl %r15d, %r15d
js 0x10869
movl %ebx, %eax
movsd (%rdi,%rax,8), %xmm0
leal (%r14,%rbx), %eax
incl %eax
xorpd %xmm2, %xmm2
xorl %ecx, %ecx
movsd (%rdi,%rcx,8), %xmm3
movapd %xmm0, %xmm1
cmpltsd %xmm3, %xmm1
movapd %xmm1, %xmm4
andpd %xmm2, %xmm1
addsd %xmm3, %xmm2
andnpd %xmm2, %xmm4
orpd %xmm4, %xmm1
incq %rcx
movapd %xmm1, %xmm2
cmpq %rcx, %rax
jne 0x10839
jmp 0x1086d
xorpd %xmm1, %xmm1
movsd 0x879b(%rip), %xmm0     # 0x19010
minsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
callq 0x2060
movsd (%rsp), %xmm0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xac68(%rip), %rdi      # 0x1b503
leaq 0xac91(%rip), %rsi      # 0x1b533
leaq 0xac92(%rip), %rcx      # 0x1b53b
movl $0xd9, %edx
callq 0x21d0