// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %r13
leaq 0x38(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x58(%rsp), %rdi
movq %r13, %rsi
callq 0x2330
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2e1d
leaq 0x58(%rsp), %rdi
callq 0x20e0
movslq %r15d, %rcx
movq 0x50(%rsp), %rax
cmpq %rax, %rcx
jg 0x3129
movslq 0x8(%rsp), %rcx
cmpq %rax, %rcx
jg 0x3129
testq %r15, %r15
js 0x3146
cmpq 0x8(%rsp), %r15
jg 0x3163
movq (%r13), %rdi
leaq 0x2325(%rip), %rsi      # 0x5359
callq 0x3a24
movq %rax, %r12
testq %rax, %rax
je 0x3180
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r13
movaps %xmm0, (%r13)
andq $0x0, 0x10(%r13)
pushq $0xa
popq %rbp
movq %r12, %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x42c5
testl %eax, %eax
jle 0x307c
movq 0x20(%rsp), %rdi
cmpb $0x23, (%rdi)
jne 0x307c
callq 0x2390
jmp 0x305a
leaq -0x1(%r15), %rax
movl $0x2710, %ecx           # imm = 0x2710
cqto
idivq %rcx
movq %rax, %r13
movq 0x38(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x41a4
imulq $0x2710, %r13, %r14     # imm = 0x2710
cmpq %r14, %r15
movq %r14, %rbx
cmovgq %r15, %rbx
pushq $0xa
popq %rbp
leaq 0x10(%rsp), %r13
cmpq %r15, %r14
jge 0x30d0
movq %r12, %rdi
movl %ebp, %esi
movq %r13, %rdx
callq 0x42c5
incq %r14
jmp 0x30b9
decq %rbx
pushq $0xa
popq %rbp
leaq 0x10(%rsp), %r15
movq 0x20(%rsp), %rdi
callq 0x2390
movq %r12, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x42c5
incq %rbx
cmpq 0x8(%rsp), %rbx
jl 0x30db
movq %r12, %rdi
callq 0x4095
movq 0x20(%rsp), %rdi
callq 0x22c0
leaq 0x38(%rsp), %rdi
callq 0x358e
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4ec0(%rip), %rdi      # 0x7ff0
leaq 0x2281(%rip), %rsi      # 0x53b8
callq 0x2220
movq %rax, %rdi
callq 0x2100
jmp 0x31a6
movq 0x4ea3(%rip), %rdi      # 0x7ff0
leaq 0x22a5(%rip), %rsi      # 0x53f9
callq 0x2220
movq %rax, %rdi
callq 0x2100
jmp 0x31a6
movq 0x4e86(%rip), %rdi      # 0x7ff0
leaq 0x22b3(%rip), %rsi      # 0x5424
callq 0x2220
movq %rax, %rdi
callq 0x2100
jmp 0x31a6
movq 0x4e69(%rip), %rdi      # 0x7ff0
leaq 0x21cd(%rip), %rsi      # 0x535b
callq 0x2220
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x2100
pushq $0x1
popq %rdi
callq 0x22e0
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x20e0
jmp 0x31ca
jmp 0x31c7
jmp 0x31c7
jmp 0x31c7
jmp 0x31c7
jmp 0x31c7
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x358e
movq %r14, %rdi
callq 0x23f0