// File: /workspace/github_cpp_makefile_star2_O1/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:
testq %rdi, %rdi
je 0x3410e
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r13
xorl %eax, %eax
testq %rsi, %rsi
je 0x34119
movslq 0x38(%r13), %rcx
testq %rcx, %rcx
je 0x34119
movq %rdi, %r12
movq 0x10(%rdi), %rdi
movslq 0x18(%r12), %rsi
addq %rcx, %rsi
shlq $0x3, %rsi
callq 0x48d0
movq %rax, 0x10(%r12)
testq %rax, %rax
je 0x34114
movl 0x18(%r12), %eax
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x38(%r13)
jle 0x34101
xorl %ebp, %ebp
movq %r12, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq 0x8(%r12), %rax
movq %rbp, %rcx
shlq $0x5, %rcx
movq %rax, 0x38(%rsp)
movl (%rax), %eax
movl $0x0, %r15d
testl %eax, %eax
je 0x33fde
movq %rax, 0x8(%rsp)
movq 0x40(%r13), %rax
movq (%rax,%rcx), %r12
movsbl (%r12), %ebx
testl %ebx, %ebx
movq %rbp, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
je 0x33f34
movb 0x1(%r12), %al
testb %al, %al
je 0x33f36
leaq 0x2(%r12), %rcx
movl %ebx, %edx
shll $0x5, %edx
subl %ebx, %edx
movsbl %al, %ebx
addl %edx, %ebx
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x33f1d
jmp 0x33f36
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
leal -0x1(%rax), %edx
andl %edx, %ebx
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %r14d
movl %ebx, %r15d
movl %edx, 0x10(%rsp)
movq %rsi, 0x40(%rsp)
movl %r15d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %ebp
leal (%r15,%r15), %r13d
andl $0x1e, %r13d
movl %ebp, %eax
movl %r13d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x33fb8
testb $0x1, %al
jne 0x33fa0
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rdi
movq %r12, %rsi
callq 0x4750
movq 0x40(%rsp), %rsi
movl 0x10(%rsp), %edx
testl %eax, %eax
je 0x33fb8
addl %r14d, %r15d
andl %edx, %r15d
incl %r14d
cmpl %ebx, %r15d
jne 0x33f5b
movq 0x8(%rsp), %rax
movl %eax, %r15d
jmp 0x33fca
movl %r13d, %ecx
shrl %cl, %ebp
testb $0x3, %bpl
movq 0x8(%rsp), %rax
cmovnel %eax, %r15d
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rbp
movq 0x28(%rsp), %rcx
movl $0x4, %r14d
cmpl %eax, %r15d
jne 0x340eb
movq %rcx, %r15
movl $0x1, %edi
movl $0x48, %esi
callq 0x4360
movq 0x10(%r12), %rcx
movslq 0x4(%rsp), %rbx
movq %rax, (%rcx,%rbx,8)
movl $0x1, %r14d
testq %rax, %rax
je 0x340eb
movq 0x40(%r13), %rax
movq (%rax,%r15), %rsi
testq %rsi, %rsi
je 0x340eb
movq (%r12), %rdi
callq 0x41f30
movq 0x10(%r12), %rcx
movq (%rcx,%rbx,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbx,8), %rax
movq $0x0, 0x10(%rax)
movq 0x40(%r13), %rax
movq (%rax,%r15), %rcx
movq %r13, %rdi
leaq 0x1f338(%rip), %rsi     # 0x53398
leaq 0x20c3a(%rip), %rdx     # 0x54ca1
callq 0x3f792
testq %rax, %rax
je 0x340a8
movq %r13, %rdi
movq %rax, %rsi
leaq 0x20c26(%rip), %rdx     # 0x54ca4
xorl %ecx, %ecx
callq 0x3ff24
testq %rax, %rax
je 0x340a8
movq (%r12), %rdi
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x41f30
movq 0x10(%r12), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rax, 0x8(%rcx)
movq 0x8(%r12), %rdi
movq 0x10(%r12), %rax
movq (%rax,%rbx,8), %rax
movq (%rax), %rsi
leaq 0x14(%rsp), %rdx
callq 0x39db3
cmpl $0x0, 0x14(%rsp)
jle 0x340eb
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rdx
movq (%rdx,%rbx,8), %rdx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rdx, (%rcx,%rax,8)
incl %ebx
xorl %r14d, %r14d
movl %ebx, 0x4(%rsp)
testb $0x3, %r14b
jne 0x34114
incq %rbp
movslq 0x38(%r13), %rax
cmpq %rax, %rbp
jl 0x33ece
movl 0x4(%rsp), %eax
movl %eax, 0x18(%r12)
xorl %eax, %eax
jmp 0x34119
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq