// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_io.c
// Function: refs_from_header
static int refs_from_header(cram_fd *fd) {
    if (!fd)
        return -1;

    refs_t *r = fd->refs;
    if (!r)
        return -1;

    sam_hdr_t *h = fd->header;
    if (!h)
        return 0;

    if (!h->hrecs) {
        if (-1 == sam_hdr_fill_hrecs(h))
            return -1;
    }

    if (h->hrecs->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. */
    ref_entry **new_ref_id = realloc(r->ref_id, (r->nref + h->hrecs->nref) * sizeof(*r->ref_id));
    if (!new_ref_id)
        return -1;
    r->ref_id = new_ref_id;

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

        k = kh_get(refs, r->h_meta, h->hrecs->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->hrecs->ref[i].name)
            return -1;

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

        /* Initialise likely filename if known */
        if ((ty = sam_hrecs_find_type_id(h->hrecs, "SQ", "SN", h->hrecs->ref[i].name))) {
            if ((tag = sam_hrecs_find_key(ty, "M5", NULL)))
                r->ref_id[j]->fn = string_dup(r->pool, tag->str+3);

            if ((tag = sam_hrecs_find_key(ty, "LN", NULL))) {
                // LN tag used when constructing consensus reference
                r->ref_id[j]->LN_length = strtoll(tag->str+3, NULL, 0);
                // See fuzz 382922241
                if (r->ref_id[j]->LN_length < 0)
                    r->ref_id[j]->LN_length = 0;
            }
        }

        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
subq $0x58, %rsp
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rdi, %rdi
je 0x4e4e3
movq 0x50(%rdi), %r14
testq %r14, %r14
je 0x4e4e3
movq 0x18(%rdi), %r15
testq %r15, %r15
je 0x4e4e1
movq 0x38(%r15), %rax
testq %rax, %rax
jne 0x4e224
movq %r15, %rdi
callq 0x8413e
cmpl $-0x1, %eax
je 0x4e4e3
movq 0x38(%r15), %rax
movslq 0x28(%rax), %rax
testq %rax, %rax
je 0x4e4e1
movq 0x10(%r14), %rdi
movslq 0x18(%r14), %rsi
addq %rax, %rsi
shlq $0x3, %rsi
callq 0x4840
testq %rax, %rax
je 0x4e4e3
movq %rax, 0x10(%r14)
movl 0x18(%r14), %ebx
movq 0x38(%r15), %rdx
cmpl $0x0, 0x28(%rdx)
jle 0x4e4dd
xorl %ecx, %ecx
movq %r14, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x8(%r14), %rax
movq %rcx, 0x40(%rsp)
leaq (%rcx,%rcx,2), %r8
movq %rax, 0x18(%rsp)
movl (%rax), %edi
movl $0x0, %r13d
testl %edi, %edi
movq %r8, 0x38(%rsp)
je 0x4e36a
movq 0x30(%rdx), %rax
movq (%rax,%r8,8), %rsi
movb (%rsi), %cl
testb %cl, %cl
movq %rdx, 0x30(%rsp)
je 0x4e2ce
leaq 0x1(%rsi), %rax
movl $0x811c9dc5, %ebx       # imm = 0x811C9DC5
movzbl %cl, %ecx
xorl %ebx, %ecx
imull $0x1000193, %ecx, %ebx  # imm = 0x1000193
movb (%rax), %cl
incq %rax
testb %cl, %cl
jne 0x4e2b8
jmp 0x4e2d3
movl $0x811c9dc5, %ebx       # imm = 0x811C9DC5
movq %rdi, 0x28(%rsp)
leal -0x1(%rdi), %ebp
andl %ebp, %ebx
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movl $0x1, %r12d
movl %ebx, %r13d
movq %rsi, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movl %r13d, %eax
shrl $0x4, %eax
movl (%rdi,%rax,4), %r14d
leal (,%r13,2), %r15d
andl $0x1e, %r15d
movl %r14d, %eax
movl %r15d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x4e352
testb $0x1, %al
jne 0x4e342
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movl %r13d, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x46c0
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
testl %eax, %eax
je 0x4e352
addl %r12d, %r13d
andl %ebp, %r13d
incl %r12d
cmpl %ebx, %r13d
jne 0x4e2f9
jmp 0x4e373
movl %r15d, %ecx
shrl %cl, %r14d
testb $0x3, %r14b
movq 0x28(%rsp), %rdi
cmovnel %edi, %r13d
movq 0x30(%rsp), %rdx
cmpl %edi, %r13d
jne 0x4e4be
movl $0x1, %edi
movl $0x50, %esi
callq 0x4300
movq 0x8(%rsp), %r14
movq 0x10(%r14), %rcx
movq 0x10(%rsp), %rbx
movslq %ebx, %rbx
movq %rax, (%rcx,%rbx,8)
testq %rax, %rax
movq 0x20(%rsp), %r15
movq 0x38(%rsp), %r12
je 0x4e4f4
movq 0x38(%r15), %rax
movq 0x30(%rax), %rax
movq (%rax,%r12,8), %rsi
testq %rsi, %rsi
je 0x4e4f4
movq (%r14), %rdi
callq 0x57fd5
movq 0x10(%r14), %rcx
movq (%rcx,%rbx,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbx,8), %rax
cmpq $0x0, (%rax)
je 0x4e4f4
movq $0x0, 0x10(%rax)
movq 0x38(%r15), %rdi
movq 0x30(%rdi), %rax
movq (%rax,%r12,8), %rcx
leaq 0x4b007(%rip), %rsi     # 0x99402
leaq 0x4b003(%rip), %rdx     # 0x99405
callq 0x85b55
testq %rax, %rax
je 0x4e480
movq %rax, %rbp
movq %rax, %rdi
leaq 0x4afef(%rip), %rsi     # 0x99408
xorl %edx, %edx
callq 0x872e1
testq %rax, %rax
je 0x4e441
movq (%r14), %rdi
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x57fd5
movq 0x10(%r14), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rax, 0x8(%rcx)
movq %rbp, %rdi
leaq 0x4d122(%rip), %rsi     # 0x9b56d
xorl %edx, %edx
callq 0x872e1
testq %rax, %rax
je 0x4e480
movq 0x8(%rax), %rdi
addq $0x3, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x40b0
movq 0x10(%r14), %rcx
movq (%rcx,%rbx,8), %rcx
testq %rax, %rax
movl $0x0, %edx
cmovleq %rdx, %rax
movq %rax, 0x18(%rcx)
movq 0x8(%r14), %rdi
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rax
movq (%rax), %rsi
leaq 0x4(%rsp), %rdx
callq 0x545ac
cmpl $0x0, 0x4(%rsp)
jle 0x4e4f4
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rdx
movq (%rdx,%rbx,8), %rdx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rdx, (%rcx,%rax,8)
incl %ebx
movq 0x38(%r15), %rdx
jmp 0x4e4c8
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rbx
movq 0x40(%rsp), %rcx
incq %rcx
movslq 0x28(%rdx), %rax
cmpq %rax, %rcx
jl 0x4e270
movl %ebx, 0x18(%r14)
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x4e4e3