// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/evo_diversityFromHaps.cpp
// Function: regionPiMain(int, char**)
int regionPiMain(int argc, char** argv) { 
    parseRegionPiOptions(argc, argv);
    
    // Load up the intervals file
    std::ifstream* bedFile = new std::ifstream(opt::bedFile.c_str());
    std::map<int, string> linearToGenomeMap;
    
    std::cerr << "Loading coordinates from the bed file " << opt::bedFile << std::endl;
    LinkedCoordsBed coords = LinkedCoordsBed(bedFile);          // The bed file needs element names in the fourth column
    std::cerr << "Done" << std::endl;
    
    // Load up the VCF file:
    std::istream* vcfFile = createReader(opt::vcfFile.c_str());
    string line; // std::unordered_map<string, MultiallelicCounts> vcfCountsMap;
    std::unordered_map<string, double> vcfPiMap;
    std::unordered_map<string, double> vcfHetMap;
    std::unordered_map<string, double> vcfInbreedingPvalMap;
    int totalVariantNumber = 0;
    
    std::cerr << "Loading variants from the VCF file " << opt::vcfFile << std::endl;
    std::vector<std::string> fields; int numSamples = -1;
    while (getline(*vcfFile, line)) {
        if (line[0] == '#' && line[1] == '#') {
        
        } else if (line[0] == '#' && line[1] == 'C') {
            fields = split(line, '\t');
            std::vector<std::string> sampleNames(fields.begin()+NUM_NON_GENOTYPE_COLUMNS,fields.end());
            numSamples = (int)sampleNames.size();
        } else {
            totalVariantNumber++;
            fields = split(line, '\t');
            string scaffold = fields[0]; string pos = fields[1]; string altAllele = fields[4];
            std::vector<string> altAlleles = split(altAllele, ','); int starPos = -1;
            std::vector<std::string>::iterator it = std::find(altAlleles.begin(), altAlleles.end(), "*");
            if (it != altAlleles.end()) { starPos = (int) std::distance(altAlleles.begin(), it); }
          /*  if (starPos != -1) {
                std::cerr << "starPos: " << starPos << std::endl;
                std::cerr << "altAllele: " << altAllele << std::endl;
            } */
            
            std::vector<std::string> genotypes(fields.begin()+NUM_NON_GENOTYPE_COLUMNS,fields.end());
            
            MultiallelicCounts* counts = new MultiallelicCounts(numSamples,starPos);
            counts->getMultiallelicCounts(genotypes);
            if (totalVariantNumber % 10000 == 0) {
                std::cerr << "Variant N: " << totalVariantNumber << std::endl;
            }
            double thisVariantPi = counts->getPiThisVariant();
            double thisVariantHet = counts->getHeterozygosityThisVariant();
            //vcfCountsMap[scaffold+"\t"+pos] = *counts;
            vcfPiMap[scaffold+"\t"+pos] = thisVariantPi;
            vcfHetMap[scaffold+"\t"+pos] = thisVariantHet;
            delete counts;
        }
    }
    std::cerr << "Done" << std::endl;
    

    string outPiFileName;
    if (opt::outFile != "") {
        outPiFileName = opt::outFile;
    } else {
        size_t suffixPos = opt::bedFile.find_last_of('.');
        if (suffixPos != std::string::npos) {
            outPiFileName = stripExtension(opt::bedFile) + "_PiVals_perElement.txt";
        } else {
            outPiFileName = opt::bedFile + "_PiVals_perElement.txt";
        }
    }
    std::ofstream* outPiFile = new std::ofstream(outPiFileName.c_str());
    
    std::cerr << "Now calculating Pi and Heterozygosity values per element to be written out into: " << outPiFileName << std::endl;
    std::vector<double> elementPiValues = coords.getMeanPerElement(vcfPiMap);
    std::vector<double> elementHetValues = coords.getMeanPerElement(vcfHetMap);
    std::cerr << "Calculations done" << std::endl;
    
    std::vector<string> elementNames = coords.getElementNames();
    std::vector<std::vector<string> > elementOuterBounds = coords.getElementOuterBoundaries();
    *outPiFile << "scaffold" << "\t" << "regionStartOnScaffold" << "\t" << "regionEndOnScaffold" << "\t" << "regionName" << "\t" << "pi" << "\t" << "heterozygosity" << std::endl;
    for (int i = 0; i < (int)elementPiValues.size(); i++) {
        *outPiFile << elementOuterBounds[i][0] << "\t" << elementOuterBounds[i][1] << "\t" << elementOuterBounds[i][2] << "\t" << elementNames[i] << "\t" << elementPiValues[i] << "\t" << elementHetValues[i] << std::endl;
    }
    
    std::cerr << "Done" << std::endl;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
callq 0x8a441
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %rbx
movq 0x55872(%rip), %rsi     # 0xdf240
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
movq %rbx, 0x110(%rsp)
leaq 0x1c8(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x535e3(%rip), %rdi     # 0xdcfe8
leaq 0xc7b4(%rip), %rsi      # 0x961c0
callq 0x8480
leaq 0x55828(%rip), %rsi     # 0xdf240
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0xa8(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x286f2
movq 0x535a4(%rip), %rdi     # 0xdcfe8
leaq 0xc79c(%rip), %rsi      # 0x961e7
callq 0x8480
movq %rax, %rdi
callq 0x8230
movq 0x55801(%rip), %rsi     # 0xdf260
leaq 0x150(%rsp), %rdi
leaq 0x188(%rsp), %rdx
callq 0x21904
leaq 0x150(%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x42587
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x8978
leaq 0x128(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x180(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
movq %rcx, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x1b8(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0x220(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
movq 0x534dd(%rip), %rdi     # 0xdcfe8
leaq 0xc6da(%rip), %rsi      # 0x961ec
callq 0x8480
leaq 0x55742(%rip), %rsi     # 0xdf260
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movl $0x0, 0xc(%rsp)
pushq $-0x1
popq %rax
leaq 0x118(%rsp), %r14
leaq 0x50(%rsp), %r15
leaq 0xc0(%rsp), %r12
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x89e58
movq 0x118(%rsp), %rax
cmpb $0x23, (%rax)
jne 0x89bfd
movzbl 0x1(%rax), %eax
cmpl $0x23, %eax
je 0x89b63
cmpl $0x43, %eax
jne 0x89bfd
movq %r15, %rdi
movq %r14, %rsi
pushq $0x9
popq %rdx
callq 0x40349
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x2f6c0
movq %r15, %rdi
callq 0x28a08
movq 0x10(%rsp), %rsi
movl $0x120, %eax            # imm = 0x120
addq %rax, %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rdi
leaq 0x28(%rsp), %rcx
callq 0x613fe
movq 0x58(%rsp), %rax
subq 0x50(%rsp), %rax
shrq $0x5, %rax
movq %rax, 0x48(%rsp)
movq %r15, %rdi
callq 0x28a08
jmp 0x89b63
movq %r15, %rdi
movq %r14, %rsi
pushq $0x9
popq %rdx
callq 0x40349
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x2f6c0
movq %r15, %rdi
callq 0x28a08
movq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x8600
movq 0x10(%rsp), %rsi
addq $0x20, %rsi
leaq 0x28(%rsp), %rdi
callq 0x8600
movq 0x10(%rsp), %rsi
subq $-0x80, %rsi
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x8600
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
pushq $0x2c
popq %rdx
callq 0x40349
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rsi
leaq 0x27d26(%rip), %rdx     # 0xb19a6
callq 0x8a792
movq %rax, %rbp
subq 0x90(%rsp), %rbp
shrq $0x5, %rbp
cmpq 0x98(%rsp), %rax
pushq $-0x1
popq %rax
cmovel %eax, %ebp
movq 0x10(%rsp), %rsi
movl $0x120, %eax            # imm = 0x120
addq %rax, %rsi
movq 0x18(%rsp), %rdx
leaq 0x138(%rsp), %rdi
leaq 0xf0(%rsp), %rcx
callq 0x613fe
movl $0xb0, %edi
callq 0x8490
movq %rax, %r14
movq %rax, %rdi
movq 0x48(%rsp), %rsi
movl %ebp, %edx
callq 0x8a5f8
movq %r14, %rdi
leaq 0x138(%rsp), %rsi
callq 0x4085c
movl 0xc(%rsp), %eax
incl %eax
movl %eax, 0xc(%rsp)
cltd
movl $0x2710, %ecx           # imm = 0x2710
idivl %ecx
testl %edx, %edx
jne 0x89d32
movq 0x532d6(%rip), %rdi     # 0xdcfe8
leaq 0x27a6f(%rip), %rsi     # 0xb1788
callq 0x8480
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x8840
movq %rax, %rdi
callq 0x8230
movq %r14, %rdi
callq 0x40a2c
movsd %xmm0, 0xe8(%rsp)
movq %r14, %rdi
callq 0x40972
movsd %xmm0, 0xe0(%rsp)
movq %r12, %r13
movq %r12, %rdi
movq %r15, %rsi
leaq 0x100ba(%rip), %rdx     # 0x99e1e
callq 0x28806
leaq 0xf0(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
callq 0x287e7
leaq 0x150(%rsp), %rdi
movq %rbp, %rsi
callq 0x2b9f4
movsd 0xe8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %rbp, %rdi
callq 0x8978
movq %r12, %r13
movq %r12, %rdi
callq 0x8978
movq %r12, %rdi
movq %r15, %rsi
leaq 0x10060(%rip), %rdx     # 0x99e1e
callq 0x28806
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
callq 0x287e7
leaq 0x188(%rsp), %rdi
movq %rbp, %rsi
callq 0x2b9f4
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %rbp, %rdi
callq 0x8978
movq %r12, %rdi
callq 0x8978
movq %r14, %rdi
callq 0x8a74e
movl $0xb0, %esi
movq %r14, %rdi
callq 0x84a0
leaq 0x138(%rsp), %rdi
callq 0x28a08
leaq 0x90(%rsp), %rdi
callq 0x28a08
leaq 0x70(%rsp), %rdi
callq 0x8978
leaq 0x28(%rsp), %rdi
callq 0x8978
movq %r15, %rdi
callq 0x8978
leaq 0x118(%rsp), %r14
jmp 0x89b63
movq 0x53189(%rip), %rdi     # 0xdcfe8
leaq 0xc381(%rip), %rsi      # 0x961e7
callq 0x8480
movq %rax, %rdi
callq 0x8230
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x553f5(%rip), %rdi     # 0xdf280
leaq 0xff8d(%rip), %rsi      # 0x99e1f
callq 0x2ea9b
testb %al, %al
je 0x89eb1
leaq 0x553de(%rip), %rsi     # 0xdf280
leaq 0x50(%rsp), %rdi
callq 0x82a0
jmp 0x89f43
leaq 0x55388(%rip), %rdi     # 0xdf240
pushq $0x2e
popq %rsi
pushq $-0x1
popq %rdx
callq 0x8940
cmpq $-0x1, %rax
je 0x89f11
leaq 0x55370(%rip), %rsi     # 0xdf240
leaq 0x70(%rsp), %rdi
callq 0x404d7
leaq 0x278b3(%rip), %rdx     # 0xb1794
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x28a29
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq 0x8550
movq %rbx, %rdi
callq 0x8978
leaq 0x70(%rsp), %rdi
jmp 0x89f3e
leaq 0x55328(%rip), %rsi     # 0xdf240
leaq 0x27875(%rip), %rdx     # 0xb1794
leaq 0x28(%rsp), %rdi
callq 0x28806
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq 0x8550
movq %rbx, %rdi
callq 0x8978
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %rbx
movq 0x50(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
movq 0x53081(%rip), %rdi     # 0xdcfe8
leaq 0x2783d(%rip), %rsi     # 0xb17ab
callq 0x8480
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x28(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x150(%rsp), %rdx
callq 0x28c48
leaq 0x70(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x188(%rsp), %rdx
callq 0x28c48
movq 0x53025(%rip), %rdi     # 0xdcfe8
leaq 0xc346(%rip), %rsi      # 0x96310
callq 0x8480
movq %rax, %rdi
callq 0x8230
leaq 0xf0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x28eb4
leaq 0xc0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x28f56
leaq 0x198bc(%rip), %rsi     # 0xa38c4
movq %rbx, %rdi
callq 0x8480
leaq 0xfe07(%rip), %rsi      # 0x99e1e
movq %rax, %rdi
callq 0x8480
leaq 0x277d7(%rip), %rsi     # 0xb17fd
movq %rax, %rdi
callq 0x8480
leaq 0xfde9(%rip), %rsi      # 0x99e1e
movq %rax, %rdi
callq 0x8480
leaq 0x277cf(%rip), %rsi     # 0xb1813
movq %rax, %rdi
callq 0x8480
leaq 0xfdcb(%rip), %rsi      # 0x99e1e
movq %rax, %rdi
callq 0x8480
leaq 0x277c5(%rip), %rsi     # 0xb1827
movq %rax, %rdi
callq 0x8480
leaq 0xfdad(%rip), %rsi      # 0x99e1e
movq %rax, %rdi
callq 0x8480
leaq 0x213cb(%rip), %rsi     # 0xab44b
movq %rax, %rdi
callq 0x8480
leaq 0xfd8f(%rip), %rsi      # 0x99e1e
movq %rax, %rdi
callq 0x8480
leaq 0x2144d(%rip), %rsi     # 0xab4eb
movq %rax, %rdi
callq 0x8480
movq %rax, %rdi
callq 0x8230
xorl %r15d, %r15d
leaq 0xfd66(%rip), %r14      # 0x99e1e
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x30(%rsp), %rax
subq 0x28(%rsp), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0x8a1a3
movq 0xc0(%rsp), %rax
movq (%rax,%r12), %rsi
movq %rbx, %rdi
callq 0x8430
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0xc0(%rsp), %rcx
movq (%rcx,%r12), %rsi
addq $0x20, %rsi
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0xc0(%rsp), %rcx
movq (%rcx,%r12), %rsi
addq $0x40, %rsi
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0xf0(%rsp), %rsi
addq %r15, %rsi
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0x28(%rsp), %rcx
movsd (%rcx,%r13,8), %xmm0
movq %rax, %rdi
callq 0x8750
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0x70(%rsp), %rcx
movsd (%rcx,%r13,8), %xmm0
movq %rax, %rdi
callq 0x8750
movq %rax, %rdi
callq 0x8230
incq %r13
addq $0x18, %r12
addq $0x20, %r15
jmp 0x8a0be
movq 0x52e3e(%rip), %rdi     # 0xdcfe8
leaq 0xc036(%rip), %rsi      # 0x961e7
callq 0x8480
movq %rax, %rdi
callq 0x8230
leaq 0xc0(%rsp), %rdi
callq 0x29054
leaq 0xf0(%rsp), %rdi
callq 0x28a08
leaq 0x70(%rsp), %rdi
callq 0x2b43c
leaq 0x28(%rsp), %rdi
callq 0x2b43c
leaq 0x50(%rsp), %rdi
callq 0x8978
leaq 0x10(%rsp), %rdi
callq 0x28a08
leaq 0x1f0(%rsp), %rdi
callq 0x21a8a
leaq 0x188(%rsp), %rdi
callq 0x21a8a
leaq 0x150(%rsp), %rdi
callq 0x21a8a
leaq 0x118(%rsp), %rdi
callq 0x8978
leaq 0xa8(%rsp), %rdi
callq 0x2a7a4
leaq 0x1c0(%rsp), %rdi
callq 0x2b344
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8a367
movq %rax, %r15
leaq 0x70(%rsp), %rdi
jmp 0x8a398
jmp 0x8a367
jmp 0x8a3a1
jmp 0x8a3a1
movq %rax, %r15
jmp 0x8a3b6
jmp 0x8a348
movq %rax, %r15
jmp 0x8a3cd
jmp 0x8a367
movq %rax, %r15
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x84a0
jmp 0x8a3d7
movq %rax, %r15
leaq 0x150(%rsp), %rdi
jmp 0x8a41a
jmp 0x8a34f
jmp 0x8a35f
movq %rax, %r15
movl $0x208, %esi            # imm = 0x208
movq %rbx, %rdi
callq 0x84a0
jmp 0x8a439
jmp 0x8a367
jmp 0x8a36c
jmp 0x8a30e
movq %rax, %r15
movl $0xb0, %esi
movq %r14, %rdi
callq 0x84a0
jmp 0x8a36f
movq %rax, %r15
jmp 0x8a389
jmp 0x8a3a1
jmp 0x8a313
movq %rax, %r15
jmp 0x8a323
movq %rax, %r15
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0xc0(%rsp), %rdi
callq 0x8978
jmp 0x8a36f
jmp 0x8a36c
jmp 0x8a36c
jmp 0x8a36c
movq %rax, %r15
jmp 0x8a393
jmp 0x8a367
jmp 0x8a3a1
jmp 0x8a343
movq %rax, %r15
jmp 0x8a37c
movq %rax, %r15
jmp 0x8a3c3
jmp 0x8a3a1
movq %rax, %r15
jmp 0x8a41f
movq %rax, %r15
jmp 0x8a3eb
movq %rax, %r15
jmp 0x8a42c
movq %rax, %r15
jmp 0x8a3d7
movq %rax, %r15
leaq 0x138(%rsp), %rdi
callq 0x28a08
leaq 0x90(%rsp), %rdi
callq 0x28a08
leaq 0x70(%rsp), %rdi
callq 0x8978
leaq 0x28(%rsp), %rdi
callq 0x8978
jmp 0x8a3d7
jmp 0x8a3a6
movq %rax, %r15
jmp 0x8a3e1
movq %rax, %r15
leaq 0xc0(%rsp), %rdi
callq 0x29054
leaq 0xf0(%rsp), %rdi
callq 0x28a08
leaq 0x70(%rsp), %rdi
callq 0x2b43c
leaq 0x28(%rsp), %rdi
callq 0x2b43c
leaq 0x50(%rsp), %rdi
callq 0x8978
leaq 0x10(%rsp), %rdi
callq 0x28a08
leaq 0x1f0(%rsp), %rdi
callq 0x21a8a
leaq 0x188(%rsp), %rdi
callq 0x21a8a
leaq 0x150(%rsp), %rdi
callq 0x21a8a
leaq 0x118(%rsp), %rdi
callq 0x8978
leaq 0xa8(%rsp), %rdi
callq 0x2a7a4
leaq 0x1c0(%rsp), %rdi
callq 0x2b344
movq %r15, %rdi
callq 0x8870