// File: /workspace/github_cpp_makefile_star2_O1/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
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r15
cmpl $0x2, 0x8(%rsi)
jne 0x5f20a
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
leaq 0x8(%rsp), %rdx
movl $0x2, (%rdx)
callq 0x565a9
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f14f
subq %rdi, %rax
shrq $0x2, %rax
movl %eax, 0x8(%rsp)
movl %eax, 0xc(%rsp)
jmp 0x5f1c4
leaq 0x8(%rsp), %rdx
movl $0x1, (%rdx)
callq 0x565a9
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f1c4
movq %r15, 0x10(%rsp)
movq $-0x1, %r15
leaq 0x4(%rsp), %r13
movq %rax, %rcx
subq 0x78(%r12), %rcx
shrq $0x2, %rcx
movl %ecx, 0xc(%rsp,%r15,4)
addq $0x4, %rax
movl $0x1, 0x4(%rsp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x565a9
movq 0x80(%r12), %rsi
incq %r15
cmpq %rsi, %rax
jne 0x5f17d
cmpq $0x3, %r15
movq 0x10(%rsp), %r15
jae 0x5f310
movslq 0x8(%rsp), %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
movq %rbx, %rdi
callq 0x3f0a2
movslq (%rax), %rax
movq (%r15), %rcx
movq %rcx, 0x10(%rsp)
leaq (%rax,%rax,2), %r13
movslq 0xc(%rsp), %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
movq %rbx, %rdi
callq 0x3f0a2
movslq (%rax), %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%r13,8), %rcx
incl (%rcx,%rax,4)
addl $-0x2, %ebp
cmpl %ebp, 0x8(%r12)
jne 0x5f301
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
leaq 0x4(%rsp), %rdx
movl $0x0, (%rdx)
callq 0x565a9
movq 0x78(%r12), %rdi
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f258
subq %rdi, %rax
shrq $0x2, %rax
movl %eax, 0x8(%rsp)
movl %eax, 0xc(%rsp)
jmp 0x5f2c5
leaq 0x4(%rsp), %rdx
movl $0x1, (%rdx)
callq 0x565a9
movq 0x80(%r12), %rsi
cmpq %rsi, %rax
je 0x5f2c5
movq %r15, %rbp
movq $-0x1, %r15
leaq 0x4(%rsp), %r13
movq %rax, %rcx
subq 0x78(%r12), %rcx
shrq $0x2, %rcx
movl %ecx, 0xc(%rsp,%r15,4)
addq $0x4, %rax
movl $0x1, 0x4(%rsp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x565a9
movq 0x80(%r12), %rsi
incq %r15
cmpq %rsi, %rax
jne 0x5f284
cmpq $0x3, %r15
movq %rbp, %r15
jae 0x5f32f
movslq 0x8(%rsp), %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
movq %rbx, %rdi
callq 0x3f0a2
movslq (%rax), %rax
movq (%r15), %r15
leaq (%rax,%rax,2), %r12
movslq 0xc(%rsp), %rsi
shlq $0x5, %rsi
addq (%r14), %rsi
movq %rbx, %rdi
callq 0x3f0a2
movslq (%rax), %rax
movq (%r15,%r12,8), %rcx
incl (%rcx,%rax,4)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x79e41(%rip), %rdi     # 0xd9158
leaq 0x79e41(%rip), %rsi     # 0xd915f
leaq 0x79e5a(%rip), %rcx     # 0xd917f
movl $0x8c, %edx
callq 0x82c0
leaq 0x79e22(%rip), %rdi     # 0xd9158
leaq 0x79e22(%rip), %rsi     # 0xd915f
leaq 0x79e3b(%rip), %rcx     # 0xd917f
movl $0xa7, %edx
callq 0x82c0