// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
testq %rdi, %rdi
je 0x50e67
movq 0x50(%rdi), %r12
testq %r12, %r12
je 0x50e67
movq 0x18(%rdi), %r13
xorl %ebx, %ebx
testq %r13, %r13
je 0x50e6c
cmpq $0x0, 0x38(%r13)
jne 0x50b7a
movq %r13, %rdi
callq 0x866a8
cmpl $-0x1, %eax
je 0x50e67
movq 0x38(%r13), %rax
movslq 0x28(%rax), %rax
testq %rax, %rax
je 0x50e6c
movq 0x10(%r12), %rdi
movslq 0x18(%r12), %rsi
addq %rax, %rsi
shlq $0x3, %rsi
callq 0x4840
testq %rax, %rax
je 0x50e67
movq %rax, 0x10(%r12)
movl 0x18(%r12), %eax
movl %eax, 0x4(%rsp)
movq 0x38(%r13), %rax
cmpl $0x0, 0x28(%rax)
jle 0x50e5a
xorl %ebx, %ebx
movq %r12, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq 0x8(%r12), %rdx
leaq (%rbx,%rbx,2), %rcx
movq %rcx, 0x8(%rsp)
movq %rdx, 0x38(%rsp)
movl (%rdx), %edx
movl $0x0, %r14d
testl %edx, %edx
je 0x50cd7
movq %rbx, 0x30(%rsp)
movq 0x30(%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rbp
movb (%rbp), %cl
testb %cl, %cl
je 0x50c2d
leaq 0x1(%rbp), %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 0x50c17
jmp 0x50c32
movl $0x811c9dc5, %ebx       # imm = 0x811C9DC5
movq %rdx, 0x28(%rsp)
decl %edx
andl %edx, %ebx
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rsi
movl $0x1, %r12d
movl %ebx, %r14d
movl %edx, 0x10(%rsp)
movq %rsi, 0x40(%rsp)
movl %r14d, %eax
shrl $0x4, %eax
movl (%rsi,%rax,4), %r13d
leal (%r14,%r14), %r15d
andl $0x1e, %r15d
movl %r13d, %eax
movl %r15d, %ecx
shrl %cl, %eax
testb $0x2, %al
jne 0x50cb5
testb $0x1, %al
jne 0x50c9d
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rax
movl %r14d, %ecx
movq (%rax,%rcx,8), %rdi
movq %rbp, %rsi
callq 0x46c0
movq 0x40(%rsp), %rsi
movl 0x10(%rsp), %edx
testl %eax, %eax
je 0x50cb5
addl %r12d, %r14d
andl %edx, %r14d
incl %r12d
cmpl %ebx, %r14d
jne 0x50c56
movq 0x28(%rsp), %rdx
movl %edx, %r14d
jmp 0x50cc8
movl %r15d, %ecx
shrl %cl, %r13d
testb $0x3, %r13b
movq 0x28(%rsp), %rdx
cmovnel %edx, %r14d
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rbx
movl $0x4, %r15d
cmpl %edx, %r14d
jne 0x50e40
movl $0x1, %edi
movl $0x50, %esi
callq 0x4300
movq 0x10(%r12), %rcx
movslq 0x4(%rsp), %rbp
movq %rax, (%rcx,%rbp,8)
movl $0x1, %r15d
testq %rax, %rax
je 0x50e40
movq 0x38(%r13), %rax
movq 0x30(%rax), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
testq %rsi, %rsi
je 0x50e40
movq (%r12), %rdi
callq 0x5aa65
movq 0x10(%r12), %rcx
movq (%rcx,%rbp,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbp,8), %rax
cmpq $0x0, (%rax)
je 0x50e40
movq %rbx, %r14
movq $0x0, 0x10(%rax)
movq 0x38(%r13), %rdi
movq 0x30(%rdi), %rax
movq 0x8(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0x4a449(%rip), %rsi     # 0x9b1bb
leaq 0x4b610(%rip), %rdx     # 0x9c389
callq 0x881f0
testq %rax, %rax
je 0x50dfa
movq %rax, %rbx
movq %rax, %rdi
leaq 0x4b5fc(%rip), %rsi     # 0x9c38c
xorl %edx, %edx
callq 0x89a0b
testq %rax, %rax
je 0x50dba
movq (%r12), %rdi
movq 0x8(%rax), %rsi
addq $0x3, %rsi
callq 0x5aa65
movq 0x10(%r12), %rcx
movq (%rcx,%rbp,8), %rcx
movq %rax, 0x8(%rcx)
movq %rbx, %rdi
leaq 0x4d5b1(%rip), %rsi     # 0x9e375
xorl %edx, %edx
callq 0x89a0b
testq %rax, %rax
je 0x50dfa
movq 0x8(%rax), %rdi
addq $0x3, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x40b0
movq 0x10(%r12), %rcx
movq (%rcx,%rbp,8), %rcx
testq %rax, %rax
movl $0x0, %edx
cmovleq %rdx, %rax
movq %rax, 0x18(%rcx)
movq 0x8(%r12), %rdi
movq 0x10(%r12), %rax
movq (%rax,%rbp,8), %rax
movq (%rax), %rsi
leaq 0x14(%rsp), %rdx
callq 0x56eea
cmpl $0x0, 0x14(%rsp)
movq %r14, %rbx
jle 0x50e40
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rdx
movq (%rdx,%rbp,8), %rdx
movq 0x20(%rcx), %rcx
movl %eax, %eax
movq %rdx, (%rcx,%rax,8)
incl %ebp
xorl %r15d, %r15d
movl %ebp, 0x4(%rsp)
testb $0x3, %r15b
jne 0x50e67
incq %rbx
movq 0x38(%r13), %rax
movslq 0x28(%rax), %rcx
cmpq %rcx, %rbx
jl 0x50bd2
movl 0x4(%rsp), %eax
movl %eax, 0x18(%r12)
xorl %ebx, %ebx
jmp 0x50e6c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq