// File: /workspace/github_cpp_makefile_star2_O3/millanek[P]Dsuite/Dsuite_utils.cpp
// Function: GeneralSetCountsWithSplits::getAFsFromGenotypeLikelihoodsOrProbabilitiesWithSplits(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::map<unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>> const&, int, int)
void GeneralSetCountsWithSplits::getAFsFromGenotypeLikelihoodsOrProbabilitiesWithSplits(const std::vector<std::string>& genotypeFields, const std::map<size_t, string>& posToSpeciesMap, const int likelihoodsOrProbabilitiesTagPosition, const int pos) {
   
    
    if (likelihoodsProbabilitiesType == LikelihoodsProbabilitiesPL || likelihoodsProbabilitiesType == LikelihoodsProbabilitiesGL) {
        setHWEpriorsFromAFfromGT();
    }
    
    getBasicCountsFromLikelihoodsOrProbabilities(genotypeFields, posToSpeciesMap, likelihoodsOrProbabilitiesTagPosition);
    
     
    // Now fill in the allele frequencies
    for(std::map<string,std::vector<double>>::iterator it = setIndividualExpectedGenotypes.begin(); it != setIndividualExpectedGenotypes.end(); ++it) {
        if (it->first == "") {
            std::cerr << "it->first " << it->first << "\t"; print_vector(it->second, std::cerr); std::cerr << std::endl;
        }
        std::vector<double> thisSetExpectedGenotypes = it->second;
        
        
        if (thisSetExpectedGenotypes.size() > 0) {
            double thisAAF = (double)vector_sum(thisSetExpectedGenotypes)/(2*thisSetExpectedGenotypes.size());
          /* Debug stuff
           if(pos == 1180 || pos == 1046) {
                std::cerr << "pos: " << pos << std::endl;
                std::cerr << "it->first: " << it->first << std::endl;
                print_vector(thisSetExpectedGenotypes, std::cerr);
                std::cerr << "thisAAF: " << thisAAF << std::endl;
            }
           */
            //std::cerr << "species: " << it->first << std::endl;
            // print_vector(thisSetExpectedGenotypes, std::cerr);
            // std::cerr << "thisAAF: " << thisAAF << std::endl;
            setAAFsFromLikelihoods.at(it->first) = thisAAF;
            
            // Take care of the splits by random sampling with replacement:
            std::random_device rd;     // only used once to initialise (seed) engine
            std::mt19937 rng(rd());    // random-number engine used (Mersenne-Twister in this case)
            std::uniform_int_distribution<int> uniAFs(0,((int)thisSetExpectedGenotypes.size() - 1)); // guaranteed unbiased
            
            
            std::vector<double> thisSetIndividualExpectedGenotypesSampledSplit1;
            std::vector<double> thisSetIndividualExpectedGenotypesSampledSplit2;
            for (int i = 0; i < thisSetExpectedGenotypes.size(); i++) {
                int random_pos_s1 = uniAFs(rng);
                int random_pos_s2 = uniAFs(rng);
                thisSetIndividualExpectedGenotypesSampledSplit1.push_back(thisSetExpectedGenotypes[random_pos_s1]);
                thisSetIndividualExpectedGenotypesSampledSplit2.push_back(thisSetExpectedGenotypes[random_pos_s2]);
            }
            
            double thisAAFsplit1 = (double)vector_sum(thisSetIndividualExpectedGenotypesSampledSplit1)/(2*thisSetExpectedGenotypes.size());
           // std::cerr << "thisAAFsplit1: " << thisAAFsplit1 << std::endl;
            double thisAAFsplit2 = (double)vector_sum(thisSetIndividualExpectedGenotypesSampledSplit2)/(2*thisSetExpectedGenotypes.size());
           // std::cerr << "thisAAFsplit2: " << thisAAFsplit2 << std::endl;

            
           // std::cerr << "it->first " << it->first << std::endl;
            try {
            setAAFsplit1fromLikelihoods.at(it->first) = thisAAFsplit1; setAAFsplit2fromLikelihoods.at(it->first) = thisAAFsplit2;
                
            if (AAint == AncestralAlleleRef) { // Ancestral allele seems to be the ref, so derived is alt
                setDAFsFromLikelihoods.at(it->first) = thisAAF;
                setDAFsplit1fromLikelihoods.at(it->first) = thisAAFsplit1;
                setDAFsplit2fromLikelihoods.at(it->first) = thisAAFsplit2;
            } else if (AAint == AncestralAlleleAlt) { // Ancestral allele seems to be alt, so derived is ref
                setDAFsFromLikelihoods.at(it->first) = (1 - thisAAF);
                setDAFsplit1fromLikelihoods.at(it->first) = 1 - thisAAFsplit1;
                setDAFsplit2fromLikelihoods.at(it->first) = 1 - thisAAFsplit2;
            }
            } catch (std::out_of_range& e) { std::cerr << "The trouble was here" << it->first << std::endl; }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2868, %rsp           # imm = 0x2868
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movl 0x2e0(%rdi), %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0xaaf0
movq %r12, %rdi
callq 0x9408
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0xb276
movq 0x80(%r12), %r14
movq %r12, 0xa0(%rsp)
leaq 0x70(%r12), %rax
movq %rax, 0xc8(%rsp)
cmpq %rax, %r14
je 0xb1c0
movq 0xa0(%rsp), %rax
leaq 0x258(%rax), %rcx
movq %rcx, 0xc0(%rsp)
leaq 0x4c8(%rax), %rcx
movq %rcx, 0xb8(%rsp)
leaq 0x4f8(%rax), %rcx
movq %rcx, 0xb0(%rsp)
leaq 0x290(%rax), %rcx
movq %rcx, 0x98(%rsp)
leaq 0x528(%rax), %rcx
movq %rcx, 0x90(%rsp)
addq $0x558, %rax            # imm = 0x558
movq %rax, 0x88(%rsp)
leaq 0x158(%rsp), %r12
leaq 0xa8(%rsp), %rbx
leaq 0x20(%r14), %rdi
movq %rdi, 0x28(%rsp)
leaq 0x3949a(%rip), %rsi     # 0x44041
callq 0x70f0
testl %eax, %eax
jne 0xac67
movl $0xa, %edx
movq 0x4d428(%rip), %r15     # 0x57fe8
movq %r15, %rdi
leaq 0x36b30(%rip), %rsi     # 0x416fa
callq 0x74b0
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %r15, %rdi
callq 0x74b0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x39452(%rip), %rsi     # 0x44040
callq 0x74b0
leaq 0x40(%r14), %rsi
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
callq 0xd408
movq %r13, %rdi
movq %r15, %rsi
movl $0x9, %edx
movl $0x1, %ecx
callq 0xd502
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0xac39
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0x4d3a8(%rip), %r15     # 0x57fe8
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x73f0
movsbl %al, %esi
movq %r15, %rdi
callq 0x7040
movq %rax, %rdi
callq 0x72e0
leaq 0x40(%r14), %rsi
movq %rsp, %rdi
callq 0xd408
movq (%rsp), %rdi
cmpq %rdi, 0x8(%rsp)
je 0xb141
leaq 0x118(%rsp), %rdi
movq %rsp, %rsi
callq 0xd408
movq 0x118(%rsp), %rdi
movq 0x120(%rsp), %rax
subq %rdi, %rax
je 0xacc8
sarq $0x3, %rax
cmpq $0x1, %rax
adcq $0x0, %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rdi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0xacb9
jmp 0xaccc
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x367e4(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x367ec(%rip), %xmm0    # 0x414e0
movapd %xmm0, 0x70(%rsp)
testq %rdi, %rdi
je 0xad0f
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0xc0(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0xd3b4
movapd 0x70(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movsd %xmm1, (%rax)
leaq 0x14e0(%rsp), %r15
movq %r15, %rdi
callq 0xd488
movq %r15, %rdi
callq 0x75a0
movl %eax, %eax
movq %rax, 0x158(%rsp)
movl $0x1, %ecx
movq %rax, %rdx
shrq $0x1e, %rdx
xorl %eax, %edx
imull $0x6c078965, %edx, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, 0x158(%rsp,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0xad6e
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
movq %rax, %rdx
subq %rcx, %rdx
shrq $0x3, %rdx
decl %edx
movq $0x270, 0x14d8(%rsp) # imm = 0x270
movl $0x0, 0xa8(%rsp)
movl %edx, 0xac(%rsp)
xorl %edx, %edx
movq %rdx, 0x60(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movq %rdx, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
cmpq %rcx, %rax
je 0xae93
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf55e
movl %eax, %r15d
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf55e
movl %eax, %r13d
movslq %r15d, %rcx
movq (%rsp), %rax
leaq (%rax,%rcx,8), %rdx
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0xae39
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x58(%rsp)
jmp 0xae47
leaq 0x50(%rsp), %rdi
callq 0xf47e
movq (%rsp), %rax
movslq %r13d, %rcx
leaq (%rax,%rcx,8), %rdx
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xae6d
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x38(%rsp)
jmp 0xae7b
leaq 0x30(%rsp), %rdi
callq 0xf47e
movq (%rsp), %rax
incq %rbp
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
ja 0xaded
leaq 0x100(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xd408
movq 0x100(%rsp), %rdi
movq 0x108(%rsp), %rax
subq %rdi, %rax
je 0xaedb
sarq $0x3, %rax
cmpq $0x1, %rax
adcq $0x0, %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rdi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0xaecc
jmp 0xaedf
xorpd %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x365d1(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x365d9(%rip), %xmm0    # 0x414e0
movapd %xmm0, 0x140(%rsp)
testq %rdi, %rdi
je 0xaf25
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
leaq 0xe8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xd408
movq 0xe8(%rsp), %rdi
movq 0xf0(%rsp), %rax
subq %rdi, %rax
je 0xaf6d
sarq $0x3, %rax
cmpq $0x1, %rax
adcq $0x0, %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rdi,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0xaf5e
jmp 0xaf71
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x3653f(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x36547(%rip), %xmm0    # 0x414e0
movapd %xmm0, 0x130(%rsp)
testq %rdi, %rdi
je 0xafb7
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0xb8(%rsp), %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0xd3b4
movapd 0x140(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x70(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rsp)
movsd %xmm1, (%rax)
movq 0xb0(%rsp), %rdi
movq %r15, %rsi
callq 0xd3b4
movapd 0x130(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movsd %xmm1, (%rax)
movq 0xa0(%rsp), %rax
movl 0x4(%rax), %eax
cmpl $0x1, %eax
je 0xb097
testl %eax, %eax
movq 0x28(%rsp), %r15
jne 0xb102
movq 0x98(%rsp), %rdi
movq %r15, %rsi
callq 0xd3b4
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0xd3b4
movsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0xd3b4
movsd 0x18(%rsp), %xmm0
jmp 0xb0fe
movq 0x98(%rsp), %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0xd3b4
movsd 0x363bc(%rip), %xmm0    # 0x41470
subsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0xd3b4
movsd 0x3639a(%rip), %xmm0    # 0x41470
subsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0xd3b4
movsd 0x36378(%rip), %xmm0    # 0x41470
subsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xb119
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xb130
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
leaq 0x14e0(%rsp), %rdi
callq 0x73d0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb153
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq %r14, %rdi
callq 0x72f0
movq %rax, %r14
cmpq 0xc8(%rsp), %rax
jne 0xab97
jmp 0xb1c0
movq %rax, %rbp
cmpl $0x1, %edx
jne 0xb214
movq %rbp, %rdi
callq 0x7170
movl $0x14, %edx
movq 0x4ce5a(%rip), %rdi     # 0x57fe8
leaq 0x36570(%rip), %rsi     # 0x41705
callq 0x74b0
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x4ce3f(%rip), %rdi     # 0x57fe8
callq 0x74b0
movq %rax, %rdi
callq 0x71b0
callq 0x76a0
jmp 0xb102
addq $0x2868, %rsp           # imm = 0x2868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xb211
movq %rax, %rbp
callq 0x76a0
jmp 0xb214
jmp 0xb26d
movq %rax, %rbp
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0xb265
movq 0xe0(%rsp), %rsi
jmp 0xb25d
jmp 0xb211
jmp 0xb211
jmp 0xb26d
movq %rax, %rbp
jmp 0xb242
jmp 0xb20a
movq %rax, %rbp
jmp 0xb24f
jmp 0xb211
movq %rax, %rbp
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xb22b
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xb242
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
leaq 0x14e0(%rsp), %rdi
callq 0x73d0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb265
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq %rbp, %rdi
callq 0x7740
movq %rax, %rdi
callq 0xd5b6
nop