// File: /workspace/github_c_makefile_star2_O3/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 0x34d5
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x3a59
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x8(%rsp), %rax
cmpq %rax, 0xb0(%rsp)
jl 0x3c9e
movq (%r15), %rdi
leaq 0x18e3(%rip), %rsi      # 0x5359
callq 0x4324
movq %rax, %rbx
testq %rax, %rax
je 0x3cc4
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 0x4c3c
testl %eax, %eax
je 0x3ae6
movq 0x40(%rsp), %rdi
cmpb $0x23, (%rdi)
jne 0x3ae6
callq 0x2380
jmp 0x3ac1
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 0x4c3c
testl %eax, %eax
je 0x3c1a
js 0x3c1a
cmpq 0x8(%rsp), %r14
jb 0x3bd7
callq 0x2070
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x156a(%rip), %xmm0     # 0x50a0
movq %r15, %xmm1
punpckldq 0x156d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1575(%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 0x153d(%rip), %xmm2     # 0x50a8
cvttsd2si %xmm2, %rax
sarq $0x3f, %rdx
andq %rdx, %rax
orq %rcx, %rax
cmpq 0x8(%rsp), %rax
jae 0x3af6
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 0x3af6
movq 0x40(%rsp), %rsi
movq %r13, %rdi
leaq 0x7(%rsp), %rdx
callq 0x298c
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x40b2
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x3c12
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2260
incq %r14
jmp 0x3af6
movq %rbx, %rdi
callq 0x49e0
movq 0x40(%rsp), %rdi
callq 0x22d0
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x3c63
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 0x3c3d
leaq 0x10(%rsp), %rdi
callq 0x3e3c
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3c8a
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 0x434b(%rip), %rdi      # 0x7ff0
leaq 0x17bb(%rip), %rsi      # 0x5467
movl $0x38, %edx
callq 0x22a0
movq 0x4333(%rip), %rdi      # 0x7ff0
callq 0x20e0
jmp 0x3cf7
movq 0x4325(%rip), %rdi      # 0x7ff0
leaq 0x1689(%rip), %rsi      # 0x535b
movl $0x1e, %edx
callq 0x22a0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0x4306(%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 0x3d53
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x3d53
jmp 0x3d50
jmp 0x3d50
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r14, %rdi
je 0x3d5d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x3d5d
jmp 0x3d47
movq %rax, %rbx
jmp 0x3d5d
jmp 0x3d50
jmp 0x3d50
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3e3c
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x3d7a
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23f0