// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/process_vcf_reorder.cpp
// Function: reorderMain(int, char**)
int reorderMain(int argc, char** argv) {
    parseReorderOptions(argc, argv);
    string fileRoot = stripExtension(opt::vcfFile);
    
    std::cerr << "Reordering columns in: " << opt::vcfFile << std::endl;
    std::cerr << "using ordering in: " << opt::newOrderFile << std::endl;
    
    // Open connection to read from the vcf file
    std::istream* vcfFile = createReader(opt::vcfFile.c_str());
    string reorderedFileName = fileRoot + opt::runName + "_reordered.vcf";
    std::ofstream* pReordered = new std::ofstream(reorderedFileName.c_str());
    
    int numChromosomes;
    int totalVariantNumber = 0;
    string line;
    std::vector<string> sampleNames;
    std::vector<string> newOrder = readSampleNamesFromTextFile(opt::newOrderFile);
    std::vector<string> fields;
    std::map<string, size_t> link;
    while (getline(*vcfFile, line)) {
        if (line[0] == '#' && line[1] == '#') {
            *pReordered << line << std::endl;
        } else if (line[0] == '#' && line[1] == 'C') {
            std::vector<std::string> fields = split(line, '\t');
            const std::vector<std::string>::size_type numSamples = fields.size() - NUM_NON_GENOTYPE_COLUMNS;
            numChromosomes = (int)numSamples * 2;
            // std::cerr << "Number of chromosomes: " << numChromosomes << std::endl;
            if (opt::sampleNameFile.empty()) {
                for (std::vector<std::string>::size_type i = NUM_NON_GENOTYPE_COLUMNS; i != fields.size(); i++) {
                    sampleNames.push_back(fields[i]);
                }
            } else {
                sampleNames = readSampleNamesFromTextFile(opt::sampleNameFile);
            }
            assert(sampleNames.size() == newOrder.size());
            link = linkVectors(newOrder, sampleNames);
            for (std::vector<std::string>::size_type i = 0; i != NUM_NON_GENOTYPE_COLUMNS; i++) {
                *pReordered << fields[i] << "\t";
            }
            
            for (std::vector<std::string>::size_type i = 0; i != sampleNames.size() - 1; i++) {
                *pReordered << newOrder[i] << "\t";
            } *pReordered << newOrder[newOrder.size()-1] << std::endl;
        } else {
            totalVariantNumber++;
            
            std::vector<std::string> fields = split(line, '\t');
            for (std::vector<std::string>::size_type i = 0; i != NUM_NON_GENOTYPE_COLUMNS; i++) {
                *pReordered << fields[i] << "\t";
            }
            
            for (std::vector<std::string>::size_type i = 0; i != sampleNames.size() - 1; i++) {
                *pReordered << fields[link[sampleNames[i]]+NUM_NON_GENOTYPE_COLUMNS] << "\t";
            } *pReordered << fields[link[sampleNames[sampleNames.size()-1]]+NUM_NON_GENOTYPE_COLUMNS] << std::endl;
 
            
        }
    }
    
    
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
callq 0x689c0
leaq 0x76495(%rip), %rsi     # 0xde8d8
leaq 0x110(%rsp), %rdi
callq 0x404d7
movq 0x74b91(%rip), %rdi     # 0xdcfe8
leaq 0x41e0c(%rip), %rsi     # 0xaa26a
callq 0x8480
leaq 0x7646e(%rip), %rsi     # 0xde8d8
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0x74b67(%rip), %rdi     # 0xdcfe8
leaq 0x41dfa(%rip), %rsi     # 0xaa282
callq 0x8480
leaq 0x76464(%rip), %rsi     # 0xde8f8
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0x7642d(%rip), %rsi     # 0xde8d8
leaq 0x50(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x21904
leaq 0x50(%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x42587
movq %rax, 0x30(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x8978
leaq 0x76458(%rip), %rdx     # 0xde938
leaq 0x50(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x28b6d
leaq 0x41d9d(%rip), %rdx     # 0xaa296
leaq 0xf0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x28a29
leaq 0x50(%rsp), %rdi
callq 0x8978
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %r14
movq 0xf0(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
leaq 0x90(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x7639b(%rip), %rsi     # 0xde8f8
leaq 0x18(%rsp), %rdi
callq 0x4237c
andq $0x0, 0xe0(%rsp)
leaq 0x58(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movq %rsp, %r13
leaq 0x50(%rsp), %rbp
leaq 0x3187a(%rip), %rbx     # 0x99e1e
movq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x68867
movq 0x80(%rsp), %rax
cmpb $0x23, (%rax)
jne 0x68601
movzbl 0x1(%rax), %eax
cmpl $0x43, %eax
je 0x686ec
cmpl $0x23, %eax
jne 0x68601
movq %r14, %rdi
leaq 0x80(%rsp), %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
jmp 0x685a4
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
pushq $0x9
popq %rdx
callq 0x40349
xorl %r15d, %r15d
cmpq $0x120, %r15            # imm = 0x120
je 0x68649
movq 0xa0(%rsp), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0x8430
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8480
addq $0x20, %r15
jmp 0x6861c
xorl %r15d, %r15d
xorl %r12d, %r12d
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
decq %rax
addq %r15, %rsi
cmpq %rax, %r12
je 0x686a8
movq %rbp, %rdi
callq 0x68b88
movq (%rax), %rax
movq 0xa0(%rsp), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %rsi
addq $0x120, %rsi            # imm = 0x120
movq %r14, %rdi
callq 0x8430
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8480
incq %r12
addq $0x20, %r15
jmp 0x6864f
movq %rbp, %rdi
callq 0x68b88
movq (%rax), %rax
movq 0xa0(%rsp), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %rsi
addq $0x120, %rsi            # imm = 0x120
movq %r14, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0xa0(%rsp), %rdi
callq 0x28a08
jmp 0x685a4
leaq 0x38(%rsp), %rdi
leaq 0x80(%rsp), %rsi
pushq $0x9
popq %rdx
callq 0x40349
cmpq $0x0, 0x76217(%rip)     # 0xde920
je 0x6882f
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
leaq 0x761f7(%rip), %rsi     # 0xde918
callq 0x4237c
movq %r13, %rdi
movq %r15, %rsi
callq 0x2f6c0
movq %r15, %rdi
callq 0x28a08
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movq 0x20(%rsp), %rcx
subq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jne 0x688cb
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x297dc
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
leaq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x68328
movq %rbp, %rdi
movq %r12, %rsi
callq 0x68c52
movq %r12, %rdi
callq 0x68bee
movq %r15, %rdi
callq 0x28a08
xorl %r15d, %r15d
cmpq $0x120, %r15            # imm = 0x120
je 0x687c8
movq 0x38(%rsp), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0x8430
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8480
addq $0x20, %r15
jmp 0x6879e
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
decq %rax
cmpq %rax, %r12
je 0x68807
movq 0x18(%rsp), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0x8430
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8480
incq %r12
addq $0x20, %r15
jmp 0x687ce
movq 0x20(%rsp), %rsi
addq $-0x20, %rsi
movq %r14, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x38(%rsp), %rdi
callq 0x28a08
jmp 0x685a4
movl $0x120, %r15d           # imm = 0x120
pushq $0x9
popq %r12
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %rax, %r12
je 0x68739
addq %r15, %rsi
movq %r13, %rdi
callq 0x2b0a4
incq %r12
addq $0x20, %r15
jmp 0x68839
leaq 0x50(%rsp), %rdi
callq 0x68bee
leaq 0xd0(%rsp), %rdi
callq 0x28a08
leaq 0x18(%rsp), %rdi
callq 0x28a08
movq %rsp, %rdi
callq 0x28a08
leaq 0x80(%rsp), %rdi
callq 0x8978
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0x110(%rsp), %rdi
callq 0x8978
xorl %eax, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x419d3(%rip), %rdi     # 0xaa2a5
leaq 0x419f2(%rip), %rsi     # 0xaa2cb
leaq 0x41a03(%rip), %rcx     # 0xaa2e3
pushq $0x5f
popq %rdx
callq 0x82f0
movq %rax, %rbx
jmp 0x68989
movq %rax, %rbx
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdi
callq 0x84a0
jmp 0x6899e
movq %rax, %rbx
jmp 0x6899e
jmp 0x68911
jmp 0x68938
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
jmp 0x689a6
jmp 0x68938
jmp 0x6894c
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x28a08
jmp 0x6894f
jmp 0x6894c
jmp 0x68941
movq %rax, %rbx
jmp 0x689ab
jmp 0x6894c
jmp 0x68941
movq %rax, %rbx
jmp 0x68968
jmp 0x6894c
jmp 0x68958
jmp 0x6894c
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x68963
jmp 0x68958
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x28a08
leaq 0x50(%rsp), %rdi
callq 0x68bee
leaq 0xd0(%rsp), %rdi
callq 0x28a08
leaq 0x18(%rsp), %rdi
callq 0x28a08
movq %rsp, %rdi
callq 0x28a08
leaq 0x80(%rsp), %rdi
callq 0x8978
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0x110(%rsp), %rdi
callq 0x8978
movq %rbx, %rdi
callq 0x8870