// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
subq $0x100, %rsp            # imm = 0x100
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
movq %rdx, %rax
subq %rcx, %rax
sarq $0x2, %rax
pxor %xmm0, %xmm0
cmpq %rcx, %rdx
je 0x4da0a
xorl %edx, %edx
movq 0x82757(%rip), %xmm1    # 0xd0130
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
cmpl $0x1, %edi
adcl $0x0, %edx
movd %edi, %xmm2
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
pcmpeqd %xmm1, %xmm2
psubd %xmm2, %xmm0
incq %rsi
cmpq %rsi, %rax
jne 0x4d9db
cvtdq2pd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
jmp 0x4da0e
xorpd %xmm1, %xmm1
movq %rax, %xmm2
punpckldq 0x7b695(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x7b69d(%rip), %xmm2    # 0xc90c0
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addpd %xmm2, %xmm3
movapd %xmm3, 0x30(%rsp)
movdqa %xmm0, %xmm2
divsd %xmm3, %xmm2
movdqa %xmm0, %xmm4
punpckhqdq %xmm0, %xmm4    # xmm4 = xmm4[1],xmm0[1]
divsd %xmm3, %xmm4
mulsd 0x7eccf(%rip), %xmm4    # 0xcc720
addsd %xmm2, %xmm4
movapd %xmm4, 0xe0(%rsp)
movsd 0x7ecaa(%rip), %xmm2    # 0xcc710
subsd %xmm4, %xmm2
movapd %xmm2, 0xf0(%rsp)
mulsd %xmm2, %xmm2
movapd %xmm2, 0xd0(%rsp)
leaq 0x40(%rsp), %rsi
movdqa %xmm0, (%rsi)
movsd %xmm1, 0x10(%rsi)
leaq 0x98(%rsp), %rdi
leaq 0xb0(%rsp), %rbx
movl $0x3, %edx
movq %rbx, %rcx
callq 0x51cd6
movapd 0x30(%rsp), %xmm2
movapd 0xd0(%rsp), %xmm4
mulsd %xmm2, %xmm4
movapd 0xe0(%rsp), %xmm3
movapd %xmm3, %xmm0
addsd %xmm3, %xmm0
movapd %xmm3, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
unpcklpd 0xf0(%rsp), %xmm3       # xmm3 = xmm3[0],mem[0]
mulpd %xmm1, %xmm3
movapd %xmm2, %xmm0
unpcklpd %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
mulpd %xmm3, %xmm0
movapd %xmm0, (%rbx)
movsd %xmm4, 0x10(%rbx)
leaq 0x40(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x2f(%rsp), %rcx
movl $0x3, %edx
callq 0x51cd6
leaq 0x70(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x37978
leaq 0x58(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x37978
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
subq %rax, %rcx
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rsi
subq %rdx, %rsi
cmpq %rsi, %rcx
jne 0x4dd0e
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x4db91
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 0x4db6d
jmp 0x4db95
xorpd %xmm0, %xmm0
movsd %xmm0, 0xb0(%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 0x4dc69
mulsd 0x7eb28(%rip), %xmm0    # 0xcc720
movsd %xmm0, 0x90(%rsp)
fldl 0x90(%rsp)
fstpt 0x10(%rsp)
flds 0x7b3f2(%rip)           # 0xc9004
fstpt (%rsp)
leaq 0x2f(%rsp), %rdx
movl $0x1, %edi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x52504
fstpt 0x30(%rsp)
fldt 0x30(%rsp)
fabs
fldl 0x7e0e8(%rip)           # 0xcbd20
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jbe 0x4dc53
leaq 0x82b42(%rip), %rdi     # 0xd0789
leaq 0x7d81b(%rip), %rsi     # 0xcb469
callq 0x530c4
fldt 0x30(%rsp)
fstpl 0x88(%rsp)
movsd 0x88(%rsp), %xmm0
jmp 0x4dc8c
movq 0xb4868(%rip), %rdi     # 0x1024d8
leaq 0x82ad5(%rip), %rsi     # 0xd074c
leaq 0xb0(%rsp), %rdx
callq 0x52256
movsd 0x7e0ac(%rip), %xmm0    # 0xcbd38
movsd %xmm0, 0x30(%rsp)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4dca9
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x4dcc3
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x4dcda
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x4dcf7
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movsd 0x7ea11(%rip), %xmm0    # 0xcc710
subsd 0x30(%rsp), %xmm0
addq $0x100, %rsp            # imm = 0x100
popq %rbx
retq
leaq 0x82961(%rip), %rdi     # 0xd0676
leaq 0x8297d(%rip), %rsi     # 0xd0699
leaq 0x82992(%rip), %rcx     # 0xd06b5
movl $0x99, %edx
callq 0x82c0
movq %rax, %rbx
jmp 0x4dd56
movq %rax, %rbx
jmp 0x4dd70
movq %rax, %rbx
jmp 0x4dd87
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4dd56
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x4dd70
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x4dd87
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x4dda4
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x8480
movq %rbx, %rdi
callq 0x8840