// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq (%rdi), %rdi
callq 0x4bcd
testl %eax, %eax
je 0x33e8
movq (%rbx), %rdi
leaq 0x2353(%rip), %rsi      # 0x5359
callq 0x432c
testq %rax, %rax
je 0x3417
movq %rax, %r14
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x29ca
leaq 0x233b(%rip), %rsi      # 0x537a
movq %r15, %rdi
callq 0x2430
movq 0x28(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
movl $0x10, %edx
callq 0x2290
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movl $0xa, %esi
movq %r15, %rdx
callq 0x4c9c
testl %eax, %eax
js 0x30af
movq 0xa0(%rsp), %rax
cmpb $0x23, (%rax)
jne 0x30af
movq 0x10(%r14), %rcx
shlq $0x10, %rcx
movzwl 0xc(%r14), %eax
orq %rcx, %rax
jmp 0x3077
movq 0x8(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x2200
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2220
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
movq %rbx, 0x68(%rsp)
je 0x311e
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x312d
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x3eaa
movl $0x1, %r15d
xorl %r13d, %r13d
leaq 0x90(%rsp), %r12
leaq 0x8(%rsp), %rbp
movq %r14, %rdi
movl $0xa, %esi
movq %r12, %rdx
callq 0x4c9c
movq 0x10(%r14), %rcx
shlq $0x10, %rcx
movzwl 0xc(%r14), %ebx
orq %rcx, %rbx
testl %eax, %eax
js 0x31b1
movq 0x8(%rsp), %rax
cmpq %rax, %rbx
je 0x31b1
incq %r13
cmpq $0x2710, %r13           # imm = 0x2710
jne 0x31a7
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3197
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x31a4
leaq 0x10(%rsp), %rdi
movq %rbp, %rdx
callq 0x3eaa
xorl %r13d, %r13d
incq %r15
movq %rbx, 0x8(%rsp)
jmp 0x3143
movq %r14, %rdi
callq 0x4ada
movq 0x68(%rsp), %r13
cmpl $0x1, %eax
jne 0x31d2
cmpq 0x8(%rsp), %rbx
jle 0x31d2
incq %r15
movq %rbx, 0x8(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x31f1
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3200
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x3eaa
movq %r14, %rdi
callq 0x4a46
movq 0xa0(%rsp), %rdi
callq 0x22d0
leaq 0xa8(%rsp), %rdi
movq %r15, %rsi
callq 0x2200
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2220
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x32b5
xorl %r12d, %r12d
leaq 0xa8(%rsp), %r14
movq (%rax,%r12,8), %rsi
movq %r14, %rdi
callq 0x2200
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2220
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
incq %r12
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jb 0x3266
leaq 0xa8(%rsp), %rdi
callq 0x2330
leaq 0x58(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0x29ca
leaq 0x2093(%rip), %rsi      # 0x537a
leaq 0x48(%rsp), %rdi
callq 0x2430
movq 0x48(%rsp), %r14
leaq 0x80(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x29ca
leaq 0x2064(%rip), %rsi      # 0x5383
leaq 0x70(%rsp), %rdi
callq 0x2430
movq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x2120
movl %eax, %ebx
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x3352
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
leaq 0x38(%rsp), %r14
je 0x336e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3385
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x4c3c(%rip), %rax      # 0x7fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0xb0(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0xa8(%rsp,%rcx)
callq 0x21b0
leaq 0x1a0(%rsp), %rdi
callq 0x2090
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0x33d4
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2260
movl %ebx, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4c01(%rip), %rdi      # 0x7ff0
leaq 0x1f2a(%rip), %rsi      # 0x5320
callq 0x2230
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
leaq 0x1f1d(%rip), %rsi      # 0x532a
movq %rax, %rdi
callq 0x2230
jmp 0x3435
movq 0x4bd2(%rip), %rdi      # 0x7ff0
leaq 0x1f36(%rip), %rsi      # 0x535b
callq 0x2230
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x22f0
jmp 0x349a
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x346d
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x346d
jmp 0x346a
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x349d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x349d
jmp 0x349a
jmp 0x348a
movq %rax, %rbx
jmp 0x34c6
jmp 0x3493
jmp 0x349a
movq %rax, %rbx
jmp 0x34b9
jmp 0x349a
movq %rax, %rbx
leaq 0x38(%rsp), %r12
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x34b9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
leaq 0xa8(%rsp), %rdi
callq 0x2350
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x34dd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23f0