// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x340, %rsp            # imm = 0x340
movq %rdi, -0x2e8(%rbp)
movq %rdi, -0x8(%rbp)
callq 0x30f0
movq %rax, %rdi
callq 0x7de0
cmpl $0x0, %eax
jne 0x42b2
movq 0x8d81(%rip), %rdi      # 0xcff0
leaq 0x50aa(%rip), %rsi      # 0x9320
callq 0x32b0
movq -0x2e8(%rbp), %rsi
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
leaq 0x5096(%rip), %rsi      # 0x932a
callq 0x32b0
movq %rax, %rdi
movq 0x8d1d(%rip), %rsi      # 0xcfc0
callq 0x32f0
movl $0x1, %edi
callq 0x33b0
movq -0x2e8(%rbp), %rdi
callq 0x30f0
movq %rax, %rdi
leaq 0x5091(%rip), %rsi      # 0x9359
callq 0x6e10
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x4313
movq 0x8d11(%rip), %rdi      # 0xcff0
leaq 0x5075(%rip), %rsi      # 0x935b
callq 0x32b0
movq -0x2e8(%rbp), %rsi
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
movq 0x8cbc(%rip), %rsi      # 0xcfc0
callq 0x32f0
movl $0x1, %edi
callq 0x33b0
movq -0x2e8(%rbp), %rsi
leaq 0x5059(%rip), %rdx      # 0x937a
leaq -0x30(%rbp), %rdi
movq %rdi, -0x2f0(%rbp)
callq 0x5390
movq -0x2f0(%rbp), %rdi
callq 0x30f0
movq %rax, %rsi
leaq -0x230(%rbp), %rdi
movl $0x10, %edx
callq 0x3340
jmp 0x4353
leaq -0x258(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3160
movq $0x0, -0x260(%rbp)
movq $0x0, -0x268(%rbp)
movq -0x10(%rbp), %rdi
movl $0xa, %esi
leaq -0x258(%rbp), %rdx
callq 0x7f40
movl %eax, -0x2f4(%rbp)
jmp 0x4399
movl -0x2f4(%rbp), %eax
movl %eax, -0x240(%rbp)
cmpl $0x0, %eax
jl 0x4422
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shlq $0x10, %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, -0x268(%rbp)
movq -0x248(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
je 0x440f
jmp 0x4422
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x47b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x47a7
movq -0x268(%rbp), %rax
movq %rax, -0x260(%rbp)
jmp 0x437c
movq -0x260(%rbp), %rsi
leaq -0x230(%rbp), %rdi
callq 0x3520
movq %rax, -0x300(%rbp)
jmp 0x443e
movq -0x300(%rbp), %rdi
movq 0x8b74(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4453
movq $0x0, -0x270(%rbp)
movq $0x1, -0x278(%rbp)
leaq -0x290(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0x5410
movq -0x308(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x5430
jmp 0x4491
movl $0x1, -0x294(%rbp)
jmp 0x449d
movq -0x10(%rbp), %rdi
movl $0xa, %esi
leaq -0x258(%rbp), %rdx
callq 0x7f40
movl %eax, -0x30c(%rbp)
jmp 0x44ba
movl -0x30c(%rbp), %eax
movl %eax, -0x294(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shlq $0x10, %rax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, -0x268(%rbp)
movq -0x270(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x270(%rbp)
cmpl $0x0, -0x294(%rbp)
jl 0x4517
movq -0x268(%rbp), %rax
cmpq -0x260(%rbp), %rax
jne 0x4580
movq -0x10(%rbp), %rdi
callq 0x7c40
movl %eax, -0x310(%rbp)
jmp 0x4528
movl -0x310(%rbp), %eax
cmpl $0x1, %eax
jne 0x457e
movq -0x268(%rbp), %rax
cmpq -0x260(%rbp), %rax
jle 0x457c
movq -0x278(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x278(%rbp)
movq -0x268(%rbp), %rax
movq %rax, -0x260(%rbp)
jmp 0x457c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x479b
jmp 0x45d4
jmp 0x45d4
cmpq $0x2710, -0x270(%rbp) # imm = 0x2710
jne 0x45ad
leaq -0x290(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x5430
jmp 0x45a2
movq $0x0, -0x270(%rbp)
jmp 0x45af
movq -0x278(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x278(%rbp)
movq -0x268(%rbp), %rax
movq %rax, -0x260(%rbp)
jmp 0x449b
leaq -0x290(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x5430
jmp 0x45e9
movq -0x10(%rbp), %rdi
callq 0x7ac0
jmp 0x45f4
movq -0x248(%rbp), %rdi
callq 0x3390
movq -0x278(%rbp), %rsi
leaq -0x230(%rbp), %rdi
callq 0x3520
movq %rax, -0x318(%rbp)
jmp 0x461c
movq -0x318(%rbp), %rdi
movq 0x8996(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x4631
movq $0x0, -0x2a0(%rbp)
movq -0x2a0(%rbp), %rax
movq %rax, -0x320(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x54a0
movq %rax, %rcx
movq -0x320(%rbp), %rax
cmpq %rcx, %rax
jae 0x46bb
movq -0x2a0(%rbp), %rsi
leaq -0x290(%rbp), %rdi
callq 0x54c0
movq (%rax), %rsi
leaq -0x230(%rbp), %rdi
callq 0x3520
movq %rax, -0x328(%rbp)
jmp 0x4690
movq -0x328(%rbp), %rdi
movq 0x8922(%rip), %rsi      # 0xcfc0
callq 0x32f0
jmp 0x46a5
jmp 0x46a7
movq -0x2a0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x2a0(%rbp)
jmp 0x463c
leaq -0x230(%rbp), %rdi
callq 0x3430
jmp 0x46c9
movq -0x2e8(%rbp), %rsi
leaq 0x4ca3(%rip), %rdx      # 0x937a
leaq -0x2c0(%rbp), %rdi
callq 0x5390
jmp 0x46e5
leaq -0x2c0(%rbp), %rdi
callq 0x30f0
movq -0x2e8(%rbp), %rsi
movq %rax, -0x330(%rbp)
leaq 0x4c7d(%rip), %rdx      # 0x9383
leaq -0x2e0(%rbp), %rdi
callq 0x5390
jmp 0x4714
leaq -0x2e0(%rbp), %rdi
callq 0x30f0
movq -0x330(%rbp), %rdi
movq %rax, %rsi
callq 0x31a0
movl %eax, -0x334(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x31b0
leaq -0x2c0(%rbp), %rdi
callq 0x31b0
leaq -0x290(%rbp), %rdi
callq 0x54e0
leaq -0x230(%rbp), %rdi
callq 0x3460
leaq -0x30(%rbp), %rdi
callq 0x31b0
movl -0x334(%rbp), %eax
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x2c0(%rbp), %rdi
callq 0x31b0
leaq -0x290(%rbp), %rdi
callq 0x54e0
leaq -0x230(%rbp), %rdi
callq 0x3460
leaq -0x30(%rbp), %rdi
callq 0x31b0
movq -0x238(%rbp), %rdi
callq 0x34f0
nopl (%rax,%rax)