// File: /workspace/github_c_makefile_star2_O2/arq5x[P]grabix/grabix.cpp
// Function: create_grabix_index(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int create_grabix_index(string bgzf_file)
{
    if (!bgzf_is_bgzf(bgzf_file.c_str()))
    {
        cerr << "[grabix] " << bgzf_file << " doesn't exist or wasn't compressed with bgzip" << endl;
        exit (1);
    }

    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);
    }

    // create an index for writing
    string index_file_name = bgzf_file + ".gbi.tmp";
    ofstream index_file(index_file_name.c_str(), ios::out);

    // add the offset for the end of the header to the index

    int status;
    kstring_t line = { 0, 0, NULL };
    int64_t prev_offset = 0;
    int64_t offset = 0;
    while ((status = bgzf_getline(bgzf_fp, '\n', &line)) >= 0)
    {
        offset = bgzf_tell(bgzf_fp);
        if (line.s[0] != '#')
            break;
        prev_offset = offset;
    }
    index_file << prev_offset << endl;

    // add the offsets for each CHUNK_SIZE
    // set of records to the index
    size_t chunk_count = 0;
    int64_t total_lines = 1;
    vector<int64_t> chunk_positions;
    chunk_positions.push_back (prev_offset);
    int eof = 1;
    while (true)
    {
        // grab the next line and store the offset
        eof = bgzf_getline(bgzf_fp, '\n', &line);
        offset = bgzf_tell(bgzf_fp);
        chunk_count++;
        // stop if we have encountered an empty line
        if (eof < 0 || offset == prev_offset)
        {
            if (bgzf_check_EOF(bgzf_fp) == 1) {
                if (offset > prev_offset) {
                    total_lines++;
                    prev_offset = offset;
                }
                break;
            }
            break;
        }
        // store the offset of this chunk start
        else if (chunk_count == CHUNK_SIZE)
        {
            chunk_positions.push_back(prev_offset);
            chunk_count = 0;
        }
        total_lines++;
        prev_offset = offset;
    }
    chunk_positions.push_back (prev_offset);
    bgzf_close(bgzf_fp);
    free(line.s);

    index_file << total_lines << endl;
    for (size_t i = 0; i < chunk_positions.size(); ++i)
    {
        index_file << chunk_positions[i] << endl;
    }
    index_file.close();

    return std::rename((bgzf_file + ".gbi.tmp").c_str(), (bgzf_file + ".gbi").c_str());
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, %r15
movq (%rdi), %rdi
callq 0x41fc
testl %eax, %eax
je 0x2d71
movq (%r15), %rdi
leaq 0x282f(%rip), %rsi      # 0x5359
callq 0x3a24
testq %rax, %rax
je 0x2da0
movq %rax, %r14
leaq 0x2838(%rip), %rdx      # 0x537a
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x350a
movq (%rbx), %rsi
leaq 0xa8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2290
leaq 0x30(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
xorl %eax, %eax
pushq $0xa
popq %rbp
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x42c5
testl %eax, %eax
js 0x2bac
movq 0x40(%rsp), %rax
cmpb $0x23, (%rax)
jne 0x2bac
movq 0x10(%r14), %rcx
shlq $0x10, %rcx
movzwl 0xc(%r14), %eax
orq %rcx, %rax
jmp 0x2b7a
movq 0x8(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x2200
movq %rax, %rdi
callq 0x2100
movq %r15, 0x28(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x8(%rsp), %rsi
callq 0x3542
pushq $0x1
popq %r15
xorl %r13d, %r13d
pushq $0xa
popq %rbp
leaq 0x30(%rsp), %r12
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x42c5
movq 0x10(%r14), %rcx
shlq $0x10, %rcx
movzwl 0xc(%r14), %ebx
orq %rcx, %rbx
testl %eax, %eax
js 0x2c44
cmpq 0x8(%rsp), %rbx
je 0x2c44
incq %r13
cmpq $0x2710, %r13           # imm = 0x2710
jne 0x2c3a
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3542
xorl %r13d, %r13d
incq %r15
movq %rbx, 0x8(%rsp)
jmp 0x2bf4
movq %r14, %rdi
callq 0x4125
movq 0x28(%rsp), %r12
cmpl $0x1, %eax
jne 0x2c65
cmpq 0x8(%rsp), %rbx
jle 0x2c65
incq %r15
movq %rbx, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3542
movq %r14, %rdi
callq 0x4095
movq 0x40(%rsp), %rdi
callq 0x22c0
leaq 0xa8(%rsp), %rdi
movq %r15, %rsi
callq 0x2200
movq %rax, %rdi
callq 0x2100
xorl %r14d, %r14d
leaq 0xa8(%rsp), %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r14
jae 0x2cd8
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0x2200
movq %rax, %rdi
callq 0x2100
incq %r14
jmp 0x2ca9
leaq 0xa8(%rsp), %rdi
callq 0x2340
leaq 0x268e(%rip), %rdx      # 0x537a
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x350a
movq 0x48(%rsp), %rbx
leaq 0x267e(%rip), %rdx      # 0x5383
leaq 0x88(%rsp), %rdi
movq %r12, %rsi
callq 0x350a
leaq 0x88(%rsp), %r14
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x2140
movl %eax, %ebx
movq %r14, %rdi
callq 0x20e0
leaq 0x48(%rsp), %rdi
callq 0x20e0
leaq 0x10(%rsp), %rdi
callq 0x358e
leaq 0xa8(%rsp), %rdi
callq 0x2360
leaq 0x68(%rsp), %rdi
callq 0x20e0
movl %ebx, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5278(%rip), %rdi      # 0x7ff0
leaq 0x25a1(%rip), %rsi      # 0x5320
callq 0x2220
movq %rax, %rdi
movq %r15, %rsi
callq 0x21e0
leaq 0x2594(%rip), %rsi      # 0x532a
movq %rax, %rdi
callq 0x2220
jmp 0x2dbe
movq 0x5249(%rip), %rdi      # 0x7ff0
leaq 0x25ad(%rip), %rsi      # 0x535b
callq 0x2220
movq %rax, %rdi
movq %r15, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x2100
pushq $0x1
popq %rdi
callq 0x22e0
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x20e0
jmp 0x2df4
jmp 0x2df1
jmp 0x2df1
movq %rax, %rbx
jmp 0x2e0b
jmp 0x2dea
jmp 0x2df1
movq %rax, %rbx
jmp 0x2dfe
jmp 0x2df1
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x358e
leaq 0xa8(%rsp), %rdi
callq 0x2360
leaq 0x68(%rsp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x23f0