// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/htslib/cram/cram_io.c
// Function: refs_from_header
static int refs_from_header(refs_t *r, cram_fd *fd, SAM_hdr *h) {
    int i, j;

    if (!r)
	return -1;

    if (!h || h->nref == 0)
	return 0;

    //fprintf(stderr, "refs_from_header for %p mode %c\n", fd, fd->mode);

    /* Existing refs are fine, as long as they're compatible with the hdr. */
    if (!(r->ref_id = realloc(r->ref_id, (r->nref + h->nref) * sizeof(*r->ref_id))))
	return -1;

    /* Copy info from h->ref[i] over to r */
    for (i = 0, j = r->nref; i < h->nref; i++) {
	SAM_hdr_type *ty;
	SAM_hdr_tag *tag;
	khint_t k;
	int n;

	k = kh_get(refs, r->h_meta, h->ref[i].name);
	if (k != kh_end(r->h_meta))
	    // Ref already known about
	    continue;

	if (!(r->ref_id[j] = calloc(1, sizeof(ref_entry))))
	    return -1;

	if (!h->ref[i].name)
	    return -1;

	r->ref_id[j]->name = string_dup(r->pool, h->ref[i].name);
	r->ref_id[j]->length = 0; // marker for not yet loaded

	/* Initialise likely filename if known */
	if ((ty = sam_hdr_find(h, "SQ", "SN", h->ref[i].name))) {
	    if ((tag = sam_hdr_find_key(h, ty, "M5", NULL))) {
		r->ref_id[j]->fn = string_dup(r->pool, tag->str+3);
		//fprintf(stderr, "Tagging @SQ %s / %s\n", r->ref_id[h]->name, r->ref_id[h]->fn);
	    }
	}

	k = kh_put(refs, r->h_meta, r->ref_id[j]->name, &n);
	if (n <= 0) // already exists or error
	    return -1;
	kh_val(r->h_meta, k) = r->ref_id[j];

	j++;
    }
    r->nref = j;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
pushq $-0x1
popq %rbp
testq %rdi, %rdi
je 0x4f29a
movq %rsi, %r14
xorl %eax, %eax
testq %rsi, %rsi
je 0x4f29c
movslq 0x38(%r14), %rcx
testq %rcx, %rcx
je 0x4f29c
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movslq 0x18(%rbx), %rsi
addq %rcx, %rsi
shlq $0x3, %rsi
callq 0x6c80
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x4f29a
movl 0x18(%rbx), %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jge 0x4f2b0
movq 0x8(%rbx), %rbp
movq 0x40(%r14), %rax
movq (%rax,%r12), %rsi
movq %rbp, %rdi
callq 0x4f03a
cmpl (%rbp), %eax
jne 0x4f28e
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0x6470
movq 0x10(%rbx), %rcx
movslq %r15d, %rbp
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x4f2ab
movq 0x40(%r14), %rax
movq (%rax,%r12), %rsi
testq %rsi, %rsi
je 0x4f2ab
movq (%rbx), %rdi
callq 0x5a5b3
movq 0x10(%rbx), %rcx
movq (%rcx,%rbp,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbp,8), %rax
andq $0x0, 0x10(%rax)
movq 0x40(%r14), %rax
movq (%rax,%r12), %rcx
movq %r14, %rdi
leaq 0x38dab(%rip), %rsi     # 0x87fba
leaq 0x38da7(%rip), %rdx     # 0x87fbd
callq 0x58d95
testq %rax, %rax
je 0x4f255
movq %r14, %rdi
movq %rax, %rsi
leaq 0x38d93(%rip), %rdx     # 0x87fc0
xorl %ecx, %ecx
callq 0x59104
testq %rax, %rax
je 0x4f255
movq (%rbx), %rdi
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x5a5b3
movq 0x10(%rbx), %rcx
movq (%rcx,%rbp,8), %rcx
movq %rax, 0x8(%rcx)
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%rax), %rsi
leaq 0x4(%rsp), %rdx
callq 0x5434e
cmpl $0x0, 0x4(%rsp)
jle 0x4f2ab
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rdx
movq (%rdx,%rbp,8), %rdx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rdx, (%rcx,%rax,8)
incl %r15d
incq %r13
addq $0x20, %r12
jmp 0x4f187
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rax
jmp 0x4f29c
movl %r15d, 0x18(%rbx)
xorl %eax, %eax
jmp 0x4f29c