// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x8(%rbp), %rdi
callq 0x651b0
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
jae 0x9cd34
movq 0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
movq -0x38(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq 0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0xd0, %rdi
movq -0x38(%rbp), %rsi
callq 0x83940
movq %rax, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x481b0
andb $0x1, %al
movb %al, -0x3d(%rbp)
movl $0x0, -0x54(%rbp)
movslq -0x54(%rbp), %rax
cmpq -0x38(%rbp), %rax
ja 0x9cd21
testb $0x1, -0x3d(%rbp)
je 0x9c9ae
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x705d0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x4d100
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x705d0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x4d100
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x9cd11
movq 0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0xd0, %rdi
movslq -0x54(%rbp), %rsi
callq 0x83940
movq %rax, -0x80(%rbp)
movq %rdx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x481b0
andb $0x1, %al
movb %al, -0x69(%rbp)
testb $0x1, -0x69(%rbp)
je 0x9ca37
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x705d0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x4d100
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x705d0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x4d100
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x9cd0f
movq 0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
movslq -0x54(%rbp), %rsi
callq 0x4d100
movl (%rax), %eax
movl %eax, -0x84(%rbp)
movslq -0x54(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x9cc7a
cmpl $0x1, -0x3c(%rbp)
jne 0x9cab9
cmpl $0x1, -0x84(%rbp)
jne 0x9cab9
movsd 0xa0baf(%rip), %xmm0    # 0x13d628
movsd %xmm0, -0x60(%rbp)
movsd 0x91682(%rip), %xmm0    # 0x12e108
movsd %xmm0, -0x68(%rbp)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd 0x9580c(%rip), %xmm0    # 0x1322b8
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x9cb88
cmpl $0x2, -0x3c(%rbp)
jne 0x9cac8
cmpl $0x0, -0x84(%rbp)
je 0x9cad7
cmpl $0x0, -0x3c(%rbp)
jne 0x9cb1c
cmpl $0x2, -0x84(%rbp)
jne 0x9cb1c
movsd 0xa0b49(%rip), %xmm0    # 0x13d628
movsd %xmm0, -0x60(%rbp)
movsd 0x957cc(%rip), %xmm0    # 0x1322b8
movsd %xmm0, -0x68(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x54(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x3ec30
movsd 0x957a6(%rip), %xmm0    # 0x1322b8
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x9cb86
cmpl $0x2, -0x3c(%rbp)
jne 0x9cb2b
cmpl $0x1, -0x84(%rbp)
je 0x9cb58
cmpl $0x1, -0x3c(%rbp)
jne 0x9cb3a
cmpl $0x2, -0x84(%rbp)
je 0x9cb58
cmpl $0x0, -0x3c(%rbp)
jne 0x9cb49
cmpl $0x1, -0x84(%rbp)
je 0x9cb58
cmpl $0x1, -0x3c(%rbp)
jne 0x9cb74
cmpl $0x0, -0x84(%rbp)
jne 0x9cb74
movsd 0x915a8(%rip), %xmm0    # 0x12e108
movsd %xmm0, -0x60(%rbp)
movsd 0x9159b(%rip), %xmm0    # 0x12e108
movsd %xmm0, -0x68(%rbp)
jmp 0x9cb84
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
jmp 0x9cb86
jmp 0x9cb88
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd (%rax), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd (%rax), %xmm0
addsd -0x68(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd (%rax), %xmm0
addsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd -0x98(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x9cd0d
movslq -0x54(%rbp), %rax
cmpq -0x38(%rbp), %rax
jne 0x9cd0b
cmpl $0x1, -0x3c(%rbp)
jne 0x9cd09
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd 0x95609(%rip), %xmm0    # 0x1322b8
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd 0x955e0(%rip), %xmm0    # 0x1322b8
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x651e0
movq %rax, %rdi
movslq -0x54(%rbp), %rsi
callq 0x3ec30
movsd 0x955b7(%rip), %xmm0    # 0x1322b8
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x9cd0b
jmp 0x9cd0d
jmp 0x9cd0f
jmp 0x9cd11
jmp 0x9cd13
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x9c945
jmp 0x9cd23
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x9c8cf
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)