// File: /workspace/github_cpp_makefile_star2_O2/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 0x9bf2
movq %r12, %rdi
callq 0x8b26
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0xa180
movq 0x80(%r12), %rcx
leaq 0x70(%r12), %rax
movq %rax, 0xa0(%rsp)
leaq 0x258(%r12), %rax
movq %rax, 0x98(%rsp)
leaq 0x4c8(%r12), %rax
movq %rax, 0x90(%rsp)
leaq 0x4f8(%r12), %rax
movq %rax, 0x88(%rsp)
leaq 0x290(%r12), %rax
movq %rax, 0x50(%rsp)
leaq 0x528(%r12), %rax
movq %rax, 0x48(%rsp)
movq %r12, 0x80(%rsp)
leaq 0x558(%r12), %rax
movq %rax, 0x40(%rsp)
leaq 0x14e0(%rsp), %r14
leaq 0x58(%rsp), %rbx
leaq 0xb0(%rsp), %rbp
cmpq 0xa0(%rsp), %rcx
je 0xa102
leaq 0x20(%rcx), %rdi
movq %rcx, %r15
movq %rdi, (%rsp)
leaq 0x2f326(%rip), %rsi     # 0x38fd1
callq 0x813d
testb %al, %al
je 0x9d20
movq 0x4732d(%rip), %r13     # 0x50fe8
movq %r13, %rdi
leaq 0x2c9e5(%rip), %rsi     # 0x366aa
callq 0x7400
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x73c0
movq %rax, %rdi
leaq 0x2f2f0(%rip), %rsi     # 0x38fd0
callq 0x7400
movq %r15, %r12
leaq 0x40(%r15), %rsi
leaq 0xf8(%rsp), %r15
movq %r15, %rdi
callq 0xba2e
movq %r15, %rdi
movq %r13, %rsi
pushq $0x9
popq %rdx
pushq $0x1
popq %rcx
callq 0xbaea
movq %r15, %rdi
callq 0xc17e
movq %r13, %rdi
callq 0x71e0
movq %r12, %r15
movq %r15, 0xa8(%rsp)
leaq 0x40(%r15), %rsi
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xba2e
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0xa0a7
leaq 0x140(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0xba2e
movq %r12, %rdi
callq 0xbb58
movsd %xmm0, 0x28(%rsp)
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x2c74b(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2c753(%rip), %xmm0    # 0x364e0
movapd %xmm0, 0x30(%rsp)
movq %r12, %rdi
callq 0xc17e
movq 0x98(%rsp), %rdi
movq (%rsp), %rsi
callq 0xb998
movapd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x28(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movsd %xmm1, (%rax)
leaq 0x158(%rsp), %r15
movq %r15, %rdi
callq 0xba7c
movq %r15, %rdi
callq 0x75a0
movl %eax, %esi
movq %r14, %rdi
callq 0xd712
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rax, %rdx
subq %rcx, %rdx
andl $0x0, 0x58(%rsp)
shrq $0x3, %rdx
decl %edx
movl %edx, 0x5c(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movapd %xmm0, 0xb0(%rsp)
andq $0x0, 0xc0(%rsp)
xorl %r13d, %r13d
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x9e99
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbad2
movl %eax, %r15d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbad2
movl %eax, %r12d
movslq %r15d, %rsi
shlq $0x3, %rsi
addq 0x8(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0xb9de
movslq %r12d, %rsi
shlq $0x3, %rsi
addq 0x8(%rsp), %rsi
movq %rbp, %rdi
callq 0xb9de
incq %r13
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
jmp 0x9e38
leaq 0x128(%rsp), %r15
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
callq 0xba2e
movq %r15, %rdi
callq 0xbb58
movsd %xmm0, 0x30(%rsp)
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm1
movq 0x2c5f9(%rip), %xmm0    # 0x364d0
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x2c5fd(%rip), %xmm0    # 0x364e0
subpd %xmm0, %xmm1
movapd %xmm1, 0xe0(%rsp)
movq %r15, %rdi
callq 0xc17e
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0xba2e
movq %r15, %rdi
callq 0xbb58
movsd %xmm0, 0x20(%rsp)
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x2c59c(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2c5a4(%rip), %xmm0    # 0x364e0
movapd %xmm0, 0xd0(%rsp)
movq %r15, %rdi
callq 0xc17e
movq 0x90(%rsp), %rdi
movq (%rsp), %r15
movq %r15, %rsi
callq 0xb998
movapd 0xe0(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
movsd %xmm1, (%rax)
movq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0xb998
movapd 0xd0(%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)
movq 0x80(%rsp), %rax
movl 0x4(%rax), %eax
cmpl $0x1, %eax
je 0xa022
testl %eax, %eax
movq (%rsp), %r15
jne 0xa083
movq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0xb998
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0xb998
movsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0xb998
movsd 0x20(%rsp), %xmm0
jmp 0xa07f
movq 0x50(%rsp), %rdi
movq (%rsp), %r15
movq %r15, %rsi
callq 0xb998
movsd 0x2c435(%rip), %xmm0    # 0x36470
subsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0xb998
movsd 0x2c416(%rip), %xmm0    # 0x36470
subsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0xb998
movsd 0x2c3f7(%rip), %xmm0    # 0x36470
subsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %rbp, %rdi
callq 0xc17e
leaq 0x60(%rsp), %rdi
callq 0xc17e
leaq 0x158(%rsp), %rdi
callq 0xbada
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xc17e
movq 0xa8(%rsp), %rdi
callq 0x7300
movq %rax, %rcx
jmp 0x9c8b
movq %rax, %r15
cmpl $0x1, %edx
jne 0xa14a
movq %r15, %rdi
callq 0x7190
movq 0x46f0d(%rip), %rdi     # 0x50fe8
leaq 0x2c5d3(%rip), %rsi     # 0x366b5
callq 0x7400
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x73c0
movq %rax, %rdi
callq 0x71e0
callq 0x76a0
jmp 0xa083
addq $0x2868, %rsp           # imm = 0x2868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa147
movq %rax, %r15
callq 0x76a0
jmp 0xa14a
movq %rax, %rdi
callq 0xbb7c
movq %rax, %r15
leaq 0xf8(%rsp), %rdi
jmp 0xa173
jmp 0xa147
jmp 0xa147
jmp 0xa13b
movq %rax, %r15
jmp 0xa16e
movq %rax, %r15
jmp 0xa161
jmp 0xa147
movq %rax, %r15
leaq 0xb0(%rsp), %rdi
callq 0xc17e
leaq 0x60(%rsp), %rdi
callq 0xc17e
leaq 0x158(%rsp), %rdi
callq 0xbada
leaq 0x8(%rsp), %rdi
callq 0xc17e
movq %r15, %rdi
callq 0x7750