// File: /workspace/github_cpp_makefile_star2_O3/millanek[P]evo/process_vcf_stats_functions.cpp
// Function: doubleton_analysis(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>&, FilterResult&, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, int>>>&)
void doubleton_analysis(std::vector<std::vector<int> >& doubletons, FilterResult& result, int numChromosomes, const std::vector<std::string> p, std::map<std::string,int>& p_int_map) {
#ifdef TESTING
    std::cerr << "Overall count: " << result.counts.overall << std::endl;
#endif
    int fields[2];
    
    if (result.counts.overall == 2) {
        std::vector<int>::iterator it_hom;
        std::vector<int>::iterator it_het;
        // Check if the doubleton is in a single homozygous individual
        it_hom = std::find(result.counts.individualsWithVariant.begin(), result.counts.individualsWithVariant.end(), 2);
        if (it_hom != result.counts.individualsWithVariant.end()) {
            int doubleton_hom = (int)std::distance(result.counts.individualsWithVariant.begin(), it_hom);
            fields[0] = doubleton_hom; fields[1] = doubleton_hom;
        } else {
            // Otherwise look at which two individuals have the two heterozygous variants
            it_het = std::find(result.counts.individualsWithVariant.begin(), result.counts.individualsWithVariant.end(), 1);
            int i = 0;
            while (it_het != result.counts.individualsWithVariant.end()) {
                int doubleton_het = (int)std::distance(result.counts.individualsWithVariant.begin(), it_het);
                fields[i] = doubleton_het;
                ++it_het;
                it_het = std::find(it_het, result.counts.individualsWithVariant.end(), 1);
                ++i;
            }
            assert(i <= 3);
        }
#ifdef TESTING
        std::cerr << "Fields 0: " << fields[0] << " Fields 1: " << fields[1] << std::endl;
        std::cerr << "pop1: " << p[fields[0]] << " pop 2: " << p[fields[1]] << std::endl;
#endif
        doubletons[p_int_map[p[fields[0]]]][p_int_map[p[fields[1]]]]++;
    }
    if (result.counts.overall == (numChromosomes - 2)) {
        std::vector<int>::iterator it_hom;
        std::vector<int>::iterator it_het;
        // Check if the doubleton is in a single homozygous individual
        it_hom = std::find(result.counts.individualsWithVariant.begin(), result.counts.individualsWithVariant.end(), 0);
        if (it_hom != result.counts.individualsWithVariant.end()) {
            int doubleton_hom = (int)std::distance(result.counts.individualsWithVariant.begin(), it_hom);
            fields[0] = doubleton_hom; fields[1] = doubleton_hom;
        } else {
            // Otherwise look at which two individuals have the two heterozygous variants
            it_het = std::find(result.counts.individualsWithVariant.begin(), result.counts.individualsWithVariant.end(), 1);
            int i = 0;
            while (it_het != result.counts.individualsWithVariant.end()) {
                int doubleton_het = (int)std::distance(result.counts.individualsWithVariant.begin(), it_het);
                fields[i] = doubleton_het;
                ++it_het;
                it_het = std::find(it_het, result.counts.individualsWithVariant.end(), 1);
                ++i;
            }
            assert(i <= 3);
        }
#ifdef TESTING
        std::cerr << "Fields 0: " << fields[0] << " Fields 1: " << fields[1] << std::endl;
        std::cerr << "pop1: " << p[fields[0]] << " pop 2: " << p[fields[1]] << std::endl;
#endif
        doubletons[p_int_map[p[fields[0]]]][p_int_map[p[fields[1]]]]++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rsi, %r12
movq %rdi, %rbp
movl 0x8(%rsi), %eax
cmpl $0x2, %eax
jne 0x5f32e
movl %edx, 0xc(%rsp)
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
leaq 0x4(%rsp), %rdx
movl $0x2, (%rdx)
callq 0x56766
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f271
movq %rax, %r13
subq %rdi, %r13
shrq $0x2, %r13
movl %r13d, 0x4(%rsp)
movl %r13d, 0x8(%rsp)
movl %r13d, %eax
jmp 0x5f2e2
leaq 0x4(%rsp), %rdx
movl $0x1, (%rdx)
callq 0x56766
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f2d9
movq $-0x1, %r15
movq %rsp, %r13
movq %rax, %rcx
subq 0x78(%r12), %rcx
shrq $0x2, %rcx
movl %ecx, 0x8(%rsp,%r15,4)
addq $0x4, %rax
movl $0x1, (%rsp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x56766
movq 0x80(%r12), %rsi
incq %r15
cmpq %rsi, %rax
jne 0x5f298
cmpq $0x2, %r15
ja 0x5f428
movl 0x4(%rsp), %eax
movl 0x8(%rsp), %r13d
movslq %eax, %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
movq %rbx, %rdi
callq 0x3c182
movslq (%rax), %rax
movq (%rbp), %rcx
movq %rcx, 0x10(%rsp)
leaq (%rax,%rax,2), %r15
movslq %r13d, %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
movq %rbx, %rdi
callq 0x3c182
movslq (%rax), %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%r15,8), %rcx
incl (%rcx,%rax,4)
movl 0x8(%r12), %eax
movl 0xc(%rsp), %edx
addl $-0x2, %edx
cmpl %edx, %eax
jne 0x5f419
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
movq %rsp, %rdx
movl $0x0, (%rdx)
callq 0x56766
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f375
movq %rax, %r13
subq %rdi, %r13
shrq $0x2, %r13
movl %r13d, %eax
jmp 0x5f3e0
movq %rsp, %rdx
movl $0x1, (%rdx)
callq 0x56766
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f3d7
movq $-0x1, %r15
movq %rsp, %r13
movq %rax, %rcx
subq 0x78(%r12), %rcx
shrq $0x2, %rcx
movl %ecx, 0x8(%rsp,%r15,4)
addq $0x4, %rax
movl $0x1, (%rsp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x56766
movq 0x80(%r12), %rsi
incq %r15
cmpq %rsi, %rax
jne 0x5f39a
cmpq $0x2, %r15
ja 0x5f447
movl 0x4(%rsp), %eax
movl 0x8(%rsp), %r13d
movslq %eax, %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
movq %rbx, %rdi
callq 0x3c182
movslq (%rax), %rax
movq %rbx, %rdi
movq (%rbp), %rbx
leaq (%rax,%rax,2), %r15
movslq %r13d, %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
callq 0x3c182
movslq (%rax), %rax
movq (%rbx,%r15,8), %rcx
incl (%rcx,%rax,4)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x73869(%rip), %rdi     # 0xd2c98
leaq 0x73869(%rip), %rsi     # 0xd2c9f
leaq 0x73882(%rip), %rcx     # 0xd2cbf
movl $0x8c, %edx
callq 0x82c0
leaq 0x7384a(%rip), %rdi     # 0xd2c98
leaq 0x7384a(%rip), %rsi     # 0xd2c9f
leaq 0x73863(%rip), %rcx     # 0xd2cbf
movl $0xa7, %edx
callq 0x82c0