// File: 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 %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movq %rax, %rcx
movq -0x140(%rbp), %rax
cmpq %rcx, %rax
je 0x7c243
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4d100
cmpl $0x0, (%rax)
jne 0x7c1fa
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4d100
cmpl $0x1, (%rax)
jne 0x7c215
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4d100
cmpl $0x2, (%rax)
jne 0x7c230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x7c232
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x7c1bc
cvtsi2sdl -0x14(%rbp), %xmm0
movsd %xmm0, -0x198(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movsd -0x198(%rbp), %xmm0
movq %rax, %xmm1
movaps 0xb1ef3(%rip), %xmm2    # 0x12e160
movaps %xmm2, -0x170(%rbp)
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xb1ef0(%rip), %xmm2    # 0x12e170
movaps %xmm2, -0x160(%rbp)
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
cvtsi2sdl -0x10(%rbp), %xmm0
movsd %xmm0, -0x190(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movsd -0x190(%rbp), %xmm0
movaps -0x170(%rbp), %xmm3
movaps -0x160(%rbp), %xmm2
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
movsd 0xb1e0a(%rip), %xmm2    # 0x12e108
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm1
movsd 0xb5fa0(%rip), %xmm0    # 0x1322b8
subsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x38(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd -0x40(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x38(%rbp), %xmm0
movl $0x2, %edi
movl %edi, -0x174(%rbp)
callq 0x835c0
movsd %xmm0, -0x188(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movsd -0x188(%rbp), %xmm0
movaps -0x170(%rbp), %xmm3
movaps -0x160(%rbp), %xmm2
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movsd -0x180(%rbp), %xmm0
movl -0x174(%rbp), %edi
movaps -0x170(%rbp), %xmm3
movaps -0x160(%rbp), %xmm2
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x835c0
movsd %xmm0, -0x150(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movaps -0x170(%rbp), %xmm3
movaps -0x160(%rbp), %xmm2
movsd -0x150(%rbp), %xmm0
movq %rax, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
cvtsi2sdl -0x14(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
cvtsi2sdl -0x10(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
cvtsi2sdl -0xc(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0x88(%rbp)
movq $0x3, -0x80(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x484d0
movq -0x148(%rbp), %rcx
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x835f0
jmp 0x7c4a7
leaq -0xa1(%rbp), %rdi
callq 0x4a130
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xf8(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xf0(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
leaq -0xf8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0x3, -0xd8(%rbp)
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x484d0
movq -0x1a0(%rbp), %rcx
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x835f0
jmp 0x7c529
leaq -0xf9(%rbp), %rdi
callq 0x4a130
leaq -0x120(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x53120
jmp 0x7c547
leaq -0x138(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x53120
jmp 0x7c55c
leaq -0x120(%rbp), %rdi
leaq -0x138(%rbp), %rsi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x83670
movsd %xmm0, -0x1a8(%rbp)
jmp 0x7c580
leaq -0x138(%rbp), %rdi
callq 0x3ecb0
leaq -0x120(%rbp), %rdi
callq 0x3ecb0
movsd -0x1a8(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x1b0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3ecb0
leaq -0x78(%rbp), %rdi
callq 0x3ecb0
movsd -0x1b0(%rbp), %xmm0
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xa1(%rbp), %rdi
callq 0x4a130
jmp 0x7c688
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xf9(%rbp), %rdi
callq 0x4a130
jmp 0x7c67f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x7c673
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x7c667
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x3ecb0
leaq -0x120(%rbp), %rdi
callq 0x3ecb0
leaq -0xd0(%rbp), %rdi
callq 0x3ecb0
leaq -0x78(%rbp), %rdi
callq 0x3ecb0
movq -0xb0(%rbp), %rdi
callq 0xab50
nopw %cs:(%rax,%rax)