// File: /workspace/github_c_makefile_star2_O1/arq5x[P]grabix/grabix.cpp
// Function: random(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long)
int random(string bgzf_file, uint64_t K)
{
    // load index into vector of offsets
    index_info index;
    load_index(bgzf_file, index);

    if ((int64_t) K > index.num_lines)
    {
        cerr << "[grabix] warning: requested more lines than in the file." << endl;
        exit(1);
    }
    else {
        // load the BGZF file
        BGZF *bgzf_fp = bgzf_open(bgzf_file.c_str(), "r");
        if (bgzf_fp == NULL)
        {
            cerr << "[grabix] could not open file: " << bgzf_file << endl;
            exit (1);
        }

        // seed our random number generator
        size_t seed = (unsigned)time(0)+(unsigned)getpid();
        srand(seed);

        // reservoir sample
        uint64_t N = 0;
        uint64_t result_size = 0;
        vector<string> sample;
        int status;
        kstring_t line = { 0, 0, NULL };

        while ((status = bgzf_getline(bgzf_fp, '\n', &line)) != 0)
        {
            if (line.s[0] == '#')
                printf("%s\n", line.s);
            else break;
        }

        while ((status = bgzf_getline(bgzf_fp, '\n', &line)) != 0)
        {
            N++;

            if (status < 0)
                break;

            if (result_size < K)
            {
                sample.push_back(line.s);
                result_size++;
            }
            else
            {
                uint64_t s = (uint64_t) ((double)rand()/(double)RAND_MAX * N);
                if (s < K)
                    sample[s] = line.s;
            }
        }
        bgzf_close(bgzf_fp);
        free(line.s);

        // report the sample
        for (size_t i = 0; i < sample.size(); ++i)
            printf("%s\n", sample[i].c_str());

    }
    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, 0x98(%rsp)
movq $0x0, 0xa8(%rsp)
leaq 0x80(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rdi), %rsi
movq 0x8(%rdi), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x29ca
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x34e5
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x3a6b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x8(%rsp), %rax
cmpq %rax, 0xb0(%rsp)
jl 0x3cb0
movq (%r15), %rdi
leaq 0x18d1(%rip), %rsi      # 0x5359
callq 0x432c
movq %rax, %rbx
testq %rax, %rax
je 0x3cd6
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x21f0
movq %rax, %r15
callq 0x2340
addl %eax, %r15d
movl %r15d, %edi
callq 0x2210
movq %r12, 0x20(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
leaq 0x30(%rsp), %r15
movq %r12, 0x10(%r15)
movapd %xmm0, (%r15)
movq %rbx, %rdi
movl $0xa, %esi
movq %r15, %rdx
callq 0x4c9c
testl %eax, %eax
je 0x3af8
movq 0x40(%rsp), %rdi
cmpb $0x23, (%rdi)
jne 0x3af8
callq 0x2380
jmp 0x3ad3
xorl %r15d, %r15d
leaq 0x30(%rsp), %r12
leaq 0x50(%rsp), %r13
xorl %r14d, %r14d
incq %r15
movq %rbx, %rdi
movl $0xa, %esi
movq %r12, %rdx
callq 0x4c9c
testl %eax, %eax
je 0x3c2c
js 0x3c2c
cmpq 0x8(%rsp), %r14
jb 0x3be9
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x1558(%rip), %xmm0     # 0x50a0
movq %r15, %xmm1
punpckldq 0x155b(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1563(%rip), %xmm1     # 0x50c0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rcx
movq %rcx, %rdx
subsd 0x152b(%rip), %xmm2     # 0x50a8
cvttsd2si %xmm2, %rax
sarq $0x3f, %rdx
andq %rdx, %rax
orq %rcx, %rax
cmpq 0x8(%rsp), %rax
jae 0x3b08
movq 0x40(%rsp), %r13
movq 0x10(%rsp), %rcx
shlq $0x5, %rax
leaq (%rcx,%rax), %rbp
movq %r15, 0x48(%rsp)
movq %rbx, %r15
movq %r12, %rbx
movq 0x8(%rcx,%rax), %r12
movq %r13, %rdi
callq 0x20c0
movq %rbp, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %rbx, %r12
movq %r15, %rbx
movq 0x48(%rsp), %r15
movq %r13, %rcx
leaq 0x50(%rsp), %r13
movq %rax, %r8
callq 0x2390
jmp 0x3b08
movq 0x40(%rsp), %rsi
movq %r13, %rdi
leaq 0x7(%rsp), %rdx
callq 0x298c
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x40c2
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x3c24
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2260
incq %r14
jmp 0x3b08
movq %rbx, %rdi
callq 0x4a46
movq 0x40(%rsp), %rdi
callq 0x22d0
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x3c75
xorl %ebx, %ebx
xorl %r14d, %r14d
movq (%rax,%rbx), %rdi
callq 0x2380
incq %r14
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
addq $0x20, %rbx
cmpq %rcx, %r14
jb 0x3c4f
leaq 0x10(%rsp), %rdi
callq 0x3e4e
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3c9c
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4339(%rip), %rdi      # 0x7ff0
leaq 0x17a9(%rip), %rsi      # 0x5467
movl $0x38, %edx
callq 0x22a0
movq 0x4321(%rip), %rdi      # 0x7ff0
callq 0x20e0
jmp 0x3d09
movq 0x4313(%rip), %rdi      # 0x7ff0
leaq 0x1677(%rip), %rsi      # 0x535b
movl $0x1e, %edx
callq 0x22a0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x42f4(%rip), %rdi      # 0x7ff0
callq 0x22a0
movq %rax, %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x22f0
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x3d65
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x3d65
jmp 0x3d62
jmp 0x3d62
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d6f
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x3d6f
jmp 0x3d59
movq %rax, %rbx
jmp 0x3d6f
jmp 0x3d62
jmp 0x3d62
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3e4e
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3d8c
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23f0