// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xb8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x5570
movq -0xb8(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3100
jmp 0x4ab9
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x47d0
jmp 0x4ac8
leaq -0x60(%rbp), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
cltq
cmpq -0x20(%rbp), %rax
jg 0x4ae9
movq -0x18(%rbp), %rax
cltq
cmpq -0x20(%rbp), %rax
jle 0x4b4f
movq 0x8500(%rip), %rdi      # 0xcff0
leaq 0x48c1(%rip), %rsi      # 0x93b8
callq 0x32b0
movq %rax, -0xc0(%rbp)
jmp 0x4b05
movq -0xc0(%rbp), %rdi
movq 0x84ad(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4b1a
movl $0x1, %edi
callq 0x33b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x4e23
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x31b0
jmp 0x4e23
cmpq $0x0, -0x10(%rbp)
jge 0x4b91
movq 0x8493(%rip), %rdi      # 0xcff0
leaq 0x4895(%rip), %rsi      # 0x93f9
callq 0x32b0
movq %rax, -0xc8(%rbp)
jmp 0x4b72
movq -0xc8(%rbp), %rdi
movq 0x8440(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4b87
movl $0x1, %edi
callq 0x33b0
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jle 0x4bd6
movq 0x844e(%rip), %rdi      # 0xcff0
leaq 0x487b(%rip), %rsi      # 0x9424
callq 0x32b0
movq %rax, -0xd0(%rbp)
jmp 0x4bb7
movq -0xd0(%rbp), %rdi
movq 0x83fb(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4bcc
movl $0x1, %edi
callq 0x33b0
movq -0xb8(%rbp), %rdi
callq 0x30f0
movq %rax, %rdi
leaq 0x476d(%rip), %rsi      # 0x9359
callq 0x6e10
movq %rax, -0xd8(%rbp)
jmp 0x4bfa
movq -0xd8(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x4c63
movq 0x83dd(%rip), %rdi      # 0xcff0
leaq 0x4741(%rip), %rsi      # 0x935b
callq 0x32b0
movq %rax, -0xe0(%rbp)
jmp 0x4c28
movq -0xb8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
callq 0x3280
movq %rax, -0xe8(%rbp)
jmp 0x4c44
movq -0xe8(%rbp), %rdi
movq 0x836e(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4c59
movl $0x1, %edi
callq 0x33b0
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3160
movq -0x78(%rbp), %rdi
movl $0xa, %esi
leaq -0x98(%rbp), %rdx
callq 0x7f40
movl %eax, -0xec(%rbp)
jmp 0x4c93
movl -0xec(%rbp), %eax
movl %eax, -0x7c(%rbp)
cmpl $0x0, %eax
jle 0x4ccd
movq -0x88(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x4cc9
movq -0x88(%rbp), %rsi
leaq 0x47a9(%rip), %rdi      # 0x9467
xorl %eax, %eax
callq 0x3030
jmp 0x4cc7
jmp 0x4ccb
jmp 0x4ccd
jmp 0x4c76
movq -0x10(%rbp), %rax
decq %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movabsq $0x346dc5d63886594b, %rcx # imm = 0x346DC5D63886594B
imulq %rcx
movq %rdx, %rax
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0xb, %rax
addq %rdx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
imulq $0x2710, %rax, %rax     # imm = 0x2710
movq %rax, -0xb0(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x38(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x54c0
movq -0xf8(%rbp), %rdi
movq (%rax), %rsi
xorl %edx, %edx
callq 0x7cf0
jmp 0x4d4a
jmp 0x4d4c
movq -0xb0(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jg 0x4d96
movq -0x78(%rbp), %rdi
movl $0xa, %esi
leaq -0x98(%rbp), %rdx
callq 0x7f40
movl %eax, -0xfc(%rbp)
jmp 0x4d79
movl -0xfc(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x4d4c
jmp 0x4d98
movq -0x88(%rbp), %rsi
leaq 0x46c1(%rip), %rdi      # 0x9467
xorl %eax, %eax
callq 0x3030
jmp 0x4daf
movq -0x78(%rbp), %rdi
movl $0xa, %esi
leaq -0x98(%rbp), %rdx
callq 0x7f40
movl %eax, -0x100(%rbp)
jmp 0x4dcc
movl -0x100(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0x18(%rbp), %rax
jle 0x4d98
movq -0x78(%rbp), %rdi
callq 0x7ac0
jmp 0x4dff
movq -0x88(%rbp), %rdi
callq 0x3390
jmp 0x4e0d
jmp 0x4e0f
leaq -0x40(%rbp), %rdi
callq 0x5590
xorl %eax, %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x5590
movq -0x68(%rbp), %rdi
callq 0x34f0
nopw %cs:(%rax,%rax)