// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %rax
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
callq 0x37308
pushq $-0x1
popq %rcx
testl %eax, %eax
je 0x37e9d
movl %ecx, %eax
jmp 0x37fc2
cmpq $0x0, 0x48(%r15)
je 0x380ae
testq %r14, %r14
je 0x37f14
movq %rbx, %rdi
callq 0x4210
movq %rax, %rbp
movq %r14, %rdi
callq 0x4210
movq %rax, %r12
addl %ebp, %eax
incl %eax
movslq %eax, %rdi
callq 0x46f0
testq %rax, %rax
je 0x37fbf
movq %rax, %r13
movslq %ebp, %rbp
movq %rax, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x4420
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 0x4420
jmp 0x37f17
xorl %r13d, %r13d
testq %r13, %r13
movq %r13, %rdi
cmoveq %rbx, %rdi
leaq 0xed00(%rip), %rsi      # 0x46c28
callq 0x45f0
movq %rax, %r12
movq %r13, %rdi
callq 0x4600
testq %r12, %r12
je 0x37faf
movq 0x48(%r15), %rax
movslq (%rax), %rdi
decq %rdi
movq %r12, %rsi
callq 0x380cd
testl %eax, %eax
js 0x37f99
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x48(%r15), %rax
movslq (%rax), %rcx
incq %r13
cmpq %rcx, %r13
jge 0x37fd1
movq 0x8(%rax), %rax
movq 0x18(%rax,%rbp), %rdi
movq %r12, %rsi
callq 0x380cd
testl %eax, %eax
js 0x37f99
movq 0x48(%r15), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax,%rbp), %rdi
addq $0x10, %rbp
movq %r12, %rsi
callq 0x380cd
testl %eax, %eax
jns 0x37f58
leaq 0x1f5a8(%rip), %rax     # 0x57548
cmpl $0x2, (%rax)
jge 0x3801a
movq %r12, %rdi
callq 0x4530
jmp 0x37fbf
leaq 0x1f592(%rip), %rax     # 0x57548
cmpl $0x2, (%rax)
jge 0x38064
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x4530
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
jns 0x37fc2
leaq 0x1f560(%rip), %rax     # 0x57548
cmpl $0x2, (%rax)
jl 0x37fbf
testq %r14, %r14
movq 0x1efd9(%rip), %rax     # 0x56fd0
movq (%rax), %r15
leaq 0xbbeb(%rip), %r12      # 0x43bec
cmovneq %r14, %r12
callq 0x4060
movl (%rax), %edi
callq 0x4190
leaq 0xec44(%rip), %rsi      # 0x46c5c
jmp 0x3808f
testq %r14, %r14
movq 0x1efac(%rip), %rax     # 0x56fd0
movq (%rax), %r15
leaq 0xbbbe(%rip), %r13      # 0x43bec
cmovneq %r14, %r13
callq 0x4060
movl (%rax), %edi
callq 0x4190
leaq 0xec3b(%rip), %rsi      # 0x46c80
leaq 0xec00(%rip), %rdx      # 0x46c4c
movq %r15, %rdi
movq %rbx, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x4770
jmp 0x37fa5
testq %r14, %r14
movq 0x1ef62(%rip), %rax     # 0x56fd0
movq (%rax), %r15
leaq 0xbb74(%rip), %r12      # 0x43bec
cmovneq %r14, %r12
callq 0x4060
movl (%rax), %edi
callq 0x4190
leaq 0xeb9c(%rip), %rsi      # 0x46c2b
leaq 0xebb6(%rip), %rdx      # 0x46c4c
movq %r15, %rdi
movq %rbx, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x4770
jmp 0x37fbf
leaq 0xeb33(%rip), %rdi      # 0x46be8
leaq 0xe899(%rip), %rsi      # 0x46955
leaq 0xeb2d(%rip), %rcx      # 0x46bf0
movl $0x47d, %edx            # imm = 0x47D
callq 0x4360