// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/process_vcf_massoko.cpp
// Function: massokoMain(int, char**)
int massokoMain(int argc, char** argv) {
    parseMassokoOptions(argc, argv);
    std::ios_base::openmode mode = std::ios_base::in;
    string fileName = opt::vcfFile;
    string fileRoot = stripExtension(fileName);
    std::map<int, std::vector<int> > counts_blue;
    
    std::cerr << "Filtering variants from: " << fileName << std::endl;
    std::cerr << "Maximum read depth (overall) set to: " << opt::max_overall_depth << std::endl;
    std::cerr << "Minimum copies for a variant (e.g. 1 for allowing singletons): " << opt::min_copies << std::endl;
    std::cerr << "Minimum read depth at the variant position: " << opt::min_depth_in_any_individual << std::endl;
    
    // Open connection to read from the vcf file
    std::ifstream* inFile = new std::ifstream(fileName.c_str(), mode);
    std::ios_base::openmode mode_out = std::ios_base::out;
    string VarFile12FileName = fileRoot + ".fixed_variants12.txt";
    string VarFile11FileName = fileRoot + ".fixed_variants11.txt";
    string VarFile10FileName = fileRoot + ".fixed_variants10_two_hets.txt";
    string VarFile10HomFileName = fileRoot + ".fixed_variants10_hom.txt";
    string VarFile9FileName = fileRoot + ".fixed_variants9_three_hets.txt";
    string VarFile9HomFileName = fileRoot + ".fixed_variants9_hom.txt";
    std::ofstream* pFixedVarFile12 = new std::ofstream(VarFile12FileName.c_str(), mode_out);
    std::ofstream* pFixedVarFile11 = new std::ofstream(VarFile11FileName.c_str(), mode_out);
    std::ofstream* pFixedVarFile10 = new std::ofstream(VarFile10FileName.c_str(), mode_out);
    std::ofstream* pFixedVarFile10hom = new std::ofstream(VarFile10HomFileName.c_str(), mode_out);
    std::ofstream* pFixedVarFile9 = new std::ofstream(VarFile9FileName.c_str(), mode_out);
    std::ofstream* pFixedVarFile9hom = new std::ofstream(VarFile9HomFileName.c_str(), mode_out);
    bool gotChromosomeNumber = false;
    int numChromosomes;
    string line;
    int totalVariantNumber = 0;
    while (getline(*inFile, line)) {
        if (line[0] == '#') {
            std::cout << line << std::endl;
            *pFixedVarFile12 << line << std::endl; 
            *pFixedVarFile11 << line << std::endl;
            *pFixedVarFile10 << line << std::endl;
        } else {
            totalVariantNumber++;
            MassokoFilterResult result;
            std::vector<std::string> fields = split(line, '\t');
            if (!gotChromosomeNumber) {
                const std::vector<std::string>::size_type numSamples = fields.size() - NUM_NON_GENOTYPE_COLUMNS;
                numChromosomes = numSamples * 2;
                std::cerr << "Number of chromosomes: " << numChromosomes << std::endl;
                gotChromosomeNumber = true;
                initializeCountsBlue(counts_blue, numChromosomes);
            }
            result.overallQuality = atoi(fields[5].c_str());
            
            
            if (result.overallQuality >= MIN_OVERALL_VARIANT_PHRED_QUAL) 
                result.maxDepthPassed = testOverallReadDepth(opt::max_overall_depth,opt::min_overall_depth,fields[7]);
            
            if (result.maxDepthPassed)
                result.biallelicPassed = testBiallelic(fields[4]);
            
            if (result.biallelicPassed) 
                result.counts = getMassokoVariantCounts(fields);
            
            // filter out sites where more than MAX_NUM_HET individuals are heterozygous
            int num_hets = getNumHets(result);
            
            if (result.biallelicPassed && result.counts.overall <= (numChromosomes - opt::min_copies) && result.counts.overall >= opt::min_copies) {
                if (result.counts.minimumDepthInAnIndividual >= opt::min_depth_in_any_individual) {
                    std::cout << line << std::endl;
                    counts_blue[result.counts.overall][result.counts.blue]++;
                    if (result.counts.overall == 12 && (result.counts.blue == 12 || result.counts.blue == 0)) {
                        *pFixedVarFile12 << line << std::endl;
                    }
                    if (result.counts.overall == 11 && (result.counts.blue == 11 || result.counts.blue == 0)) {
                        *pFixedVarFile11 << line << std::endl;
                    }
                    if (result.counts.overall == 13 && (result.counts.blue == 12 || result.counts.blue == 1)) {
                        *pFixedVarFile11 << line << std::endl;
                    }
                    if (result.counts.overall == 10 && (result.counts.blue == 10 || result.counts.blue == 0) && (num_hets == 2)) {
                        *pFixedVarFile10 << line << std::endl;
                    } else if (result.counts.overall == 10 && (result.counts.blue == 10 || result.counts.blue == 0)) {
                        *pFixedVarFile10hom << line << std::endl;
                    }
                    if (result.counts.overall == 14 && (result.counts.blue == 12 || result.counts.blue == 2) && (num_hets == 2)) {
                        *pFixedVarFile10 << line << std::endl;
                    } else if (result.counts.overall == 14 && (result.counts.blue == 14 || result.counts.blue == 2)) {
                        *pFixedVarFile10hom << line << std::endl;
                    }
                    if (result.counts.overall == 9 && (result.counts.blue == 9 || result.counts.blue == 0) && (num_hets == 3)) {
                        *pFixedVarFile9 << line << std::endl;
                    } else if (result.counts.overall == 9 && (result.counts.blue == 9 || result.counts.blue == 0)) {
                        *pFixedVarFile9hom << line << std::endl;
                    }
                    if (result.counts.overall == 15 && (result.counts.blue == 12 || result.counts.blue == 3) && (num_hets == 3)) {
                        *pFixedVarFile9 << line << std::endl;
                    } else if (result.counts.overall == 15 && (result.counts.blue == 12 || result.counts.blue == 3)) {
                        *pFixedVarFile9hom << line << std::endl;
                    }
                }
            }
        }
    }
    for (int i = numChromosomes - opt::min_copies; i >= opt::min_copies; i--) {
        for (std::vector<std::string>::size_type j = 0; j != counts_blue[i].size(); j++) {
            if (j == (counts_blue[i].size()-1)) {
                std::cerr << counts_blue[i][j];
            } else {
                std::cerr << counts_blue[i][j] << "\t";
            }
        } std::cerr << std::endl;
    } 
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
callq 0x508d8
leaq 0x8e445(%rip), %rsi     # 0xde3b8
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0x8600
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x404d7
leaq 0x170(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x8d034(%rip), %rdi     # 0xdcfe8
leaq 0x515ed(%rip), %rsi     # 0xa15a8
callq 0x8480
leaq 0x88(%rsp), %rsi
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0x8d009(%rip), %rdi     # 0xdcfe8
leaq 0x5160d(%rip), %rsi     # 0xa15f3
callq 0x8480
movl 0x8d59b(%rip), %esi     # 0xdd58c
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
callq 0x8230
movq 0x8cfe0(%rip), %rdi     # 0xdcfe8
leaq 0x51630(%rip), %rsi     # 0xa163f
callq 0x8480
movl 0x8d56e(%rip), %esi     # 0xdd588
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
callq 0x8230
movq 0x8cfb7(%rip), %rdi     # 0xdcfe8
leaq 0x51647(%rip), %rsi     # 0xa167f
callq 0x8480
movl 0x8d54d(%rip), %esi     # 0xdd590
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
callq 0x8230
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %r14
movq 0x88(%rsp), %rsi
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
leaq 0x52dea(%rip), %rdx     # 0xa2e64
leaq 0x148(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x28806
leaq 0x52de7(%rip), %rdx     # 0xa2e7a
leaq 0x128(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x28806
leaq 0x52de4(%rip), %rdx     # 0xa2e90
leaq 0x108(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x28806
leaq 0x52dea(%rip), %rdx     # 0xa2eaf
leaq 0xe8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x28806
leaq 0x52deb(%rip), %rdx     # 0xa2ec9
leaq 0xc8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x28806
leaq 0x52df2(%rip), %rdx     # 0xa2ee9
leaq 0xa8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x28806
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %rbx
movq 0x148(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %r12
movq 0x128(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %r15
movq 0x108(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %rbp
movq 0xe8(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %r13
movq 0xc8(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq %rbx, 0x28(%rsp)
movq 0xa8(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x84e0
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %ebx, %ebx
leaq 0x68(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x505fd
movq 0x68(%rsp), %rax
cmpb $0x23, (%rax)
jne 0x5026d
movq 0x8cd98(%rip), %rdi     # 0xdcfb8
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq %r12, %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq %r15, %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
jmp 0x501f2
leaq 0x198(%rsp), %rdi
callq 0x50b44
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
pushq $0x9
popq %rdx
callq 0x40349
testb $0x1, %bl
jne 0x502d7
movq 0x38(%rsp), %rbp
subq 0x30(%rsp), %rbp
movq 0x8cd48(%rip), %rdi     # 0xdcfe8
leaq 0x5037c(%rip), %rsi     # 0xa0623
callq 0x8480
shrq $0x4, %rbp
andl $-0x2, %ebp
addl $-0x12, %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x8840
movq %rax, %rdi
callq 0x8230
leaq 0x168(%rsp), %rdi
movl %ebp, %esi
callq 0x4fe80
movq 0x30(%rsp), %rbx
movq 0xa0(%rbx), %rdi
callq 0x86f0
movl %eax, 0x198(%rsp)
cmpl $0x1d, %eax
jle 0x5031f
movl 0x8d292(%rip), %edi     # 0xdd58c
addq $0xe0, %rbx
xorl %esi, %esi
movq %rbx, %rdx
callq 0x41f6c
movb %al, 0x19c(%rsp)
testb %al, %al
jne 0x50329
movb 0x2a0(%rsp), %bl
jmp 0x50340
testb $0x1, 0x19c(%rsp)
je 0x50316
movq 0x30(%rsp), %rdi
subq $-0x80, %rdi
callq 0x41f35
movl %eax, %ebx
movb %al, 0x2a0(%rsp)
testb $0x1, %bl
je 0x50379
leaq 0x2a8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0x4fcec
leaq 0x1a0(%rsp), %rdi
movq %rbx, %rsi
callq 0x50b62
movq %rbx, %rdi
callq 0x289b4
movb 0x2a0(%rsp), %bl
leaq 0x198(%rsp), %rdi
callq 0x4ff29
movl %eax, 0xc(%rsp)
testb $0x1, %bl
je 0x505df
movl 0x1a0(%rsp), %eax
movl 0x8d1e8(%rip), %ecx     # 0xdd588
cmpl %ecx, %eax
jl 0x505df
movl %ebp, %edx
subl %ecx, %edx
cmpl %edx, %eax
jg 0x505df
movl 0x1a4(%rsp), %eax
cmpl 0x8d1cf(%rip), %eax     # 0xdd590
jl 0x505df
movq 0x8cbea(%rip), %rdi     # 0xdcfb8
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x168(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x50af6
movslq 0x29c(%rsp), %rcx
movq (%rax), %rax
incl (%rax,%rcx,4)
movl 0x1a0(%rsp), %eax
cmpl $0xc, %eax
jne 0x5043d
movl 0x29c(%rsp), %eax
cmpl $0xc, %eax
je 0x50421
testl %eax, %eax
jne 0x505df
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movl 0x1a0(%rsp), %eax
cmpl $0xb, %eax
jne 0x50470
movl 0x29c(%rsp), %eax
cmpl $0xb, %eax
je 0x50456
testl %eax, %eax
jne 0x504ec
movq %r12, %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movl 0x1a0(%rsp), %eax
cmpl $0xd, %eax
jne 0x504a4
movl 0x29c(%rsp), %eax
cmpl $0xc, %eax
je 0x5048a
cmpl $0x1, %eax
jne 0x505df
movq %r12, %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movl 0x1a0(%rsp), %eax
cmpl $0xa, %eax
jne 0x504ec
movl 0x29c(%rsp), %eax
cmpl $0xa, %eax
sete %cl
testl %eax, %eax
sete %dl
cmpl $0x2, 0xc(%rsp)
jne 0x504c9
orb %dl, %cl
movq %r15, %rdi
jne 0x504dc
movq 0x18(%rsp), %rdi
cmpl $0xa, %eax
je 0x504dc
movq 0x18(%rsp), %rdi
testl %eax, %eax
jne 0x504ec
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movl 0x1a0(%rsp), %eax
cmpl $0xe, %eax
jne 0x50544
movl 0x29c(%rsp), %eax
cmpl $0xc, %eax
sete %cl
cmpl $0x2, %eax
sete %dl
cmpl $0x2, 0xc(%rsp)
jne 0x50519
orb %dl, %cl
movq %r15, %rdi
jne 0x5052d
movq 0x18(%rsp), %rdi
cmpl $0xe, %eax
je 0x5052d
movq 0x18(%rsp), %rdi
cmpl $0x2, %eax
jne 0x5053d
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movl 0x1a0(%rsp), %eax
cmpl $0x9, %eax
jne 0x5058e
movl 0x29c(%rsp), %eax
cmpl $0x9, %eax
sete %cl
testl %eax, %eax
sete %dl
cmpl $0x3, 0xc(%rsp)
jne 0x5056b
orb %dl, %cl
movq 0x20(%rsp), %rdi
jne 0x5057e
movq 0x10(%rsp), %rdi
cmpl $0x9, %eax
je 0x5057e
movq 0x10(%rsp), %rdi
testl %eax, %eax
jne 0x5058e
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
cmpl $0xf, 0x1a0(%rsp)
jne 0x505df
movl 0x29c(%rsp), %eax
cmpl $0xc, %eax
sete %cl
cmpl $0x3, %eax
sete %dl
cmpl $0x3, 0xc(%rsp)
jne 0x505bb
orb %dl, %cl
movq 0x20(%rsp), %rdi
jne 0x505cf
movq 0x10(%rsp), %rdi
cmpl $0xc, %eax
je 0x505cf
movq 0x10(%rsp), %rdi
cmpl $0x3, %eax
jne 0x505df
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x30(%rsp), %rdi
callq 0x28a08
leaq 0x1a0(%rsp), %rdi
callq 0x289b4
movb $0x1, %bl
jmp 0x501f2
movl 0x8cf85(%rip), %eax     # 0xdd588
subl %eax, %ebp
leaq 0x168(%rsp), %rbx
leaq 0x198(%rsp), %r14
movq 0x8c9cc(%rip), %r15     # 0xdcfe8
leaq 0x497fb(%rip), %r12     # 0x99e1e
movl %ebp, 0x198(%rsp)
cmpl %eax, %ebp
jl 0x506d0
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x50af6
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x2, %rcx
cmpq %rcx, %r13
je 0x506b4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x50af6
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x2, %rcx
decq %rcx
cmpq %rcx, %r13
jne 0x5068a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x50af6
movq (%rax), %rax
movl (%rax,%r13,4), %esi
movq %r15, %rdi
callq 0x8840
jmp 0x506af
movq %rbx, %rdi
movq %r14, %rsi
callq 0x50af6
movq (%rax), %rax
movl (%rax,%r13,4), %esi
movq %r15, %rdi
callq 0x8840
movq %rax, %rdi
movq %r12, %rsi
callq 0x8480
incq %r13
jmp 0x50635
movq %r15, %rdi
callq 0x8230
movl 0x198(%rsp), %ebp
decl %ebp
movl 0x8cebd(%rip), %eax     # 0xdd588
jmp 0x50623
leaq 0x68(%rsp), %rdi
callq 0x8978
leaq 0xa8(%rsp), %rdi
callq 0x8978
leaq 0xc8(%rsp), %rdi
callq 0x8978
leaq 0xe8(%rsp), %rdi
callq 0x8978
leaq 0x108(%rsp), %rdi
callq 0x8978
leaq 0x128(%rsp), %rdi
callq 0x8978
leaq 0x148(%rsp), %rdi
callq 0x8978
leaq 0x168(%rsp), %rdi
callq 0x50b8a
leaq 0x48(%rsp), %rdi
callq 0x8978
leaq 0x88(%rsp), %rdi
callq 0x8978
xorl %eax, %eax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5082c
jmp 0x5082c
movq %rax, %r13
jmp 0x50839
jmp 0x5082c
jmp 0x50851
movq %rax, %r13
movl $0x200, %esi            # imm = 0x200
movq 0x10(%rsp), %rdi
jmp 0x507d4
jmp 0x507de
movq %r13, %rdi
movq %rax, %r13
movl $0x200, %esi            # imm = 0x200
jmp 0x507d4
jmp 0x507de
movq %rax, %r13
movl $0x200, %esi            # imm = 0x200
movq %rbp, %rdi
jmp 0x507d4
jmp 0x507de
movq %rax, %r13
movl $0x200, %esi            # imm = 0x200
movq %r15, %rdi
jmp 0x507d4
jmp 0x507de
movq %rax, %r13
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdi
jmp 0x507d4
jmp 0x507de
movq %rax, %r13
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x84a0
jmp 0x5085e
movq %rax, %r13
jmp 0x5085e
movq %rax, %r13
jmp 0x5086b
movq %rax, %r13
jmp 0x50878
movq %rax, %r13
jmp 0x50885
movq %rax, %r13
jmp 0x50892
movq %rax, %r13
jmp 0x5089f
jmp 0x50848
movq %rax, %r13
movl $0x208, %esi            # imm = 0x208
movq %r14, %rdi
callq 0x84a0
jmp 0x508ac
jmp 0x50848
movq %rax, %r13
jmp 0x508c3
movq %rax, %r13
leaq 0x30(%rsp), %rdi
callq 0x28a08
leaq 0x1a0(%rsp), %rdi
callq 0x289b4
jmp 0x50854
movq %rax, %r13
jmp 0x508ac
jmp 0x50851
jmp 0x50851
movq %rax, %r13
leaq 0x68(%rsp), %rdi
callq 0x8978
leaq 0xa8(%rsp), %rdi
callq 0x8978
leaq 0xc8(%rsp), %rdi
callq 0x8978
leaq 0xe8(%rsp), %rdi
callq 0x8978
leaq 0x108(%rsp), %rdi
callq 0x8978
leaq 0x128(%rsp), %rdi
callq 0x8978
leaq 0x148(%rsp), %rdi
callq 0x8978
leaq 0x168(%rsp), %rdi
callq 0x50b8a
leaq 0x48(%rsp), %rdi
callq 0x8978
leaq 0x88(%rsp), %rdi
callq 0x8978
movq %r13, %rdi
callq 0x8870