// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x100(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x5570
movq -0x100(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x3100
jmp 0x4e75
leaq -0x58(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x47d0
jmp 0x4e84
leaq -0x58(%rbp), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jle 0x4efd
movq 0x8152(%rip), %rdi      # 0xcff0
leaq 0x45c6(%rip), %rsi      # 0x946b
callq 0x32b0
movq %rax, -0x108(%rbp)
jmp 0x4eb3
movq -0x108(%rbp), %rdi
movq 0x80ff(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4ec8
movl $0x1, %edi
callq 0x33b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x52e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x31b0
jmp 0x52e1
movq -0x100(%rbp), %rdi
callq 0x30f0
movq %rax, %rdi
leaq 0x4446(%rip), %rsi      # 0x9359
callq 0x6e10
movq %rax, -0x110(%rbp)
jmp 0x4f21
movq -0x110(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x4f8a
movq 0x80b6(%rip), %rdi      # 0xcff0
leaq 0x441a(%rip), %rsi      # 0x935b
callq 0x32b0
movq %rax, -0x118(%rbp)
jmp 0x4f4f
movq -0x100(%rbp), %rsi
movq -0x118(%rbp), %rdi
callq 0x3280
movq %rax, -0x120(%rbp)
jmp 0x4f6b
movq -0x120(%rbp), %rdi
movq 0x8047(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4f80
movl $0x1, %edi
callq 0x33b0
xorl %eax, %eax
movl %eax, %edi
callq 0x3290
movl %eax, -0x124(%rbp)
callq 0x3450
movl %eax, %ecx
movl -0x124(%rbp), %eax
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl %eax, %edi
callq 0x32a0
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x55b0
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3160
movq -0x70(%rbp), %rdi
movl $0xa, %esi
leaq -0xc0(%rbp), %rdx
callq 0x7f40
movl %eax, -0x128(%rbp)
jmp 0x5008
movl -0x128(%rbp), %eax
movl %eax, -0xa4(%rbp)
cmpl $0x0, %eax
je 0x5056
movq -0xb0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x5052
movq -0xb0(%rbp), %rsi
leaq 0x4431(%rip), %rdi      # 0x9467
xorl %eax, %eax
callq 0x3030
jmp 0x503f
jmp 0x5054
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x52bf
jmp 0x5056
jmp 0x4feb
jmp 0x5058
movq -0x70(%rbp), %rdi
movl $0xa, %esi
leaq -0xc0(%rbp), %rdx
callq 0x7f40
movl %eax, -0x12c(%rbp)
jmp 0x5075
movl -0x12c(%rbp), %eax
movl %eax, -0xa4(%rbp)
cmpl $0x0, %eax
je 0x5222
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
cmpl $0x0, -0xa4(%rbp)
jge 0x50a4
jmp 0x5222
movq -0x88(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x516d
movq -0xb0(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x3500
movq -0x140(%rbp), %rsi
movq -0x138(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0x3bb0
jmp 0x50f2
leaq -0xa0(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x55d0
jmp 0x5107
leaq -0xe0(%rbp), %rdi
callq 0x31b0
leaq -0xe1(%rbp), %rdi
callq 0x3330
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x521d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x515c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x31b0
leaq -0xe1(%rbp), %rdi
callq 0x3330
jmp 0x52bf
callq 0x3090
cvtsi2sd %eax, %xmm0
movsd 0x3f1a(%rip), %xmm1     # 0x9098
divsd %xmm1, %xmm0
movaps 0x3f27(%rip), %xmm2     # 0x90b0
movq -0x80(%rbp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x3f26(%rip), %xmm2     # 0x90c0
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movsd 0x3edc(%rip), %xmm1     # 0x90a0
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x521b
movq -0xb0(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0xf0(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x5600
movq -0x148(%rbp), %rsi
movq %rax, %rdi
callq 0x3410
jmp 0x5219
jmp 0x521b
jmp 0x521d
jmp 0x5058
movq -0x70(%rbp), %rdi
callq 0x7ac0
jmp 0x522d
movq -0xb0(%rbp), %rdi
callq 0x3390
movq $0x0, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5620
movq %rax, %rcx
movq -0x150(%rbp), %rax
cmpq %rcx, %rax
jae 0x52b1
movq -0xf8(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x5600
movq %rax, %rdi
callq 0x30f0
movq %rax, %rsi
leaq 0x41d5(%rip), %rdi      # 0x9467
xorl %eax, %eax
callq 0x3030
jmp 0x529b
jmp 0x529d
movq -0xf8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xf8(%rbp)
jmp 0x5244
leaq -0xa0(%rbp), %rdi
callq 0x5640
jmp 0x52cd
leaq -0xa0(%rbp), %rdi
callq 0x5640
jmp 0x52e1
leaq -0x38(%rbp), %rdi
callq 0x5590
xorl %eax, %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x5590
movq -0x60(%rbp), %rdi
callq 0x34f0
nopw %cs:(%rax,%rax)