// File: ekg[P]tsvtools/tsvsplit.cpp
// Function: main
int main(int argc, char** argv) {

    if (argc == 1) {
        cerr << "usage: " << argv[0] << " [-v] FIELD1 FIELD2 FIELD3 ..." << endl
             << "splits out the fields from the input tab-separated value stream"
             << "specifying '-v' inverts the split, excluding the listed fields" << endl;
        exit(1);
    }

    int i = 1;
    bool invert = false;
    if (string(argv[i]) == "-v") {
        invert = true;
        i = 2;
    }

    vector<string> fieldnames;
    for (; i < argc; ++i) {
        fieldnames.push_back(argv[i]);
    }

    string header;
    getline(cin, header);
    if (header.empty()) {
        cerr << "could not get header line" << endl;
        exit(1);
    }

    vector<string> header_fields;
    split(header, '\t', header_fields);

    // if we are inverting the selection, invert the list of fieldnames
    if (invert) {
        map<string, bool> fieldnamesToRemove;
        for (vector<string>::iterator f = fieldnames.begin(); f != fieldnames.end(); ++f) {
            fieldnamesToRemove[*f] = true;
        }
        fieldnames.clear();
        for (vector<string>::iterator h = header_fields.begin(); h != header_fields.end(); ++h) {
            if (fieldnamesToRemove.find(*h) == fieldnamesToRemove.end()) {
                fieldnames.push_back(*h);
            }
        }
    }

    vector<int> columns;

    for (vector<string>::iterator f = fieldnames.begin(); f != fieldnames.end(); ++f) {
        const string& fieldname = *f;
        int i = 0;
        for (vector<string>::iterator h = header_fields.begin(); h != header_fields.end(); ++h) {
            const string& headername = *h;
            if (headername == fieldname) {
                columns.push_back(i);
                break;
            }
            ++i;
        }
    }

    cout << join(fieldnames, "\t") << endl;

    string line;
    getline(cin, line);
    while (!line.empty()) {
        vector<string> fields = split(line, '\t');
        for (vector<int>::iterator c = columns.begin(); c != columns.end(); ++c) {
            cout << (c == columns.begin() ? "" : "\t") << fields[*c];
        }
        cout << endl;
        getline(cin, line);
    }

    return 0;

}

