// File: millanek[P]evo/process_vcf_utils.cpp
// Function: calculateInbreedingCoefficient(std::vector<int, std::allocator<int>>&)
double calculateInbreedingCoefficient(std::vector<int>& individualsWithVariant) {
    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 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 number of heterozygotes:
    double HWAa = 2*p*q;

    
    // Get the inbreeding coefficient
    double F = (HWAa - pAa) / HWAa;
    return F;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
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, -0x58(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
je 0x7c08a
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4d100
cmpl $0x0, (%rax)
jne 0x7c044
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 0x7c05f
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 0x7c07a
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x7c07c
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x7c00c
cvtsi2sdl -0x14(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movsd -0x88(%rbp), %xmm0
movq %rax, %xmm1
movaps 0xb20ac(%rip), %xmm2    # 0x12e160
movaps %xmm2, -0x80(%rbp)
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xb20ac(%rip), %xmm2    # 0x12e170
movaps %xmm2, -0x70(%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, -0x60(%rbp)
movq -0x8(%rbp), %rdi
callq 0x467f0
movaps -0x80(%rbp), %xmm3
movaps -0x70(%rbp), %xmm2
movsd -0x60(%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
divsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm1
movsd -0x30(%rbp), %xmm0
movsd 0xb1fd5(%rip), %xmm2    # 0x12e108
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0xb6170(%rip), %xmm0    # 0x1322b8
subsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0xb756e(%rip), %xmm0    # 0x1336c8
mulsd -0x38(%rbp), %xmm0
mulsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
divsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)