// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x48a07
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x48c6c
cmpq $0x0, -0x20(%rbp)
je 0x48a18
movq -0x20(%rbp), %rax
cmpl $0x0, 0x38(%rax)
jne 0x48a24
movl $0x0, -0x4(%rbp)
jmp 0x48c6c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x20(%rbp), %rcx
addl 0x38(%rcx), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x4990
movq -0x10(%rbp), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0x48a60
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x48c6c
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x38(%rcx), %eax
jge 0x48c5b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x48840
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl (%rcx), %eax
je 0x48abb
jmp 0x48c4d
movl $0x1, %edi
movl $0x48, %esi
callq 0x4370
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x28(%rbp), %rdx
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0x48aec
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x48c6c
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0x48b11
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x48c6c
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x60170
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq $0x0, 0x10(%rax)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
leaq 0x328d8(%rip), %rsi     # 0x7b45a
leaq 0x34503(%rip), %rdx     # 0x7d08c
callq 0x5d1a0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x48beb
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq 0x344e8(%rip), %rdx     # 0x7d08f
xorl %eax, %eax
movl %eax, %ecx
callq 0x5d910
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x48be9
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x60170
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x8(%rax)
jmp 0x48beb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rsi
leaq -0x40(%rbp), %rdx
callq 0x53bb0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x40(%rbp)
jg 0x48c21
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x48c6c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x48a71
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)