// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/header.c
// Function: sam_hdr_update_target_arrays
int sam_hdr_update_target_arrays(sam_hdr_t *bh, const sam_hrecs_t *hrecs,
                                 int refs_changed) {
    if (!bh || !hrecs)
        return -1;

    if (refs_changed < 0)
        return 0;

    // Grow arrays if necessary
    if (bh->n_targets < hrecs->nref) {
        char **new_names = realloc(bh->target_name,
                                   hrecs->nref * sizeof(*new_names));
        if (!new_names)
            return -1;
        bh->target_name = new_names;
        uint32_t *new_lens = realloc(bh->target_len,
                                     hrecs->nref * sizeof(*new_lens));
        if (!new_lens)
            return -1;
        bh->target_len = new_lens;
    }

    // Update names and lengths where changed
    // hrecs->refs_changed is the first ref that has been updated, so ones
    // before that can be skipped.
    int i;
    khint_t k;
    khash_t(s2i) *long_refs = (khash_t(s2i) *) bh->sdict;
    for (i = refs_changed; i < hrecs->nref; i++) {
        if (i >= bh->n_targets
            || strcmp(bh->target_name[i], hrecs->ref[i].name) != 0) {
            if (i < bh->n_targets)
                free(bh->target_name[i]);
            bh->target_name[i] = strdup(hrecs->ref[i].name);
            if (!bh->target_name[i])
                return -1;
        }
        if (hrecs->ref[i].len < UINT32_MAX) {
            bh->target_len[i] = hrecs->ref[i].len;

            if (!long_refs)
                continue;

            // Check if we have an old length, if so remove it.
            k = kh_get(s2i, long_refs, bh->target_name[i]);
            if (k < kh_end(long_refs))
                kh_del(s2i, long_refs, k);
        } else {
            bh->target_len[i] = UINT32_MAX;
            if (bh->hrecs != hrecs) {
                // Called from sam_hdr_dup; need to add sdict entries
                if (!long_refs) {
                    if (!(bh->sdict = long_refs = kh_init(s2i)))
                        return -1;
                }

                // Add / update length
                int absent;
                k = kh_put(s2i, long_refs, bh->target_name[i], &absent);
                if (absent < 0)
                    return -1;
                kh_val(long_refs, k) = hrecs->ref[i].len;
            }
        }
    }

    // Free up any names that have been removed
    for (; i < bh->n_targets; i++) {
        if (long_refs) {
            k = kh_get(s2i, long_refs, bh->target_name[i]);
            if (k < kh_end(long_refs))
                kh_del(s2i, long_refs, k);
        }
        free(bh->target_name[i]);
    }

    bh->n_targets = hrecs->nref;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
pushq $-0x1
popq %rbp
orb %al, %cl
jne 0x6932c
movl %edx, %r12d
testl %edx, %edx
js 0x69325
movq %rsi, %r13
movq %rdi, %rbx
movslq 0x28(%rsi), %rsi
cmpl %esi, (%rdi)
jge 0x691a6
movq 0x20(%rbx), %rdi
shlq $0x3, %rsi
callq 0x4840
testq %rax, %rax
je 0x6932c
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rdi
movslq 0x28(%r13), %rsi
shlq $0x2, %rsi
callq 0x4840
testq %rax, %rax
je 0x6932c
movq %rax, 0x10(%rbx)
movq 0x30(%rbx), %r15
movl %r12d, %r12d
imulq $0x18, %r12, %rsi
movq %r13, (%rsp)
cmpl %r12d, 0x28(%r13)
jle 0x6931a
movq 0x30(%r13), %rbp
movq 0x20(%rbx), %r14
cmpl %r12d, (%rbx)
movq %rsi, 0x8(%rsp)
jle 0x691ff
movq (%r14,%r12,8), %r13
movq (%rbp,%rsi), %rsi
movq %r13, %rdi
callq 0x46c0
testl %eax, %eax
je 0x69226
movq %r13, %rdi
callq 0x45c0
movq (%rsp), %rax
movq 0x30(%rax), %rbp
movq 0x20(%rbx), %r14
movq 0x8(%rsp), %rsi
movq (%rbp,%rsi), %rdi
callq 0x4930
movq %rax, (%r14,%r12,8)
movq 0x20(%rbx), %r14
movq (%r14,%r12,8), %r13
testq %r13, %r13
je 0x69329
movq (%rsp), %rax
movq 0x30(%rax), %rbp
movq 0x8(%rsp), %rsi
movq 0x8(%rbp,%rsi), %rax
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
cmpq %rcx, %rax
jg 0x69270
movq 0x10(%rbx), %rcx
movl %eax, (%rcx,%r12,4)
testq %r15, %r15
je 0x692d3
movq %r15, %rdi
movq %r13, %rsi
callq 0xefef
cmpl (%r15), %eax
jae 0x69265
movq %r15, %rdi
movl %eax, %esi
callq 0xf53a
movq (%rsp), %r13
movq 0x8(%rsp), %rsi
jmp 0x692da
movq 0x10(%rbx), %rax
orl $-0x1, (%rax,%r12,4)
movq (%rsp), %r13
cmpq %r13, 0x38(%rbx)
je 0x692da
testq %r15, %r15
jne 0x692a1
callq 0xef80
movq %rax, 0x30(%rbx)
testq %rax, %rax
je 0x69329
movq %rax, %r15
movq 0x20(%rbx), %r14
movq (%r14,%r12,8), %rsi
movq %r15, %rdi
leaq 0x14(%rsp), %rdx
callq 0xf354
cmpl $0x0, 0x14(%rsp)
js 0x69329
movq 0x30(%r13), %rcx
movq 0x8(%rsp), %rsi
movq 0x8(%rcx,%rsi), %rcx
movq 0x20(%r15), %rdx
movl %eax, %eax
movq %rcx, (%rdx,%rax,8)
jmp 0x692da
xorl %r15d, %r15d
movq (%rsp), %r13
incq %r12
addq $0x18, %rsi
jmp 0x691b5
testq %r15, %r15
je 0x6930a
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %r15, %rdi
callq 0xefef
cmpl (%r15), %eax
jae 0x6930a
movq %r15, %rdi
movl %eax, %esi
callq 0xf53a
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rdi
callq 0x45c0
incq %r12
cmpl %r12d, (%rbx)
jg 0x692e6
movl 0x28(%r13), %eax
movl %eax, (%rbx)
xorl %ebp, %ebp
jmp 0x6932c
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq