// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x65080
cmpl $0x0, %eax
je 0x66451
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x667b6
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x6645e
jmp 0x6647d
leaq 0x17c43(%rip), %rdi     # 0x7e0a8
leaq 0x179a9(%rip), %rsi     # 0x7de15
movl $0x47d, %edx            # imm = 0x47D
leaq 0x17c38(%rip), %rcx     # 0x7e0b0
callq 0x4360
movq $0x0, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x664fa
movq -0x18(%rbp), %rdi
callq 0x4230
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4230
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
addl -0x30(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdi
callq 0x47a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x664cc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x667b6
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x4460
movq -0x28(%rbp), %rdi
movslq -0x2c(%rbp), %rax
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movl -0x30(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x4460
cmpq $0x0, -0x28(%rbp)
je 0x6650b
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x66513
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
leaq 0x17bca(%rip), %rsi     # 0x7e0e8
callq 0x4690
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x66537
movq -0x28(%rbp), %rdi
callq 0x46a0
cmpq $0x0, -0x38(%rbp)
jne 0x665c6
leaq 0x24097(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jle 0x665ba
movq 0x23a7b(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x66575
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x66582
leaq 0x15f50(%rip), %rax     # 0x7c4cc
movq %rax, -0x60(%rbp)
jmp 0x66582
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %r8
movq %rax, %r9
leaq 0x17b3f(%rip), %rsi     # 0x7e0eb
leaq 0x17b59(%rip), %rdx     # 0x7e10c
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x667b6
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl (%rax), %eax
subl $0x1, %eax
movslq %eax, %rdi
movq -0x38(%rbp), %rsi
callq 0x667d0
cmpl $0x0, %eax
jge 0x665e9
jmp 0x6670f
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
cmpl (%rcx), %eax
jge 0x66667
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x667d0
cmpl $0x0, %eax
jge 0x6662d
jmp 0x6670f
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x667d0
cmpl $0x0, %eax
jge 0x6665a
jmp 0x6670f
jmp 0x6665c
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x665f0
movq -0x38(%rbp), %rdi
callq 0x4590
cmpl $0x0, %eax
jge 0x66703
leaq 0x23f60(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jle 0x666f7
movq 0x23944(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x666ac
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x666b9
leaq 0x15e19(%rip), %rax     # 0x7c4cc
movq %rax, -0x80(%rbp)
jmp 0x666b9
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rcx
movq -0x88(%rbp), %r8
movq %rax, %r9
leaq 0x17a33(%rip), %rsi     # 0x7e11c
leaq 0x17a1c(%rip), %rdx     # 0x7e10c
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x667b6
movl $0x0, -0x4(%rbp)
jmp 0x667b6
leaq 0x23eca(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jle 0x667a6
movq 0x238aa(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x6674f
movq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x6675f
leaq 0x15d76(%rip), %rax     # 0x7c4cc
movq %rax, -0xa0(%rbp)
jmp 0x6675f
movq -0xa0(%rbp), %rax
movq %rax, -0xa8(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rcx
movq -0xa8(%rbp), %r8
movq %rax, %r9
leaq 0x179a8(%rip), %rsi     # 0x7e140
leaq 0x1796d(%rip), %rdx     # 0x7e10c
movb $0x0, %al
callq 0x4830
movq -0x38(%rbp), %rdi
callq 0x4590
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)