// File: 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
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 0x4230
movq %rax, -0x30d0(%rbp)
movl $0x0, -0x30d4(%rbp)
movl $0x0, -0x30d8(%rbp)
cmpq $0x0, -0x30c8(%rbp)
jne 0x4a983
callq 0x4b240
movq %rax, -0x30c8(%rbp)
cmpq $0x0, %rax
jne 0x4a981
jmp 0x4b02a
jmp 0x4a983
movq -0x18(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x44f0
cmpl $0x0, %eax
je 0x4a9ac
cmpl $0x0, -0x1c(%rbp)
je 0x4a9a7
movq -0x18(%rbp), %rdi
callq 0x4480
jmp 0x4b02a
movq -0x30c8(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x4a9d6
movq -0x30c8(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x65880
cmpl $0x0, %eax
je 0x4a9d4
jmp 0x4b02a
jmp 0x4a9d6
movq -0x30c8(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x30c8(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x60170
movq -0x30c8(%rbp), %rcx
movq %rax, 0x20(%rcx)
cmpq $0x0, %rax
jne 0x4aa0e
jmp 0x4b02a
cmpq $0x4, -0x30d0(%rbp)
jbe 0x4aa55
movq -0x18(%rbp), %rdi
movq -0x30d0(%rbp), %rax
subq $0x4, %rax
addq %rax, %rdi
leaq 0x32799(%rip), %rsi     # 0x7d1ca
callq 0x4810
cmpl $0x0, %eax
jne 0x4aa55
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 0x30f88(%rip), %rsi     # 0x7b9ef
xorl %edx, %edx
callq 0x493b0
movq -0x30c8(%rbp), %rcx
movq %rax, 0x28(%rcx)
cmpq $0x0, %rax
jne 0x4aa84
jmp 0x4b02a
leaq -0x10c0(%rbp), %rdi
movq -0x30c8(%rbp), %rax
movq 0x20(%rax), %rcx
leaq 0x3297b(%rip), %rsi     # 0x7d418
movl $0xffb, %edx            # imm = 0xFFB
movb $0x0, %al
callq 0x4100
leaq -0x10c0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x44f0
cmpl $0x0, %eax
je 0x4aad8
cmpl $0x0, -0x1c(%rbp)
je 0x4aad3
leaq -0x10c0(%rbp), %rdi
callq 0x4480
jmp 0x4b02a
leaq -0x10c0(%rbp), %rdi
leaq 0x30f09(%rip), %rsi     # 0x7b9ef
callq 0x4690
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
jne 0x4ab0f
cmpl $0x0, -0x1c(%rbp)
je 0x4ab0a
leaq -0x10c0(%rbp), %rdi
callq 0x4480
jmp 0x4b02a
jmp 0x4ab11
leaq -0x30c0(%rbp), %rdi
movq -0xb8(%rbp), %rdx
movl $0x2000, %esi           # imm = 0x2000
callq 0x4930
cmpq $0x0, %rax
je 0x4b01d
movl $0x48, %edi
callq 0x47a0
movq %rax, -0x30e0(%rbp)
cmpq $0x0, -0x30e0(%rbp)
jne 0x4ab5b
movq $0x0, -0x8(%rbp)
jmp 0x4b05b
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 0x4ab9d
movq -0x30e8(%rbp), %rax
movsbl (%rax), %edi
callq 0x54550
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x30fd(%rbp)
movb -0x30fd(%rbp), %al
testb $0x1, %al
jne 0x4aba9
jmp 0x4abbf
jmp 0x4abab
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x4ab69
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 0x60170
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 0x4ac2c
movq -0x30e8(%rbp), %rax
movsbl (%rax), %edi
callq 0x54550
cmpl $0x0, %eax
setne %al
movb %al, -0x30fe(%rbp)
movb -0x30fe(%rbp), %al
testb $0x1, %al
jne 0x4ac38
jmp 0x4ac4c
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x4abfa
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x40c0
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 0x4aca4
movq -0x30e8(%rbp), %rax
movsbl (%rax), %edi
callq 0x54550
cmpl $0x0, %eax
setne %al
movb %al, -0x30ff(%rbp)
movb -0x30ff(%rbp), %al
testb $0x1, %al
jne 0x4acb0
jmp 0x4acc4
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x4ac72
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x40c0
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 0x4ad1c
movq -0x30e8(%rbp), %rax
movsbl (%rax), %edi
callq 0x54550
cmpl $0x0, %eax
setne %al
movb %al, -0x3100(%rbp)
movb -0x3100(%rbp), %al
testb $0x1, %al
jne 0x4ad28
jmp 0x4ad3c
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x4acea
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x4820
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 0x4ad92
movq -0x30e8(%rbp), %rax
movsbl (%rax), %edi
callq 0x54550
cmpl $0x0, %eax
setne %al
movb %al, -0x3101(%rbp)
movb -0x3101(%rbp), %al
testb $0x1, %al
jne 0x4ad9e
jmp 0x4adb2
movq -0x30e8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30e8(%rbp)
jmp 0x4ad60
movq -0x30e8(%rbp), %rdi
leaq -0x30e8(%rbp), %rsi
movl $0xa, %edx
callq 0x4820
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 0x53bb0
movl %eax, -0x30f0(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x30ec(%rbp), %eax
jne 0x4ae74
movq -0x30e0(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x4b05b
cmpl $0x0, -0x30ec(%rbp)
je 0x4aea2
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 0x4af2e
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 0x4aef6
movq -0x30f8(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x4aee8
movq -0x30f8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x4aef6
movq -0x30e0(%rbp), %rdi
callq 0x46a0
jmp 0x4af2c
cmpq $0x0, -0x30f8(%rbp)
je 0x4af0c
movq -0x30f8(%rbp), %rdi
callq 0x46a0
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 0x4af2e
movl -0x30d4(%rbp), %eax
cmpl -0x30d8(%rbp), %eax
jl 0x4afe2
cmpl $0x0, -0x30d8(%rbp)
je 0x4af59
movl -0x30d8(%rbp), %eax
shll %eax
movl %eax, -0x3108(%rbp)
jmp 0x4af66
movl $0x10, %eax
movl %eax, -0x3108(%rbp)
jmp 0x4af66
movl -0x3108(%rbp), %eax
movl %eax, -0x30d8(%rbp)
movq -0x30c8(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x30d8(%rbp), %rsi
shlq $0x3, %rsi
callq 0x4990
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 0x4afe0
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 0x4afa7
jmp 0x4afe2
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 0x4ab11
movq -0x30c8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4b05b
cmpq $0x0, -0xb8(%rbp)
je 0x4b040
movq -0xb8(%rbp), %rdi
callq 0x4590
cmpq $0x0, -0x10(%rbp)
jne 0x4b053
movq -0x30c8(%rbp), %rdi
callq 0x48510
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x3110, %rsp           # imm = 0x3110
popq %rbp
retq
nopl (%rax,%rax)