// File: /workspace/github_cpp_makefile_star2_O1/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 $0x168, %rsp            # imm = 0x168
callq 0x89eba
leaq 0x83292(%rip), %rsi     # 0x10c8a0
leaq 0x110(%rsp), %rdi
callq 0x50803
movq 0x819c6(%rip), %rdi     # 0x10afe8
leaq 0x58971(%rip), %rsi     # 0xe1f9a
movl $0x17, %edx
callq 0x84e0
movq 0x83266(%rip), %rsi     # 0x10c8a0
movq 0x83267(%rip), %rdx     # 0x10c8a8
movq 0x819a0(%rip), %rdi     # 0x10afe8
callq 0x84e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0x8196a(%rip), %rdi     # 0x10afe8
leaq 0x5892d(%rip), %rsi     # 0xe1fb2
movl $0x13, %edx
callq 0x84e0
movq 0x8322a(%rip), %rsi     # 0x10c8c0
movq 0x8322b(%rip), %rdx     # 0x10c8c8
movq 0x81944(%rip), %rdi     # 0x10afe8
callq 0x84e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
movq 0x831c6(%rip), %rsi     # 0x10c8a0
leaq 0x58(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x286de
leaq 0x58(%rsp), %rdi
movl $0x8, %esi
callq 0x53224
movq %rax, %rbx
leaq 0x68(%rsp), %r14
movq -0x10(%r14), %rdi
cmpq %r14, %rdi
je 0x89716
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x831e3(%rip), %rdx     # 0x10c900
leaq 0x58(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x31379
leaq 0x58890(%rip), %rsi     # 0xe1fc6
leaq 0x58(%rsp), %rdi
callq 0x88c0
leaq 0xb8(%rsp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x89770
movq %rdx, 0xa8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xb8(%rsp)
jmp 0x89776
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0xb0(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x897a8
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x8480
movl $0x200, %edi            # imm = 0x200
callq 0x8470
movq %rax, %r14
movq 0xa8(%rsp), %rsi
movq %rax, %rdi
movl $0x10, %edx
callq 0x84c0
leaq 0x98(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
leaq 0x830ce(%rip), %rsi     # 0x10c8c0
leaq 0xe0(%rsp), %rdi
callq 0x52fb6
xorps %xmm0, %xmm0
movaps %xmm0, 0x130(%rsp)
xorl %eax, %eax
movq %rax, 0x140(%rsp)
leaq 0x60(%rsp), %rcx
movl $0x0, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x88(%rsp), %r15
leaq 0x58(%rsp), %rbp
leaq 0x4843b(%rip), %r13     # 0xd1c7e
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8870
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x89cb1
movq 0x88(%rsp), %rsi
movb (%rsi), %al
cmpb $0x23, %al
jne 0x8997e
cmpb $0x23, 0x1(%rsi)
jne 0x898ce
movq 0x90(%rsp), %rdx
movq %r14, %rdi
callq 0x84e0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
jmp 0x89843
cmpb $0x23, %al
jne 0x8997e
cmpb $0x43, 0x1(%rsi)
jne 0x8997e
leaq 0xc8(%rsp), %rdi
movq %r15, %rsi
movl $0x9, %edx
callq 0x50624
cmpq $0x0, 0x82feb(%rip)     # 0x10c8e8
je 0x89ab9
leaq 0xf8(%rsp), %r15
movq %r15, %rdi
leaq 0x82fcb(%rip), %rsi     # 0x10c8e0
callq 0x52fb6
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps (%rsp), %xmm0
movq 0x10(%rsp), %rax
movups 0xf8(%rsp), %xmm1
movaps %xmm1, (%rsp)
movq 0x108(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movaps 0x20(%rsp), %xmm1
movaps %xmm0, 0x20(%rsp)
movq %rax, 0x30(%rsp)
movups %xmm1, 0xf8(%rsp)
movq $0x0, 0x108(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x3123c
movq %r15, %rdi
callq 0x3123c
jmp 0x89b0e
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
movl $0x9, %edx
callq 0x50624
movl $0x8, %r15d
movq 0x20(%rsp), %rax
movq -0x8(%rax,%r15), %rsi
movq (%rax,%r15), %rdx
movq %r14, %rdi
callq 0x84e0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x84e0
addq $0x20, %r15
cmpq $0x128, %r15            # imm = 0x128
jne 0x89996
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
decq %rax
je 0x89a42
xorl %r12d, %r12d
xorl %r15d, %r15d
addq %r12, %rsi
movq %rbp, %rdi
callq 0x8a170
movq (%rax), %rax
movq 0x20(%rsp), %rcx
shlq $0x5, %rax
movq 0x120(%rcx,%rax), %rsi
movq 0x128(%rcx,%rax), %rdx
movq %r14, %rdi
callq 0x84e0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x84e0
incq %r15
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
decq %rax
addq $0x20, %r12
cmpq %rax, %r15
jne 0x899e4
shlq $0x5, %rax
addq %rsi, %rax
movq %rbp, %rdi
movq %rax, %rsi
callq 0x8a170
movq (%rax), %rax
movq 0x20(%rsp), %rcx
shlq $0x5, %rax
movq 0x120(%rcx,%rax), %rsi
movq 0x128(%rcx,%rax), %rdx
movq %r14, %rdi
callq 0x84e0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
leaq 0x20(%rsp), %rdi
callq 0x3123c
leaq 0x88(%rsp), %r15
jmp 0x89843
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rax
subq %rsi, %rax
cmpq $0x120, %rax            # imm = 0x120
je 0x89b0e
movl $0x9, %r15d
movl $0x120, %r12d           # imm = 0x120
addq %r12, %rsi
movq %rsp, %rdi
callq 0x33db0
incq %r15
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
addq $0x20, %r12
cmpq %rax, %r15
jne 0x89ae0
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movq 0xe8(%rsp), %rcx
subq 0xe0(%rsp), %rcx
cmpq %rcx, %rax
jne 0x89d5c
leaq 0x150(%rsp), %r15
movq %r15, %rdi
movq %rsp, %rsi
callq 0x3273a
leaq 0x20(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
movq %r15, %rdx
callq 0x894cc
movq %rbp, %rdi
callq 0x8a264
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x89bb2
movl 0x28(%rsp), %ecx
movl %ecx, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movups 0x38(%rsp), %xmm0
movups %xmm0, 0x70(%rsp)
leaq 0x60(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x48(%rsp), %rax
movq %rax, 0x80(%rsp)
xorl %eax, %eax
movq %rax, 0x30(%rsp)
leaq 0x28(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x8a1ec
movq %r15, %rdi
callq 0x3123c
movl $0x8, %r15d
movq 0xc8(%rsp), %rax
movq -0x8(%rax,%r15), %rsi
movq (%rax,%r15), %rdx
movq %r14, %rdi
callq 0x84e0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x84e0
addq $0x20, %r15
cmpq $0x128, %r15            # imm = 0x128
jne 0x89bca
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cmpq $0x20, %rax
je 0x89c62
movq $-0x1, %r15
movl $0x8, %r12d
movq 0xe0(%rsp), %rax
movq -0x8(%rax,%r12), %rsi
movq (%rax,%r12), %rdx
movq %r14, %rdi
callq 0x84e0
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x84e0
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
addq $-0x2, %rax
incq %r15
addq $0x20, %r12
cmpq %rax, %r15
jne 0x89c1c
movq 0xe8(%rsp), %rax
movq -0x20(%rax), %rsi
movq -0x18(%rax), %rdx
movq %r14, %rdi
callq 0x84e0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x8450
movsbl %al, %esi
movq %r12, %rdi
callq 0x8040
movq %rax, %rdi
callq 0x8300
leaq 0xc8(%rsp), %rdi
jmp 0x89aa7
leaq 0x58(%rsp), %rdi
callq 0x8a1ec
leaq 0x130(%rsp), %rdi
callq 0x3123c
leaq 0xe0(%rsp), %rdi
callq 0x3123c
movq %rsp, %rdi
callq 0x3123c
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x89d02
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x89d27
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x89d48
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x8480
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x58272(%rip), %rdi     # 0xe1fd5
leaq 0x58291(%rip), %rsi     # 0xe1ffb
leaq 0x582a2(%rip), %rcx     # 0xe2013
movl $0x5f, %edx
callq 0x82c0
movq %rax, %rbx
jmp 0x89e3f
movq %rax, %rbx
movl $0x200, %esi            # imm = 0x200
movq %r14, %rdi
jmp 0x89e67
movq %rax, %rbx
jmp 0x89e6c
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
jne 0x89dc4
jmp 0x89e91
jmp 0x89dea
movq %rax, %rbx
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x89e91
movq 0x68(%rsp), %rsi
jmp 0x89e89
jmp 0x89dea
jmp 0x89dff
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x3123c
jmp 0x89e02
jmp 0x89dff
jmp 0x89df8
jmp 0x89dff
movq %rax, %rbx
jmp 0x89e91
jmp 0x89dff
jmp 0x89df8
jmp 0x89dff
movq %rax, %rbx
jmp 0x89e1b
jmp 0x89e0e
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x89e16
jmp 0x89e0e
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3123c
leaq 0x58(%rsp), %rdi
callq 0x8a1ec
leaq 0x130(%rsp), %rdi
callq 0x3123c
leaq 0xe0(%rsp), %rdi
callq 0x3123c
movq %rsp, %rdi
callq 0x3123c
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x89e6c
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x8480
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x89e91
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x8480
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x89eb2
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x8480
movq %rbx, %rdi
callq 0x8840