// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/process_vcf_fst.cpp
// Function: getVariantCountsForFst(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&, SetCounts*, std::vector<unsigned long, std::allocator<unsigned long>> const&, std::vector<unsigned long, std::allocator<unsigned long>> const&)
void getVariantCountsForFst(const std::vector<std::string>& fields, SetCounts* thisVariantCounts, const std::vector<size_t>& set1_loci, const std::vector<size_t>& set2_loci) {

    int numSamples = (int)fields.size()-NUM_NON_GENOTYPE_COLUMNS;
    thisVariantCounts->individualsWithVariant.assign(numSamples,0);
    thisVariantCounts->missingGenotypesPerIndividual.assign(numSamples,false);
    thisVariantCounts->haplotypesWithVariant.assign(numSamples*2,0);
    
    thisVariantCounts->set1individualsWithVariant.assign(set1_loci.size(),0);
    thisVariantCounts->set2individualsWithVariant.assign(set2_loci.size(),0);
    int n1 = (int)(set1_loci.size()*2); int n2 = (int)(set2_loci.size()*2);
    thisVariantCounts->set1HaplotypeVariant.assign(n1,0);
    thisVariantCounts->set2HaplotypeVariant.assign(n2,0);
    thisVariantCounts->set1_n_withoutMissing = n1;
    thisVariantCounts->set2_n_withoutMissing = n2;
    
    std::vector<std::string> altAlleles = split(fields[4], ',');
    thisVariantCounts->n_alt_alleles = (int)altAlleles.size();
    int alleleAsMissing = -1;
    for (int i = 0; i < (int)altAlleles.size(); i++) {
        if (altAlleles[i] == "*") {
            thisVariantCounts->n_alt_alleles = thisVariantCounts->n_alt_alleles - 1;
            alleleAsMissing = i + 1;
        }
        if (altAlleles[i].length() > 1) {
            thisVariantCounts->bIndel = true;
        }
    }
    
    if (thisVariantCounts->n_alt_alleles == 1) {
        int alt = 1;

        if (alleleAsMissing == 1) alt = 2;

        int set1i = 0; int set2i = 0; int set1hapI = 0; int set2hapI = 0;
        std::vector<std::string> genotypes(fields.begin()+NUM_NON_GENOTYPE_COLUMNS,fields.end());
        //std::cerr << fields[0] << "\t" << fields[1] << "\tgenotypes.size()" << genotypes.size() << std::endl;
        for (const size_t i : set1_loci) {
            int v1int = genotypes[i][0] - '0'; int v2int = genotypes[i][2] - '0';
            if (v1int == alt) {
                thisVariantCounts->set1Count++; thisVariantCounts->set1individualsWithVariant[set1i]++;
                thisVariantCounts->set1HaplotypeVariant[set1hapI]++;
            } else if (genotypes[i][0] == '.' || v1int == alleleAsMissing) {
               //std::cerr << fields[0] << "\t" << fields[1] << "\t" << genotypes[i][0] << "\t" << i << std::endl;
                thisVariantCounts->missingGenotypesPerIndividual[i] = true;
                thisVariantCounts->set1_n_withoutMissing = thisVariantCounts->set1_n_withoutMissing - 1;
            }
            if (v2int == alt) {
                thisVariantCounts->set1Count++; thisVariantCounts->set1individualsWithVariant[set1i]++;
                thisVariantCounts->set1HaplotypeVariant[set1hapI+1]++;
            } else if (genotypes[i][2] == '.' || v2int == alleleAsMissing) {
                thisVariantCounts->missingGenotypesPerIndividual[i] = true;
                //std::cerr << fields[0] << "\t" << fields[1] << "\t" << genotypes[i][2] << std::endl;
                thisVariantCounts->set1_n_withoutMissing = thisVariantCounts->set1_n_withoutMissing - 1;
            }
            set1i++; set1hapI = set1hapI+2;
        }
        for (const size_t i : set2_loci) {
            int v1int = genotypes[i][0] - '0'; int v2int = genotypes[i][2] - '0';
            if (genotypes[i][0] - '0' == alt) {
                thisVariantCounts->set2Count++; thisVariantCounts->set2individualsWithVariant[set2i]++;
                thisVariantCounts->set2HaplotypeVariant[set2hapI]++;
            } else if (genotypes[i][0] == '.' || v1int == alleleAsMissing) {
                thisVariantCounts->missingGenotypesPerIndividual[i] = true;
                thisVariantCounts->set2_n_withoutMissing = thisVariantCounts->set2_n_withoutMissing - 1;
            }
            if (genotypes[i][2] - '0' == alt) {
                thisVariantCounts->set2Count++; thisVariantCounts->set2individualsWithVariant[set2i]++;
                thisVariantCounts->set2HaplotypeVariant[set2hapI+1]++;
            } else if (genotypes[i][2] == '.' || v2int == alleleAsMissing) {
                thisVariantCounts->missingGenotypesPerIndividual[i] = true;
                thisVariantCounts->set2_n_withoutMissing = thisVariantCounts->set2_n_withoutMissing - 1;
            }
            set2i++; set2hapI = set2hapI+2;
        }
        for (std::vector<std::string>::size_type i = 0; i != genotypes.size(); i++) {
            if (genotypes[i][0] - '0' == alt)
                thisVariantCounts->overall++;
            if (genotypes[i][2] - '0' == alt)
                thisVariantCounts->overall++;
        }
    }
    // std::cerr << "got here" << std::endl;
    /*if (fields[1] == "13433") {
        std::cerr << thisVariantCounts.set1Count << std::endl;
        std::cerr << thisVariantCounts.set2Count << std::endl;
        print_vector(thisVariantCounts.set1individualsWithVariant, std::cerr);
        print_vector(thisVariantCounts.set2individualsWithVariant, std::cerr);
    } */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r13
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
movq 0x8(%rdi), %r14
subq (%rdi), %r14
shrq $0x5, %r14
leal -0x9(%r14), %eax
leaq 0x8(%rsi), %rdi
movslq %eax, %rbp
leaq 0x30(%rsp), %rdx
andl $0x0, (%rdx)
movq %rbp, %rsi
callq 0x44dfe
leaq 0x20(%rbx), %rdi
xorl %r12d, %r12d
movq %rdi, 0x28(%rsp)
movq %rbp, %rsi
xorl %edx, %edx
callq 0x452c4
leaq 0x48(%rbx), %rdi
leal -0x12(,%r14,2), %eax
movslq %eax, %rsi
leaq 0x30(%rsp), %rax
andl $0x0, (%rax)
leaq 0x30(%rsp), %r14
movq %r14, %rdx
callq 0x44dfe
leaq 0x90(%rbx), %rdi
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
andl $0x0, (%r14)
leaq 0x30(%rsp), %r14
movq %r14, %rdx
callq 0x44dfe
leaq 0xa8(%rbx), %rdi
movq 0x8(%r13), %rsi
subq (%r13), %rsi
sarq $0x3, %rsi
andl $0x0, (%r14)
leaq 0x30(%rsp), %rdx
callq 0x44dfe
movq 0x8(%r15), %rax
movq %r15, 0x20(%rsp)
subq (%r15), %rax
shrq $0x2, %rax
movq 0x8(%r13), %r14
movq %r13, 0x70(%rsp)
subq (%r13), %r14
andl $-0x2, %eax
shrq $0x2, %r14
andl $-0x2, %r14d
leaq 0x60(%rbx), %rdi
movslq %eax, %rbp
leaq 0x30(%rsp), %rax
andl $0x0, (%rax)
leaq 0x30(%rsp), %r15
movq %rbp, %rsi
movq %r15, %rdx
callq 0x44dfe
leaq 0x78(%rbx), %rdi
movslq %r14d, %r13
andl $0x0, (%r15)
leaq 0x30(%rsp), %rdx
movq %r13, %rsi
callq 0x44dfe
movl %ebp, 0xc8(%rbx)
leaq 0xcc(%rbx), %rax
movq %rax, 0x50(%rsp)
movl %r13d, 0xcc(%rbx)
movq 0x10(%rsp), %r13
movq (%r13), %rsi
subq $-0x80, %rsi
leaq 0x30(%rsp), %r15
pushq $0x2c
popq %rdx
movq %r15, %rdi
callq 0x40349
movq (%r15), %rdi
movq 0x8(%r15), %rax
movq %rax, %rcx
subq %rdi, %rcx
shrq $0x5, %rcx
movl %ecx, 0xd0(%rbx)
pushq $-0x1
popq %rcx
movl %ecx, 0xc(%rsp)
leaq 0x46b60(%rip), %rbp     # 0xb19a6
xorl %r14d, %r14d
subq %rdi, %rax
shrq $0x5, %rax
cltq
cmpq %rax, %r14
jge 0x6ae96
addq %r12, %rdi
movq %rbp, %rsi
callq 0x2193f
testb %al, %al
je 0x6ae74
decl 0xd0(%rbx)
leal 0x1(%r14), %eax
movl %eax, 0xc(%rsp)
movq 0x30(%rsp), %rdi
cmpq $0x2, 0x8(%rdi,%r12)
jb 0x6ae88
movb $0x1, 0xd4(%rbx)
incq %r14
movq 0x38(%rsp), %rax
addq $0x20, %r12
jmp 0x6ae49
cmpl $0x1, 0xd0(%rbx)
jne 0x6b106
xorl %r14d, %r14d
cmpl $0x1, 0xc(%rsp)
sete %bpl
movl $0x120, %esi            # imm = 0x120
addq (%r13), %rsi
movq 0x8(%r13), %rdx
leaq 0x58(%rsp), %rdi
leaq 0x1f(%rsp), %rcx
callq 0x70cd4
movb %bpl, %r14b
incl %r14d
movl %r14d, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %rcx, 0x48(%rsp)
leaq (%rcx,%r13), %rax
cmpq 0x20(%rsp), %rax
je 0x6afc3
movq (%rcx,%r15,2), %r14
movq 0x58(%rsp), %rax
movq %r14, %r12
shlq $0x5, %r12
movq (%rax,%r12), %rdx
movsbl (%rdx), %ecx
leal -0x30(%rcx), %eax
movsbl 0x2(%rdx), %ebp
cmpl 0x10(%rsp), %eax
jne 0x6af40
incl 0xc0(%rbx)
movq 0x90(%rbx), %rax
incl (%rax,%r15)
movq 0x60(%rbx), %rax
incl (%rax,%r15,2)
jmp 0x6af61
cmpb $0x2e, %cl
je 0x6af4b
cmpl 0xc(%rsp), %eax
jne 0x6af61
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x439c0
orq %rdx, (%rax)
decl 0xc8(%rbx)
addl $-0x30, %ebp
cmpl 0x10(%rsp), %ebp
jne 0x6af86
incl 0xc0(%rbx)
movq 0x90(%rbx), %rax
incl (%rax,%r15)
movq 0x60(%rbx), %rax
incl 0x4(%rax,%r15,2)
jmp 0x6afb1
movq 0x58(%rsp), %rax
movq (%rax,%r12), %rax
cmpb $0x2e, 0x2(%rax)
je 0x6af9b
cmpl 0xc(%rsp), %ebp
jne 0x6afb1
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x439c0
orq %rdx, (%rax)
decl 0xc8(%rbx)
addq $0x4, %r15
addq $0x8, %r13
movq 0x48(%rsp), %rcx
jmp 0x6aef2
movq 0x70(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x20(%rsp), %rax
addq %r12, %rax
cmpq 0x48(%rsp), %rax
je 0x6b0bf
movq (%rax), %r15
movq 0x58(%rsp), %rax
movq %r15, %rbp
shlq $0x5, %rbp
movq (%rax,%rbp), %rax
movsbl (%rax), %edx
leal -0x30(%rdx), %ecx
movsbl 0x2(%rax), %r13d
cmpl 0x10(%rsp), %ecx
jne 0x6b031
incl 0xc4(%rbx)
movq 0xa8(%rbx), %rcx
incl (%rcx,%r14,2)
movq 0x78(%rbx), %rcx
incl (%rcx,%r12)
jmp 0x6b05c
cmpb $0x2e, %dl
je 0x6b03c
cmpl 0xc(%rsp), %ecx
jne 0x6b05c
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x439c0
orq %rdx, (%rax)
movq 0x50(%rsp), %rax
decl (%rax)
movq 0x58(%rsp), %rax
movq (%rax,%rbp), %rax
movsbl 0x2(%rax), %eax
leal -0x30(%rax), %ecx
cmpl 0x10(%rsp), %ecx
jne 0x6b089
incl 0xc4(%rbx)
movq 0xa8(%rbx), %rax
incl (%rax,%r14,2)
movq 0x78(%rbx), %rax
addq %r12, %rax
addq $0x4, %rax
pushq $0x1
jmp 0x6b0af
cmpb $0x2e, %al
je 0x6b098
addl $-0x30, %r13d
cmpl 0xc(%rsp), %r13d
jne 0x6b0b2
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x439c0
orq %rdx, (%rax)
movq 0x50(%rsp), %rax
pushq $-0x1
popq %rcx
addl %ecx, (%rax)
addq $0x2, %r14
addq $0x8, %r12
jmp 0x6afdf
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
movl 0x10(%rsp), %edi
subq $0x1, %rcx
jb 0x6b0fc
movq (%rax), %rdx
movsbl (%rdx), %esi
addl $-0x30, %esi
cmpl %edi, %esi
jne 0x6b0e9
incl (%rbx)
movsbl 0x2(%rdx), %edx
addl $-0x30, %edx
cmpl %edi, %edx
jne 0x6b0f6
incl (%rbx)
addq $0x20, %rax
jmp 0x6b0d4
leaq 0x58(%rsp), %rdi
callq 0x28a08
leaq 0x30(%rsp), %rdi
callq 0x28a08
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x6b137
jmp 0x6b12a
jmp 0x6b12a
jmp 0x6b12a
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x28a08
leaq 0x30(%rsp), %rdi
callq 0x28a08
movq %rbx, %rdi
callq 0x8870