// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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 0x291d7
movq %rsi, %r14
xorl %eax, %eax
testq %rsi, %rsi
je 0x291d9
movslq 0x38(%r14), %rcx
testq %rcx, %rcx
je 0x291d9
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movslq 0x18(%rbx), %rsi
addq %rcx, %rsi
shlq $0x3, %rsi
callq 0x48c0
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x291d7
movl 0x18(%rbx), %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jge 0x291ed
movq 0x8(%rbx), %rbp
movq 0x40(%r14), %rax
movq (%rax,%r12), %rsi
movq %rbp, %rdi
callq 0x28f77
cmpl (%rbp), %eax
jne 0x291cb
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0x4370
movq 0x10(%rbx), %rcx
movslq %r15d, %rbp
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x291e8
movq 0x40(%r14), %rax
movq (%rax,%r12), %rsi
testq %rsi, %rsi
je 0x291e8
movq (%rbx), %rdi
callq 0x34873
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 0x1cb78(%rip), %rsi     # 0x45cc4
leaq 0x1cb74(%rip), %rdx     # 0x45cc7
callq 0x33011
testq %rax, %rax
je 0x29192
movq %r14, %rdi
movq %rax, %rsi
leaq 0x1cb60(%rip), %rdx     # 0x45cca
xorl %ecx, %ecx
callq 0x33380
testq %rax, %rax
je 0x29192
movq (%rbx), %rdi
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x34873
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 0x2e50a
cmpl $0x0, 0x4(%rsp)
jle 0x291e8
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 0x290c4
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rax
jmp 0x291d9
movl %r15d, 0x18(%rbx)
xorl %eax, %eax
jmp 0x291d9