// File: /workspace/github_cpp_makefile_star2_O2/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 0x2e53
movl %edi, %r15d
pushq $0x1
popq %rbx
leaq 0x26fc(%rip), %r12      # 0x51cf
leaq 0x56e6(%rip), %r13      # 0x81c0
leaq 0x148(%rsp), %rbp
movl %ebx, 0x8(%rsp)
andl $0x0, 0x148(%rsp)
movl %r15d, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x22f0
testl %eax, %eax
je 0x2b38
cmpl $0x63, %eax
je 0x2b25
cmpl $0x6e, %eax
jne 0x2b8a
movq 0x54a1(%rip), %rax      # 0x7fb8
movq (%rax), %rdi
callq 0x2280
movl %eax, 0x8(%rsp)
jmp 0x2ae6
movq 0x548c(%rip), %rax      # 0x7fb8
movq (%rax), %rdi
callq 0x2280
movl %eax, %ebx
jmp 0x2ae6
movslq 0x148(%rsp), %rax
shlq $0x5, %rax
cmpq $0x0, 0x10(%rax,%r13)
jne 0x2ae6
addq %r13, %rax
movq (%rax), %rsi
leaq 0x267b(%rip), %rdi      # 0x51d4
xorl %eax, %eax
callq 0x2030
movq 0x5451(%rip), %rax      # 0x7fb8
movq (%rax), %rsi
testq %rsi, %rsi
je 0x2b7d
leaq 0x2668(%rip), %rdi      # 0x51de
xorl %eax, %eax
callq 0x2030
pushq $0xa
popq %rdi
callq 0x2240
jmp 0x2ae6
cmpl $-0x1, %eax
jne 0x2e4e
leaq 0x148(%rsp), %r14
movq %r14, %rdi
callq 0x21f0
leaq 0x2641(%rip), %rsi      # 0x51eb
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x2190
leaq 0x148(%rsp), %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x2100
leaq 0x148(%rsp), %rdi
callq 0x2040
movslq 0xc(%rsp), %rdi
callq 0x245c
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq 0x53ce(%rip), %rdi      # 0x7fd8
leaq 0x50(%rsp), %rsi
callq 0x2060
cmpq $0x0, 0x58(%rsp)
je 0x2eec
leaq 0x50(%rsp), %rdi
pushq $0x9
popq %rsi
leaq 0x70(%rsp), %rdx
callq 0x3f18
andq $0x0, 0x40(%rsp)
leaq 0x120(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0xf0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x5343(%rip), %rdi      # 0x7fd8
leaq 0xb0(%rsp), %rsi
callq 0x2060
leaq 0x90(%rsp), %r14
leaq 0xb0(%rsp), %r15
leaq 0x10(%rsp), %r13
leaq 0xd0(%rsp), %r12
cmpq $0x0, 0xb8(%rsp)
je 0x2d50
movq %r14, %rdi
movq %r15, %rsi
pushq $0x9
popq %rdx
callq 0x3fbd
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x90(%rsp), %rbp
cmpq 0x98(%rsp), %rbp
je 0x2d1f
movq (%rbp), %rdi
callq 0x2210
movsd %xmm0, 0xd0(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3666
addq $0x20, %rbp
jmp 0x2cf2
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x30c2
movq 0x52a5(%rip), %rdi      # 0x7fd8
movq %r15, %rsi
callq 0x2060
movq %r13, %rdi
callq 0x3302
movq %r14, %rdi
callq 0x30ee
jmp 0x2cbf
movq 0x5271(%rip), %rdi      # 0x7fc8
leaq 0x50(%rsp), %rsi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
leaq 0x10(%rsp), %r14
leaq 0xd0(%rsp), %r12
leaq 0x2494(%rip), %r13      # 0x5211
leaq 0x90(%rsp), %rbp
movq 0x523c(%rip), %r15      # 0x7fc8
subl $0x1, %ebx
jb 0x2de8
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movl 0x8(%rsp), %edx
callq 0x2991
movq %r12, %rdi
movq %r14, %rsi
callq 0x27b3
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x310f
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2180
movq %rax, %rdi
callq 0x20d0
movq %rbp, %rdi
callq 0x20c0
movq %r12, %rdi
callq 0x3302
movq %r14, %rdi
callq 0x3764
jmp 0x2d8c
leaq 0xb0(%rsp), %rdi
callq 0x20c0
leaq 0xe8(%rsp), %rdi
callq 0x323a
leaq 0x118(%rsp), %rdi
callq 0x329e
leaq 0x30(%rsp), %rdi
callq 0x31b6
leaq 0x70(%rsp), %rdi
callq 0x30ee
leaq 0x50(%rsp), %rdi
callq 0x20c0
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 0x5196(%rip), %rdi      # 0x7ff0
leaq 0x2217(%rip), %rsi      # 0x5078
callq 0x21b0
movq (%r14), %rsi
movq %rax, %rdi
callq 0x21b0
leaq 0x2208(%rip), %rsi      # 0x5080
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
leaq 0x2211(%rip), %rsi      # 0x50a0
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
leaq 0x223f(%rip), %rsi      # 0x50e5
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
leaq 0x226b(%rip), %rsi      # 0x5128
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
leaq 0x229e(%rip), %rsi      # 0x5172
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rdi
callq 0x2200
movq 0x50fd(%rip), %rdi      # 0x7ff0
leaq 0x22fd(%rip), %rsi      # 0x51f7
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
jmp 0x2ee4
jmp 0x2f0b
movq %rax, %rbx
jmp 0x2fb8
movq %rax, %rbx
jmp 0x2fa4
jmp 0x2f29
movq %rax, %rbx
jmp 0x2f4b
movq %rax, %rbx
jmp 0x2f3e
jmp 0x2f29
movq %rax, %rbx
jmp 0x2f73
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x20c0
leaq 0xd0(%rsp), %rdi
callq 0x3302
leaq 0x10(%rsp), %rdi
callq 0x3764
jmp 0x2f73
jmp 0x2f59
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3302
leaq 0x90(%rsp), %rdi
callq 0x30ee
leaq 0xb0(%rsp), %rdi
callq 0x20c0
leaq 0xe8(%rsp), %rdi
callq 0x323a
leaq 0x118(%rsp), %rdi
callq 0x329e
leaq 0x30(%rsp), %rdi
callq 0x31b6
leaq 0x70(%rsp), %rdi
callq 0x30ee
leaq 0x50(%rsp), %rdi
callq 0x20c0
leaq 0x148(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2300
nop