// File: /workspace/github_c_makefile_star2_O3/arq5x[P]grabix/grabix.cpp
// Function: grab(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, long, long)
int grab(string bgzf_file, int64_t from_line, int64_t to_line)
{
    // load index into vector of offsets
    index_info index;
    load_index(bgzf_file, index);

    if (((int) from_line > index.num_lines)
        ||
        ((int) to_line > index.num_lines))
    {
        cerr << "[grabix] requested lines exceed the number of lines in the file." << endl;
        exit(1);
    }
    else if (from_line < 0)
    {
        cerr << "[grabix] indexes must be positive numbers." << endl;
        exit(1);
    }
    else if (from_line > to_line)
    {
        cerr << "[grabix] requested end line is less than the requested begin line." << 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);
        }

        // dump the header if there is one
        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;
        }

        // easier to work in 0-based space
        int64_t from_line_0  = from_line - 1;
        // get the chunk index for the requested line
        int64_t requested_chunk = from_line_0 / CHUNK_SIZE;
        // derive the first line in that chunk
        int64_t chunk_line_start = (requested_chunk * CHUNK_SIZE);

        // jump to the correct offset for the relevant chunk
        // and fast forward until we find the requested line
        bgzf_seek (bgzf_fp, index.chunk_offsets[requested_chunk], SEEK_SET);
        while (chunk_line_start <= from_line_0)
        {
            status = bgzf_getline(bgzf_fp, '\n', &line);
            chunk_line_start++;
        }
        // now, print each line until we reach the end of the requested block
        do
        {
            printf("%s\n", line.s);
            status = bgzf_getline(bgzf_fp, '\n', &line);
            chunk_line_start++;
        } while (chunk_line_start <= to_line);

        bgzf_close(bgzf_fp);
        free(line.s);
    }
    return EXIT_SUCCESS;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x50(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rdi), %rsi
movq 0x8(%rdi), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x29ca
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x34d5
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x379b
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2260
movslq %r14d, %rcx
movq 0x38(%rsp), %rax
cmpq %rax, %rcx
jg 0x38df
movslq %ebx, %rcx
cmpq %rax, %rcx
jg 0x38df
testq %r14, %r14
js 0x3908
cmpq %rbx, %r14
jg 0x392e
movq (%r12), %rdi
leaq 0x1b84(%rip), %rsi      # 0x5359
callq 0x4324
movq %rax, %r15
testq %rax, %rax
je 0x3954
xorps %xmm0, %xmm0
movq %rsp, %r12
movaps %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movq %r15, %rdi
movl $0xa, %esi
movq %r12, %rdx
callq 0x4c3c
testl %eax, %eax
jle 0x381f
movq 0x10(%rsp), %rdi
cmpb $0x23, (%rdi)
jne 0x381f
callq 0x2380
jmp 0x37fa
leaq -0x1(%r14), %rax
movabsq $0x346dc5d63886594b, %rcx # imm = 0x346DC5D63886594B
imulq %rcx
movq %rdx, %r12
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0xb, %r12
addq %rax, %r12
movq 0x20(%rsp), %rax
movq (%rax,%r12,8), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x4b08
imulq $0x2710, %r12, %r12     # imm = 0x2710
movq %r14, %r13
subq %r12, %r13
jle 0x387e
movq %rsp, %r12
movq %r15, %rdi
movl $0xa, %esi
movq %r12, %rdx
callq 0x4c3c
decq %r13
jne 0x3866
movq %r14, %r12
decq %r12
movq %rsp, %r14
movq 0x10(%rsp), %rdi
callq 0x2380
movq %r15, %rdi
movl $0xa, %esi
movq %r14, %rdx
callq 0x4c3c
incq %r12
cmpq %rbx, %r12
jl 0x3884
movq %r15, %rdi
callq 0x49e0
movq 0x10(%rsp), %rdi
callq 0x22d0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x38cf
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
xorl %eax, %eax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x470a(%rip), %rdi      # 0x7ff0
leaq 0x1acb(%rip), %rsi      # 0x53b8
movl $0x40, %edx
callq 0x22a0
movq 0x46f2(%rip), %rdi      # 0x7ff0
callq 0x20e0
jmp 0x3989
movq 0x46e1(%rip), %rdi      # 0x7ff0
leaq 0x1ae3(%rip), %rsi      # 0x53f9
movl $0x2a, %edx
callq 0x22a0
movq 0x46c9(%rip), %rdi      # 0x7ff0
callq 0x20e0
jmp 0x3989
movq 0x46bb(%rip), %rdi      # 0x7ff0
leaq 0x1ae8(%rip), %rsi      # 0x5424
movl $0x42, %edx
callq 0x22a0
movq 0x46a3(%rip), %rdi      # 0x7ff0
callq 0x20e0
jmp 0x3989
movq 0x4695(%rip), %rdi      # 0x7ff0
leaq 0x19f9(%rip), %rsi      # 0x535b
movl $0x1e, %edx
callq 0x22a0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0x4674(%rip), %rdi      # 0x7ff0
callq 0x22a0
movq %rax, %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x22f0
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r15, %rdi
je 0x39bc
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x39bc
jmp 0x39b9
jmp 0x39b9
jmp 0x39b9
jmp 0x39b9
jmp 0x39b9
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x39d3
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23f0