// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdi, %r15
callq 0x43baa
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x44bc0
cmpq $0x0, 0x48(%r15)
je 0x44bff
testq %r14, %r14
je 0x44a9d
movq %r12, %rdi
callq 0x4230
movq %rax, %rbx
movq %r14, %rdi
callq 0x4230
movq %r14, 0x10(%rsp)
movq %r12, %r14
movq %rax, %r12
addl %ebx, %eax
incl %eax
movslq %eax, %rdi
callq 0x4700
testq %rax, %rax
je 0x44bad
movq %rax, %r13
movslq %ebx, %rbx
movq %rax, %rdi
movq %r14, (%rsp)
movq %r14, %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 0x10(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x4420
leaq 0xf450(%rip), %rsi      # 0x53ed8
movq %r13, %rdi
callq 0x4600
movq %rax, %r12
movq %r13, %rdi
callq 0x4610
jmp 0x44ab3
leaq 0xf434(%rip), %rsi      # 0x53ed8
movq %r12, (%rsp)
movq %r12, %rdi
callq 0x4600
movq %rax, %r12
testq %r12, %r12
je 0x44bb4
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 0x4a30
cmpq $0x1, %rax
jne 0x44b99
movq 0x48(%r15), %rax
cmpl $0x2, (%rax)
jl 0x44b68
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 0x4a30
cmpq $0x1, %rax
jne 0x44b99
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 0x4a30
cmpq $0x1, %rax
jne 0x44b99
incq %rbx
movq 0x48(%r15), %rax
movslq (%rax), %rcx
addq $0x10, %rbp
cmpq %rcx, %rbx
jl 0x44aff
movq %r12, %rdi
callq 0x4530
xorl %ebp, %ebp
testl %eax, %eax
jns 0x44bc0
leaq 0x1d9d3(%rip), %rax     # 0x62550
cmpl $0x2, (%rax)
jl 0x44bad
xorl %edi, %edi
testq %r14, %r14
sete %dil
movq %r14, %rsi
movq (%rsp), %rdx
callq 0x5d11
jmp 0x44bad
leaq 0x1d9b0(%rip), %rax     # 0x62550
cmpl $0x2, (%rax)
jge 0x44bd1
movq %r12, %rdi
callq 0x4530
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x44bc0
leaq 0x1d995(%rip), %rax     # 0x62550
cmpl $0x2, (%rax)
jge 0x44be8
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 0x5cbe
jmp 0x44ba5
xorl %edi, %edi
testq %r14, %r14
sete %dil
movq %r14, %rsi
movq (%rsp), %rdx
callq 0x5d64
jmp 0x44bad
leaq 0xf292(%rip), %rdi      # 0x53e98
leaq 0xeff8(%rip), %rsi      # 0x53c05
leaq 0xf28c(%rip), %rcx      # 0x53ea0
movl $0x47d, %edx            # imm = 0x47D
callq 0x4350