// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x3110, %rsp           # imm = 0x3110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30c8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x62c0
movq %rax, -0x30d0(%rbp)
movl $0x0, -0x30d4(%rbp)
movl $0x0, -0x30d8(%rbp)
cmpq $0x0, -0x30c8(%rbp)
jne 0x82ec3
callq 0x837d0
movq %rax, -0x30c8(%rbp)
cmpq $0x0, %rax
jne 0x82ec1
jmp 0x835b5
jmp 0x82ec3
movq -0x18(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x6630
cmpl $0x0, %eax
je 0x82eec
cmpl $0x0, -0x1c(%rbp)
je 0x82ee7
movq -0x18(%rbp), %rdi
callq 0x6590
jmp 0x835b5
movq -0x30c8(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x82f16
movq -0x30c8(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x9d380
cmpl $0x0, %eax
je 0x82f14
jmp 0x835b5
jmp 0x82f16
movq -0x30c8(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x30c8(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x981b0
movq -0x30c8(%rbp), %rcx
movq %rax, 0x20(%rcx)
cmpq $0x0, %rax
jne 0x82f4e
jmp 0x835b5
cmpq $0x4, -0x30d0(%rbp)
jbe 0x82f95
movq -0x18(%rbp), %rdi
movq -0x30d0(%rbp), %rax
subq $0x4, %rax
addq %rax, %rdi
leaq 0x60184(%rip), %rsi     # 0xe30f5
callq 0x6a40
cmpl $0x0, %eax
jne 0x82f95
movq -0x30c8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x30d0(%rbp), %rcx
subq $0x4, %rcx
movb $0x0, (%rax,%rcx)
movq -0x30c8(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x5cdef(%rip), %rsi     # 0xdfd96
xorl %edx, %edx
callq 0x81ae0
movq -0x30c8(%rbp), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x82fc4
jmp 0x835b5
leaq -0x10c0(%rbp), %rdi
movq -0x30c8(%rbp), %rax
movq 0x20(%rax), %rcx
leaq 0x6026b(%rip), %rsi     # 0xe3248
movl $0xffb, %edx            # imm = 0xFFB
movb $0x0, %al
callq 0x6160
leaq -0x10c0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x6630
cmpl $0x0, %eax
je 0x83018
cmpl $0x0, -0x1c(%rbp)
je 0x83013
leaq -0x10c0(%rbp), %rdi
callq 0x6590
jmp 0x835b5
leaq -0x10c0(%rbp), %rdi
leaq 0x5cd70(%rip), %rsi     # 0xdfd96
callq 0x6860
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
jne 0x8304f
cmpl $0x0, -0x1c(%rbp)
je 0x8304a
leaq -0x10c0(%rbp), %rdi
callq 0x6590
jmp 0x835b5
jmp 0x83051
leaq -0x30c0(%rbp), %rdi
movq -0xb8(%rbp), %rdx
movl $0x2000, %esi           # imm = 0x2000
callq 0x6c00
cmpq $0x0, %rax
je 0x835a8
movl $0x48, %edi
callq 0x69c0
movq %rax, -0x30e0(%rbp)
cmpq $0x0, -0x30e0(%rbp)
jne 0x8309b
movq $0x0, -0x8(%rbp)
jmp 0x835e6
leaq -0x30c0(%rbp), %rax
movq %rax, -0x30e8(%rbp)
movq -0x30e8(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x30fd(%rbp)
je 0x830ec
callq 0x6780
movq (%rax), %rax
movq -0x30e8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x30fd(%rbp)
movb -0x30fd(%rbp), %al
testb $0x1, %al
jne 0x830f8
jmp 0x8310e
jmp 0x830fa
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x830a9
movq -0x30e8(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30e8(%rbp)
movb $0x0, (%rax)
movq -0x30c8(%rbp), %rax
movq (%rax), %rdi
leaq -0x30c0(%rbp), %rsi
callq 0x981b0
movq %rax, %rcx
movq -0x30e0(%rbp), %rax
movq %rcx, (%rax)
movq -0x30e8(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x30fe(%rbp)
je 0x8318a
callq 0x6780
movq (%rax), %rax
movq -0x30e8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
movb %al, -0x30fe(%rbp)
movb -0x30fe(%rbp), %al
testb $0x1, %al
jne 0x83196
jmp 0x831aa
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x83149
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x6130
movq %rax, %rcx
movq -0x30e0(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30e8(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x30ff(%rbp)
je 0x83211
callq 0x6780
movq (%rax), %rax
movq -0x30e8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
movb %al, -0x30ff(%rbp)
movb -0x30ff(%rbp), %al
testb $0x1, %al
jne 0x8321d
jmp 0x83231
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x831d0
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x6130
movq %rax, %rcx
movq -0x30e0(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30e8(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x3100(%rbp)
je 0x83298
callq 0x6780
movq (%rax), %rax
movq -0x30e8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
movb %al, -0x3100(%rbp)
movb -0x3100(%rbp), %al
testb $0x1, %al
jne 0x832a4
jmp 0x832b8
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x83257
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x6a50
movl %eax, %ecx
movq -0x30e0(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x30e8(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x3101(%rbp)
je 0x8331d
callq 0x6780
movq (%rax), %rax
movq -0x30e8(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
movb %al, -0x3101(%rbp)
movb -0x3101(%rbp), %al
testb $0x1, %al
jne 0x83329
jmp 0x8333d
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x832dc
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x6a50
movl %eax, %ecx
movq -0x30e0(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x30c8(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x30e0(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30e0(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x30e0(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x30e0(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x30e0(%rbp), %rax
movl $0x0, 0x40(%rax)
movq -0x30c8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x30e0(%rbp), %rax
movq (%rax), %rsi
leaq -0x30ec(%rbp), %rdx
callq 0x8bf10
movl %eax, -0x30f0(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x30ec(%rbp), %eax
jne 0x833ff
movq -0x30e0(%rbp), %rdi
callq 0x6880
movq $0x0, -0x8(%rbp)
jmp 0x835e6
cmpl $0x0, -0x30ec(%rbp)
je 0x8342d
movq -0x30e0(%rbp), %rdx
movq -0x30c8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x30f0(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0x834b9
movq -0x30c8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x30f0(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30f8(%rbp)
cmpq $0x0, -0x30f8(%rbp)
je 0x83481
movq -0x30f8(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x83473
movq -0x30f8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x83481
movq -0x30e0(%rbp), %rdi
callq 0x6880
jmp 0x834b7
cmpq $0x0, -0x30f8(%rbp)
je 0x83497
movq -0x30f8(%rbp), %rdi
callq 0x6880
movq -0x30e0(%rbp), %rdx
movq -0x30c8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x30f0(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0x834b9
movl -0x30d4(%rbp), %eax
cmpl -0x30d8(%rbp), %eax
jl 0x8356d
cmpl $0x0, -0x30d8(%rbp)
je 0x834e4
movl -0x30d8(%rbp), %eax
shll %eax
movl %eax, -0x3108(%rbp)
jmp 0x834f1
movl $0x10, %eax
movl %eax, -0x3108(%rbp)
jmp 0x834f1
movl -0x3108(%rbp), %eax
movl %eax, -0x30d8(%rbp)
movq -0x30c8(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x30d8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x6c80
movq %rax, %rcx
movq -0x30c8(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x30d4(%rbp), %eax
movl %eax, -0x30fc(%rbp)
movl -0x30fc(%rbp), %eax
cmpl -0x30d8(%rbp), %eax
jge 0x8356b
movq -0x30c8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x30fc(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x30fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30fc(%rbp)
jmp 0x83532
jmp 0x8356d
movq -0x30e0(%rbp), %rdx
movq -0x30c8(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x30d4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x30d4(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x30d4(%rbp)
movq -0x30c8(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x83051
movq -0x30c8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x835e6
cmpq $0x0, -0xb8(%rbp)
je 0x835cb
movq -0xb8(%rbp), %rdi
callq 0x66e0
cmpq $0x0, -0x10(%rbp)
jne 0x835de
movq -0x30c8(%rbp), %rdi
callq 0x80c40
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x3110, %rsp           # imm = 0x3110
popq %rbp
retq
nopw %cs:(%rax,%rax)