// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/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 0x44bf0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x45afe
cmpq $0x0, 0x48(%r15)
je 0x45b3f
testq %r14, %r14
movq %rbx, 0x8(%rsp)
je 0x459c2
movq %rbx, %rdi
callq 0x4230
movq %rax, %rbx
movq %r14, %rdi
callq 0x4230
movq %rax, %r12
addl %ebx, %eax
incl %eax
movslq %eax, %rdi
callq 0x4700
testq %rax, %rax
je 0x45aeb
movq %rax, %r13
movslq %ebx, %rbx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x4420
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 0x4420
movq 0x8(%rsp), %rbx
jmp 0x459c9
movq %r14, (%rsp)
xorl %r13d, %r13d
testq %r13, %r13
movq %r13, %rdi
cmoveq %rbx, %rdi
leaq 0x1023e(%rip), %rsi     # 0x55c18
callq 0x4600
movq %rax, %r12
testq %r13, %r13
je 0x459ef
movq %r13, %rdi
callq 0x4610
testq %r12, %r12
je 0x45af2
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 0x4a30
cmpq $0x1, %rax
jne 0x45ad7
movq 0x48(%r15), %rax
cmpl $0x2, (%rax)
jl 0x45aa4
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 0x4a30
cmpq $0x1, %rax
jne 0x45ad7
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 0x4a30
cmpq $0x1, %rax
jne 0x45ad7
incq %rbx
movq 0x48(%r15), %rax
movslq (%rax), %rcx
addq $0x10, %rbp
cmpq %rcx, %rbx
jl 0x45a3b
movq %r12, %rdi
callq 0x4530
xorl %ebp, %ebp
testl %eax, %eax
movq 0x8(%rsp), %rdx
jns 0x45afe
leaq 0x1ea92(%rip), %rax     # 0x64550
cmpl $0x2, (%rax)
jl 0x45aeb
xorl %edi, %edi
movq (%rsp), %rsi
testq %rsi, %rsi
sete %dil
callq 0x5db3
jmp 0x45aeb
leaq 0x1ea72(%rip), %rax     # 0x64550
cmpl $0x2, (%rax)
jge 0x45b0f
movq %r12, %rdi
callq 0x4530
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x45afe
leaq 0x1ea57(%rip), %rax     # 0x64550
cmpl $0x2, (%rax)
jge 0x45b28
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 0x5d60
jmp 0x45ae3
xorl %edi, %edi
movq (%rsp), %rsi
testq %rsi, %rsi
sete %dil
movq %rbx, %rdx
callq 0x5e06
jmp 0x45aeb
leaq 0x10092(%rip), %rdi     # 0x55bd8
leaq 0xfdf8(%rip), %rsi      # 0x55945
leaq 0x1008c(%rip), %rcx     # 0x55be0
movl $0x47d, %edx            # imm = 0x47D
callq 0x4350