// File: /workspace/github_c_makefile_star2_O1/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 $0x3038, %rsp           # imm = 0x3038
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r14
movq %rsi, %rdi
callq 0x41d0
movq %rax, %r12
movq %r14, %rbx
testq %r14, %r14
jne 0x52004
callq 0x52713
movq %rax, %rbx
testq %rax, %rax
je 0x5240e
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x5201a
callq 0xd4a4
testl %eax, %eax
jne 0x52410
movq $0x0, 0x28(%rbx)
leaq 0x46e17(%rip), %rsi     # 0x98e40
movq %r15, %rdi
callq 0x40e0
testq %rax, %rax
je 0x52078
movq %rax, %r13
movq (%rbx), %rdi
movq %rax, %rdx
subq %r15, %rdx
movq %r15, %rsi
callq 0x5aab0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x52410
addq $0x7, %r13
leaq 0x49465(%rip), %rdx     # 0x9b4c7
leaq 0x30(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rcx
xorl %eax, %eax
callq 0x4800
jmp 0x520d2
cmpq $0x5, %r12
jb 0x5209a
leaq (%r15,%r12), %rdi
addq $-0x4, %rdi
leaq 0x4c338(%rip), %rsi     # 0x9e3c5
callq 0x46c0
testl %eax, %eax
je 0x524ad
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x5aa65
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x52410
leaq 0x4c4a9(%rip), %rdx     # 0x9e562
leaq 0x30(%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 0x510b7
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x52444
leaq 0x494d3(%rip), %rsi     # 0x9b5c4
leaq 0x30(%rsp), %rdi
xorl %eax, %eax
callq 0x10014
testq %rax, %rax
je 0x52467
movq %rax, %r15
movq $0x0, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
movq %r14, 0x10(%rsp)
movq %rax, 0x18(%rsp)
leaq 0x1030(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r15, %rdx
callq 0xf209
testq %rax, %rax
je 0x5249c
movl $0x50, %edi
callq 0x4680
movl $0x1, %r12d
testq %rax, %rax
je 0x523fd
movq %rax, %rbp
movb 0x1030(%rsp), %r13b
leaq 0x1030(%rsp), %rax
testb %r13b, %r13b
je 0x5219a
callq 0x4510
movq (%rax), %rcx
leaq 0x1030(%rsp), %rax
movzbl %r13b, %edx
testb $0x20, 0x1(%rcx,%rdx,2)
jne 0x5219a
movb 0x1(%rax), %r13b
incq %rax
testb %r13b, %r13b
jne 0x52183
leaq 0x1(%rax), %rcx
movq %rcx, (%rsp)
movb $0x0, (%rax)
movq (%rbx), %rdi
leaq 0x1030(%rsp), %rsi
callq 0x5aa65
movq %rax, (%rbp)
movq (%rsp), %r13
movb (%r13), %r14b
testb %r14b, %r14b
je 0x521e5
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x521e5
movb 0x1(%r13), %r14b
incq %r13
testb %r14b, %r14b
jne 0x521ce
movq %r13, (%rsp)
movq %r13, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x40b0
movq %rax, 0x10(%rbp)
movq (%rsp), %r13
movb (%r13), %r14b
testb %r14b, %r14b
je 0x52229
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x52229
movb 0x1(%r13), %r14b
incq %r13
testb %r14b, %r14b
jne 0x52212
movq %r13, (%rsp)
movq %r13, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x40b0
movq %rax, 0x20(%rbp)
movq (%rsp), %r13
movb (%r13), %r14b
testb %r14b, %r14b
je 0x5226d
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x5226d
movb 0x1(%r13), %r14b
incq %r13
testb %r14b, %r14b
jne 0x52256
movq %r13, (%rsp)
movq %r13, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x46d0
movl %eax, 0x28(%rbp)
movq (%rsp), %r13
movb (%r13), %r14b
testb %r14b, %r14b
je 0x522b0
callq 0x4510
movq (%rax), %rax
movzbl %r14b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x522b0
movb 0x1(%r13), %r14b
incq %r13
testb %r14b, %r14b
jne 0x52299
movq %r13, (%rsp)
movq %r13, %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 0x2c(%rsp), %rdx
callq 0x56eea
movl 0x2c(%rsp), %ecx
testl %ecx, %ecx
je 0x5230b
cmpl $-0x1, %ecx
jne 0x5233c
movq %rbp, %rdi
callq 0x45c0
movq 0x10(%rsp), %r14
jmp 0x523fd
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %r14d
movq (%rcx,%r14,8), %rdi
testq %rdi, %rdi
movq 0x20(%rsp), %r12
je 0x5235b
cmpq $0x0, 0x30(%rdi)
jne 0x52332
cmpq $0x0, 0x10(%rdi)
je 0x52356
movq %rbp, %rdi
callq 0x45c0
jmp 0x52367
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rbp, (%rcx,%rax,8)
movq 0x10(%rsp), %r14
movq 0x20(%rsp), %r12
jmp 0x5236c
callq 0x45c0
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
movq %rbp, (%rax,%r14,8)
movq 0x10(%rsp), %r14
cmpl %r12d, 0xc(%rsp)
jl 0x523e2
testl %r12d, %r12d
leal (%r12,%r12), %eax
movl %eax, %r15d
movl $0x10, %eax
cmovel %eax, %r15d
movq 0x10(%rbx), %rdi
movslq %r15d, %r13
leaq (,%r13,8), %rsi
callq 0x4840
testq %rax, %rax
movq %r15, 0x20(%rsp)
je 0x523d2
movq %rax, 0x10(%rbx)
xorl %r12d, %r12d
movl 0xc(%rsp), %ecx
cmpl %r15d, %ecx
jge 0x523d8
movslq %ecx, %rcx
movq 0x18(%rsp), %r15
movq 0x10(%rbx), %rdx
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %r13
jne 0x523bc
jmp 0x523dd
movl $0x2, %r12d
movq 0x18(%rsp), %r15
testq %rax, %rax
je 0x523fd
movq 0x10(%rbx), %rax
movl 0xc(%rsp), %edx
movslq %edx, %rcx
movq %rbp, (%rax,%rcx,8)
incl %edx
movl %edx, 0xc(%rsp)
movl %edx, 0x18(%rbx)
xorl %r12d, %r12d
testl %r12d, %r12d
je 0x52124
cmpl $0x2, %r12d
je 0x52413
jmp 0x5242d
xorl %ebx, %ebx
xorl %r15d, %r15d
testq %r15, %r15
je 0x52420
movq %r15, %rdi
callq 0xf730
testq %r14, %r14
jne 0x5242d
movq %rbx, %rdi
callq 0x507eb
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x3038, %rsp           # imm = 0x3038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rbx), %rcx
leaq 0x4c11c(%rip), %rsi     # 0x9e56b
leaq 0x4c123(%rip), %rdx     # 0x9e579
xorl %r15d, %r15d
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x52413
leaq 0x4c0fd(%rip), %rsi     # 0x9e56b
leaq 0x47615(%rip), %rdx     # 0x99a8a
xorl %r15d, %r15d
leaq 0x30(%rsp), %rcx
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
testl %ebp, %ebp
je 0x52413
leaq 0x30(%rsp), %rdi
callq 0x43d0
jmp 0x52410
movq %r15, %rdi
callq 0xf6d0
testl %eax, %eax
jns 0x5242f
jmp 0x52413
cmpq $0x0, 0x20(%rbx)
jne 0x524d3
addq $-0x4, %r12
movq (%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5aab0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x52410
leaq 0x48fed(%rip), %rdx     # 0x9b4c7
leaq 0x30(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rcx
jmp 0x5206f