// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
pushq $-0x1
popq %r15
testq %rdi, %rdi
je 0x40ced
movq 0x50(%rdi), %r14
testq %r14, %r14
je 0x40ced
movq 0x18(%rdi), %rbx
xorl %ebp, %ebp
testq %rbx, %rbx
je 0x40cf0
movq 0x38(%rbx), %rax
testq %rax, %rax
jne 0x40b4a
movq %rbx, %rdi
callq 0x6933d
cmpl $-0x1, %eax
je 0x40ced
movq 0x38(%rbx), %rax
movslq 0x28(%rax), %rax
testq %rax, %rax
je 0x40cf0
movq 0x10(%r14), %rdi
movslq 0x18(%r14), %rsi
addq %rax, %rsi
shlq $0x3, %rsi
callq 0x4840
testq %rax, %rax
je 0x40ced
movq %rax, 0x10(%r14)
movl 0x18(%r14), %ebp
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x38(%rbx), %rax
movslq 0x28(%rax), %rcx
cmpq %rcx, %r13
jge 0x40d06
movq 0x8(%r14), %r12
movq 0x30(%rax), %rax
movq (%rax,%r15), %rsi
movq %r12, %rdi
callq 0x409e2
cmpl (%r12), %eax
jne 0x40ce1
pushq $0x1
popq %rdi
pushq $0x50
popq %rsi
callq 0x4300
movq 0x10(%r14), %rcx
movl %ebp, (%rsp)
movslq %ebp, %rbp
movq %rax, (%rcx,%rbp,8)
testq %rax, %rax
je 0x40d01
movq 0x38(%rbx), %rax
movq 0x30(%rax), %rax
movq (%rax,%r15), %rsi
testq %rsi, %rsi
je 0x40d01
movq (%r14), %rdi
callq 0x49393
movq 0x10(%r14), %rcx
movq (%rcx,%rbp,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbp,8), %rax
cmpq $0x0, (%rax)
je 0x40d01
andq $0x0, 0x10(%rax)
movq 0x38(%rbx), %rdi
movq 0x30(%rdi), %rax
movq (%rax,%r15), %rcx
leaq 0x3a751(%rip), %rsi     # 0x7b372
leaq 0x3a74d(%rip), %rdx     # 0x7b375
callq 0x6a8fd
testq %rax, %rax
je 0x40ca6
movq %rax, %r12
movq %rax, %rdi
leaq 0x3a739(%rip), %rsi     # 0x7b378
xorl %edx, %edx
callq 0x6b93e
testq %rax, %rax
je 0x40c67
movq (%r14), %rdi
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x49393
movq 0x10(%r14), %rcx
movq (%rcx,%rbp,8), %rcx
movq %rax, 0x8(%rcx)
movq %r12, %rdi
leaq 0x3c6f7(%rip), %rsi     # 0x7d368
xorl %edx, %edx
callq 0x6b93e
testq %rax, %rax
je 0x40ca6
movq 0x8(%rax), %rdi
addq $0x3, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x40b0
movq 0x10(%r14), %rcx
movq (%rcx,%rbp,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,%rbp,8), %rax
movq (%rax), %rsi
leaq 0x4(%rsp), %rdx
callq 0x46248
cmpl $0x0, 0x4(%rsp)
jle 0x40d01
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rdx
movq (%rdx,%rbp,8), %rdx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rdx, (%rcx,%rax,8)
movl (%rsp), %ebp
incl %ebp
incq %r13
addq $0x18, %r15
jmp 0x40b82
movl %r15d, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rbp
jmp 0x40cf0
movl %ebp, 0x18(%r14)
xorl %ebp, %ebp
jmp 0x40cf0