// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/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_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;

	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 0x4230
movq %rax, %r14
movq %r15, %rbx
testq %r15, %r15
jne 0x346d1
callq 0x34cf5
movq %rax, %rbx
testq %rax, %rax
je 0x347ad
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x44a0
testl %eax, %eax
je 0x346f8
testl %ebp, %ebp
jne 0x347ce
testq %r15, %r15
jne 0x347b7
jmp 0x347af
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x3470a
callq 0x441e8
testl %eax, %eax
jne 0x346ea
movq $0x0, 0x28(%rbx)
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x40f12
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x346ea
movq %rax, %r13
cmpq $0x5, %r14
jb 0x34755
leaq (%r12,%r14), %rdi
addq $-0x4, %rdi
leaq 0x1e94c(%rip), %rsi     # 0x5308a
callq 0x4750
testl %eax, %eax
jne 0x34755
addq $-0x4, %r14
movb $0x0, (%r13,%r14)
movq 0x20(%rbx), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x337d2
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x346ea
movq 0x20(%rbx), %rcx
leaq 0x1eb65(%rip), %rsi     # 0x532d8
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
movl $0xffb, %edx            # imm = 0xFFB
xorl %eax, %eax
callq 0x4110
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x44a0
testl %eax, %eax
je 0x347db
testl %ebp, %ebp
je 0x346ea
leaq 0xb0(%rsp), %rdi
jmp 0x347d1
xorl %ebx, %ebx
movq %rbx, %rdi
callq 0x32dfc
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 0x4440
jmp 0x346ea
leaq 0x1d15f(%rip), %rsi     # 0x51941
leaq 0xb0(%rsp), %rdi
callq 0x4600
testq %rax, %rax
je 0x3479b
movq %rax, %r13
leaq 0x10b0(%rsp), %r12
movq %r12, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %rax, %rdx
callq 0x4860
testq %rax, %rax
je 0x347b9
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %r13, 0x18(%rsp)
movl $0x48, %edi
callq 0x4700
testq %rax, %rax
je 0x347b7
movq %rax, %rbp
movb 0x10b0(%rsp), %r13b
movq %r12, %rax
testb %r13b, %r13b
je 0x34863
callq 0x4580
movq (%rax), %rcx
movq %r12, %rax
movzbl %r13b, %edx
testb $0x20, 0x1(%rcx,%rdx,2)
jne 0x34863
movb 0x1(%rax), %r13b
incq %rax
testb %r13b, %r13b
jne 0x3484c
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rsp)
movb $0x0, (%rax)
movq (%rbx), %rdi
movq %r12, %rsi
callq 0x40f12
movq %rax, (%rbp)
movq 0x8(%rsp), %r13
movb (%r13), %r12b
testb %r12b, %r12b
je 0x348ab
callq 0x4580
movq (%rax), %rax
movzbl %r12b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x348ab
movb 0x1(%r13), %r12b
incq %r13
testb %r12b, %r12b
jne 0x34894
movq %r13, 0x8(%rsp)
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x40d0
movq %rax, 0x10(%rbp)
movq 0x8(%rsp), %r13
movb (%r13), %r12b
testb %r12b, %r12b
je 0x348f3
callq 0x4580
movq (%rax), %rax
movzbl %r12b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x348f3
movb 0x1(%r13), %r12b
incq %r13
testb %r12b, %r12b
jne 0x348dc
movq %r13, 0x8(%rsp)
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x40d0
movq %rax, 0x18(%rbp)
movq 0x8(%rsp), %r13
movb (%r13), %r12b
testb %r12b, %r12b
je 0x3493b
callq 0x4580
movq (%rax), %rax
movzbl %r12b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x3493b
movb 0x1(%r13), %r12b
incq %r13
testb %r12b, %r12b
jne 0x34924
movq %r13, 0x8(%rsp)
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x4760
movl %eax, 0x20(%rbp)
movq 0x8(%rsp), %r13
movb (%r13), %r12b
testb %r12b, %r12b
je 0x34982
callq 0x4580
movq (%rax), %rax
movzbl %r12b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x34982
movb 0x1(%r13), %r12b
incq %r13
testb %r12b, %r12b
jne 0x3496b
movq %r13, 0x8(%rsp)
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movl $0xa, %edx
callq 0x4760
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 0x14(%rsp), %rdx
callq 0x3914d
movl 0x14(%rsp), %ecx
testl %ecx, %ecx
je 0x349ea
cmpl $-0x1, %ecx
leaq 0x10b0(%rsp), %r12
je 0x34ac8
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rbp, (%rcx,%rax,8)
jmp 0x34a2f
movq 0x8(%rbx), %rcx
movq 0x20(%rcx), %rcx
movl %eax, %r13d
movq (%rcx,%r13,8), %rdi
testq %rdi, %rdi
leaq 0x10b0(%rsp), %r12
je 0x34a2b
cmpq $0x0, 0x28(%rdi)
jne 0x34a14
cmpq $0x0, 0x10(%rdi)
je 0x34a1e
movq %rbp, %rdi
callq 0x4610
jmp 0x34a2f
callq 0x4610
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rcx
movq %rbp, (%rcx,%r13,8)
movslq %r15d, %rcx
movq 0x10(%rbx), %rax
cmpq %rcx, %r14
movq 0x18(%rsp), %r13
jl 0x34a9f
testl %r15d, %r15d
leal (%r15,%r15), %ecx
movl %ecx, %r15d
movl $0x10, %ecx
cmovel %ecx, %r15d
movslq %r15d, %r12
leaq (,%r12,8), %rsi
movq %rax, %rdi
callq 0x48d0
movq %rax, 0x10(%rbx)
cmpq %r12, %r14
jge 0x34a97
movl %r15d, %eax
movq %r14, %rcx
leaq 0x10b0(%rsp), %r12
movq 0x10(%rbx), %rdx
movq $0x0, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x34a7d
movq 0x10(%rbx), %rax
jmp 0x34a9f
leaq 0x10b0(%rsp), %r12
movq %rbp, (%rax,%r14,8)
incq %r14
movl %r14d, 0x18(%rbx)
movq %r12, %rdi
movl $0x2000, %esi           # imm = 0x2000
movq %r13, %rdx
callq 0x4860
testq %rax, %rax
jne 0x3481f
jmp 0x347b9
movq %rbp, %rdi
callq 0x4610
jmp 0x347b7