// File: /workspace/github_cpp_makefile_star2_O1/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 0x5f62a
movq 0x38(%rsp), %rcx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rdx
movq 0x78(%rcx), %rax
movq %rax, -0x48(%rsp)
movq 0xd8(%rcx), %r14
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq %rdx, -0x38(%rsp)
xorl %edx, %edx
leaq 0x3f(%rsi), %rax
movq %rax, -0x40(%rsp)
movsd 0x70069(%rip), %xmm0    # 0xcf440
movsd 0x70071(%rip), %xmm1    # 0xcf450
movsd 0x79d21(%rip), %xmm2    # 0xd9108
leaq 0x3f(%rdx), %rax
testq %rdx, %rdx
cmovnsq %rdx, %rax
sarq $0x6, %rax
movq %rdx, %rcx
andq -0x40(%rsp), %rcx
xorl %esi, %esi
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
cmpq %rdi, %rcx
setbe %sil
movl $0x1, %r12d
movl %edx, %ecx
shlq %cl, %r12
leaq (%r14,%rax,8), %rax
andq -0x8(%rax,%rsi,8), %r12
movq -0x48(%rsp), %rax
movl (%rax,%rdx,4), %ebp
leaq (,%rdx,8), %rax
leaq (%rax,%rax,2), %r13
movq -0x10(%rsp), %rax
movq (%rax), %rdi
addq %r13, %rdi
movq -0x8(%rsp), %rax
movq (%rax), %rax
addq %r13, %rax
movq -0x30(%rsp), %rcx
leaq (%rcx,%r13), %r9
movq -0x28(%rsp), %rcx
movq (%rcx), %r8
addq %r13, %r8
movq -0x20(%rsp), %rcx
movq (%rcx), %r10
addq %r13, %r10
movq -0x18(%rsp), %rcx
movq (%rcx), %rsi
addq %rsi, %r13
leaq 0x1(%rdx), %rcx
xorl %r15d, %r15d
testq %r12, %r12
jne 0x5f496
movq %r15, %r11
shrq $0x6, %r11
movq (%r14,%r11,8), %r11
btq %r15, %r11
jae 0x5f4b5
movq (%rdi), %r11
incl (%r11,%r15,4)
movq (%rax), %r11
incl (%r11,%r15,4)
incq %r15
addq $0x18, %rsi
cmpq %r15, %rcx
jne 0x5f480
jmp 0x5f61c
cmpq %r15, %rdx
jbe 0x5f4ec
movq -0x48(%rsp), %r11
movl (%r11,%r15,4), %r11d
cmpl $0x1, %ebp
jne 0x5f536
cmpl $0x1, %r11d
jne 0x5f536
movq (%r13), %r11
movsd (%r11,%r15,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%r11,%r15,8)
movapd %xmm2, %xmm4
movapd %xmm1, %xmm3
jmp 0x5f565
cmpl $0x1, %ebp
sete %r11b
cmpq %r15, %rdx
sete %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0x5f4a4
movq (%r9), %r11
movsd (%r11,%r15,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%r11,%r15,8)
movq (%r8), %r11
movsd (%r11,%r15,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%r11,%r15,8)
movq (%r10), %r11
movsd (%r11,%r15,8), %xmm3
addsd %xmm0, %xmm3
jmp 0x5f590
cmpl $0x2, %ebp
jne 0x5f540
testl %r11d, %r11d
je 0x5f54a
testl %ebp, %ebp
jne 0x5f59b
cmpl $0x2, %r11d
jne 0x5f59b
movq (%rsi), %r11
movsd (%r11,%rdx,8), %xmm3
addsd %xmm0, %xmm3
movsd %xmm3, (%r11,%rdx,8)
movapd %xmm2, %xmm4
movapd %xmm0, %xmm3
movq (%r9), %r11
addsd (%r11,%r15,8), %xmm4
movsd %xmm4, (%r11,%r15,8)
movq (%r8), %r11
movsd (%r11,%r15,8), %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%r11,%r15,8)
movq (%r10), %r11
addsd (%r11,%r15,8), %xmm3
movsd %xmm3, (%r11,%r15,8)
jmp 0x5f4a4
cmpl $0x2, %r11d
sete -0x4a(%rsp)
testl %r11d, %r11d
sete -0x4c(%rsp)
cmpl $0x1, %r11d
sete %bl
testl %ebp, %ebp
sete -0x4b(%rsp)
cmpl $0x2, %ebp
sete -0x49(%rsp)
cmpl $0x1, %ebp
sete %r11b
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
testb %bl, -0x49(%rsp)
jne 0x5f565
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
andb %r11b, -0x4a(%rsp)
jne 0x5f565
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
andb %bl, -0x4b(%rsp)
jne 0x5f565
movapd %xmm1, %xmm4
movapd %xmm1, %xmm3
andb -0x4c(%rsp), %r11b
jne 0x5f565
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
jmp 0x5f565
movq %rcx, %rdx
cmpq -0x38(%rsp), %rcx
jne 0x5f3e7
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq