// File: karel-brinda[P]ococo/ext/htslib/bgzf.c
// Function: bgzf_index_load
int bgzf_index_load(BGZF *fp, const char *bname, const char *suffix)
{
    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,"rb");
    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;
    }

    fp->idx = (bgzidx_t*) calloc(1,sizeof(bgzidx_t));
    if (fp->idx == NULL) goto fail;
    uint64_t x;
    if (fread_uint64(&x, idx) < 0) goto fail;

    fp->idx->noffs = fp->idx->moffs = x + 1;
    fp->idx->offs  = (bgzidx1_t*) malloc(fp->idx->moffs*sizeof(bgzidx1_t));
    if (fp->idx->offs == NULL) goto fail;
    fp->idx->offs[0].caddr = fp->idx->offs[0].uaddr = 0;

    int i;
    for (i=1; i<fp->idx->noffs; i++)
    {
        if (fread_uint64(&fp->idx->offs[i].caddr, idx) < 0) goto fail;
        if (fread_uint64(&fp->idx->offs[i].uaddr, idx) < 0) goto fail;
    }

    if (fclose(idx) != 0) goto fail;
    return 0;

 fail:
    if (hts_verbose > 1)
    {
        fprintf(stderr, "[E::%s] Error reading %s%s : %s\n",
                __func__, bname, suffix ? suffix : "", strerror(errno));
    }
    fclose(idx);
    if (fp->idx) {
        free(fp->idx->offs);
        free(fp->idx);
        fp->idx = NULL;
    }
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x668c4
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 0x66896
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x66bac
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 0x668d5
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x668dd
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x148c9(%rip), %rsi     # 0x7b1b1
callq 0x4690
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x66901
movq -0x28(%rbp), %rdi
callq 0x46a0
cmpq $0x0, -0x38(%rbp)
jne 0x66990
leaq 0x23ccd(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jle 0x66984
movq 0x236b1(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x6693f
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x6694c
leaq 0x15b86(%rip), %rax     # 0x7c4cc
movq %rax, -0x68(%rbp)
jmp 0x6694c
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rcx
movq -0x70(%rbp), %r8
movq %rax, %r9
leaq 0x17775(%rip), %rsi     # 0x7e0eb
leaq 0x177e7(%rip), %rdx     # 0x7e164
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x66bac
movl $0x1, %edi
movl $0x18, %esi
callq 0x4370
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x669ba
jmp 0x66ae0
movq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x66bc0
cmpl $0x0, %eax
jge 0x669d1
jmp 0x66ae0
movq -0x40(%rbp), %rax
addq $0x1, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movslq 0x4(%rax), %rdi
shlq $0x4, %rdi
callq 0x47a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0x66a28
jmp 0x66ae0
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rax
movq $0x0, 0x8(%rax)
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
cmpl (%rcx), %eax
jge 0x66ac4
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rdi
movslq -0x44(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
addq $0x8, %rdi
movq -0x38(%rbp), %rsi
callq 0x66bc0
cmpl $0x0, %eax
jge 0x66a90
jmp 0x66ae0
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rdi
movslq -0x44(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x66bc0
cmpl $0x0, %eax
jge 0x66ab7
jmp 0x66ae0
jmp 0x66ab9
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x66a56
movq -0x38(%rbp), %rdi
callq 0x4590
cmpl $0x0, %eax
je 0x66ad4
jmp 0x66ae0
movl $0x0, -0x4(%rbp)
jmp 0x66bac
leaq 0x23af9(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jle 0x66b67
movq 0x234dd(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x66b16
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x66b26
leaq 0x159af(%rip), %rax     # 0x7c4cc
movq %rax, -0x88(%rbp)
jmp 0x66b26
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rcx
movq -0x90(%rbp), %r8
movq %rax, %r9
leaq 0x1761b(%rip), %rsi     # 0x7e174
leaq 0x17604(%rip), %rdx     # 0x7e164
movb $0x0, %al
callq 0x4830
movq -0x38(%rbp), %rdi
callq 0x4590
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x66ba5
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x46a0
movq -0x10(%rbp), %rax
movq $0x0, 0x48(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)