// File: /workspace/github_c_makefile_star2_O3/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:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x57cea
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
testq %rsi, %rsi
je 0x57cd3
movslq 0x38(%r14), %rax
testq %rax, %rax
je 0x57cd3
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
movslq 0x18(%rbx), %rsi
addq %rax, %rsi
shlq $0x3, %rsi
callq 0x6c80
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x57cd7
movl 0x18(%rbx), %r15d
movl 0x38(%r14), %eax
testl %eax, %eax
jle 0x57ccf
movl %eax, 0x8(%rsp)
xorl %ecx, %ecx
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq 0x8(%rbx), %rax
movq %rcx, %rdx
shlq $0x5, %rdx
movq %rax, 0x28(%rsp)
movl (%rax), %edi
movl $0x0, %ebx
testl %edi, %edi
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
je 0x57b9f
movq 0x40(%r14), %rax
movq (%rax,%rdx), %rsi
movsbl (%rsi), %r15d
testl %r15d, %r15d
je 0x57b12
movb 0x1(%rsi), %al
testb %al, %al
je 0x57b15
leaq 0x2(%rsi), %rcx
movl %r15d, %edx
shll $0x5, %edx
subl %r15d, %edx
movsbl %al, %r15d
addl %edx, %r15d
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x57af7
jmp 0x57b15
xorl %r15d, %r15d
movq %rdi, 0x30(%rsp)
leal -0x1(%rdi), %r12d
andl %r12d, %r15d
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rdi
movl $0x1, %r14d
movl %r15d, %ebx
movq %rsi, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movl %ebx, %eax
shrl $0x4, %eax
movl (%rdi,%rax,4), %ebp
leal (%rbx,%rbx), %r13d
andl $0x1e, %r13d
movl %ebp, %eax
movl %r13d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x57b8e
testb $0x1, %al
jne 0x57b7e
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x6a50
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
testl %eax, %eax
je 0x57b8e
addl %r14d, %ebx
andl %r12d, %ebx
incl %r14d
cmpl %r15d, %ebx
jne 0x57b3d
jmp 0x57ba7
movl %r13d, %ecx
shrl %cl, %ebp
testb $0x3, %bpl
movq 0x30(%rsp), %rdi
cmovnel %edi, %ebx
cmpl %edi, %ebx
jne 0x57ca5
movl $0x1, %edi
movl $0x48, %esi
callq 0x6470
movq 0x18(%rsp), %rbx
movq 0x10(%rbx), %rcx
movq 0x20(%rsp), %r15
movslq %r15d, %r15
movq %rax, (%rcx,%r15,8)
testq %rax, %rax
movq 0x10(%rsp), %r14
movq 0x38(%rsp), %r12
je 0x57cd7
movq 0x40(%r14), %rax
movq (%rax,%r12), %rsi
testq %rsi, %rsi
je 0x57cd7
movq (%rbx), %rdi
callq 0x651b2
movq 0x10(%rbx), %rcx
movq (%rcx,%r15,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%r15,8), %rax
movq $0x0, 0x10(%rax)
movq 0x40(%r14), %rax
movq (%rax,%r12), %rcx
movq %r14, %rdi
leaq 0x415ef(%rip), %rsi     # 0x9920f
leaq 0x415eb(%rip), %rdx     # 0x99212
callq 0x62c06
testq %rax, %rax
je 0x57c66
movq %r14, %rdi
movq %rax, %rsi
leaq 0x415d7(%rip), %rdx     # 0x99215
xorl %ecx, %ecx
callq 0x63344
testq %rax, %rax
je 0x57c66
movq (%rbx), %rdi
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x651b2
movq 0x10(%rbx), %rcx
movq (%rcx,%r15,8), %rcx
movq %rax, 0x8(%rcx)
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rax
movq (%rax,%r15,8), %rax
movq (%rax), %rsi
leaq 0xc(%rsp), %rdx
callq 0x5d6b6
cmpl $0x0, 0xc(%rsp)
jle 0x57cd7
movq 0x8(%rbx), %rcx
movq 0x10(%rbx), %rdx
movq (%rdx,%r15,8), %rdx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rdx, (%rcx,%rax,8)
incl %r15d
movl 0x38(%r14), %eax
jmp 0x57cb8
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r14
movq 0x20(%rsp), %r15
movl 0x8(%rsp), %eax
movq 0x40(%rsp), %rcx
incq %rcx
movl %eax, 0x8(%rsp)
cltq
cmpq %rax, %rcx
jl 0x57aad
movl %r15d, 0x18(%rbx)
xorl %eax, %eax
jmp 0x57cdc
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq