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

    if (argc < 2) {
        cerr << "usage: " << argv[0] << " -r sample_rate" << endl
             << "samples lines from stdin at sample_rate rate" << endl
             << "specify -H to always print header line" << endl;
        exit(1);
    }

    double sample_rate = 1.0;
    bool header = false;

    while (true) {
        static struct option long_options[] =
        {  
            {"header", no_argument, 0, 'H'},
            {"rate",   required_argument, 0, 'r'},

        };
        int option_index = 0;

        int c = getopt_long (argc, argv, "Hr:", 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 'H':
            header = true;
            break;

          case 'r':
            sample_rate = atof(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 line;
    getline(cin, line);
    if (header) {
        cout << line << endl;
    }
    getline(cin, line);

    while (!line.empty()) {
        cout << line << endl;
        if (genrand_real1() < sample_rate) {
            cout << line << endl;
        }
        getline(cin, line);
    }

    return 0;

}

// Assembly:
subq $0x298, %rsp            # imm = 0x298
movl $0x0, 0x294(%rsp)
movl %edi, 0x290(%rsp)
movq %rsi, 0x288(%rsp)
cmpl $0x2, 0x290(%rsp)
jge 0x1a59
movq 0x261a(%rip), %rdi      # 0x3ff0
leaq 0x683(%rip), %rsi       # 0x2060
callq 0x10d0
movq %rax, %rdi
movq 0x288(%rsp), %rax
movq (%rax), %rsi
callq 0x10d0
movq %rax, %rdi
leaq 0x669(%rip), %rsi       # 0x2068
callq 0x10d0
movq %rax, %rdi
movq 0x25a2(%rip), %rsi      # 0x3fb0
callq 0x10e0
movq %rax, %rdi
leaq 0x65b(%rip), %rsi       # 0x2078
callq 0x10d0
movq %rax, %rdi
movq 0x2584(%rip), %rsi      # 0x3fb0
callq 0x10e0
movq %rax, %rdi
leaq 0x66a(%rip), %rsi       # 0x20a5
callq 0x10d0
movq %rax, %rdi
movq 0x2566(%rip), %rsi      # 0x3fb0
callq 0x10e0
movl $0x1, %edi
callq 0x1100
movsd 0x5cf(%rip), %xmm0      # 0x2030
movsd %xmm0, 0x280(%rsp)
movb $0x0, 0x27f(%rsp)
movl $0x0, 0x278(%rsp)
movl 0x290(%rsp), %edi
movq 0x288(%rsp), %rsi
leaq 0x640(%rip), %rdx       # 0x20d3
leaq 0x2656(%rip), %rcx      # 0x40f0
leaq 0x278(%rsp), %r8
callq 0x1140
movl %eax, 0x274(%rsp)
cmpl $-0x1, 0x274(%rsp)
jne 0x1abd
jmp 0x1b9b
movl 0x274(%rsp), %eax
movl %eax, 0x2c(%rsp)
testl %eax, %eax
je 0x1aef
jmp 0x1ace
movl 0x2c(%rsp), %eax
subl $0x48, %eax
je 0x1b6d
jmp 0x1add
movl 0x2c(%rsp), %eax
subl $0x72, %eax
je 0x1b77
jmp 0x1b91
movslq 0x278(%rsp), %rcx
leaq 0x25f2(%rip), %rax      # 0x40f0
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
je 0x1b11
jmp 0x1b96
movslq 0x278(%rsp), %rcx
leaq 0x25d0(%rip), %rax      # 0x40f0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
leaq 0x5a6(%rip), %rdi       # 0x20d7
movb $0x0, %al
callq 0x1030
movq 0x2479(%rip), %rax      # 0x3fb8
cmpq $0x0, (%rax)
je 0x1b5d
movq 0x246c(%rip), %rax      # 0x3fb8
movq (%rax), %rsi
leaq 0x58b(%rip), %rdi       # 0x20e1
movb $0x0, %al
callq 0x1030
leaq 0x58a(%rip), %rdi       # 0x20ee
movb $0x0, %al
callq 0x1030
jmp 0x1b96
movb $0x1, 0x27f(%rsp)
jmp 0x1b96
movq 0x243a(%rip), %rax      # 0x3fb8
movq (%rax), %rdi
callq 0x1110
movsd %xmm0, 0x280(%rsp)
jmp 0x1b96
callq 0x1070
jmp 0x1a72
leaq 0x60(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x10f0
movq 0x20(%rsp), %rdi
leaq 0x53a(%rip), %rsi       # 0x20f0
movl $0x8, %edx
callq 0x10c0
jmp 0x1bc2
leaq 0x60(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl $0x4, %edx
callq 0x1080
jmp 0x1bd8
leaq 0x60(%rsp), %rdi
callq 0x1040
jmp 0x1be4
movslq 0x50(%rsp), %rdi
callq 0x12b0
leaq 0x30(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x1120
movq 0x18(%rsp), %rsi
movq 0x23cf(%rip), %rdi      # 0x3fd8
callq 0x1050
jmp 0x1c10
testb $0x1, 0x27f(%rsp)
je 0x1c77
movq 0x23a7(%rip), %rdi      # 0x3fc8
leaq 0x30(%rsp), %rsi
callq 0x10b0
movq %rax, 0x10(%rsp)
jmp 0x1c32
movq 0x10(%rsp), %rdi
movq 0x2372(%rip), %rsi      # 0x3fb0
callq 0x10e0
jmp 0x1c45
jmp 0x1c77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x1d54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x1090
jmp 0x1d54
movq 0x235a(%rip), %rdi      # 0x3fd8
leaq 0x30(%rsp), %rsi
callq 0x1050
jmp 0x1c8a
jmp 0x1c8c
leaq 0x30(%rsp), %rdi
callq 0x1150
xorb $-0x1, %al
testb $0x1, %al
jne 0x1ca1
jmp 0x1d26
movq 0x2320(%rip), %rdi      # 0x3fc8
leaq 0x30(%rsp), %rsi
callq 0x10b0
movq %rax, 0x8(%rsp)
jmp 0x1cb9
movq 0x8(%rsp), %rdi
movq 0x22eb(%rip), %rsi      # 0x3fb0
callq 0x10e0
jmp 0x1ccc
callq 0x1840
movaps %xmm0, %xmm1
movsd 0x280(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1d0e
movq 0x22de(%rip), %rdi      # 0x3fc8
leaq 0x30(%rsp), %rsi
callq 0x10b0
movq %rax, (%rsp)
jmp 0x1cfa
movq (%rsp), %rdi
movq 0x22ab(%rip), %rsi      # 0x3fb0
callq 0x10e0
jmp 0x1d0c
jmp 0x1d0e
movq 0x22c3(%rip), %rdi      # 0x3fd8
leaq 0x30(%rsp), %rsi
callq 0x1050
jmp 0x1d21
jmp 0x1c8c
movl $0x0, 0x294(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x1090
leaq 0x60(%rsp), %rdi
callq 0x1060
movl 0x294(%rsp), %eax
addq $0x298, %rsp            # imm = 0x298
retq
leaq 0x60(%rsp), %rdi
callq 0x1060
movq 0x58(%rsp), %rdi
callq 0x1160