// File: karel-brinda[P]ococo/ext/htslib/faidx.c
// Function: fai_load
faidx_t *fai_load(const char *fn)
{
    char *str;
    FILE *fp;
    faidx_t *fai;
    str = (char*)calloc(strlen(fn) + 5, 1);
    sprintf(str, "%s.fai", fn);

    if (hisremote(str))
    {
        fp = download_and_open(str);
        if ( !fp )
        {
            fprintf(stderr, "[fai_load] failed to open remote FASTA index %s\n", str);
            free(str);
            return 0;
        }
    }
    else
        fp = fopen(str, "rb");

    if (fp == 0) {
        fprintf(stderr, "[fai_load] build FASTA index.\n");
        if (fai_build(fn) < 0) {
            free(str);
            return 0;
        }
        fp = fopen(str, "rb");
        if (fp == 0) {
            fprintf(stderr, "[fai_load] failed to open FASTA index: %s\n", strerror(errno));
            free(str);
            return 0;
        }
    }

    fai = fai_read(fp, str);
    fclose(fp);
    free(str);
    if (fai == NULL) {
        return NULL;
    }

    fai->bgzf = bgzf_open(fn, "rb");
    if (fai->bgzf == 0) {
        fprintf(stderr, "[fai_load] fail to open FASTA file.\n");
        return 0;
    }
    if ( fai->bgzf->is_compressed==1 )
    {
        if ( bgzf_index_load(fai->bgzf, fn, ".gzi") < 0 )
        {
            fprintf(stderr, "[fai_load] failed to load .gzi index: %s[.gzi]\n", fn);
            fai_destroy(fai);
            return NULL;
        }
    }
    return fai;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4230
movq %rax, %rdi
addq $0x5, %rdi
movl $0x1, %esi
callq 0x4370
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x1473f(%rip), %rsi     # 0x7d1c8
movb $0x0, %al
callq 0x4100
movq -0x18(%rbp), %rdi
callq 0x6b720
cmpl $0x0, %eax
je 0x68ae6
movq -0x18(%rbp), %rdi
callq 0x68ca0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x68ae4
movq 0x21517(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x15ace(%rip), %rsi     # 0x7e595
movb $0x0, %al
callq 0x4830
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x68c89
jmp 0x68afa
movq -0x18(%rbp), %rdi
leaq 0x126c0(%rip), %rsi     # 0x7b1b1
callq 0x4690
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x68ba3
movq 0x214c4(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x15ab0(%rip), %rsi     # 0x7e5c6
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rdi
callq 0x68800
cmpl $0x0, %eax
jge 0x68b41
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x68c89
movq -0x18(%rbp), %rdi
leaq 0x12665(%rip), %rsi     # 0x7b1b1
callq 0x4690
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x68ba1
movq 0x2146d(%rip), %rax     # 0x89fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
callq 0x4070
movl (%rax), %edi
callq 0x4190
movq -0x30(%rbp), %rdi
movq %rax, %rdx
leaq 0x15a61(%rip), %rsi     # 0x7e5e5
movb $0x0, %al
callq 0x4830
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x68c89
jmp 0x68ba3
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x68e70
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4590
movq -0x18(%rbp), %rdi
callq 0x46a0
cmpq $0x0, -0x28(%rbp)
jne 0x68bda
movq $0x0, -0x8(%rbp)
jmp 0x68c89
movq -0x10(%rbp), %rdi
leaq 0x125cc(%rip), %rsi     # 0x7b1b1
callq 0x62d50
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x68c20
movq 0x213cb(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x15a01(%rip), %rsi     # 0x7e610
movb $0x0, %al
callq 0x4830
movq $0x0, -0x8(%rbp)
jmp 0x68c89
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
shrl $0x1d, %eax
andl $0x3, %eax
cmpl $0x1, %eax
jne 0x68c81
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
leaq 0x14589(%rip), %rdx     # 0x7d1cf
callq 0x66830
cmpl $0x0, %eax
jge 0x68c7f
movq 0x21379(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x159d0(%rip), %rsi     # 0x7e635
movb $0x0, %al
callq 0x4830
movq -0x28(%rbp), %rdi
callq 0x68520
movq $0x0, -0x8(%rbp)
jmp 0x68c89
jmp 0x68c81
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)