// File: /workspace/github_cpp_makefile_star2_O1/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 0xab44
movq %r12, %rdi
callq 0x93f2
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0xb2c6
movq 0x80(%r12), %r14
movq %r12, 0xa0(%rsp)
leaq 0x70(%r12), %rax
movq %rax, 0xc8(%rsp)
cmpq %rax, %r14
je 0xb211
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 0x383b6(%rip), %rsi     # 0x42fb1
callq 0x70f0
testl %eax, %eax
jne 0xacbb
movl $0xa, %edx
movq 0x4c3d4(%rip), %r15     # 0x56fe8
movq %r15, %rdi
leaq 0x35a8c(%rip), %rsi     # 0x406aa
callq 0x74b0
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %r15, %rdi
callq 0x74b0
movl $0x1, %edx
movq %rax, %rdi
leaq 0x3836e(%rip), %rsi     # 0x42fb0
callq 0x74b0
leaq 0x40(%r14), %rsi
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
callq 0xd4a2
movq %r13, %rdi
movq %r15, %rsi
movl $0x9, %edx
movl $0x1, %ecx
callq 0xd59c
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0xac8d
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0x4c354(%rip), %r15     # 0x56fe8
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 0xd4a2
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0xb18e
leaq 0x118(%rsp), %rdi
movq %rsp, %rsi
callq 0xd4a2
movq 0x118(%rsp), %rdi
movq 0x120(%rsp), %rax
subq %rdi, %rax
je 0xad1c
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 0xad0d
jmp 0xad20
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x35790(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x35798(%rip), %xmm0    # 0x404e0
movapd %xmm0, 0x70(%rsp)
testq %rdi, %rdi
je 0xad63
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0xc0(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0xd44e
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 0xd522
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 0xadc2
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 0xaee4
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf3ae
movl %eax, %r15d
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf3ae
movl %eax, %r13d
movslq %r15d, %rdx
shlq $0x3, %rdx
addq (%rsp), %rdx
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0xae8d
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x58(%rsp)
jmp 0xae97
leaq 0x50(%rsp), %rdi
callq 0xf2ce
movslq %r13d, %rdx
shlq $0x3, %rdx
addq (%rsp), %rdx
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xaec1
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x38(%rsp)
jmp 0xaecb
leaq 0x30(%rsp), %rdi
callq 0xf2ce
incq %rbp
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x3, %rax
cmpq %rbp, %rax
ja 0xae41
leaq 0x100(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xd4a2
movq 0x100(%rsp), %rdi
movq 0x108(%rsp), %rax
subq %rdi, %rax
je 0xaf2c
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 0xaf1d
jmp 0xaf30
xorpd %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x35580(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x35588(%rip), %xmm0    # 0x404e0
movapd %xmm0, 0x140(%rsp)
testq %rdi, %rdi
je 0xaf76
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
leaq 0xe8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xd4a2
movq 0xe8(%rsp), %rdi
movq 0xf0(%rsp), %rax
subq %rdi, %rax
je 0xafbe
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 0xafaf
jmp 0xafc2
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x354ee(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x354f6(%rip), %xmm0    # 0x404e0
movapd %xmm0, 0x130(%rsp)
testq %rdi, %rdi
je 0xb008
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0xb8(%rsp), %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0xd44e
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 0xd44e
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 0xb0e8
testl %eax, %eax
movq 0x28(%rsp), %r15
jne 0xb153
movq 0x98(%rsp), %rdi
movq %r15, %rsi
callq 0xd44e
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0xd44e
movsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0xd44e
movsd 0x18(%rsp), %xmm0
jmp 0xb14f
movq 0x98(%rsp), %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0xd44e
movsd 0x3536b(%rip), %xmm0    # 0x40470
subsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0xd44e
movsd 0x35349(%rip), %xmm0    # 0x40470
subsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x88(%rsp), %rdi
movq %r15, %rsi
callq 0xd44e
movsd 0x35327(%rip), %xmm0    # 0x40470
subsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xb16a
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xb181
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
leaq 0x14e0(%rsp), %rdi
callq 0x73d0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb1a4
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq %r14, %rdi
callq 0x72f0
movq %rax, %r14
cmpq 0xc8(%rsp), %rax
jne 0xabeb
jmp 0xb211
movq %rax, %rbp
cmpl $0x1, %edx
jne 0xb265
movq %rbp, %rdi
callq 0x7170
movl $0x14, %edx
movq 0x4be09(%rip), %rdi     # 0x56fe8
leaq 0x354cf(%rip), %rsi     # 0x406b5
callq 0x74b0
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x4bdee(%rip), %rdi     # 0x56fe8
callq 0x74b0
movq %rax, %rdi
callq 0x71b0
callq 0x76a0
jmp 0xb153
addq $0x2868, %rsp           # imm = 0x2868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xb262
movq %rax, %rbp
callq 0x76a0
jmp 0xb265
jmp 0xb2be
movq %rax, %rbp
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0xb2b6
movq 0xe0(%rsp), %rsi
jmp 0xb2ae
jmp 0xb262
jmp 0xb262
jmp 0xb2be
movq %rax, %rbp
jmp 0xb293
jmp 0xb25b
movq %rax, %rbp
jmp 0xb2a0
jmp 0xb262
movq %rax, %rbp
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xb27c
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0xb293
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
leaq 0x14e0(%rsp), %rdi
callq 0x73d0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb2b6
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq %rbp, %rdi
callq 0x7740
movq %rax, %rdi
callq 0xd650