// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x48, %rsp
movq %r8, %r14
movq %rcx, 0x28(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x80(%rsp), %rax
addq $0xd8, %rax
movq %rax, 0x40(%rsp)
pushq $0x1
popq %r12
xorl %ebx, %ebx
movq %r9, 0x20(%rsp)
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rax, %rbx
jae 0x4c29f
movq 0x80(%rsp), %rax
movq 0x78(%rax), %rax
movl (%rax,%rbx,4), %eax
movl %eax, 0xc(%rsp)
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x439c0
movq 0x20(%rsp), %r9
movq %rdx, %r13
andq (%rax), %r13
movq %rbx, 0x18(%rsp)
imulq $0x18, %rbx, %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x4c28f
testq %r13, %r13
je 0x4c07a
movq (%r14), %rax
movq (%rax,%r15), %rax
incl (%rax,%rbp,4)
jmp 0x4c09b
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x439c0
testq %rdx, (%rax)
je 0x4c0ae
movq (%r14), %rax
movq (%rax,%r15), %rax
incl (%rax,%rbp,4)
movq 0x20(%rsp), %r9
movq (%r9), %rax
movq (%rax,%r15), %rax
incl (%rax,%rbp,4)
incq %rbp
addq $0x18, %rbx
jmp 0x4c060
cmpq %rbp, 0x18(%rsp)
movq 0x20(%rsp), %r9
jbe 0x4c110
movq 0x80(%rsp), %rax
movq 0x78(%rax), %rax
movl (%rax,%rbp,4), %ecx
cmpl $0x1, 0xc(%rsp)
jne 0x4c16d
cmpl $0x1, %ecx
jne 0x4c16d
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
movsd (%rax,%rbp,8), %xmm0
addsd 0x4b4fa(%rip), %xmm0    # 0x975f0
movsd %xmm0, (%rax,%rbp,8)
movsd 0x55285(%rip), %xmm1    # 0xa1388
movsd 0x4b4f5(%rip), %xmm0    # 0x97600
jmp 0x4c1bb
cmpl $0x1, 0xc(%rsp)
jne 0x4c0a5
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
movsd (%rax,%rbp,8), %xmm0
movsd 0x4b4c0(%rip), %xmm1    # 0x975f0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rbp,8)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
movsd (%rax,%rbp,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rbp,8)
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
movsd (%rax,%rbp,8), %xmm0
addsd %xmm1, %xmm0
jmp 0x4c1fc
cmpl $0x2, 0xc(%rsp)
jne 0x4c178
testl %ecx, %ecx
je 0x4c188
cmpl $0x0, 0xc(%rsp)
jne 0x4c206
cmpl $0x2, %ecx
jne 0x4c206
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbx), %rax
movq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x4b44a(%rip), %xmm2    # 0x975f0
addsd %xmm2, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movsd 0x551d1(%rip), %xmm1    # 0xa1388
movapd %xmm2, %xmm0
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
addsd (%rax,%rbp,8), %xmm1
movsd %xmm1, (%rax,%rbp,8)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
movsd (%rax,%rbp,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rbp,8)
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
addsd (%rax,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
jmp 0x4c0a5
cmpl $0x2, %ecx
sete %dil
testl %ecx, %ecx
sete %al
cmpl $0x1, %ecx
sete %dl
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
sete %sil
cmpl $0x2, %ecx
sete %r8b
cmpl $0x1, %ecx
sete %cl
movsd 0x4b3c9(%rip), %xmm0    # 0x97600
movapd %xmm0, %xmm1
testb %dl, %r8b
jne 0x4c1bb
movsd 0x4b3b4(%rip), %xmm0    # 0x97600
movapd %xmm0, %xmm1
andb %cl, %dil
jne 0x4c1bb
movsd 0x4b39f(%rip), %xmm0    # 0x97600
movapd %xmm0, %xmm1
andb %dl, %sil
jne 0x4c1bb
movsd 0x4b38a(%rip), %xmm0    # 0x97600
movapd %xmm0, %xmm1
andb %al, %cl
jne 0x4c1bb
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
jmp 0x4c1bb
movq 0x18(%rsp), %rbx
incq %rbx
incq %r12
jmp 0x4c00b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq