// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/cram/cram_io.c
// Function: refs_load_fai
static refs_t *refs_load_fai(refs_t *r_orig, const char *fn, int is_err) {
    hFILE *fp = NULL;
    char fai_fn[PATH_MAX];
    char line[8192];
    refs_t *r = r_orig;
    size_t fn_l = strlen(fn);
    int id = 0, id_alloc = 0;

    RP("refs_load_fai %s\n", fn);

    if (!r)
        if (!(r = refs_create()))
            goto err;

    if (r->fp)
        if (bgzf_close(r->fp) != 0)
            goto err;
    r->fp = NULL;

    /* Look for a FASTA##idx##FAI format */
    char *fn_delim = strstr(fn, HTS_IDX_DELIM);
    if (fn_delim) {
        if (!(r->fn = string_ndup(r->pool, fn, fn_delim - fn)))
            goto err;
        fn_delim += strlen(HTS_IDX_DELIM);
        snprintf(fai_fn, PATH_MAX, "%s", fn_delim);
    } else {
        /* An index file was provided, instead of the actual reference file */
        if (fn_l > 4 && strcmp(&fn[fn_l-4], ".fai") == 0) {
            if (!r->fn) {
                if (!(r->fn = string_ndup(r->pool, fn, fn_l-4)))
                    goto err;
            }
            snprintf(fai_fn, PATH_MAX, "%s", fn);
        } else {
        /* Only the reference file provided. Get the index file name from it */
            if (!(r->fn = string_dup(r->pool, fn)))
                goto err;
            snprintf(fai_fn, PATH_MAX, "%.*s.fai", PATH_MAX-5, fn);
        }
    }

    if (!(r->fp = bgzf_open_ref(r->fn, "r", 0))) {
        hts_log_error("Failed to open reference file '%s'", r->fn);
        goto err;
    }

    if (!(fp = hopen(fai_fn, "r"))) {
        hts_log_error("Failed to open index file '%s'", fai_fn);
        if (is_err)
            perror(fai_fn);
        goto err;
    }
    while (hgets(line, 8192, fp) != NULL) {
        ref_entry *e = malloc(sizeof(*e));
        char *cp;
        int n;
        khint_t k;

        if (!e)
            return NULL;

        // id
        for (cp = line; *cp && !isspace_c(*cp); cp++)
            ;
        *cp++ = 0;
        e->name = string_dup(r->pool, line);

        // length
        while (*cp && isspace_c(*cp))
            cp++;
        e->length = strtoll(cp, &cp, 10);

        // offset
        while (*cp && isspace_c(*cp))
            cp++;
        e->offset = strtoll(cp, &cp, 10);

        // bases per line
        while (*cp && isspace_c(*cp))
            cp++;
        e->bases_per_line = strtol(cp, &cp, 10);

        // line length
        while (*cp && isspace_c(*cp))
            cp++;
        e->line_length = strtol(cp, &cp, 10);

        // filename
        e->fn = r->fn;

        e->count = 0;
        e->seq = NULL;
        e->mf = NULL;
        e->is_md5 = 0;
        e->validated_md5 = 0;

        k = kh_put(refs, r->h_meta, e->name, &n);
        if (-1 == n)  {
            free(e);
            return NULL;
        }

        if (n) {
            kh_val(r->h_meta, k) = e;
        } else {
            ref_entry *re = kh_val(r->h_meta, k);
            if (re && (re->count != 0 || re->length != 0)) {
                /* Keep old */
                free(e);
            } else {
                /* Replace old */
                if (re)
                    free(re);
                kh_val(r->h_meta, k) = e;
            }
        }

        if (id >= id_alloc) {
            ref_entry **new_refs;
            int x;

            id_alloc = id_alloc ?id_alloc*2 : 16;
            new_refs = realloc(r->ref_id, id_alloc * sizeof(*r->ref_id));
            if (!new_refs)
                goto err;
            r->ref_id = new_refs;

            for (x = id; x < id_alloc; x++)
                r->ref_id[x] = NULL;
        }
        r->ref_id[id] = e;
        r->nref = ++id;
    }

    if(hclose(fp) < 0)
        goto err;
    return r;

 err:
    if (fp)
        hclose_abruptly(fp);

    if (!r_orig)
        refs_free(r);

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3028, %rsp           # imm = 0x3028
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, %rdi
callq 0x41d0
movq %rax, %r12
movq %r14, %rbx
testq %r14, %r14
jne 0x41c80
callq 0x421be
movq %rax, %rbx
testq %rax, %rax
je 0x41fb7
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x41c96
callq 0xbc96
testl %eax, %eax
jne 0x42024
andq $0x0, 0x28(%rbx)
leaq 0x3622f(%rip), %rsi     # 0x77ed1
movq %r15, %rdi
callq 0x40e0
testq %rax, %rax
je 0x41cf1
movq %rax, %r13
movq (%rbx), %rdi
movq %rax, %rdx
subq %r15, %rdx
movq %r15, %rsi
callq 0x493ba
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x42024
addq $0x7, %r13
leaq 0x3874e(%rip), %rdx     # 0x7a429
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rcx
xorl %eax, %eax
callq 0x4800
jmp 0x41d4b
cmpq $0x5, %r12
jb 0x41d13
leaq (%r15,%r12), %rdi
addq $-0x4, %rdi
leaq 0x3b6b2(%rip), %rsi     # 0x7d3b8
callq 0x46c0
testl %eax, %eax
je 0x42048
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x49393
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x42024
leaq 0x3b823(%rip), %rdx     # 0x7d555
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movl $0xffb, %ecx            # imm = 0xFFB
movq %r15, %r8
xorl %eax, %eax
callq 0x4800
movq 0x20(%rbx), %rdi
xorl %esi, %esi
callq 0x40f42
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x41fbb
movq %r14, 0x18(%rsp)
leaq 0x387b7(%rip), %rsi     # 0x7a526
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
xorl %eax, %eax
callq 0xe19b
testq %rax, %rax
je 0x41fd9
movq %rax, %r13
xorl %ebp, %ebp
leaq 0x1020(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r13, %rdx
callq 0xd4a0
testq %rax, %rax
je 0x4200b
pushq $0x50
popq %rdi
callq 0x4680
testq %rax, %rax
je 0x42031
movq %rax, %r12
leaq 0x1021(%rsp), %r15
movb -0x1(%r15), %al
testb %al, %al
je 0x41ddf
movsbl %al, %edi
callq 0x466b8
testl %eax, %eax
jne 0x41ddf
incq %r15
jmp 0x41dc6
movq %r15, 0x8(%rsp)
movb $0x0, -0x1(%r15)
movq (%rbx), %rdi
leaq 0x1020(%rsp), %rsi
callq 0x49393
movq %rax, (%r12)
movb (%r15), %al
testb %al, %al
je 0x41e15
movsbl %al, %edi
callq 0x466b8
testl %eax, %eax
je 0x41e15
incq %r15
jmp 0x41dfd
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x40b0
movq %rax, 0x10(%r12)
movq 0x8(%rsp), %r15
movb (%r15), %al
testb %al, %al
je 0x41e4c
movsbl %al, %edi
callq 0x466b8
testl %eax, %eax
je 0x41e4c
incq %r15
jmp 0x41e34
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x40b0
movq %rax, 0x20(%r12)
movq 0x8(%rsp), %r15
movb (%r15), %al
testb %al, %al
je 0x41e83
movsbl %al, %edi
callq 0x466b8
testl %eax, %eax
je 0x41e83
incq %r15
jmp 0x41e6b
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x46d0
movl %eax, 0x28(%r12)
movq 0x8(%rsp), %r15
movb (%r15), %al
testb %al, %al
je 0x41eba
movsbl %al, %edi
callq 0x466b8
testl %eax, %eax
je 0x41eba
incq %r15
jmp 0x41ea2
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x46d0
movl %eax, 0x2c(%r12)
movq 0x20(%rbx), %rax
movq %rax, 0x8(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%r12)
movups %xmm0, 0x30(%r12)
movq 0x8(%rbx), %rdi
movq (%r12), %rsi
leaq 0x14(%rsp), %rdx
callq 0x46248
movl 0x14(%rsp), %ecx
movl %eax, %r15d
testl %ecx, %ecx
je 0x41f1c
cmpl $-0x1, %ecx
je 0x42083
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
jmp 0x41f45
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x41f45
cmpq $0x0, 0x30(%rdi)
jne 0x41f3b
cmpq $0x0, 0x10(%rdi)
je 0x41fad
movq %r12, %rdi
callq 0x45c0
jmp 0x41f49
movq %r12, (%rax,%r15,8)
movslq %r14d, %rax
movq 0x10(%rbx), %rdi
cmpq %rax, %rbp
jge 0x41f64
movq %r12, (%rdi,%rbp,8)
incq %rbp
movl %ebp, 0x18(%rbx)
jmp 0x41d8c
testl %r14d, %r14d
leal (%r14,%r14), %eax
movl %eax, %r14d
pushq $0x10
popq %rax
cmovel %eax, %r14d
movslq %r14d, %rsi
shlq $0x3, %rsi
callq 0x4840
testq %rax, %rax
je 0x42017
movq %rax, 0x10(%rbx)
cmpl %ebp, %r14d
movl %ebp, %eax
cmovgl %r14d, %eax
movq %rbp, %rcx
movq 0x10(%rbx), %rdi
cmpq %rcx, %rax
je 0x41f55
andq $0x0, (%rdi,%rcx,8)
incq %rcx
jmp 0x41f9a
callq 0x45c0
jmp 0x41f12
xorl %ebx, %ebx
jmp 0x42029
movq 0x20(%rbx), %rcx
leaq 0x3b598(%rip), %rsi     # 0x7d55e
leaq 0x3b59f(%rip), %rdx     # 0x7d56c
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x42024
leaq 0x3b57e(%rip), %rsi     # 0x7d55e
leaq 0x36b34(%rip), %rdx     # 0x78b1b
pushq $0x1
popq %rdi
leaq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x110c6
testl %ebp, %ebp
movq 0x18(%rsp), %r14
je 0x42024
leaq 0x20(%rsp), %rdi
callq 0x43d0
jmp 0x42024
movq %r13, %rdi
callq 0xd8ee
testl %eax, %eax
jns 0x42033
movq %r13, %rdi
callq 0xd94c
movq 0x18(%rsp), %r14
testq %r14, %r14
jne 0x42031
movq %rbx, %rdi
callq 0x407fe
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x3028, %rsp           # imm = 0x3028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x20(%rbx)
jne 0x4206a
addq $-0x4, %r12
movq (%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x493ba
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x42024
leaq 0x383b8(%rip), %rdx     # 0x7a429
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rcx
jmp 0x41ce8
movq %r12, %rdi
callq 0x45c0
jmp 0x42031