// File: /workspace/github_cpp_makefile_star2_O1/millanek[P]evo/process_vcf_utils.cpp
// Function: calculateChiSqPvalForInbreeding(std::vector<int, std::allocator<int>>&)
double calculateChiSqPvalForInbreeding(std::vector<int>& individualsWithVariant) {
    // Get the observed numbers of genotypes
    int naa = 0; int nAa = 0; int nAA = 0;
    for (std::vector<int>::size_type i = 0; i != individualsWithVariant.size(); i++) {
        if (individualsWithVariant[i] == 0) naa++;
        if (individualsWithVariant[i] == 1) nAa++;
        if (individualsWithVariant[i] == 2) nAA++;
    }
    
    // Get the observed proportions of alt-hom and hets
    double pAA = (double)nAA / individualsWithVariant.size();
    double pAa = (double)nAa / individualsWithVariant.size();
    // Allele frequencies
    double p = pAA + (0.5 * pAa);
    double q = 1 - p;
    // Get the Hardy-Weinberg prediction for expected proportion of heterozygotes:
    double HWAa = 2*p*q;
    
    // Get the expected numbers of genotypes:
    double expAA = pow(p, 2) * individualsWithVariant.size();
    double expAa = HWAa * individualsWithVariant.size();
    double expaa = pow(q, 2) * individualsWithVariant.size();
    
    // Perform the chi-Sqared test
    std::vector<double> observed{static_cast<double>(nAA),static_cast<double>(nAa),static_cast<double>(naa)};
    std::vector<double> expected{expAA,expAa,expaa};
    double pVal = pearson_chi_sq_goodness_of_fit(observed, expected, 1);
    return pVal;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0xf8, %rsp
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
movq %rdx, %rax
subq %rcx, %rax
sarq $0x2, %rax
cmpq %rcx, %rdx
je 0x509e9
xorl %edx, %edx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
movl (%rcx,%r9,4), %r10d
cmpl $0x1, %r10d
adcl $0x0, %edx
xorl %r11d, %r11d
cmpl $0x1, %r10d
sete %r11b
addl %r11d, %esi
xorl %r11d, %r11d
cmpl $0x2, %r10d
sete %r11b
addl %r11d, %r8d
incq %r9
cmpq %r9, %rax
jne 0x509ab
cvtsi2sd %r8d, %xmm0
cvtsi2sd %esi, %xmm1
cvtsi2sd %edx, %xmm2
jmp 0x509f5
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
movq %rax, %xmm3
punpckldq 0x7979e(%rip), %xmm3 # xmm3 = xmm3[0],mem[0],xmm3[1],mem[1]
subpd 0x797a6(%rip), %xmm3    # 0xca1b0
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
movapd %xmm0, %xmm3
divsd %xmm4, %xmm3
movapd %xmm1, %xmm5
divsd %xmm4, %xmm5
mulsd 0x7ea22(%rip), %xmm5    # 0xcf450
addsd %xmm3, %xmm5
movsd %xmm5, 0x50(%rsp)
movsd 0x7ea00(%rip), %xmm4    # 0xcf440
subsd %xmm5, %xmm4
movsd %xmm4, 0x40(%rsp)
movapd %xmm5, %xmm3
movq 0x8(%rdi), %r14
subq (%rdi), %r14
mulsd %xmm5, %xmm3
movsd %xmm3, 0x30(%rsp)
sarq $0x2, %r14
mulsd %xmm4, %xmm4
movsd %xmm4, 0xa8(%rsp)
leaq 0x90(%rsp), %rsi
movsd %xmm0, (%rsi)
movsd %xmm1, 0x8(%rsi)
movsd %xmm2, 0x10(%rsi)
leaq 0xc8(%rsp), %rdi
leaq 0xe0(%rsp), %rbx
movl $0x3, %edx
movq %rbx, %rcx
callq 0x54cd2
movq %r14, %xmm0
punpckldq 0x796f0(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x796f8(%rip), %xmm0    # 0xca1b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0xa8(%rsp), %xmm2
mulsd %xmm1, %xmm2
movsd 0x50(%rsp), %xmm0
addsd %xmm0, %xmm0
mulsd 0x40(%rsp), %xmm0
mulsd %xmm1, %xmm0
mulsd 0x30(%rsp), %xmm1
movsd %xmm1, (%rbx)
movsd %xmm0, 0x8(%rbx)
movsd %xmm2, 0x10(%rbx)
leaq 0x90(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
leaq 0x2f(%rsp), %rcx
movl $0x3, %edx
callq 0x54cd2
leaq 0x78(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3a864
leaq 0x60(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3a864
movq 0x78(%rsp), %rax
movq 0x80(%rsp), %rcx
subq %rax, %rcx
movq 0x60(%rsp), %rdx
movq 0x68(%rsp), %rsi
subq %rdx, %rsi
cmpq %rsi, %rcx
jne 0x50d57
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x50b9a
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorpd %xmm0, %xmm0
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm1
movsd (%rdx,%rsi,8), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %rcx
jne 0x50b76
jmp 0x50b9e
xorpd %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
movq %xmm0, %rax
leaq -0x1(%rax), %rcx
movabsq $0xfffffffffffff, %rdx  # imm = 0xFFFFFFFFFFFFF
cmpq %rdx, %rcx
setb %cl
movq %rax, %rdx
btrq $0x3f, %rdx
testq %rdx, %rdx
sete %sil
orb %cl, %sil
movabsq $-0x10000000000000, %rcx # imm = 0xFFF0000000000000
addq %rdx, %rcx
shrq $0x35, %rcx
cmpl $0x3ff, %ecx            # imm = 0x3FF
setb %cl
testq %rax, %rax
setns %al
andb %cl, %al
orb %sil, %al
je 0x50caa
mulsd 0x7e84b(%rip), %xmm0    # 0xcf450
movsd %xmm0, 0xc0(%rsp)
fldl 0xc0(%rsp)
fstpt 0x10(%rsp)
flds 0x793e5(%rip)           # 0xca004
fstpt (%rsp)
leaq 0x2f(%rsp), %rdx
movl $0x1, %edi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x29040
fstpt 0x30(%rsp)
fldt 0x30(%rsp)
fabs
xorpd %xmm0, %xmm0
fldl 0x86247(%rip)           # 0xd6e90
fld %st(1)
fstpt 0x50(%rsp)
fld %st(0)
fstpt 0x40(%rsp)
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jbe 0x50c84
leaq 0x7bab7(%rip), %rdi     # 0xcc71b
leaq 0x7ba3e(%rip), %rsi     # 0xcc6a9
callq 0x554ea
fldt 0x30(%rsp)
fstpl 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
fldt 0x50(%rsp)
fldt 0x40(%rsp)
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
fldt 0x30(%rsp)
fstpl 0xb0(%rsp)
ja 0x50ccd
movsd 0xb0(%rsp), %xmm0
jmp 0x50ccd
movq 0xba847(%rip), %rdi     # 0x10b4f8
leaq 0x86568(%rip), %rsi     # 0xd7220
leaq 0xe0(%rsp), %rdx
callq 0x5523c
movsd 0x7d363(%rip), %xmm0    # 0xce030
movsd %xmm0, 0x30(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x50cea
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x50d04
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x50d21
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x50d3e
movq 0xd8(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movsd 0x7e6fa(%rip), %xmm0    # 0xcf440
subsd 0x30(%rsp), %xmm0
addq $0xf8, %rsp
popq %rbx
popq %r14
retq
leaq 0x863ec(%rip), %rdi     # 0xd714a
leaq 0x86408(%rip), %rsi     # 0xd716d
leaq 0x8641d(%rip), %rcx     # 0xd7189
movl $0x99, %edx
callq 0x82c0
movq %rax, %rbx
jmp 0x50d9f
movq %rax, %rbx
jmp 0x50db9
movq %rax, %rbx
jmp 0x50dd6
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x50d9f
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x50db9
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x50dd6
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x50df3
movq 0xd8(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq %rbx, %rdi
callq 0x8840