// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rdi, %r15
callq 0x285a0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x294ae
cmpq $0x0, 0x48(%r15)
je 0x294ef
testq %r14, %r14
movq %rbx, 0x8(%rsp)
je 0x29372
movq %rbx, %rdi
callq 0x3380
movq %rax, %rbx
movq %r14, %rdi
callq 0x3380
movq %rax, %r12
addl %ebx, %eax
incl %eax
movslq %eax, %rdi
callq 0x3240
testq %rax, %rax
je 0x2949b
movq %rax, %r13
movslq %ebx, %rbx
movq %rax, %rdi
movq 0x8(%rsp), %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 %r14, (%rsp)
movq %r14, %rsi
movq %r12, %rdx
callq 0x3850
movq 0x8(%rsp), %rbx
jmp 0x29379
movq %r14, (%rsp)
xorl %r13d, %r13d
testq %r13, %r13
movq %r13, %rdi
cmoveq %rbx, %rdi
leaq 0x24858(%rip), %rsi     # 0x4dbe2
callq 0x3250
movq %rax, %r12
testq %r13, %r13
je 0x2939f
movq %r13, %rdi
callq 0x3370
testq %r12, %r12
je 0x294a2
movq 0x48(%r15), %rax
movslq (%rax), %rax
decq %rax
leaq 0x10(%rsp), %rdi
movq %rax, (%rdi)
movl $0x8, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x3770
cmpq $0x1, %rax
jne 0x29487
movq 0x48(%r15), %rax
cmpl $0x2, (%rax)
jl 0x29454
movl $0x1, %ebx
xorl %ebp, %ebp
leaq 0x10(%rsp), %r13
movq 0x8(%rax), %rax
movq 0x18(%rax,%rbp), %rax
movq %rax, 0x10(%rsp)
movl $0x8, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x3770
cmpq $0x1, %rax
jne 0x29487
movq 0x48(%r15), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax,%rbp), %rax
movq %rax, 0x10(%rsp)
movl $0x8, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x3770
cmpq $0x1, %rax
jne 0x29487
incq %rbx
movq 0x48(%r15), %rax
movslq (%rax), %rcx
addq $0x10, %rbp
cmpq %rcx, %rbx
jl 0x293eb
movq %r12, %rdi
callq 0x36c0
xorl %ebp, %ebp
testl %eax, %eax
movq 0x8(%rsp), %rdx
jns 0x294ae
leaq 0x3501a(%rip), %rax     # 0x5e488
cmpl $0x2, (%rax)
jl 0x2949b
xorl %edi, %edi
movq (%rsp), %rsi
testq %rsi, %rsi
sete %dil
callq 0x49e5
jmp 0x2949b
leaq 0x34ffa(%rip), %rax     # 0x5e488
cmpl $0x2, (%rax)
jge 0x294bf
movq %r12, %rdi
callq 0x36c0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x294ae
leaq 0x34fdf(%rip), %rax     # 0x5e488
cmpl $0x2, (%rax)
jge 0x294d8
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
movq (%rsp), %rsi
testq %rsi, %rsi
sete %dil
movq 0x8(%rsp), %rdx
callq 0x4992
jmp 0x29493
xorl %edi, %edi
movq (%rsp), %rsi
testq %rsi, %rsi
sete %dil
movq %rbx, %rdx
callq 0x4a38
jmp 0x2949b
leaq 0x26a02(%rip), %rdi     # 0x4fef8
leaq 0x26768(%rip), %rsi     # 0x4fc65
leaq 0x269fc(%rip), %rcx     # 0x4ff00
movl $0x47d, %edx            # imm = 0x47D
callq 0x3180