// File: /workspace/github_cpp_makefile_star2_O3/millanek[P]evo/process_vcf_stats_functions.cpp
// Function: diffs_between_individuals(std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>&, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>&, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>&, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>&, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>&, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>&, FilterResult&)
void diffs_between_individuals(std::vector<std::vector<double> >& diffs,std::vector<std::vector<double> >& diffs_me, std::vector<std::vector<double> >& diffs_me_bootstrap, std::vector<std::vector<double> >& diffs_Hets_vs_Homs, std::vector<std::vector<int> >& pairwise_missingness, std::vector<std::vector<int> >& pairwise_missingness_bootstrap, FilterResult& result) {
    for (std::vector<std::vector<int> >::size_type i = 0; i < diffs.size(); i++) {
        const int ind_i = result.counts.individualsWithVariant[i];
        const bool ind_i_missing = result.counts.missingGenotypesPerIndividual[i];
        for (int j = 0; j <= i; j++) {
            double diff_measure_Richard; double diff_measure_Me;
            if (ind_i_missing) {
                diff_measure_Me = 0;
                diff_measure_Richard = 0;
                pairwise_missingness[i][j]++;
                pairwise_missingness_bootstrap[i][j]++;
            } else {
                const bool ind_j_missing = result.counts.missingGenotypesPerIndividual[j];
                if (ind_j_missing) {
                    diff_measure_Me = 0;
                    diff_measure_Richard = 0;
                    pairwise_missingness[i][j]++;
                    pairwise_missingness_bootstrap[i][j]++;
                } else {
                    const int ind_j = result.counts.individualsWithVariant[j];
                    if (j < i) {
                        if (ind_i == 1 && ind_j == 1) { // a pair of individuals are both heterozygous for a variant
                            diff_measure_Richard = DIFF_WEIGHT_BOTH_HETS_RICHARD;
                            diff_measure_Me = DIFF_WEIGHT_BOTH_HETS_ME;
                            diffs_Hets_vs_Homs[i][j]++;  // Cases where both are heterozygous are added in the bottom left corner
                        } else if ((ind_i == 2 && ind_j == 0) || (ind_i == 0 && ind_j == 2)) { // one is hom for reference allele the other is hom for alternative allele
                            diff_measure_Richard = DIFF_WEIGHT_HOM_DIFFERENCE_RICHARD;
                            diff_measure_Me = DIFF_WEIGHT_HOM_DIFFERENCE_ME;
                            diffs_Hets_vs_Homs[j][i]++;  // Homozygous differences are added in the top right corner
                        }
                        // one is homozygous the other is heterozygous for alternative allele
                        else if ((ind_i == 2 && ind_j == 1) || (ind_i == 1 && ind_j == 2) || (ind_i == 0 && ind_j == 1) || (ind_i == 1 && ind_j == 0)) {
                            diff_measure_Richard = DIFF_WEIGHT_ONE_HOM_ONE_HET;
                            diff_measure_Me = DIFF_WEIGHT_ONE_HOM_ONE_HET;
                        } else {
                            diff_measure_Richard = 0;
                            diff_measure_Me = 0;
                        }   
                        diffs[i][j] = diffs[i][j] + diff_measure_Richard;
                        diffs_me[i][j] = diffs_me[i][j] + diff_measure_Me;
                        diffs_me_bootstrap[i][j] = diffs_me_bootstrap[i][j] + diff_measure_Me;
                    } else if (j == i) { // Fill in the diagonal, the number of hets
                        if (ind_i == 1) {
                            diffs[i][j]++;
                            diffs_me[i][j]++;
                            diffs_me_bootstrap[i][j]++;
                        }
                    }
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %r9, -0x8(%rsp)
movq %r8, -0x10(%rsp)
movq %rcx, -0x18(%rsp)
movq %rdx, -0x20(%rsp)
movq %rsi, -0x28(%rsp)
movq (%rdi), %rcx
movq 0x8(%rdi), %rax
movq %rcx, -0x30(%rsp)
subq %rcx, %rax
je 0x5f6e7
movq 0x38(%rsp), %rcx
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rdx
movq 0x78(%rcx), %rax
movq %rax, -0x40(%rsp)
movq 0xd8(%rcx), %rbx
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq %rdx, -0x38(%rsp)
movl $0x1, %r14d
xorl %ecx, %ecx
movsd 0x6d22e(%rip), %xmm0    # 0xcc710
movsd 0x6d236(%rip), %xmm1    # 0xcc720
movsd 0x73756(%rip), %xmm2    # 0xd2c48
movq -0x40(%rsp), %rax
movl (%rax,%rcx,4), %ebp
movl %ecx, %eax
shrl $0x6, %eax
movl $0x1, %r15d
shlq %cl, %r15
andq (%rbx,%rax,8), %r15
leaq (,%rcx,8), %rax
leaq (%rax,%rax,2), %r12
movq -0x10(%rsp), %rax
movq (%rax), %r13
addq %r12, %r13
movq -0x8(%rsp), %rax
movq (%rax), %rax
addq %r12, %rax
movq -0x30(%rsp), %rdx
leaq (%rdx,%r12), %r11
movq -0x28(%rsp), %rdx
movq (%rdx), %r8
addq %r12, %r8
movq -0x20(%rsp), %rdx
movq (%rdx), %r10
addq %r12, %r10
movq -0x18(%rsp), %rdx
movq (%rdx), %rdx
addq %rdx, %r12
xorl %esi, %esi
testq %r15, %r15
jne 0x5f56e
movl %esi, %edi
shrl $0x6, %edi
movq (%rbx,%rdi,8), %rdi
btq %rsi, %rdi
jae 0x5f58c
movq (%r13), %rdi
incl (%rdi,%rsi,4)
movq (%rax), %rdi
incl (%rdi,%rsi,4)
incq %rsi
addq $0x18, %rdx
cmpq %rsi, %r14
jne 0x5f55a
jmp 0x5f6d6
cmpq %rsi, %rcx
jbe 0x5f5bf
movq -0x40(%rsp), %rdi
movl (%rdi,%rsi,4), %edi
cmpl $0x1, %ebp
jne 0x5f5f4
cmpl $0x1, %edi
jne 0x5f5f4
movq (%r12), %rdi
movsd (%rdi,%rsi,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%rdi,%rsi,8)
movapd %xmm2, %xmm4
movapd %xmm1, %xmm3
jmp 0x5f61f
cmpl $0x1, %ebp
jne 0x5f57b
movq (%r11), %rdi
movsd (%rdi,%rsi,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%rdi,%rsi,8)
movq (%r8), %rdi
movsd (%rdi,%rsi,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%rdi,%rsi,8)
movq (%r10), %rdi
movsd (%rdi,%rsi,8), %xmm3
addsd %xmm0, %xmm3
jmp 0x5f645
cmpl $0x2, %ebp
jne 0x5f5fd
testl %edi, %edi
je 0x5f606
testl %ebp, %ebp
jne 0x5f64f
cmpl $0x2, %edi
jne 0x5f64f
movq (%rdx), %rdi
movsd (%rdi,%rcx,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%rdi,%rcx,8)
movapd %xmm2, %xmm4
movapd %xmm0, %xmm3
movq (%r11), %rdi
addsd (%rdi,%rsi,8), %xmm4
movsd %xmm4, (%rdi,%rsi,8)
movq (%r8), %rdi
movsd (%rdi,%rsi,8), %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%rdi,%rsi,8)
movq (%r10), %rdi
addsd (%rdi,%rsi,8), %xmm3
movsd %xmm3, (%rdi,%rsi,8)
jmp 0x5f57b
cmpl $0x2, %edi
sete -0x42(%rsp)
testl %edi, %edi
sete -0x45(%rsp)
cmpl $0x1, %edi
sete %dil
testl %ebp, %ebp
sete -0x44(%rsp)
cmpl $0x2, %ebp
sete -0x41(%rsp)
cmpl $0x1, %ebp
sete %r9b
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
movb %dil, -0x43(%rsp)
testb %dil, -0x41(%rsp)
jne 0x5f61f
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
andb %r9b, -0x42(%rsp)
jne 0x5f61f
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
movb -0x44(%rsp), %dil
andb -0x43(%rsp), %dil
jne 0x5f61f
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
andb -0x45(%rsp), %r9b
jne 0x5f61f
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
jmp 0x5f61f
incq %rcx
incq %r14
cmpq -0x38(%rsp), %rcx
jne 0x5f4f2
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq