// File: 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
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rcx, -0x198(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x9c544
leaq -0x38(%rbp), %rdi
callq 0x9d790
leaq -0x40(%rbp), %rdi
callq 0x9d790
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x58(%rbp)
movl $0x2, -0x5c(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
callq 0x9d7b0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x68(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x705f0
testb $0x1, %al
jne 0x9c2eb
jmp 0x9c329
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x70630
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x9c4b9
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x98(%rbp)
movl $0x1, -0x9c(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
leaq -0x9c(%rbp), %rdx
callq 0x9d7b0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0xa8(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x705f0
testb $0x1, %al
jne 0x9c3ca
jmp 0x9c48d
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0xb8(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x70630
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %ecx
movslq -0xa0(%rbp), %rax
movl %ecx, -0x30(%rbp,%rax,4)
leaq -0x40(%rbp), %rdi
callq 0x70960
movq -0x40(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0xd8(%rbp)
movl $0x1, -0xdc(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
leaq -0xdc(%rbp), %rdx
callq 0x9d7b0
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x9c399
cmpl $0x3, -0xa0(%rbp)
jg 0x9c498
jmp 0x9c4b7
leaq 0xa11d9(%rip), %rdi     # 0x13d678
leaq 0xa11d9(%rip), %rsi     # 0x13d67f
movl $0x8c, %edx
leaq 0xa11ed(%rip), %rcx     # 0x13d69f
callq 0xa390
jmp 0x9c4b9
movq -0x198(%rbp), %rdi
movq -0x8(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movslq -0x30(%rbp), %rsi
callq 0x52960
movq -0x1b8(%rbp), %rdi
movq %rax, %rsi
callq 0x59b00
movq -0x1b0(%rbp), %rdi
movslq (%rax), %rsi
callq 0x705d0
movq -0x198(%rbp), %rdi
movq %rax, -0x1a0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movslq -0x2c(%rbp), %rsi
callq 0x52960
movq -0x1a8(%rbp), %rdi
movq %rax, %rsi
callq 0x59b00
movq -0x1a0(%rbp), %rdi
movslq (%rax), %rsi
callq 0x4d100
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl -0x14(%rbp), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jne 0x9c88e
leaq -0xe8(%rbp), %rdi
callq 0x9d790
leaq -0xf0(%rbp), %rdi
callq 0x9d790
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x108(%rbp)
movl $0x0, -0x10c(%rbp)
movq -0x100(%rbp), %rdi
movq -0x108(%rbp), %rsi
leaq -0x10c(%rbp), %rdx
callq 0x9d7b0
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x118(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x705f0
testb $0x1, %al
jne 0x9c60b
jmp 0x9c661
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0x128(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x128(%rbp), %rdi
movq -0x130(%rbp), %rsi
callq 0x70630
movl %eax, -0x11c(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x9c803
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0x140(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x148(%rbp)
movl $0x1, -0x14c(%rbp)
movq -0x140(%rbp), %rdi
movq -0x148(%rbp), %rsi
leaq -0x14c(%rbp), %rdx
callq 0x9d7b0
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl $0x0, -0x150(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x158(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x705f0
testb $0x1, %al
jne 0x9c708
jmp 0x9c7d7
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46a90
movq %rax, -0x168(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x168(%rbp), %rdi
movq -0x170(%rbp), %rsi
callq 0x70630
movl %eax, -0x15c(%rbp)
movl -0x15c(%rbp), %ecx
movslq -0x150(%rbp), %rax
movl %ecx, -0x30(%rbp,%rax,4)
leaq -0xf0(%rbp), %rdi
callq 0x70960
movq -0xf0(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x70, %rdi
callq 0x46ac0
movq %rax, -0x188(%rbp)
movl $0x1, -0x18c(%rbp)
movq -0x180(%rbp), %rdi
movq -0x188(%rbp), %rsi
leaq -0x18c(%rbp), %rdx
callq 0x9d7b0
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl -0x150(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x150(%rbp)
jmp 0x9c6d4
cmpl $0x3, -0x150(%rbp)
jg 0x9c7e2
jmp 0x9c801
leaq 0xa0e8f(%rip), %rdi     # 0x13d678
leaq 0xa0e8f(%rip), %rsi     # 0x13d67f
movl $0xa7, %edx
leaq 0xa0ea3(%rip), %rcx     # 0x13d69f
callq 0xa390
jmp 0x9c803
movq -0x198(%rbp), %rdi
movq -0x8(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movslq -0x30(%rbp), %rsi
callq 0x52960
movq -0x1d8(%rbp), %rdi
movq %rax, %rsi
callq 0x59b00
movq -0x1d0(%rbp), %rdi
movslq (%rax), %rsi
callq 0x705d0
movq -0x198(%rbp), %rdi
movq %rax, -0x1c0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movslq -0x2c(%rbp), %rsi
callq 0x52960
movq -0x1c8(%rbp), %rdi
movq %rax, %rsi
callq 0x59b00
movq -0x1c0(%rbp), %rdi
movslq (%rax), %rsi
callq 0x4d100
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopw (%rax,%rax)