// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/htslib/cram/cram_io.c
// Function: refs_load_fai
static refs_t *refs_load_fai(refs_t *r_orig, char *fn, int is_err) {
    struct stat sb;
    FILE *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;

    /* Open reference, for later use */
    if (stat(fn, &sb) != 0) {
	if (is_err)
	    perror(fn);
	goto err;
    }

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

    if (!(r->fn = string_dup(r->pool, fn)))
	goto err;
	
    if (fn_l > 4 && strcmp(&fn[fn_l-4], ".fai") == 0)
	r->fn[fn_l-4] = 0;

    if (!(r->fp = bgzf_open_ref(r->fn, "r", 0)))
	goto err;

    /* Parse .fai file and load meta-data */
    sprintf(fai_fn, "%.*s.fai", PATH_MAX-5, r->fn);

    if (stat(fai_fn, &sb) != 0) {
	if (is_err)
	    perror(fai_fn);
	goto err;
    }
    if (!(fp = fopen(fai_fn, "r"))) {
	if (is_err)
	    perror(fai_fn);
	goto err;
    }
    while (fgets(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(*cp); cp++)
	    ;
	*cp++ = 0;
	e->name = string_dup(r->pool, line);
	
	// length
	while (*cp && isspace(*cp))
	    cp++;
	e->length = strtoll(cp, &cp, 10);

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

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

	// line length
	while (*cp && isspace(*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;

	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) {
	    int x;

	    id_alloc = id_alloc ?id_alloc*2 : 16;
	    r->ref_id = realloc(r->ref_id, id_alloc * sizeof(*r->ref_id));

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

    return r;

 err:
    if (fp)
	fclose(fp);

    if (!r_orig)
	refs_free(r);
    
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30b8, %rsp           # imm = 0x30B8
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x62d0
movq %rax, %r15
movq %r14, %rbx
testq %r14, %r14
jne 0x58ba0
callq 0x59189
movq %rax, %rbx
testq %rax, %rax
je 0x58c7f
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x6630
testl %eax, %eax
je 0x58bc6
testl %ebp, %ebp
je 0x58c81
movq %r12, %rdi
callq 0x6590
jmp 0x58c81
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x58bdc
callq 0x68168
testl %eax, %eax
jne 0x58c81
movq $0x0, 0x28(%rbx)
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x65194
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x58c81
cmpq $0x5, %r15
jb 0x58c27
movq %rax, %r13
leaq (%r12,%r15), %rdi
addq $-0x4, %rdi
leaq 0x4052e(%rip), %rsi     # 0x99142
callq 0x6a50
testl %eax, %eax
jne 0x58c27
addq $-0x4, %r15
movb $0x0, (%r13,%r15)
movq 0x20(%rbx), %rdi
xorl %esi, %esi
callq 0x57e94
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x58c81
movq 0x20(%rbx), %rcx
leaq 0x4064f(%rip), %rsi     # 0x99295
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
movl $0xffb, %edx            # imm = 0xFFB
xorl %eax, %eax
callq 0x6170
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x6630
testl %eax, %eax
je 0x58ca6
testl %ebp, %ebp
je 0x58c81
leaq 0xb0(%rsp), %rdi
jmp 0x58bbc
xorl %ebx, %ebx
testq %r14, %r14
jne 0x58c8e
movq %rbx, %rdi
callq 0x574b6
xorl %r14d, %r14d
movq %r14, %rax
addq $0x30b8, %rsp           # imm = 0x30B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3d17c(%rip), %rsi     # 0x95e29
leaq 0xb0(%rsp), %rdi
callq 0x6880
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x58c6e
xorl %r14d, %r14d
leaq 0x10b0(%rsp), %r12
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq %r12, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq 0x18(%rsp), %rdx
callq 0x6c00
testq %rax, %rax
je 0x58f73
movl $0x48, %edi
callq 0x69d0
testq %rax, %rax
je 0x58ea4
movq %rax, %rbp
movb 0x10b0(%rsp), %r15b
movq %r12, %rax
testb %r15b, %r15b
je 0x58d3e
callq 0x6790
movq (%rax), %rcx
movq %r12, %rax
movsbq %r15b, %rdx
testb $0x20, 0x1(%rcx,%rdx,2)
jne 0x58d3e
movb 0x1(%rax), %r15b
incq %rax
testb %r15b, %r15b
jne 0x58d27
leaq 0x1(%rax), %rcx
movq %rcx, (%rsp)
movb $0x0, (%rax)
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x65194
movq %rax, (%rbp)
movq (%rsp), %r15
movb (%r15), %r12b
testb %r12b, %r12b
je 0x58d83
callq 0x6790
movq (%rax), %rax
movsbq %r12b, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x58d83
movb 0x1(%r15), %r12b
incq %r15
testb %r12b, %r12b
jne 0x58d6c
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x6130
movq %rax, 0x10(%rbp)
movq (%rsp), %r15
movb (%r15), %r12b
testb %r12b, %r12b
je 0x58dc6
callq 0x6790
movq (%rax), %rax
movsbq %r12b, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x58dc6
movb 0x1(%r15), %r12b
incq %r15
testb %r12b, %r12b
jne 0x58daf
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x6130
movq %rax, 0x18(%rbp)
movq (%rsp), %r15
movb (%r15), %r12b
testb %r12b, %r12b
je 0x58e09
callq 0x6790
movq (%rax), %rax
movsbq %r12b, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x58e09
movb 0x1(%r15), %r12b
incq %r15
testb %r12b, %r12b
jne 0x58df2
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x6a60
movl %eax, 0x20(%rbp)
movq (%rsp), %r15
movb (%r15), %r12b
testb %r12b, %r12b
je 0x58e4b
callq 0x6790
movq (%rax), %rax
movsbq %r12b, %rcx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x58e4b
movb 0x1(%r15), %r12b
incq %r15
testb %r12b, %r12b
jne 0x58e34
movq %r15, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0xa, %edx
callq 0x6a60
movl %eax, 0x24(%rbp)
movq 0x20(%rbx), %rax
movq %rax, 0x8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x34(%rbp)
movups %xmm0, 0x28(%rbp)
movq 0x8(%rbx), %rdi
movq (%rbp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x5d31c
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
je 0x58eab
cmpl $-0x1, %ecx
leaq 0x10b0(%rsp), %r12
jne 0x58edf
movq %rbp, %rdi
callq 0x68a0
xorl %eax, %eax
jmp 0x58f66
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %r15d
movq (%rcx,%r15,8), %rdi
testq %rdi, %rdi
leaq 0x10b0(%rsp), %r12
je 0x58ef4
cmpq $0x0, 0x28(%rdi)
jne 0x58ed5
cmpq $0x0, 0x10(%rdi)
je 0x58eef
movq %rbp, %rdi
callq 0x68a0
jmp 0x58f00
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rbp, (%rcx,%rax,8)
jmp 0x58f00
callq 0x68a0
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
movq %rbp, (%rax,%r15,8)
movq 0x10(%rsp), %rax
cmpl %eax, %r13d
jl 0x58f52
testl %eax, %eax
leal (%rax,%rax), %eax
movl %eax, %ecx
movl $0x10, %eax
cmovel %eax, %ecx
movq 0x10(%rbx), %rdi
movq %rcx, 0x10(%rsp)
movslq %ecx, %r15
leaq (,%r15,8), %rsi
callq 0x6c80
movq %rax, 0x10(%rbx)
cmpl %r15d, %r13d
jge 0x58f52
movslq %r13d, %rax
movq 0x10(%rbx), %rcx
movq $0x0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x58f3e
movq 0x10(%rbx), %rax
movslq %r13d, %rcx
movq %rbp, (%rax,%rcx,8)
incl %r13d
movl %r13d, 0x18(%rbx)
movb $0x1, %al
testb %al, %al
jne 0x58cdb
jmp 0x58c91
movq %rbx, %r14
jmp 0x58c91