// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rdi, %rbx
leaq 0x98(%rsp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x2330
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x2e1d
leaq 0x70(%rsp), %rdi
callq 0x20e0
cmpq %r14, 0xb0(%rsp)
jl 0x33f9
movq (%rbx), %rdi
leaq 0x2110(%rip), %rsi      # 0x5359
callq 0x3a24
movq %rax, %r15
testq %rax, %rax
je 0x3416
xorl %edi, %edi
callq 0x21f0
movq %rax, %rbx
callq 0x2350
addl %eax, %ebx
movl %ebx, %edi
callq 0x2210
andq $0x0, 0x20(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
leaq 0x30(%rsp), %rbx
movapd %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
pushq $0xa
popq %rbp
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x42c5
testl %eax, %eax
je 0x32b5
movq 0x40(%rsp), %rdi
cmpb $0x23, (%rdi)
jne 0x32b5
callq 0x2390
jmp 0x3293
xorl %ebx, %ebx
pushq $0xa
popq %rbp
leaq 0x30(%rsp), %r12
xorl %r13d, %r13d
incq %rbx
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x42c5
testl %eax, %eax
je 0x338c
js 0x338c
cmpq %r14, %r13
jb 0x3357
callq 0x2070
cvtsi2sd %eax, %xmm0
divsd 0x1daa(%rip), %xmm0     # 0x50a0
movq %rbx, %xmm1
punpckldq 0x1dad(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1db5(%rip), %xmm1     # 0x50c0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
movq %rax, %rcx
subsd 0x1d7d(%rip), %xmm2     # 0x50a8
cvttsd2si %xmm2, %rdi
sarq $0x3f, %rcx
andq %rcx, %rdi
orq %rax, %rdi
cmpq %r14, %rdi
jae 0x32c2
movq 0x40(%rsp), %rsi
shlq $0x5, %rdi
addq 0x10(%rsp), %rdi
callq 0x2430
jmp 0x32c2
movq 0x40(%rsp), %rsi
leaq 0x50(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x2836
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x385a
leaq 0x50(%rsp), %rdi
callq 0x20e0
incq %r13
jmp 0x32c2
movq %r15, %rdi
callq 0x4095
movq 0x40(%rsp), %rdi
callq 0x22c0
xorl %ebx, %ebx
xorl %r14d, %r14d
leaq 0x98(%rsp), %r15
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x5, %rcx
cmpq %rcx, %r14
jae 0x33d3
movq (%rax,%rbx), %rdi
callq 0x2390
incq %r14
addq $0x20, %rbx
jmp 0x33ab
leaq 0x10(%rsp), %rdi
callq 0x3562
movq %r15, %rdi
callq 0x358e
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4bf0(%rip), %rdi      # 0x7ff0
leaq 0x2060(%rip), %rsi      # 0x5467
callq 0x2220
movq %rax, %rdi
callq 0x2100
jmp 0x343c
movq 0x4bd3(%rip), %rdi      # 0x7ff0
leaq 0x1f37(%rip), %rsi      # 0x535b
callq 0x2220
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x2100
pushq $0x1
popq %rdi
callq 0x22e0
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x20e0
jmp 0x3474
jmp 0x3471
jmp 0x3471
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x20e0
jmp 0x347e
jmp 0x3468
movq %rax, %rbx
jmp 0x347e
jmp 0x3471
jmp 0x3471
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3562
leaq 0x98(%rsp), %rdi
callq 0x358e
movq %rbx, %rdi
callq 0x23f0