// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/process_vcf_utils.cpp
// Function: getThreeSetVariantCountsAA4(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>>>> const&, std::vector<unsigned long, std::allocator<unsigned long>> const&, std::vector<unsigned long, std::allocator<unsigned long>> const&, std::vector<unsigned long, std::allocator<unsigned long>> const&, std::vector<unsigned long, std::allocator<unsigned long>> const&)
ThreeSetCounts getThreeSetVariantCountsAA4(const std::vector<std::string>& fields, const std::vector<size_t>& set1_loci, const std::vector<size_t>& set2_loci, const std::vector<size_t>& set3_loci, const std::vector<size_t>& AA_loci) {
    ThreeSetCounts thisVariantCounts;
    int AAaltCount = 0;
    thisVariantCounts.individualsWithVariant.assign((fields.size()-NUM_NON_GENOTYPE_COLUMNS),0);
    // std::cerr << fields[0] << "\t" << fields[1] << std::endl;
    for (std::vector<std::string>::size_type i = NUM_NON_GENOTYPE_COLUMNS; i != fields.size(); i++) {
        if (fields[i][0] == '1') {
            thisVariantCounts.overall++;
            if (std::find(set1_loci.begin(), set1_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != set1_loci.end()) { thisVariantCounts.set1AltCount++; }
            if (std::find(set2_loci.begin(), set2_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != set2_loci.end()) { thisVariantCounts.set2AltCount++; }
            if (std::find(set3_loci.begin(), set3_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != set3_loci.end()) { thisVariantCounts.set3AltCount++; }
            if (std::find(AA_loci.begin(), AA_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != AA_loci.end()) { AAaltCount++; }
            thisVariantCounts.individualsWithVariant[i- NUM_NON_GENOTYPE_COLUMNS]++;
        }
        if (fields[i][2] == '1') {
            thisVariantCounts.overall++;
            if (std::find(set1_loci.begin(), set1_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != set1_loci.end()) { thisVariantCounts.set1AltCount++; }
            if (std::find(set2_loci.begin(), set2_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != set2_loci.end()) { thisVariantCounts.set2AltCount++; }
            if (std::find(set3_loci.begin(), set3_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != set3_loci.end()) { thisVariantCounts.set3AltCount++; }
            if (std::find(AA_loci.begin(), AA_loci.end(), i-NUM_NON_GENOTYPE_COLUMNS) != AA_loci.end()) { AAaltCount++; }
            thisVariantCounts.individualsWithVariant[i-NUM_NON_GENOTYPE_COLUMNS]++;
        }
    }
    thisVariantCounts.set1RefCount = (int)(set1_loci.size() * 2) - thisVariantCounts.set1AltCount;
    thisVariantCounts.set2RefCount = (int)(set2_loci.size() * 2) - thisVariantCounts.set2AltCount;
    thisVariantCounts.set3RefCount = (int)(set3_loci.size() * 2) - thisVariantCounts.set3AltCount;
    //thisVariantCounts.set4RefCount = (int)(set4_loci.size() * 2) - thisVariantCounts.set4AltCount;
    
    thisVariantCounts.set1AltAF = (double)thisVariantCounts.set1AltCount/(set1_loci.size() * 2);
    thisVariantCounts.set2AltAF = (double)thisVariantCounts.set2AltCount/(set2_loci.size() * 2);
    thisVariantCounts.set3AltAF = (double)thisVariantCounts.set3AltCount/(set3_loci.size() * 2);
    //thisVariantCounts.set4AltAF = (double)thisVariantCounts.set4AltCount/(set4_loci.size() * 2);
    
    // Fill in derived allele frequencies if possible
    if (AAaltCount == 0) {
        thisVariantCounts.set1daAF = (double)thisVariantCounts.set1AltCount/(set1_loci.size() * 2);
        thisVariantCounts.set2daAF = (double)thisVariantCounts.set2AltCount/(set2_loci.size() * 2);
        thisVariantCounts.set3daAF = (double)thisVariantCounts.set3AltCount/(set3_loci.size() * 2);
       // thisVariantCounts.set4daAF = (double)thisVariantCounts.set4AltCount/(set4_loci.size() * 2);
    } else if (AAaltCount == 2) {
        thisVariantCounts.set1daAF = (double)thisVariantCounts.set1RefCount/(set1_loci.size() * 2);
        thisVariantCounts.set2daAF = (double)thisVariantCounts.set2RefCount/(set2_loci.size() * 2);
        thisVariantCounts.set3daAF = (double)thisVariantCounts.set3RefCount/(set3_loci.size() * 2);
        //thisVariantCounts.set4daAF = (double)thisVariantCounts.set4RefCount/(set4_loci.size() * 2);
    } else if (AAaltCount == 1) {
    } else {
        std::cerr << "Error: Outgroup can only be one individual here" << std::endl;
        exit(1);
    }
    
    return thisVariantCounts;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %r13
movq %r8, %r12
movq %rcx, 0x18(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
callq 0x439fa
movq %rbx, 0x10(%rsp)
leaq 0x50(%rbx), %rdi
movq 0x8(%r15), %rsi
movq %r15, 0x30(%rsp)
subq (%r15), %rsi
sarq $0x5, %rsi
addq $-0x9, %rsi
leaq 0x8(%rsp), %rdx
andl $0x0, (%rdx)
movq %rdi, 0x20(%rsp)
callq 0x44dfe
xorl %ebx, %ebx
pushq $0x48
popq %rbp
xorl %r15d, %r15d
movq %r14, 0x28(%rsp)
leaq 0x9(%rbx), %rcx
movq 0x30(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rdx, %rcx
je 0x41dd2
movq (%rax,%rbp,4), %rax
cmpb $0x31, (%rax)
jne 0x41d0b
movq 0x10(%rsp), %rax
incl (%rax)
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x45ac4
cmpq 0x8(%r14), %rax
movq 0x18(%rsp), %r14
je 0x41c81
movq 0x10(%rsp), %rax
incl 0x8(%rax)
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x45ac4
cmpq 0x8(%r14), %rax
je 0x41ca5
movq 0x10(%rsp), %rax
incl 0xc(%rax)
movq (%r12), %rdi
movq 0x8(%r12), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x45ac4
movq 0x28(%rsp), %r14
cmpq 0x8(%r12), %rax
je 0x41cd1
movq 0x10(%rsp), %rax
incl 0x10(%rax)
movq (%r13), %rdi
movq 0x8(%r13), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x45ac4
xorl %ecx, %ecx
cmpq 0x8(%r13), %rax
setne %cl
movq 0x20(%rsp), %rax
movq (%rax), %rax
incl (%rax,%rbx,4)
addl %ecx, %r15d
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,4), %rax
cmpb $0x31, 0x2(%rax)
jne 0x41dc6
movq 0x10(%rsp), %rax
incl (%rax)
movq (%r14), %rdi
movq 0x8(%r14), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x45ac4
cmpq 0x8(%r14), %rax
movq 0x18(%rsp), %rcx
je 0x41d45
movq 0x10(%rsp), %rax
incl 0x8(%rax)
movq (%rcx), %rdi
movq 0x8(%rcx), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
movq %rcx, %r14
callq 0x45ac4
cmpq 0x8(%r14), %rax
je 0x41d6c
movq 0x10(%rsp), %rax
incl 0xc(%rax)
movq (%r12), %rdi
movq 0x8(%r12), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x45ac4
movq 0x28(%rsp), %r14
cmpq 0x8(%r12), %rax
je 0x41d98
movq 0x10(%rsp), %rax
incl 0x10(%rax)
movq (%r13), %rdi
movq 0x8(%r13), %rsi
movq %rbx, 0x8(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x45ac4
xorl %ecx, %ecx
cmpq 0x8(%r13), %rax
setne %cl
addl %ecx, %r15d
movq 0x20(%rsp), %rax
movq (%rax), %rax
incl (%rax,%rbx,4)
addq $0x8, %rbp
incq %rbx
jmp 0x41c24
movq 0x8(%r12), %rcx
subq (%r12), %rcx
movq %rcx, %rsi
shrq $0x2, %rsi
andl $-0x2, %esi
movq 0x10(%rsp), %rax
movl 0x10(%rax), %edx
subl %edx, %esi
movq 0x18(%rsp), %r8
movq 0x8(%r8), %xmm1
movq 0x8(%r14), %xmm0
punpcklqdq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0]
movq (%r8), %xmm1
movq (%r14), %xmm2
punpcklqdq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0]
psubq %xmm2, %xmm0
movl %esi, 0x1c(%rax)
movdqa %xmm0, %xmm3
psrlq $0x2, %xmm3
pshufd $0xe8, %xmm3, %xmm1     # xmm1 = xmm3[0,2,2,3]
pand 0x5d211(%rip), %xmm1    # 0x9f040
movq 0x8(%rax), %xmm2
psubd %xmm2, %xmm1
cvtdq2pd %xmm2, %xmm2
pand 0x5d20c(%rip), %xmm3    # 0x9f050
por 0x5d214(%rip), %xmm3    # 0x9f060
psrad $0x2, %xmm0
psrlq $0x20, %xmm0
por 0x5d212(%rip), %xmm0    # 0x9f070
movq %xmm1, 0x14(%rax)
subpd 0x5d215(%rip), %xmm0    # 0x9f080
addpd %xmm3, %xmm0
divpd %xmm0, %xmm2
movupd %xmm2, 0x20(%rax)
cvtsi2sd %edx, %xmm3
sarq $0x2, %rcx
movq %rcx, %xmm5
punpckldq 0x503b3(%rip), %xmm5 # xmm5 = xmm5[0],mem[0],xmm5[1],mem[1]
subpd 0x503bb(%rip), %xmm5    # 0x92250
movapd %xmm5, %xmm4
unpckhpd %xmm5, %xmm4            # xmm4 = xmm4[1],xmm5[1]
addsd %xmm5, %xmm4
divsd %xmm4, %xmm3
movsd %xmm3, 0x30(%rax)
testl %r15d, %r15d
je 0x41ed2
cmpl $0x1, %r15d
je 0x41edc
cmpl $0x2, %r15d
jne 0x41eeb
cvtdq2pd %xmm1, %xmm1
divpd %xmm0, %xmm1
movupd %xmm1, 0x38(%rax)
cvtsi2sd %esi, %xmm3
divsd %xmm4, %xmm3
jmp 0x41ed7
movupd %xmm2, 0x38(%rax)
movsd %xmm3, 0x48(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9b0f6(%rip), %rdi     # 0xdcfe8
leaq 0x5d340(%rip), %rsi     # 0x9f239
callq 0x8480
movq %rax, %rdi
callq 0x8230
pushq $0x1
popq %rdi
callq 0x8540
jmp 0x41f20
jmp 0x41f20
jmp 0x41f20
jmp 0x41f20
jmp 0x41f20
jmp 0x41f20
jmp 0x41f20
jmp 0x41f20
jmp 0x41f20
movq %rax, %rbx
movq 0x10(%rsp), %rdi
callq 0x2eb64
movq %rbx, %rdi
callq 0x8870