// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]Dsuite/Dsuite_utils.cpp
// Function: GeneralSetCountsWithSplits::getSplitCountsNew(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&)
void GeneralSetCountsWithSplits::getSplitCountsNew(const std::vector<std::string>& genotypes, const std::map<size_t, string>& posToSpeciesMap) {
    
    getBasicCountsWithSplitsNew(genotypes, posToSpeciesMap);
    
    // If at least one of the outgroup individuals has non-missing data
    // Find out what is the "ancestral allele" - i.e. the one more common in the outgroup
    try {
        if (setAlleleCounts.at("Outgroup") > 0) {
            if ((double)vector_sum(setGenotypes.at("Outgroup"))/setGenotypes.at("Outgroup").size() < 0.5) { AAint = AncestralAlleleRef; }
            else { AAint = AncestralAlleleAlt; } 
        }
    } catch (std::out_of_range& e) { AAint = -1; }
    
    // Now fill in the allele frequencies
    double totalAAF = 0; int numNonZeroCounts = 0;
    for(std::map<string,std::vector<int>>::iterator it = setGenotypes.begin(); it != setGenotypes.end(); ++it) {
        if (it->first == "") {
            std::cerr << "it->first " << it->first << "\t"; print_vector(it->second, std::cerr); std::cerr << std::endl;
        }
        std::vector<int> thisSetGenotypes = setGenotypes.at(it->first);
        std::vector<double> thisSetIndividualAFs = setIndividualAFs.at(it->first);
        
        if (thisSetGenotypes.size() > 0) {
            numNonZeroCounts++;
            double thisAAF = (double)vector_sum(thisSetGenotypes)/thisSetGenotypes.size();
           // print_vector(thisSetGenotypes, std::cerr);
           // std::cerr << "thisAAF: " << thisAAF << std::endl;
            setAAFs[it->first] = thisAAF; totalAAF += 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> uni(0,((int)thisSetGenotypes.size() - 1)); // guaranteed unbiased
            std::uniform_int_distribution<int> uniAFs(0,((int)thisSetIndividualAFs.size() - 1)); // guaranteed unbiased
            
         /*   std::vector<int> thisSetGenotypesSampledSplit1; std::vector<int> thisSetGenotypesSampledSplit2;
            for (int i = 0; i < thisSetGenotypes.size(); i++) {
                int random_pos_s1 = uni(rng);
                int random_pos_s2 = uni(rng);
                thisSetGenotypesSampledSplit1.push_back(thisSetGenotypes[random_pos_s1]);
                thisSetGenotypesSampledSplit2.push_back(thisSetGenotypes[random_pos_s2]);
            }
          */
            
            std::vector<double> thisSetIndividualAFsSampledSplit1; std::vector<double> thisSetIndividualAFsSampledSplit2;
            for (int i = 0; i < thisSetIndividualAFs.size(); i++) {
                int random_pos_s1 = uniAFs(rng);
                int random_pos_s2 = uniAFs(rng);
                thisSetIndividualAFsSampledSplit1.push_back(thisSetIndividualAFs[random_pos_s1]);
                thisSetIndividualAFsSampledSplit2.push_back(thisSetIndividualAFs[random_pos_s2]);
            }
            
          //  double thisAAFsplit1 = vector_average(thisSetGenotypesSampledSplit1);
          //  double thisAAFsplit2 = vector_average(thisSetGenotypesSampledSplit2);
            double thisAAFsplit1 = vector_average(thisSetIndividualAFsSampledSplit1);
            double thisAAFsplit2 = vector_average(thisSetIndividualAFsSampledSplit2);
            setAAFsplit1[it->first] = thisAAFsplit1; setAAFsplit2[it->first] = thisAAFsplit2;
            
            // Count correction as in admixtools
         //   double ya = vector_sum(thisSetGenotypes); double yb = thisSetGenotypes.size() - vector_sum(thisSetGenotypes);
         //   double yt = (double)thisSetGenotypes.size();
         //   double h = ya * yb / (yt * (yt - 1.0));
            //std::cerr << "it->first: " << it->first << std::endl;
            //std::cerr << "ya: " << ya << " ; yb: " << yb << " ; yt: " << yt << std::endl;
            //std::cerr << "h: " << h << " ; h / yt: " << h / yt << std::endl;
            
          //  setCorrectionFactors[it->first] = h / yt;
            
           // std::cerr << "it->first " << it->first << std::endl;
            try {
            if (AAint == AncestralAlleleRef) { // Ancestral allele seems to be the ref, so derived is alt
                setDAFs[it->first] = thisAAF;
                setDAFsplit1[it->first] = thisAAFsplit1; setDAFsplit2[it->first] = thisAAFsplit2;
            } else if (AAint == AncestralAlleleAlt) { // Ancestral allele seems to be alt, so derived is ref
                setDAFs[it->first] = (1 - thisAAF);
                setDAFsplit1[it->first] = 1 - thisAAFsplit1;
                setDAFsplit2[it->first] = 1 - thisAAFsplit2;
            }
                } catch (std::out_of_range& e) { std::cerr << "The trouble was here" << it->first << std::endl; }
        }
    }
    averageAAF = totalAAF/numNonZeroCounts;
    if (AAint == AncestralAlleleRef) averageDAF = averageAAF;
    else if (AAint == AncestralAlleleAlt) averageDAF = (1 - averageAAF);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2898, %rsp           # imm = 0x2898
movq %rdi, %r14
callq 0xa490
leaq 0x2c3c8(%rip), %rsi     # 0x36a9a
leaq 0x188(%rsp), %rdi
leaq 0x1510(%rsp), %rdx
callq 0x8102
leaq 0xc8(%r14), %rdi
leaq 0x188(%rsp), %rsi
callq 0xb760
movl (%rax), %ebx
leaq 0x188(%rsp), %rdi
callq 0x7898
testl %ebx, %ebx
jle 0xa80c
leaq 0x2c381(%rip), %rsi     # 0x36a9a
leaq 0x188(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x8102
leaq 0x8(%r14), %rbx
leaq 0x188(%rsp), %rsi
movq %rbx, %rdi
callq 0xbc7c
leaq 0x110(%rsp), %rdi
movq %rax, %rsi
callq 0xbcc2
leaq 0x110(%rsp), %rdi
callq 0xbc54
movsd %xmm0, (%rsp)
leaq 0x2c32f(%rip), %rsi     # 0x36a9a
leaq 0x1510(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x8102
leaq 0x1510(%rsp), %rsi
movq %rbx, %rdi
callq 0xbc7c
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x2, %rcx
movq %rcx, %xmm0
punpckldq 0x2bd2b(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2bd33(%rip), %xmm0    # 0x364e0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd (%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
leaq 0x1510(%rsp), %rdi
callq 0x7898
leaq 0x110(%rsp), %rdi
callq 0xc1b2
leaq 0x188(%rsp), %rdi
callq 0x7898
movsd 0x2bc82(%rip), %xmm0    # 0x36478
ucomisd (%rsp), %xmm0
jbe 0xa804
andl $0x0, 0x4(%r14)
jmp 0xa80c
movl $0x1, 0x4(%r14)
leaq 0x8(%r14), %rax
movq %rax, 0xe0(%rsp)
movq 0x20(%r14), %rcx
leaq 0x10(%r14), %rax
movq %rax, 0xe8(%rsp)
leaq 0x38(%r14), %rax
movq %rax, 0xd8(%rsp)
leaq 0x1e8(%r14), %rax
movq %rax, 0xd0(%rsp)
leaq 0x3a8(%r14), %rax
movq %rax, 0xc8(%rsp)
leaq 0x3d8(%r14), %rax
movq %rax, 0xc0(%rsp)
leaq 0x220(%r14), %rax
movq %rax, 0x68(%rsp)
leaq 0x408(%r14), %rax
movq %rax, 0x60(%rsp)
movq %r14, 0x70(%rsp)
leaq 0x438(%r14), %rax
movq %rax, 0x58(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x1510(%rsp), %r12
leaq 0x98(%rsp), %r14
leaq 0xf0(%rsp), %rbx
cmpq 0xe8(%rsp), %rcx
je 0xad0a
movq %rcx, 0x78(%rsp)
leaq 0x20(%rcx), %r13
movq %r13, %rdi
leaq 0x2e6fb(%rip), %rsi     # 0x38fd1
callq 0x813d
testb %al, %al
movq %r13, (%rsp)
je 0xa951
movq 0x466fe(%rip), %r15     # 0x50fe8
movq %r15, %rdi
leaq 0x2bdb6(%rip), %rsi     # 0x366aa
callq 0x7400
movq %rax, %rdi
movq %r13, %rsi
callq 0x73c0
movq %rax, %rdi
leaq 0x2e6c2(%rip), %rsi     # 0x38fd0
callq 0x7400
movq 0x78(%rsp), %rax
leaq 0x40(%rax), %rsi
leaq 0x140(%rsp), %r13
movq %r13, %rdi
callq 0xbcc2
movq %r13, %rdi
movq %r15, %rsi
pushq $0x9
popq %rdx
pushq $0x1
popq %rcx
callq 0xbd0f
movq %r13, %rdi
callq 0xc1b2
movq %r15, %rdi
callq 0x71e0
movq (%rsp), %r13
movq 0xe0(%rsp), %rdi
movq %r13, %rsi
callq 0xbc7c
leaq 0x80(%rsp), %rdi
movq %rax, %rsi
callq 0xbcc2
movq 0xd8(%rsp), %rdi
movq %r13, %rsi
callq 0xbd7c
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0xba2e
movq 0x88(%rsp), %rax
cmpq 0x80(%rsp), %rax
je 0xac9c
leaq 0x170(%rsp), %r15
movq %r15, %rdi
leaq 0x80(%rsp), %rsi
callq 0xbcc2
movq %r15, %rdi
callq 0xbc54
movsd %xmm0, 0x8(%rsp)
movq 0x88(%rsp), %rax
subq 0x80(%rsp), %rax
sarq $0x2, %rax
movq %rax, %xmm0
punpckldq 0x2bae5(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2baed(%rip), %xmm0    # 0x364e0
movapd %xmm0, 0x40(%rsp)
movq %r15, %rdi
callq 0xc1b2
movq 0xd0(%rsp), %rdi
movq (%rsp), %rsi
callq 0xb7a6
movapd 0x40(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm1, (%rax)
leaq 0x188(%rsp), %r15
movq %r15, %rdi
callq 0xba7c
movq %r15, %rdi
callq 0x75a0
movl %eax, %esi
movq %r12, %rdi
callq 0xd712
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rax, %rdx
subq %rcx, %rdx
andl $0x0, 0x98(%rsp)
shrq $0x3, %rdx
decl %edx
movl %edx, 0x9c(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
movapd %xmm0, 0xf0(%rsp)
andq $0x0, 0x100(%rsp)
xorl %r15d, %r15d
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r15, %rax
jbe 0xab2b
movq %r14, %rdi
movq %r12, %rsi
callq 0xbad2
movl %eax, %ebp
movq %r14, %rdi
movq %r12, %rsi
callq 0xbad2
movl %eax, %r13d
movslq %ebp, %rsi
shlq $0x3, %rsi
addq 0x28(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0xb9de
movslq %r13d, %rsi
shlq $0x3, %rsi
addq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0xb9de
incq %r15
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
jmp 0xaac8
leaq 0x158(%rsp), %r15
movq %r15, %rdi
leaq 0xa0(%rsp), %rsi
callq 0xba2e
movq %r15, %rdi
callq 0xba00
movsd %xmm0, 0x40(%rsp)
movq %r15, %rdi
callq 0xc17e
leaq 0x128(%rsp), %rdi
movq %rbx, %rsi
callq 0xba2e
leaq 0x128(%rsp), %r15
movq %r15, %rdi
callq 0xba00
movsd %xmm0, 0x20(%rsp)
movq %r15, %rdi
callq 0xc17e
movq 0xc8(%rsp), %rdi
movq (%rsp), %rsi
callq 0xb7a6
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0xc0(%rsp), %rdi
movq (%rsp), %rsi
callq 0xb7a6
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x70(%rsp), %rax
movl 0x4(%rax), %eax
cmpl $0x1, %eax
je 0xac19
testl %eax, %eax
movq (%rsp), %r15
jne 0xac7a
movq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0xb7a6
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0xb7a6
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0xb7a6
movsd 0x20(%rsp), %xmm0
jmp 0xac76
movq 0x68(%rsp), %rdi
movq (%rsp), %r15
movq %r15, %rsi
callq 0xb7a6
movsd 0x2b83e(%rip), %xmm0    # 0x36470
subsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0xb7a6
movsd 0x2b81f(%rip), %xmm0    # 0x36470
subsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0xb7a6
movsd 0x2b800(%rip), %xmm0    # 0x36470
subsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %rbx, %rdi
callq 0xc17e
leaq 0xa0(%rsp), %rdi
callq 0xc17e
leaq 0x188(%rsp), %rdi
callq 0xbada
leaq 0x28(%rsp), %rdi
callq 0xc17e
leaq 0x80(%rsp), %rdi
callq 0xc1b2
movq 0x78(%rsp), %rdi
callq 0x7300
movq %rax, %rcx
jmp 0xa8b5
movq %rax, %r15
cmpl $0x1, %edx
jne 0xae07
movq %r15, %rdi
callq 0x7190
movq 0x46308(%rip), %rdi     # 0x50fe8
leaq 0x2b9ce(%rip), %rsi     # 0x366b5
callq 0x7400
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x73c0
movq %rax, %rdi
callq 0x71e0
callq 0x76a0
jmp 0xac7a
cvtsi2sdl 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x70(%rsp), %rcx
movapd %xmm1, %xmm0
movsd %xmm1, 0x218(%rcx)
movl 0x4(%rcx), %eax
testl %eax, %eax
je 0xad47
cmpl $0x1, %eax
jne 0xad4f
movsd 0x2b731(%rip), %xmm1    # 0x36470
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movsd %xmm0, 0x250(%rcx)
addq $0x2898, %rsp           # imm = 0x2898
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xae04
movq %rax, %r15
callq 0x76a0
jmp 0xae07
movq %rax, %rdi
callq 0xbb7c
movq %rdx, %rbx
movq %rax, %r15
leaq 0x1510(%rsp), %rdi
callq 0x7898
jmp 0xad96
movq %rdx, %rbx
movq %rax, %r15
leaq 0x110(%rsp), %rdi
callq 0xc1b2
jmp 0xadb7
jmp 0xada9
jmp 0xadb1
movq %rdx, %rbx
movq %rax, %r15
jmp 0xadc4
movq %rdx, %rbx
movq %rax, %r15
leaq 0x188(%rsp), %rdi
callq 0x7898
cmpl $0x1, %ebx
jne 0xae45
movq %r15, %rdi
callq 0x7190
orl $-0x1, 0x4(%r14)
callq 0x76a0
jmp 0xa80c
movq %rax, %r15
leaq 0x140(%rsp), %rdi
jmp 0xae40
jmp 0xae04
jmp 0xadf1
movq %rax, %r15
jmp 0xae2e
movq %rax, %r15
jmp 0xae21
jmp 0xae04
movq %rax, %r15
jmp 0xae38
jmp 0xae04
movq %rax, %r15
leaq 0xf0(%rsp), %rdi
callq 0xc17e
leaq 0xa0(%rsp), %rdi
callq 0xc17e
leaq 0x188(%rsp), %rdi
callq 0xbada
leaq 0x28(%rsp), %rdi
callq 0xc17e
leaq 0x80(%rsp), %rdi
callq 0xc1b2
movq %r15, %rdi
callq 0x7750
nop