// File: /workspace/github_c_makefile_star2_O3/zhou-lab[P]tbmate/htslib/bgzf.c
// Function: bgzf_index_dump
int bgzf_index_dump(BGZF *fp, const char *bname, const char *suffix)
{
    if (bgzf_flush(fp) != 0) return -1;

    assert(fp->idx);
    char *tmp = NULL;
    if ( suffix )
    {
        int blen = strlen(bname);
        int slen = strlen(suffix);
        tmp = (char*) malloc(blen + slen + 1);
        if ( !tmp ) return -1;
        memcpy(tmp,bname,blen);
        memcpy(tmp+blen,suffix,slen+1);
    }

    FILE *idx = fopen(tmp?tmp:bname,"wb");
    if ( tmp ) free(tmp);
    if ( !idx ) {
        if (hts_verbose > 1)
        {
            fprintf(stderr, "[E::%s] Error opening %s%s : %s\n",
                    __func__, bname, suffix ? suffix : "", strerror(errno));
        }
        return -1;
    }

    // Note that the index contains one extra record when indexing files opened
    // for reading. The terminating record is not present when opened for writing.
    // This is not a bug.

    int i;
    if (fwrite_uint64(fp->idx->noffs - 1, idx) < 0) goto fail;
    for (i=1; i<fp->idx->noffs; i++)
    {
        if (fwrite_uint64(fp->idx->offs[i].caddr, idx) < 0) goto fail;
        if (fwrite_uint64(fp->idx->offs[i].uaddr, idx) < 0) goto fail;
    }

    if (fclose(idx) < 0)
    {
        if (hts_verbose > 1)
        {
            fprintf(stderr, "[E::%s] Error on closing %s%s : %s\n",
                    __func__, bname, suffix ? suffix : "", strerror(errno));
        }
        return -1;
    }
    return 0;

 fail:
    if (hts_verbose > 1)
    {
        fprintf(stderr, "[E::%s] Error writing to %s%s : %s\n",
                __func__, bname, suffix ? suffix : "", strerror(errno));
    }
    fclose(idx);
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
callq 0x2835e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x29374
cmpq $0x0, 0x48(%r15)
je 0x293b3
testq %r14, %r14
je 0x29251
movq %r12, %rdi
callq 0x3380
movq %rax, %rbx
movq %r14, %rdi
callq 0x3380
movq %r14, 0x10(%rsp)
movq %r12, %r14
movq %rax, %r12
addl %ebx, %eax
incl %eax
movslq %eax, %rdi
callq 0x3240
testq %rax, %rax
je 0x29361
movq %rax, %r13
movslq %ebx, %rbx
movq %rax, %rdi
movq %r14, (%rsp)
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3850
movq %r13, %rdi
addq %rbx, %rdi
shlq $0x20, %r12
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
sarq $0x20, %r12
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x3850
leaq 0x249a6(%rip), %rsi     # 0x4dbe2
movq %r13, %rdi
callq 0x3250
movq %rax, %r12
movq %r13, %rdi
callq 0x3370
jmp 0x29267
leaq 0x2498a(%rip), %rsi     # 0x4dbe2
movq %r12, (%rsp)
movq %r12, %rdi
callq 0x3250
movq %rax, %r12
testq %r12, %r12
je 0x29368
movq 0x48(%r15), %rax
movslq (%rax), %rax
decq %rax
leaq 0x8(%rsp), %rdi
movq %rax, (%rdi)
movl $0x8, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x3770
cmpq $0x1, %rax
jne 0x2934d
movq 0x48(%r15), %rax
cmpl $0x2, (%rax)
jl 0x2931c
movl $0x1, %ebx
xorl %ebp, %ebp
leaq 0x8(%rsp), %r13
movq 0x8(%rax), %rax
movq 0x18(%rax,%rbp), %rax
movq %rax, 0x8(%rsp)
movl $0x8, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x3770
cmpq $0x1, %rax
jne 0x2934d
movq 0x48(%r15), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax,%rbp), %rax
movq %rax, 0x8(%rsp)
movl $0x8, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x3770
cmpq $0x1, %rax
jne 0x2934d
incq %rbx
movq 0x48(%r15), %rax
movslq (%rax), %rcx
addq $0x10, %rbp
cmpq %rcx, %rbx
jl 0x292b3
movq %r12, %rdi
callq 0x36c0
xorl %ebp, %ebp
testl %eax, %eax
jns 0x29374
leaq 0x36157(%rip), %rax     # 0x5f488
cmpl $0x2, (%rax)
jl 0x29361
xorl %edi, %edi
testq %r14, %r14
sete %dil
movq %r14, %rsi
movq (%rsp), %rdx
callq 0x492b
jmp 0x29361
leaq 0x36134(%rip), %rax     # 0x5f488
cmpl $0x2, (%rax)
jge 0x29385
movq %r12, %rdi
callq 0x36c0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x29374
leaq 0x36119(%rip), %rax     # 0x5f488
cmpl $0x2, (%rax)
jge 0x2939c
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
testq %r14, %r14
sete %dil
movq %r14, %rsi
movq (%rsp), %rdx
callq 0x48d8
jmp 0x29359
xorl %edi, %edi
testq %r14, %r14
sete %dil
movq %r14, %rsi
movq (%rsp), %rdx
callq 0x497e
jmp 0x29361
leaq 0x26d0e(%rip), %rdi     # 0x500c8
leaq 0x26a74(%rip), %rsi     # 0x4fe35
leaq 0x26d08(%rip), %rcx     # 0x500d0
movl $0x47d, %edx            # imm = 0x47D
callq 0x3180