// File: /workspace/github_c_makefile_star2_O3/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 0x4b67
testl %eax, %eax
je 0x33d8
movq (%rbx), %rdi
leaq 0x2347(%rip), %rsi      # 0x5359
callq 0x4324
testq %rax, %rax
je 0x3407
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 0x232f(%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 0x4c3c
testl %eax, %eax
js 0x30bb
movq 0xa0(%rsp), %rax
cmpb $0x23, (%rax)
jne 0x30bb
movq 0x10(%r14), %rcx
shlq $0x10, %rcx
movzwl 0xc(%r14), %eax
orq %rcx, %rax
jmp 0x3083
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
movq %rbx, 0x68(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x8(%rsp), %rdx
xorl %esi, %esi
callq 0x3e9a
movl $0x1, %r15d
xorl %r13d, %r13d
leaq 0x90(%rsp), %r12
leaq 0x8(%rsp), %rbp
movq %r14, %rdi
movl $0xa, %esi
movq %r12, %rdx
callq 0x4c3c
movq 0x10(%r14), %rcx
shlq $0x10, %rcx
movzwl 0xc(%r14), %ebx
orq %rcx, %rbx
testl %eax, %eax
js 0x319f
movq 0x8(%rsp), %rax
cmpq %rax, %rbx
je 0x319f
incq %r13
cmpq $0x2710, %r13           # imm = 0x2710
jne 0x3195
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3185
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3192
leaq 0x10(%rsp), %rdi
movq %rbp, %rdx
callq 0x3e9a
xorl %r13d, %r13d
incq %r15
movq %rbx, 0x8(%rsp)
jmp 0x3131
movq %r14, %rdi
callq 0x4a74
movq 0x68(%rsp), %r13
movq 0x8(%rsp), %rcx
cmpl $0x1, %eax
jne 0x31c5
cmpq %rcx, %rbx
jle 0x31c5
incq %r15
movq %rbx, 0x8(%rsp)
jmp 0x31c8
movq %rcx, %rbx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x31e2
movq %rbx, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x31f1
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x3e9a
movq %r14, %rdi
callq 0x49e0
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 0x32a5
xorl %ebx, %ebx
leaq 0xa8(%rsp), %r14
movq (%rax,%rbx,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 %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rbx
jb 0x3256
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 0x20a3(%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 0x2074(%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 0x3342
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
leaq 0x38(%rsp), %r14
je 0x335e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3375
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
movq 0x4c4c(%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 0x33c4
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 0x4c11(%rip), %rdi      # 0x7ff0
leaq 0x1f3a(%rip), %rsi      # 0x5320
callq 0x2230
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
leaq 0x1f2d(%rip), %rsi      # 0x532a
movq %rax, %rdi
callq 0x2230
jmp 0x3425
movq 0x4be2(%rip), %rdi      # 0x7ff0
leaq 0x1f46(%rip), %rsi      # 0x535b
callq 0x2230
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x20e0
movl $0x1, %edi
callq 0x22f0
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x345b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x345b
jmp 0x3458
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x348d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x348d
jmp 0x348a
jmp 0x348a
jmp 0x347a
movq %rax, %rbx
jmp 0x34b6
jmp 0x3483
jmp 0x348a
movq %rax, %rbx
jmp 0x34a9
jmp 0x348a
movq %rax, %rbx
leaq 0x38(%rsp), %r12
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x34a9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2260
leaq 0xa8(%rsp), %rdi
callq 0x2350
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x34cd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x23f0