// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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 ) 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 ( fp->is_be )
    {
        uint64_t x = fp->idx->noffs - 1;
        fwrite(ed_swap_8p(&x), 1, sizeof(x), idx);
        for (i=1; i<fp->idx->noffs; i++)
        {
            x = fp->idx->offs[i].caddr; fwrite(ed_swap_8p(&x), 1, sizeof(x), idx);
            x = fp->idx->offs[i].uaddr; fwrite(ed_swap_8p(&x), 1, sizeof(x), idx);
        }
    }
    else
    {
        uint64_t x = fp->idx->noffs - 1;
        fwrite(&x, 1, sizeof(x), idx);
        for (i=1; i<fp->idx->noffs; i++)
        {
            fwrite(&fp->idx->offs[i].caddr, 1, sizeof(fp->idx->offs[i].caddr), idx);
            fwrite(&fp->idx->offs[i].uaddr, 1, sizeof(fp->idx->offs[i].uaddr), idx);
        }
    }
    fclose(idx);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
callq 0x5ccd1
pushq $-0x1
popq %rbp
testl %eax, %eax
jne 0x5d8b7
cmpq $0x0, 0x48(%r14)
je 0x5d8c8
testq %r15, %r15
je 0x5d756
movq %rbx, %rdi
callq 0x62d0
movq %rax, %r12
movq %r15, %rdi
callq 0x62d0
movq %rax, %r13
movq %r12, 0x8(%rsp)
addl %r12d, %eax
incl %eax
movslq %eax, %rdi
callq 0x69d0
testq %rax, %rax
je 0x5d8b7
movq %rax, %r12
movslq 0x8(%rsp), %rdx
movq %rdx, 0x8(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x6570
movq %r12, %rdi
addq 0x8(%rsp), %rdi
shlq $0x20, %r13
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r13
sarq $0x20, %r13
movq %r15, %rsi
movq %r13, %rdx
callq 0x6570
jmp 0x5d759
xorl %r12d, %r12d
testq %r12, %r12
cmovneq %r12, %rbx
leaq 0x243df(%rip), %rsi     # 0x81b46
movq %rbx, %rdi
callq 0x6880
movq %rax, %rbx
movq %r12, %rdi
callq 0x68a0
movq %rbx, 0x8(%rsp)
testq %rbx, %rbx
je 0x5d8b7
testb $0xc, 0x2(%r14)
je 0x5d82d
movq 0x48(%r14), %rax
movslq (%rax), %rax
decq %rax
bswapq %rax
leaq 0x10(%rsp), %rdi
movq %rax, (%rdi)
pushq $0x1
popq %r12
pushq $0x8
popq %rdx
movq %r12, %rsi
movq 0x8(%rsp), %rcx
callq 0x6e70
xorl %ebp, %ebp
pushq $0x8
popq %r15
movq 0x8(%rsp), %rbx
movq 0x48(%r14), %rax
movslq (%rax), %rcx
cmpq %rcx, %r12
jge 0x5d8ab
movq 0x8(%rax), %rax
movq 0x18(%rax,%rbp), %rax
bswapq %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %r13
movq %r13, %rdi
pushq $0x1
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x6e70
movq 0x48(%r14), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax,%rbp), %rax
bswapq %rax
movq %rax, 0x10(%rsp)
movq %r13, %rdi
pushq $0x1
popq %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x6e70
incq %r12
addq $0x10, %rbp
jmp 0x5d7c7
movq 0x48(%r14), %rax
movslq (%rax), %rax
decq %rax
leaq 0x10(%rsp), %rdi
movq %rax, (%rdi)
pushq $0x1
popq %r15
pushq $0x8
popq %r12
movq %r15, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
callq 0x6e70
pushq $0x10
popq %r13
movq %r15, %rbp
movq 0x8(%rsp), %rbx
movq 0x48(%r14), %rax
movslq (%rax), %rcx
cmpq %rcx, %rbp
jge 0x5d8ab
movq 0x8(%rax), %rax
leaq (%rax,%r13), %rdi
addq $0x8, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6e70
movq 0x48(%r14), %rax
movq 0x8(%rax), %rdi
addq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6e70
incq %rbp
addq $0x10, %r13
jmp 0x5d863
movq 0x8(%rsp), %rdi
callq 0x66e0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b24f(%rip), %rdi     # 0x88b1e
leaq 0x2b14f(%rip), %rsi     # 0x88a25
leaq 0x2b249(%rip), %rcx     # 0x88b26
movl $0x3cf, %edx            # imm = 0x3CF
callq 0x6460