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

    if (argc < 2) {
        cerr << "usage: " << argv[0] << " -c sample_count -n sample_size" << endl
             << "bootstraps a new table by randomly sampling subsets of tab-separated" << endl
             << "records from stdin.  generates sample_count subsets of sample_size" << endl
             << "randomly selected from lines of input.  for each sampled subset, outputs." << endl
             << "a row of tab-separated averaged values for each value in the table." << endl;
        exit(1);
    }

    unsigned int sample_size = 1;
    unsigned int sample_count = 1;

    while (true) {
        static struct option long_options[] =
        {   
            {"sample-count", required_argument, 0, 'c'},
            {"sample-size",   required_argument, 0, 'n'},

        };
        int option_index = 0;

        int c = getopt_long (argc, argv, "n:c:", long_options, &option_index);

          if (c == -1)
            break;

          switch (c) {
            case 0:
            /* If this option set a flag, do nothing else now. */
            if (long_options[option_index].flag != 0)
              break;
            printf ("option %s", long_options[option_index].name);
            if (optarg)
              printf (" with arg %s", optarg);
            printf ("\n");
            break;

          case 'n':
            sample_size = atoi(optarg);
            break;

          case 'c':
            sample_count = atoi(optarg);
            break;

          default:
            abort();
            break;

        }
    }

    // seed prng with random bits from /dev/random
    fstream random;
    random.open("/dev/random", fstream::in);
    int seed;
    random.get((char*) &seed, sizeof(int));
    random.close();

    init_genrand(seed);


    string header;
    vector<string> header_fields;
    getline(cin, header);

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

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

    map<string, unsigned int> counts;
    map<string, long double> totals;

    string line;
    getline(cin, line);
    while (!line.empty()) {
        vector<string> fields = split(line, '\t');
        int i = 0;
        vector<double> vals;
        for (vector<string>::iterator f = fields.begin(); f != fields.end(); ++f) {
            vals.push_back(atof(f->c_str()));
        }
        lines.push_back(vals);
        getline(cin, line);
    }

    cout << header << endl;

    for (int i = 0; i < sample_count; ++i) {
        vector<vector<double>* > subset = randomsubset(lines, sample_size);
        vector<double> avgs = subsetavg(subset);
        cout << join(avgs, "\t") << endl;
    }

    return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
movq %rsi, %r14
cmpl $0x2, %edi
jl 0x3240
movl %edi, %ebp
movl $0x1, 0x8(%rsp)
leaq 0x24ca(%rip), %r15      # 0x51cf
leaq 0x54b4(%rip), %r12      # 0x81c0
leaq 0x148(%rsp), %r13
movq 0x529d(%rip), %rbx      # 0x7fb8
movl $0x1, 0xc(%rsp)
movl $0x0, 0x148(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x22f0
cmpl $0x62, %eax
jle 0x2d62
cmpl $0x63, %eax
je 0x2db3
cmpl $0x6e, %eax
jne 0x323b
movq (%rbx), %rdi
callq 0x2280
movl %eax, 0xc(%rsp)
jmp 0x2d23
testl %eax, %eax
jne 0x2dc4
movslq 0x148(%rsp), %rax
shlq $0x5, %rax
cmpq $0x0, 0x10(%rax,%r12)
jne 0x2d23
addq %r12, %rax
movq (%rax), %rsi
leaq 0x244d(%rip), %rdi      # 0x51d4
xorl %eax, %eax
callq 0x2040
movq (%rbx), %rsi
testq %rsi, %rsi
je 0x2da4
leaq 0x2441(%rip), %rdi      # 0x51de
xorl %eax, %eax
callq 0x2040
movl $0xa, %edi
callq 0x2240
jmp 0x2d23
movq (%rbx), %rdi
callq 0x2280
movl %eax, 0x8(%rsp)
jmp 0x2d23
cmpl $-0x1, %eax
jne 0x323b
leaq 0x148(%rsp), %r14
movq %r14, %rdi
callq 0x2210
leaq 0x2407(%rip), %rsi      # 0x51eb
movq %r14, %rdi
movl $0x8, %edx
callq 0x2190
movq 0x148(%rsp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %ecx
leaq 0x148(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movl $0x4, %edx
callq 0x2150
leaq 0x148(%rsp), %rdi
callq 0x2050
movl 0x34(%rsp), %eax
movq %rax, 0x53f4(%rip)      # 0x8230
movl $0x1, %ecx
leaq 0x53e8(%rip), %rdx      # 0x8230
movq %rax, %rsi
shrq $0x1e, %rsi
xorl %eax, %esi
imull $0x6c078965, %esi, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, (%rdx,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0x2e48
movl $0x270, 0x533d(%rip)    # imm = 0x270
leaq 0x68(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq %rax, 0xe0(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movq 0x5139(%rip), %rdi      # 0x7fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %edx
movq 0x511e(%rip), %r14      # 0x7fd8
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x2320
cmpq $0x0, 0x60(%rsp)
je 0x32db
leaq 0x58(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
movl $0x9, %esi
callq 0x40c4
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
xorl %eax, %eax
movq %rax, 0xc0(%rsp)
leaq 0x120(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0xf0(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x88(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %edx
movq 0x5074(%rip), %rdi      # 0x7fd8
leaq 0x78(%rsp), %rsi
callq 0x2320
cmpq $0x0, 0x80(%rsp)
je 0x307d
leaq 0x38(%rsp), %rbx
leaq 0x78(%rsp), %r15
leaq 0x10(%rsp), %r13
leaq 0x98(%rsp), %rbp
movq %rbx, %r12
movq %rbx, %rdi
movq %r15, %r14
movq %r15, %rsi
movl $0x9, %edx
callq 0x41be
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x38(%rsp), %rbx
movq 0x40(%rsp), %r15
cmpq %r15, %rbx
je 0x300c
movq (%rbx), %rdi
callq 0x2230
movsd %xmm0, 0x98(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2ff6
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3006
movq %r13, %rdi
movq %rbp, %rdx
callq 0x3a0a
movq 0x40(%rsp), %r15
addq $0x20, %rbx
jmp 0x2fc5
leaq 0xb0(%rsp), %rdi
movq %r13, %rsi
callq 0x348c
movq %r14, %r15
movq 0x4fb2(%rip), %rcx      # 0x7fd8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %edx
movq 0x4f94(%rip), %rdi      # 0x7fd8
movq %r15, %rsi
callq 0x2320
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
movq %r12, %rbx
je 0x3066
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %rbx, %rdi
callq 0x34b8
cmpq $0x0, 0x80(%rsp)
jne 0x2f94
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x4f3a(%rip), %rdi      # 0x7fc8
callq 0x2200
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
cmpl $0x0, 0x8(%rsp)
je 0x31a8
leaq 0x48(%rsp), %rbx
leaq 0x10(%rsp), %r14
leaq 0x98(%rsp), %r12
leaq 0x38(%rsp), %r15
movq 0x4ee2(%rip), %r13      # 0x7fc8
movq %r14, %rdi
leaq 0xb0(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x2b5e
movq %r12, %rdi
movq %r14, %rsi
callq 0x2885
movq %r15, %rdi
movq %r12, %rsi
leaq 0x20ff(%rip), %rdx      # 0x5211
callq 0x3516
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r13, %rdi
callq 0x2200
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x316a
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3187
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x319e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
decl 0x8(%rsp)
jne 0x30e6
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x31ca
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0xe8(%rsp), %rdi
callq 0x369e
leaq 0x118(%rsp), %rdi
callq 0x3716
leaq 0xb0(%rsp), %rdi
callq 0x35f0
leaq 0xd0(%rsp), %rdi
callq 0x34b8
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x321a
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x148(%rsp), %rdi
callq 0x2070
xorl %eax, %eax
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x20f0
movq 0x4da9(%rip), %rdi      # 0x7ff0
leaq 0x1e2a(%rip), %rsi      # 0x5078
callq 0x21c0
movq (%r14), %rsi
movq %rax, %rdi
callq 0x21c0
leaq 0x1e1b(%rip), %rsi      # 0x5080
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1e24(%rip), %rsi      # 0x50a0
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1e52(%rip), %rsi      # 0x50e5
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1e7e(%rip), %rsi      # 0x5128
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1eb1(%rip), %rsi      # 0x5172
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
movl $0x1, %edi
callq 0x2220
movq 0x4d0e(%rip), %rdi      # 0x7ff0
leaq 0x1f0e(%rip), %rsi      # 0x51f7
movl $0x19, %edx
callq 0x2200
movq 0x4cf6(%rip), %rdi      # 0x7ff0
callq 0x20d0
jmp 0x32d1
jmp 0x3303
movq %rax, %rbx
jmp 0x3411
movq %rax, %rbx
jmp 0x33e8
jmp 0x3321
jmp 0x3321
movq %rax, %rbx
jmp 0x3362
movq %rax, %rbx
jmp 0x3345
movq %rax, %rbx
jmp 0x339f
jmp 0x337b
movq %rbx, %rcx
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %rcx, %rdi
je 0x3345
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3362
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x339f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
jmp 0x339f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3395
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
leaq 0x38(%rsp), %rdi
callq 0x34b8
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x33c1
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0xe8(%rsp), %rdi
callq 0x369e
leaq 0x118(%rsp), %rdi
callq 0x3716
leaq 0xb0(%rsp), %rdi
callq 0x35f0
leaq 0xd0(%rsp), %rdi
callq 0x34b8
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x3411
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x148(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2300