// File: 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
movq %rsp, %rbp
subq $0x330, %rsp            # imm = 0x330
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0xd3f90
leaq 0xc7833(%rip), %rsi     # 0x19ad98
leaq -0x30(%rbp), %rdi
callq 0x7be70
movq 0xc5a7b(%rip), %rdi     # 0x198ff0
leaq 0x7323e(%rip), %rsi     # 0x1467ba
callq 0xa5c0
movq %rax, -0x220(%rbp)
jmp 0xd358a
movq -0x220(%rbp), %rdi
leaq 0xc7800(%rip), %rsi     # 0x19ad98
callq 0xa520
movq %rax, -0x228(%rbp)
jmp 0xd35a6
movq -0x228(%rbp), %rdi
movq 0xc59d4(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0xd35bb
movq 0xc5a2e(%rip), %rdi     # 0x198ff0
leaq 0x73209(%rip), %rsi     # 0x1467d2
callq 0xa5c0
movq %rax, -0x230(%rbp)
jmp 0xd35d7
movq -0x230(%rbp), %rdi
leaq 0xc77d3(%rip), %rsi     # 0x19adb8
callq 0xa520
movq %rax, -0x238(%rbp)
jmp 0xd35f3
movq -0x238(%rbp), %rdi
movq 0xc5987(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0xd3608
leaq 0xc7789(%rip), %rdi     # 0x19ad98
callq 0xa220
movq %rax, -0x248(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0xab60
movq -0x248(%rbp), %rsi
movq -0x240(%rbp), %rdx
leaq -0x68(%rbp), %rdi
callq 0x2d6c0
jmp 0xd3644
leaq -0x68(%rbp), %rdi
movl $0x8, %esi
callq 0x80e30
movq %rax, -0x250(%rbp)
jmp 0xd365b
leaq -0x68(%rbp), %rdi
callq 0xac88
leaq -0x69(%rbp), %rdi
callq 0xa640
movq -0x250(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0xc7779(%rip), %rdx     # 0x19adf8
leaq -0xb0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x3e290
jmp 0xd3691
leaq 0x7314e(%rip), %rdx     # 0x1467e6
leaq -0x90(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3e020
jmp 0xd36ad
leaq -0xb0(%rbp), %rdi
callq 0xac88
movl $0x200, %edi            # imm = 0x200
callq 0xa5d0
movq %rax, -0x258(%rbp)
jmp 0xd36cc
leaq -0x90(%rbp), %rdi
callq 0xa220
movq -0x258(%rbp), %rdi
movq %rax, %rsi
movl $0x10, %edx
callq 0xa650
jmp 0xd36ee
movq -0x258(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x0, -0xc0(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0xa820
leaq -0xf8(%rbp), %rdi
callq 0x48da0
leaq 0xc7693(%rip), %rsi     # 0x19adb8
leaq -0x110(%rbp), %rdi
callq 0x80aa0
jmp 0xd3733
leaq -0x128(%rbp), %rdi
callq 0x48da0
leaq -0x158(%rbp), %rdi
callq 0xd4280
movq -0x48(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0xa0d0
movq %rax, -0x260(%rbp)
jmp 0xd3764
movq -0x260(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0xa740
movb %al, -0x261(%rbp)
jmp 0xd3782
movb -0x261(%rbp), %al
testb $0x1, %al
jne 0xd3791
jmp 0xd3ed9
xorl %eax, %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0xac60
movq %rax, -0x270(%rbp)
jmp 0xd37aa
movq -0x270(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0xd38d4
leaq -0xe0(%rbp), %rdi
movl $0x1, %esi
callq 0xac60
movq %rax, -0x278(%rbp)
jmp 0xd37d7
movq -0x278(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0xd38d4
movq -0xb8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0xa520
movq %rax, -0x280(%rbp)
jmp 0xd3806
movq -0x280(%rbp), %rdi
movq 0xc5774(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0xd381b
jmp 0xd3ed4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xd3f7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xd3854
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xac88
leaq -0x69(%rbp), %rdi
callq 0xa640
jmp 0xd3f7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xac88
jmp 0xd3f7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xd3f71
movq -0x258(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x200, %esi            # imm = 0x200
callq 0xa5e0
jmp 0xd3f71
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xd3f59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xd3f35
xorl %eax, %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0xac60
movq %rax, -0x288(%rbp)
jmp 0xd38ed
movq -0x288(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0xd3c9d
leaq -0xe0(%rbp), %rdi
movl $0x1, %esi
callq 0xac60
movq %rax, -0x290(%rbp)
jmp 0xd391a
movq -0x290(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x43, %eax
jne 0xd3c9d
leaq -0x170(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movl $0x9, %edx
callq 0x7bbe0
jmp 0xd3947
leaq -0x170(%rbp), %rdi
callq 0x3ffd0
subq $0x9, %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
shll %eax
movl %eax, -0xbc(%rbp)
leaq 0xc7464(%rip), %rdi     # 0x19add8
callq 0xaa70
testb $0x1, %al
jne 0xd3982
jmp 0xd3a03
movq $0x9, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x298(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x3ffd0
movq %rax, %rcx
movq -0x298(%rbp), %rax
cmpq %rcx, %rax
je 0xd3a01
movq -0x180(%rbp), %rsi
leaq -0x170(%rbp), %rdi
callq 0x3d940
movq %rax, %rsi
leaq -0xf8(%rbp), %rdi
callq 0x48dc0
jmp 0xd39da
jmp 0xd39dc
movq -0x180(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x180(%rbp)
jmp 0xd398d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xd3c8c
jmp 0xd3a37
leaq 0xc73ce(%rip), %rsi     # 0x19add8
leaq -0x198(%rbp), %rdi
callq 0x80aa0
jmp 0xd3a18
leaq -0xf8(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x50c30
leaq -0x198(%rbp), %rdi
callq 0x3dfa0
leaq -0xf8(%rbp), %rdi
callq 0x3ffd0
movq %rax, -0x2a0(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x3ffd0
movq %rax, %rcx
movq -0x2a0(%rbp), %rax
cmpq %rcx, %rax
jne 0xd3a67
jmp 0xd3a86
leaq 0x72d87(%rip), %rdi     # 0x1467f5
leaq 0x72da6(%rip), %rsi     # 0x14681b
movl $0x5f, %edx
leaq 0x72db2(%rip), %rcx     # 0x146833
callq 0xa390
leaq -0x1e0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x3fee0
jmp 0xd3a9b
leaq -0x1c8(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0x1e0(%rbp), %rdx
callq 0xd32f0
jmp 0xd3ab7
leaq -0x158(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0xd4430
leaq -0x1c8(%rbp), %rdi
callq 0xd4410
leaq -0x1e0(%rbp), %rdi
callq 0x3dfa0
movq $0x0, -0x1e8(%rbp)
cmpq $0x9, -0x1e8(%rbp)
je 0xd3b7c
movq -0xb8(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movq -0x1e8(%rbp), %rsi
leaq -0x170(%rbp), %rdi
callq 0x3d940
movq -0x2b0(%rbp), %rdi
movq %rax, %rsi
callq 0xa520
movq %rax, -0x2a8(%rbp)
jmp 0xd3b34
movq -0x2a8(%rbp), %rdi
leaq 0x6239c(%rip), %rsi     # 0x135ede
callq 0xa5c0
jmp 0xd3b49
jmp 0xd3b4b
movq -0x1e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1e8(%rbp)
jmp 0xd3aed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x3dfa0
jmp 0xd3c8c
movq $0x0, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rax
movq %rax, -0x2b8(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3ffd0
movq %rax, %rcx
movq -0x2b8(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0xd3c1b
movq -0xb8(%rbp), %rax
movq %rax, -0x2c8(%rbp)
movq -0x1f0(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x3d940
movq -0x2c8(%rbp), %rdi
movq %rax, %rsi
callq 0xa520
movq %rax, -0x2c0(%rbp)
jmp 0xd3bed
movq -0x2c0(%rbp), %rdi
leaq 0x622e3(%rip), %rsi     # 0x135ede
callq 0xa5c0
jmp 0xd3c02
jmp 0xd3c04
movq -0x1f0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1f0(%rbp)
jmp 0xd3b87
movq -0xb8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
leaq -0x110(%rbp), %rdi
movq %rdi, -0x2e0(%rbp)
callq 0x3ffd0
movq -0x2e0(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x3d940
movq -0x2d8(%rbp), %rdi
movq %rax, %rsi
callq 0xa520
movq %rax, -0x2d0(%rbp)
jmp 0xd3c66
movq -0x2d0(%rbp), %rdi
movq 0xc5314(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0xd3c7b
leaq -0x170(%rbp), %rdi
callq 0x3dfa0
jmp 0xd3ed2
leaq -0x170(%rbp), %rdi
callq 0x3dfa0
jmp 0xd3f35
movl -0xc0(%rbp), %eax
incl %eax
movl %eax, -0xc0(%rbp)
leaq -0x208(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movl $0x9, %edx
callq 0x7bbe0
jmp 0xd3cc5
movq $0x0, -0x210(%rbp)
cmpq $0x9, -0x210(%rbp)
je 0xd3d5f
movq -0xb8(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq -0x210(%rbp), %rsi
leaq -0x208(%rbp), %rdi
callq 0x3d940
movq -0x2f0(%rbp), %rdi
movq %rax, %rsi
callq 0xa520
movq %rax, -0x2e8(%rbp)
jmp 0xd3d17
movq -0x2e8(%rbp), %rdi
leaq 0x621b9(%rip), %rsi     # 0x135ede
callq 0xa5c0
jmp 0xd3d2c
jmp 0xd3d2e
movq -0x210(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x210(%rbp)
jmp 0xd3cd0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x3dfa0
jmp 0xd3f35
movq $0x0, -0x218(%rbp)
movq -0x218(%rbp), %rax
movq %rax, -0x2f8(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3ffd0
movq %rax, %rcx
movq -0x2f8(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0xd3e34
movq -0xb8(%rbp), %rax
movq %rax, -0x308(%rbp)
movq -0x218(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x3d940
movq %rax, %rsi
leaq -0x158(%rbp), %rdi
callq 0xd4330
movq %rax, -0x300(%rbp)
jmp 0xd3dd4
movq -0x300(%rbp), %rax
movq (%rax), %rsi
addq $0x9, %rsi
leaq -0x208(%rbp), %rdi
callq 0x3d940
movq -0x308(%rbp), %rdi
movq %rax, %rsi
callq 0xa520
movq %rax, -0x310(%rbp)
jmp 0xd3e06
movq -0x310(%rbp), %rdi
leaq 0x620ca(%rip), %rsi     # 0x135ede
callq 0xa5c0
jmp 0xd3e1b
jmp 0xd3e1d
movq -0x218(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x218(%rbp)
jmp 0xd3d6a
movq -0xb8(%rbp), %rax
movq %rax, -0x328(%rbp)
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x320(%rbp)
callq 0x3ffd0
movq -0x320(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x3d940
movq %rax, %rsi
leaq -0x158(%rbp), %rdi
callq 0xd4330
movq %rax, -0x318(%rbp)
jmp 0xd3e7f
movq -0x318(%rbp), %rax
movq (%rax), %rsi
addq $0x9, %rsi
leaq -0x208(%rbp), %rdi
callq 0x3d940
movq -0x328(%rbp), %rdi
movq %rax, %rsi
callq 0xa520
movq %rax, -0x330(%rbp)
jmp 0xd3eb1
movq -0x330(%rbp), %rdi
movq 0xc50c9(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0xd3ec6
leaq -0x208(%rbp), %rdi
callq 0x3dfa0
jmp 0xd3ed4
jmp 0xd374b
leaq -0x158(%rbp), %rdi
callq 0xd4410
leaq -0x128(%rbp), %rdi
callq 0x3dfa0
leaq -0x110(%rbp), %rdi
callq 0x3dfa0
leaq -0xf8(%rbp), %rdi
callq 0x3dfa0
leaq -0xe0(%rbp), %rdi
callq 0xac88
leaq -0x90(%rbp), %rdi
callq 0xac88
leaq -0x30(%rbp), %rdi
callq 0xac88
xorl %eax, %eax
addq $0x330, %rsp            # imm = 0x330
popq %rbp
retq
leaq -0x158(%rbp), %rdi
callq 0xd4410
leaq -0x128(%rbp), %rdi
callq 0x3dfa0
leaq -0x110(%rbp), %rdi
callq 0x3dfa0
leaq -0xf8(%rbp), %rdi
callq 0x3dfa0
leaq -0xe0(%rbp), %rdi
callq 0xac88
leaq -0x90(%rbp), %rdi
callq 0xac88
leaq -0x30(%rbp), %rdi
callq 0xac88
movq -0x38(%rbp), %rdi
callq 0xab50
nop