// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
callq 0x21218
pushq $-0x1
popq %rcx
testl %eax, %eax
je 0x21dad
movl %ecx, %eax
jmp 0x21ed2
cmpq $0x0, 0x48(%r15)
je 0x21fbe
testq %r14, %r14
je 0x21e24
movq %rbx, %rdi
callq 0x3380
movq %rax, %rbp
movq %r14, %rdi
callq 0x3380
movq %rax, %r12
addl %ebp, %eax
incl %eax
movslq %eax, %rdi
callq 0x3240
testq %rax, %rax
je 0x21ecf
movq %rax, %r13
movslq %ebp, %rbp
movq %rax, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x3860
movq %r13, %rdi
addq %rbp, %rdi
shlq $0x20, %r12
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r12
sarq $0x20, %r12
movq %r14, %rsi
movq %r12, %rdx
callq 0x3860
jmp 0x21e27
xorl %r13d, %r13d
testq %r13, %r13
movq %r13, %rdi
cmoveq %rbx, %rdi
leaq 0x1ddaa(%rip), %rsi     # 0x3fbe2
callq 0x3250
movq %rax, %r12
movq %r13, %rdi
callq 0x3370
testq %r12, %r12
je 0x21ebf
movq 0x48(%r15), %rax
movslq (%rax), %rdi
decq %rdi
movq %r12, %rsi
callq 0x21fdd
testl %eax, %eax
js 0x21ea9
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x48(%r15), %rax
movslq (%rax), %rcx
incq %r13
cmpq %rcx, %r13
jge 0x21ee1
movq 0x8(%rax), %rax
movq 0x18(%rax,%rbp), %rdi
movq %r12, %rsi
callq 0x21fdd
testl %eax, %eax
js 0x21ea9
movq 0x48(%r15), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax,%rbp), %rdi
addq $0x10, %rbp
movq %r12, %rsi
callq 0x21fdd
testl %eax, %eax
jns 0x21e68
leaq 0x315e0(%rip), %rax     # 0x53490
cmpl $0x2, (%rax)
jge 0x21f2a
movq %r12, %rdi
callq 0x36d0
jmp 0x21ecf
leaq 0x315ca(%rip), %rax     # 0x53490
cmpl $0x2, (%rax)
jge 0x21f74
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x36d0
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jns 0x21ed2
leaq 0x31598(%rip), %rax     # 0x53490
cmpl $0x2, (%rax)
jl 0x21ecf
testq %r14, %r14
movq 0x310e1(%rip), %rax     # 0x52fe8
movq (%rax), %r15
leaq 0x1e42e(%rip), %r12     # 0x4033f
cmovneq %r14, %r12
callq 0x35e0
movl (%rax), %edi
callq 0x34e0
leaq 0x20031(%rip), %rsi     # 0x41f59
jmp 0x21f9f
testq %r14, %r14
movq 0x310b4(%rip), %rax     # 0x52fe8
movq (%rax), %r15
leaq 0x1e401(%rip), %r13     # 0x4033f
cmovneq %r14, %r13
callq 0x35e0
movl (%rax), %edi
callq 0x34e0
leaq 0x20028(%rip), %rsi     # 0x41f7d
leaq 0x1ffed(%rip), %rdx     # 0x41f49
movq %r15, %rdi
movq %rbx, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x37f0
jmp 0x21eb5
testq %r14, %r14
movq 0x3106a(%rip), %rax     # 0x52fe8
movq (%rax), %r15
leaq 0x1e3b7(%rip), %r12     # 0x4033f
cmovneq %r14, %r12
callq 0x35e0
movl (%rax), %edi
callq 0x34e0
leaq 0x1ff89(%rip), %rsi     # 0x41f28
leaq 0x1ffa3(%rip), %rdx     # 0x41f49
movq %r15, %rdi
movq %rbx, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x37f0
jmp 0x21ecf
leaq 0x1ff23(%rip), %rdi     # 0x41ee8
leaq 0x1fc89(%rip), %rsi     # 0x41c55
leaq 0x1ff1d(%rip), %rcx     # 0x41ef0
movl $0x47d, %edx            # imm = 0x47D
callq 0x3180