// File: /workspace/github_c_makefile_star2_O3/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 0x4f550
callq 0x4fc28
movq %rax, %rbx
testq %rax, %rax
je 0x4f935
movq %r14, 0x18(%rsp)
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x4f56b
callq 0xd3cd
testl %eax, %eax
jne 0x4f97c
movq $0x0, 0x28(%rbx)
leaq 0x46966(%rip), %rsi     # 0x95ee0
movq %r15, %rdi
callq 0x40e0
testq %rax, %rax
je 0x4f5c9
movq %rax, %r13
movq (%rbx), %rdi
movq %rax, %rdx
subq %r15, %rdx
movq %r15, %rsi
callq 0x5801b
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x4f97c
addq $0x7, %r13
leaq 0x48f1b(%rip), %rdx     # 0x984ce
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rcx
xorl %eax, %eax
callq 0x4800
jmp 0x4f623
cmpq $0x5, %r12
jb 0x4f5eb
leaq (%r15,%r12), %rdi
addq $-0x4, %rdi
leaq 0x4bfdf(%rip), %rsi     # 0x9b5bd
callq 0x46c0
testl %eax, %eax
je 0x4f9a3
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x57fd5
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x4f97c
leaq 0x4c150(%rip), %rdx     # 0x9b75a
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 0x4e72f
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x4f939
leaq 0x48f89(%rip), %rsi     # 0x985cb
leaq 0x20(%rsp), %rdi
xorl %eax, %eax
callq 0xfe44
testq %rax, %rax
je 0x4f959
movq %rax, %r15
leaq 0x1020(%rsp), %r14
movq %r14, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %rax, %rdx
callq 0xf065
testq %rax, %rax
je 0x4f91f
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r15, 0x10(%rsp)
movl $0x50, %edi
callq 0x4680
testq %rax, %rax
je 0x4f98c
movq %rax, %rbp
movb 0x1020(%rsp), %r15b
movq %r14, %rax
testb %r15b, %r15b
je 0x4f6ce
callq 0x4510
movq (%rax), %rcx
movq %r14, %rax
movzbl %r15b, %edx
testb $0x20, 0x1(%rcx,%rdx,2)
jne 0x4f6ce
movb 0x1(%rax), %r15b
incq %rax
testb %r15b, %r15b
jne 0x4f6b7
leaq 0x1(%rax), %rcx
movq %rcx, (%rsp)
movb $0x0, (%rax)
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x57fd5
movq %rax, (%rbp)
movq (%rsp), %r15
movb (%r15), %r14b
testb %r14b, %r14b
je 0x4f713
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x4f713
movb 0x1(%r15), %r14b
incq %r15
testb %r14b, %r14b
jne 0x4f6fc
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x40b0
movq %rax, 0x10(%rbp)
movq (%rsp), %r15
movb (%r15), %r14b
testb %r14b, %r14b
je 0x4f756
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x4f756
movb 0x1(%r15), %r14b
incq %r15
testb %r14b, %r14b
jne 0x4f73f
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x40b0
movq %rax, 0x20(%rbp)
movq (%rsp), %r15
movb (%r15), %r14b
testb %r14b, %r14b
je 0x4f799
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x4f799
movb 0x1(%r15), %r14b
incq %r15
testb %r14b, %r14b
jne 0x4f782
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x46d0
movl %eax, 0x28(%rbp)
movq (%rsp), %r15
movb (%r15), %r14b
testb %r14b, %r14b
je 0x4f7db
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x4f7db
movb 0x1(%r15), %r14b
incq %r15
testb %r14b, %r14b
jne 0x4f7c4
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x46d0
movl %eax, 0x2c(%rbp)
movq 0x20(%rbx), %rax
movq %rax, 0x8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rbp)
movups %xmm0, 0x30(%rbp)
movq 0x8(%rbx), %rdi
movq (%rbp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x545ac
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
je 0x4f840
cmpl $-0x1, %ecx
leaq 0x1020(%rsp), %r14
je 0x4f9de
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rbp, (%rcx,%rax,8)
jmp 0x4f885
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %r15d
movq (%rcx,%r15,8), %rdi
testq %rdi, %rdi
leaq 0x1020(%rsp), %r14
je 0x4f881
cmpq $0x0, 0x30(%rdi)
jne 0x4f86a
cmpq $0x0, 0x10(%rdi)
je 0x4f874
movq %rbp, %rdi
callq 0x45c0
jmp 0x4f885
callq 0x45c0
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rcx
movq %rbp, (%rcx,%r15,8)
movslq %r13d, %rcx
movq 0x10(%rbx), %rax
cmpq %rcx, %r12
movq 0x10(%rsp), %r15
jl 0x4f8fb
testl %r13d, %r13d
leal (%r13,%r13), %ecx
movl %ecx, %r13d
movl $0x10, %ecx
cmovel %ecx, %r13d
movslq %r13d, %r14
leaq (,%r14,8), %rsi
movq %rax, %rdi
callq 0x4840
testq %rax, %rax
je 0x4f92b
movq %rax, 0x10(%rbx)
cmpq %r14, %r12
jge 0x4f8f3
movl %r13d, %eax
movq %r12, %rcx
leaq 0x1020(%rsp), %r14
movq 0x10(%rbx), %rdx
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4f8d9
movq 0x10(%rbx), %rax
jmp 0x4f8fb
leaq 0x1020(%rsp), %r14
movq %rbp, (%rax,%r12,8)
incq %r12
movl %r12d, 0x18(%rbx)
movq %r14, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r15, %rdx
callq 0xf065
testq %rax, %rax
jne 0x4f686
movq %r15, %rdi
callq 0xf4df
testl %eax, %eax
jns 0x4f98e
movq %r15, %rdi
callq 0xf53f
jmp 0x4f97c
xorl %ebx, %ebx
jmp 0x4f984
movq 0x20(%rbx), %rcx
leaq 0x4be1f(%rip), %rsi     # 0x9b763
leaq 0x4be26(%rip), %rdx     # 0x9b771
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x4f97c
leaq 0x4be03(%rip), %rsi     # 0x9b763
leaq 0x471c3(%rip), %rdx     # 0x96b2a
leaq 0x20(%rsp), %rcx
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
testl %ebp, %ebp
jne 0x4f9e8
cmpq $0x0, 0x18(%rsp)
jne 0x4f98c
movq %rbx, %rdi
callq 0x4de72
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 0x4f9c5
addq $-0x4, %r12
movq (%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5801b
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x4f97c
leaq 0x48b02(%rip), %rdx     # 0x984ce
leaq 0x20(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rcx
jmp 0x4f5c0
movq %rbp, %rdi
callq 0x45c0
jmp 0x4f98c
leaq 0x20(%rsp), %rdi
callq 0x43d0
jmp 0x4f97c