// Assembly:
subq $0x258, %rsp            # imm = 0x258
movl $0x0, 0x254(%rsp)
movl %edi, 0x250(%rsp)
movq %rsi, 0x248(%rsp)
cmpl $0x1, 0x250(%rsp)
jne 0x2576
movq 0x8aee(%rip), %rdi      # 0xaff0
leaq 0x4afb(%rip), %rsi      # 0x7004
callq 0x21f0
movq %rax, %rdi
movq 0x248(%rsp), %rax
movq (%rax), %rsi
callq 0x21f0
movq %rax, %rdi
leaq 0x4ae1(%rip), %rsi      # 0x700c
callq 0x21f0
movq %rax, %rdi
movq 0x8a7e(%rip), %rsi      # 0xafb8
callq 0x2230
movq %rax, %rdi
leaq 0x4ae2(%rip), %rsi      # 0x702b
callq 0x21f0
movq %rax, %rdi
leaq 0x4b13(%rip), %rsi      # 0x706b
callq 0x21f0
movq %rax, %rdi
movq 0x8a51(%rip), %rsi      # 0xafb8
callq 0x2230
movl $0x1, %edi
callq 0x2280
movl $0x1, 0x244(%rsp)
movb $0x0, 0x243(%rsp)
movq 0x248(%rsp), %rax
movslq 0x244(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x60(%rsp)
leaq 0x21f(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x2350
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq 0x220(%rsp), %rdi
callq 0x2e40
jmp 0x25cd
leaq 0x4ad6(%rip), %rsi      # 0x70aa
leaq 0x220(%rsp), %rdi
callq 0x2e10
movb %al, 0x5f(%rsp)
jmp 0x25e7
leaq 0x220(%rsp), %rdi
callq 0x2110
leaq 0x21f(%rsp), %rdi
callq 0x2250
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0x260b
jmp 0x2669
movb $0x1, 0x243(%rsp)
movl $0x2, 0x244(%rsp)
jmp 0x2669
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
jmp 0x2657
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
leaq 0x220(%rsp), %rdi
callq 0x2110
leaq 0x21f(%rsp), %rdi
callq 0x2250
jmp 0x2dfa
leaq 0x1f0(%rsp), %rdi
callq 0x2f00
movl 0x244(%rsp), %eax
cmpl 0x250(%rsp), %eax
jge 0x275e
movq 0x248(%rsp), %rax
movslq 0x244(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x48(%rsp)
leaq 0x1cf(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x2350
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq 0x1d0(%rsp), %rdi
callq 0x2e40
jmp 0x26ce
leaq 0x1f0(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x2f10
jmp 0x26e5
leaq 0x1d0(%rsp), %rdi
callq 0x2110
leaq 0x1cf(%rsp), %rdi
callq 0x2250
movl 0x244(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x244(%rsp)
jmp 0x2676
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
jmp 0x274c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x2110
leaq 0x1cf(%rsp), %rdi
callq 0x2250
jmp 0x2ded
leaq 0x1a8(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x22c0
movq 0x40(%rsp), %rsi
movq 0x885c(%rip), %rdi      # 0xafd8
callq 0x2060
jmp 0x2783
leaq 0x1a8(%rsp), %rdi
callq 0x2330
testb $0x1, %al
jne 0x2796
jmp 0x27e6
movq 0x8853(%rip), %rdi      # 0xaff0
leaq 0x4909(%rip), %rsi      # 0x70ad
callq 0x21f0
movq %rax, 0x38(%rsp)
jmp 0x27b0
movq 0x38(%rsp), %rdi
movq 0x87fc(%rip), %rsi      # 0xafb8
callq 0x2230
jmp 0x27c3
movl $0x1, %edi
callq 0x2280
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
jmp 0x2de0
leaq 0x190(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x2f00
movq 0x30(%rsp), %rdx
leaq 0x1a8(%rsp), %rdi
movl $0x9, %esi
callq 0x6590
jmp 0x2811
testb $0x1, 0x243(%rsp)
je 0x29e3
leaq 0x160(%rsp), %rdi
callq 0x2f40
leaq 0x1f0(%rsp), %rdi
callq 0x2f50
movq %rax, 0x158(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0x2fc0
movq %rax, 0x150(%rsp)
leaq 0x158(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x2f80
testb $0x1, %al
jne 0x2871
jmp 0x28eb
leaq 0x158(%rsp), %rdi
callq 0x30e0
movq %rax, %rsi
leaq 0x160(%rsp), %rdi
callq 0x2ff0
movq %rax, 0x28(%rsp)
jmp 0x2895
movq 0x28(%rsp), %rax
movb $0x1, (%rax)
leaq 0x158(%rsp), %rdi
callq 0x30f0
jmp 0x2841
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
jmp 0x2dd3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x3220
jmp 0x2dd3
leaq 0x1f0(%rsp), %rdi
callq 0x3110
leaq 0x190(%rsp), %rdi
callq 0x2f50
movq %rax, 0x148(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x2fc0
movq %rax, 0x140(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x2f80
testb $0x1, %al
jne 0x2940
jmp 0x29d6
leaq 0x148(%rsp), %rdi
callq 0x30e0
movq %rax, %rsi
leaq 0x160(%rsp), %rdi
callq 0x3150
movq %rax, 0x20(%rsp)
jmp 0x2964
movq 0x20(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x3180
movq %rax, 0x130(%rsp)
leaq 0x138(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x3130
testb $0x1, %al
jne 0x29a1
jmp 0x29c2
leaq 0x148(%rsp), %rdi
callq 0x30e0
movq %rax, %rsi
leaq 0x1f0(%rsp), %rdi
callq 0x31b0
jmp 0x29c0
jmp 0x29c2
jmp 0x29c4
leaq 0x148(%rsp), %rdi
callq 0x30f0
jmp 0x290d
leaq 0x160(%rsp), %rdi
callq 0x3220
leaq 0x118(%rsp), %rdi
callq 0x3230
leaq 0x1f0(%rsp), %rdi
callq 0x2f50
movq %rax, 0x110(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0x2fc0
movq %rax, 0x108(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x2f80
testb $0x1, %al
jne 0x2a38
jmp 0x2b39
leaq 0x110(%rsp), %rdi
callq 0x30e0
movq %rax, 0x100(%rsp)
movl $0x0, 0xfc(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x2f50
movq %rax, 0xf0(%rsp)
leaq 0x190(%rsp), %rdi
callq 0x2fc0
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x2f80
testb $0x1, %al
jne 0x2aa0
jmp 0x2b25
leaq 0xf0(%rsp), %rdi
callq 0x30e0
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rdi
movq 0x100(%rsp), %rsi
callq 0x3240
testb $0x1, %al
jne 0x2ad0
jmp 0x2b02
leaq 0x118(%rsp), %rdi
leaq 0xfc(%rsp), %rsi
callq 0x32e0
jmp 0x2ae7
jmp 0x2b25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
jmp 0x2dc6
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x30f0
jmp 0x2a6d
jmp 0x2b27
leaq 0x110(%rsp), %rdi
callq 0x30f0
jmp 0x2a05
leaq 0x4587(%rip), %rdx      # 0x70c7
leaq 0xc0(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x3350
jmp 0x2b57
movq 0x846a(%rip), %rdi      # 0xafc8
leaq 0xc0(%rsp), %rsi
callq 0x21c0
movq %rax, 0x18(%rsp)
jmp 0x2b72
movq 0x18(%rsp), %rdi
movq 0x843a(%rip), %rsi      # 0xafb8
callq 0x2230
jmp 0x2b85
leaq 0xc0(%rsp), %rdi
callq 0x2110
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x22c0
movq 0x10(%rsp), %rsi
movq 0x8428(%rip), %rdi      # 0xafd8
callq 0x2060
jmp 0x2bb7
jmp 0x2bb9
leaq 0xa0(%rsp), %rdi
callq 0x2330
xorb $-0x1, %al
testb $0x1, %al
jne 0x2bd1
jmp 0x2d5e
leaq 0x88(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movl $0x9, %edx
callq 0x66a0
jmp 0x2bed
leaq 0x118(%rsp), %rdi
callq 0x3480
movq %rax, 0x80(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x34f0
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x34b0
testb $0x1, %al
jne 0x2c2f
jmp 0x2d21
leaq 0x118(%rsp), %rdi
callq 0x3480
movq %rax, 0x70(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3520
movb %al, %cl
leaq 0x446c(%rip), %rax      # 0x70c8
leaq 0x4464(%rip), %rsi      # 0x70c7
testb %cl, %cl
cmovneq %rax, %rsi
movq 0x8358(%rip), %rdi      # 0xafc8
callq 0x21f0
movq %rax, 0x8(%rsp)
jmp 0x2c7c
leaq 0x80(%rsp), %rdi
callq 0x3580
movslq (%rax), %rsi
leaq 0x88(%rsp), %rdi
callq 0x3560
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x21c0
jmp 0x2ca8
jmp 0x2caa
leaq 0x80(%rsp), %rdi
callq 0x3590
jmp 0x2c02
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x2110
jmp 0x2dc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
jmp 0x2db9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x210(%rsp)
movl %eax, 0x20c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x35b0
jmp 0x2db9
movq 0x82a0(%rip), %rdi      # 0xafc8
movq 0x8289(%rip), %rsi      # 0xafb8
callq 0x2230
jmp 0x2d36
movq 0x829b(%rip), %rdi      # 0xafd8
leaq 0xa0(%rsp), %rsi
callq 0x2060
jmp 0x2d4c
leaq 0x88(%rsp), %rdi
callq 0x35b0
jmp 0x2bb9
movl $0x0, 0x254(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x2110
leaq 0x118(%rsp), %rdi
callq 0x3610
leaq 0x190(%rsp), %rdi
callq 0x35b0
leaq 0x1a8(%rsp), %rdi
callq 0x2110
leaq 0x1f0(%rsp), %rdi
callq 0x35b0
movl 0x254(%rsp), %eax
addq $0x258, %rsp            # imm = 0x258
retq
leaq 0xa0(%rsp), %rdi
callq 0x2110
leaq 0x118(%rsp), %rdi
callq 0x3610
leaq 0x190(%rsp), %rdi
callq 0x35b0
leaq 0x1a8(%rsp), %rdi
callq 0x2110
leaq 0x1f0(%rsp), %rdi
callq 0x35b0
movq 0x210(%rsp), %rdi
callq 0x2340
nopw (%rax,%rax)