// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rsi, %rdi
callq 0x62d0
movq %rax, %r14
movq %r15, %rbx
testq %r15, %r15
jne 0x502fe
callq 0x50796
movq %rax, %rbx
testq %rax, %rax
je 0x503d7
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x6630
testl %eax, %eax
je 0x50325
testl %ebp, %ebp
jne 0x503f8
testq %r15, %r15
jne 0x503e1
jmp 0x503d9
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x50337
callq 0x5d0b0
testl %eax, %eax
jne 0x50317
andq $0x0, 0x28(%rbx)
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x5a5b3
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x50317
movq %rax, %r13
cmpq $0x5, %r14
jb 0x5037f
leaq (%r12,%r14), %rdi
addq $-0x4, %rdi
leaq 0x37d90(%rip), %rsi     # 0x880f8
callq 0x6a50
testl %eax, %eax
jne 0x5037f
addq $-0x4, %r14
movb $0x0, (%r13,%r14)
movq 0x20(%rbx), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x4f686
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x50317
movq 0x20(%rbx), %rcx
leaq 0x37eae(%rip), %rsi     # 0x8824b
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
movl $0xffb, %edx            # imm = 0xFFB
xorl %eax, %eax
callq 0x6170
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x6630
testl %eax, %eax
je 0x50405
testl %ebp, %ebp
je 0x50317
leaq 0xb0(%rsp), %rdi
jmp 0x503fb
xorl %ebx, %ebx
movq %rbx, %rdi
callq 0x4ee57
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x30b8, %rsp           # imm = 0x30B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x6590
jmp 0x50317
leaq 0x349fd(%rip), %rsi     # 0x84e09
leaq 0xb0(%rsp), %rdi
callq 0x6880
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x503c5
xorl %r14d, %r14d
xorl %r13d, %r13d
leaq 0x10b0(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
movq 0x18(%rsp), %rdx
callq 0x6c00
testq %rax, %rax
je 0x503e3
pushq $0x48
popq %rdi
callq 0x69d0
testq %rax, %rax
je 0x503e1
movq %rax, %r12
leaq 0x10b1(%rsp), %r15
movsbq -0x1(%r15), %rbp
testq %rbp, %rbp
je 0x5047b
callq 0x6790
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbp,2)
jne 0x5047b
incq %r15
jmp 0x5045d
movq %r15, 0x8(%rsp)
movb $0x0, -0x1(%r15)
movq (%rbx), %rdi
leaq 0x10b0(%rsp), %rsi
callq 0x5a5b3
movq %rax, (%r12)
movsbq (%r15), %rbp
testq %rbp, %rbp
je 0x504b6
callq 0x6790
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbp,2)
je 0x504b6
incq %r15
jmp 0x50499
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x6130
movq %rax, 0x10(%r12)
movq 0x8(%rsp), %r15
movsbq (%r15), %rbp
testq %rbp, %rbp
je 0x504f2
callq 0x6790
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbp,2)
je 0x504f2
incq %r15
jmp 0x504d5
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x6130
movq %rax, 0x18(%r12)
movq 0x8(%rsp), %r15
movsbq (%r15), %rbp
testq %rbp, %rbp
je 0x5052e
callq 0x6790
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbp,2)
je 0x5052e
incq %r15
jmp 0x50511
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x6a60
movl %eax, 0x20(%r12)
movq 0x8(%rsp), %r15
movsbq (%r15), %rbp
testq %rbp, %rbp
je 0x5056a
callq 0x6790
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbp,2)
je 0x5056a
incq %r15
jmp 0x5054d
movq %r15, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x6a60
movl %eax, 0x24(%r12)
movq 0x20(%rbx), %rax
movq %rax, 0x8(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x34(%r12)
movups %xmm0, 0x28(%r12)
movq 0x8(%rbx), %rdi
movq (%r12), %rsi
leaq 0x14(%rsp), %rdx
callq 0x5434e
movl 0x14(%rsp), %ecx
movl %eax, %r15d
testl %ecx, %ecx
je 0x505cc
cmpl $-0x1, %ecx
je 0x50662
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
jmp 0x505f5
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
movq (%rax,%r15,8), %rdi
testq %rdi, %rdi
je 0x505f5
cmpq $0x0, 0x28(%rdi)
jne 0x505eb
cmpq $0x0, 0x10(%rdi)
je 0x50658
movq %r12, %rdi
callq 0x68a0
jmp 0x505f9
movq %r12, (%rax,%r15,8)
movslq %r13d, %rax
movq 0x10(%rbx), %rdi
cmpq %rax, %r14
jge 0x50615
movq %r12, (%rdi,%r14,8)
incq %r14
movl %r14d, 0x18(%rbx)
jmp 0x50429
testl %r13d, %r13d
leal (%r13,%r13), %eax
movl %eax, %r13d
pushq $0x10
popq %rax
cmovel %eax, %r13d
movslq %r13d, %r15
leaq (,%r15,8), %rsi
callq 0x6c80
movq %rax, 0x10(%rbx)
cmpl %r14d, %r15d
cmovlel %r14d, %r15d
movq %r14, %rax
movq 0x10(%rbx), %rdi
cmpq %rax, %r15
je 0x50605
andq $0x0, (%rdi,%rax,8)
incq %rax
jmp 0x50645
callq 0x68a0
jmp 0x505c2
movq %r12, %rdi
callq 0x68a0
jmp 0x503e1