// File: /workspace/github_cpp_makefile_star2_O1/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 0x322f
movl %edi, %r15d
movl $0x1, %ebx
leaq 0x54c6(%rip), %r13      # 0x81c0
leaq 0x148(%rsp), %rbp
movl $0x1, 0xc(%rsp)
movl $0x0, 0x148(%rsp)
movl %r15d, %edi
movq %r14, %rsi
leaq 0x24ad(%rip), %rdx      # 0x51cf
movq %r13, %rcx
movq %rbp, %r8
callq 0x22f0
cmpl $0x62, %eax
jg 0x2d93
cmpl $-0x1, %eax
je 0x2db6
testl %eax, %eax
jne 0x322a
movslq 0x148(%rsp), %rax
shlq $0x5, %rax
movb $0x1, %r12b
cmpq $0x0, 0x10(%rax,%r13)
jne 0x2dcf
addq %r13, %rax
movq (%rax), %rsi
leaq 0x2471(%rip), %rdi      # 0x51d4
xorl %eax, %eax
callq 0x2040
movq 0x5247(%rip), %rax      # 0x7fb8
movq (%rax), %rsi
testq %rsi, %rsi
je 0x2d87
leaq 0x245e(%rip), %rdi      # 0x51de
xorl %eax, %eax
callq 0x2040
movl $0xa, %edi
callq 0x2240
jmp 0x2dcf
cmpl $0x63, %eax
je 0x2dbb
cmpl $0x6e, %eax
jne 0x322a
movq 0x5210(%rip), %rax      # 0x7fb8
movq (%rax), %rdi
callq 0x2280
movl %eax, 0xc(%rsp)
jmp 0x2dcc
xorl %r12d, %r12d
jmp 0x2dcf
movq 0x51f6(%rip), %rax      # 0x7fb8
movq (%rax), %rdi
callq 0x2280
movl %eax, %ebx
movb $0x1, %r12b
testb %r12b, %r12b
jne 0x2d0a
leaq 0x148(%rsp), %r14
movq %r14, %rdi
callq 0x2210
leaq 0x23fc(%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, 0x53e9(%rip)      # 0x8230
movl $0x1, %ecx
leaq 0x53dd(%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 0x2e53
movl $0x270, 0x5332(%rip)    # imm = 0x270
leaq 0x88(%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 0x512b(%rip), %rdi      # 0x7fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %edx
movq 0x5110(%rip), %r14      # 0x7fd8
leaq 0x78(%rsp), %rsi
movq %r14, %rdi
callq 0x2320
cmpq $0x0, 0x80(%rsp)
je 0x32ca
leaq 0x78(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
movl $0x9, %esi
callq 0x40b4
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 0x68(%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 0x5066(%rip), %r14      # 0x7fd8
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x2320
cmpq $0x0, 0x60(%rsp)
je 0x306e
leaq 0x38(%rsp), %r15
leaq 0x10(%rsp), %rbp
leaq 0x98(%rsp), %r13
movq %r15, %r12
movq %r15, %rdi
leaq 0x58(%rsp), %rsi
movl $0x9, %edx
callq 0x41ae
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x38(%rsp), %r15
cmpq 0x40(%rsp), %r15
je 0x300c
movq (%r15), %rdi
callq 0x2230
movsd %xmm0, 0x98(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x2ffb
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3006
movq %rbp, %rdi
movq %r13, %rdx
callq 0x39f0
addq $0x20, %r15
jmp 0x2fc8
leaq 0xb0(%rsp), %rdi
movq %rbp, %rsi
callq 0x347c
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %edx
movq %r14, %rdi
leaq 0x58(%rsp), %rsi
callq 0x2320
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
movq %r12, %r15
je 0x305a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq %r15, %rdi
callq 0x34a8
cmpq $0x0, 0x60(%rsp)
jne 0x2f9d
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq 0x4f46(%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
testl %ebx, %ebx
je 0x3197
leaq 0x10(%rsp), %r14
leaq 0x98(%rsp), %r12
leaq 0x38(%rsp), %rbp
movq 0x4ef6(%rip), %r15      # 0x7fc8
movq %r14, %rdi
leaq 0xb0(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x2b4b
movq %r12, %rdi
movq %r14, %rsi
callq 0x2878
movq %rbp, %rdi
movq %r12, %rsi
leaq 0x2113(%rip), %rdx      # 0x5211
callq 0x3503
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x2200
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x315b
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3178
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x318f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
decl %ebx
jne 0x30d2
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x31b3
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0xe8(%rsp), %rdi
callq 0x3684
leaq 0x118(%rsp), %rdi
callq 0x36fc
leaq 0xb0(%rsp), %rdi
callq 0x35dc
leaq 0xd0(%rsp), %rdi
callq 0x34a8
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x3209
movq 0x88(%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 0x4dba(%rip), %rdi      # 0x7ff0
leaq 0x1e3b(%rip), %rsi      # 0x5078
callq 0x21c0
movq (%r14), %rsi
movq %rax, %rdi
callq 0x21c0
leaq 0x1e2c(%rip), %rsi      # 0x5080
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1e35(%rip), %rsi      # 0x50a0
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1e63(%rip), %rsi      # 0x50e5
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1e8f(%rip), %rsi      # 0x5128
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
leaq 0x1ec2(%rip), %rsi      # 0x5172
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20d0
movl $0x1, %edi
callq 0x2220
movq 0x4d1f(%rip), %rdi      # 0x7ff0
leaq 0x1f1f(%rip), %rsi      # 0x51f7
movl $0x19, %edx
callq 0x2200
movq 0x4d07(%rip), %rdi      # 0x7ff0
callq 0x20d0
jmp 0x32c0
jmp 0x32f2
movq %rax, %rbx
jmp 0x3402
movq %rax, %rbx
jmp 0x33d3
jmp 0x3310
jmp 0x3310
movq %rax, %rbx
jmp 0x3353
movq %rax, %rbx
jmp 0x3336
movq %rax, %rbx
jmp 0x3390
jmp 0x336c
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x3336
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21e0
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3353
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3390
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
jmp 0x3390
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3386
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21e0
leaq 0x38(%rsp), %rdi
callq 0x34a8
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x33ac
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0xe8(%rsp), %rdi
callq 0x3684
leaq 0x118(%rsp), %rdi
callq 0x36fc
leaq 0xb0(%rsp), %rdi
callq 0x35dc
leaq 0xd0(%rsp), %rdi
callq 0x34a8
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x3402
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21e0
leaq 0x148(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2300
nop