// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x86692
movl %edx, %r15d
xorl %ebp, %ebp
testl %edx, %edx
js 0x86697
movq %rsi, %r8
movq %rdi, %r14
movslq 0x28(%rsi), %rsi
cmpl %esi, (%rdi)
movq %r8, 0x8(%rsp)
jge 0x86494
movq 0x20(%r14), %rdi
shlq $0x3, %rsi
callq 0x4840
testq %rax, %rax
je 0x86692
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rdi
movq 0x8(%rsp), %rax
movslq 0x28(%rax), %rsi
shlq $0x2, %rsi
callq 0x4840
testq %rax, %rax
je 0x86692
movq %rax, 0x10(%r14)
movq 0x8(%rsp), %r8
movq 0x30(%r14), %r13
cmpl %r15d, 0x28(%r8)
jle 0x86643
movl %r15d, %r15d
leaq (,%r15,8), %rax
leaq (,%r15,4), %rcx
movq %rcx, 0x20(%rsp)
movq %rax, 0x10(%rsp)
leaq (%rax,%rax,2), %r12
xorl %ebp, %ebp
movq %r15, 0x28(%rsp)
movq %r13, 0x30(%rsp)
movl (%r14), %ebx
addl %ebp, %r15d
cmpl %r15d, %ebx
jle 0x8651a
movq 0x20(%r14), %rax
addq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rdi
movq 0x30(%r8), %rax
movq (%rax,%r12), %rsi
movq %r8, %r13
callq 0x46c0
testl %eax, %eax
je 0x8654c
cmpl %r15d, %ebx
movq %r13, %r8
jle 0x8651a
movq 0x20(%r14), %rax
addq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rdi
callq 0x45c0
movq 0x8(%rsp), %r8
movq 0x30(%r8), %rax
movq (%rax,%r12), %rdi
movq %r8, %r13
callq 0x4930
movq 0x20(%r14), %rcx
movq 0x10(%rsp), %rdx
addq %rdx, %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x20(%r14), %rax
addq %rdx, %rax
cmpq $0x0, (%rax,%rbp,8)
je 0x86692
movq 0x30(%r13), %rax
movq 0x8(%rax,%r12), %rax
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
cmpq %rcx, %rax
movq %r13, %r8
jg 0x865ad
movq 0x10(%r14), %rcx
addq 0x20(%rsp), %rcx
movl %eax, (%rcx,%rbp,4)
movq 0x30(%rsp), %r13
testq %r13, %r13
movq 0x28(%rsp), %r15
je 0x86626
movq 0x20(%r14), %rax
addq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x11074
cmpl (%r13), %eax
jae 0x865a6
movq %r13, %rdi
movl %eax, %esi
callq 0x1162d
movq 0x8(%rsp), %r8
jmp 0x86629
movq 0x10(%r14), %rax
addq 0x20(%rsp), %rax
movl $0xffffffff, (%rax,%rbp,4) # imm = 0xFFFFFFFF
cmpq %r8, 0x38(%r14)
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r13
je 0x86629
testq %r13, %r13
jne 0x865e7
callq 0x10ffc
movq %rax, %r13
movq %rax, 0x30(%r14)
testq %rax, %rax
je 0x86692
movq 0x20(%r14), %rax
addq 0x10(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
leaq 0x1c(%rsp), %rdx
callq 0x1143a
cmpl $0x0, 0x1c(%rsp)
js 0x86692
movq 0x8(%rsp), %r8
movq 0x30(%r8), %rcx
movq 0x8(%rcx,%r12), %rcx
movq 0x20(%r13), %rdx
movl %eax, %eax
movq %rcx, (%rdx,%rax,8)
jmp 0x86629
xorl %r13d, %r13d
incq %rbp
addq $0x18, %r12
leal (%r15,%rbp), %eax
cmpl %eax, 0x28(%r8)
jg 0x864ca
addl %ebp, %r15d
xorl %ebp, %ebp
cmpl (%r14), %r15d
jge 0x86685
movl %r15d, %ebx
testq %r13, %r13
je 0x86670
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %rdi
callq 0x11074
cmpl (%r13), %eax
jae 0x86670
movq %r13, %rdi
movl %eax, %esi
callq 0x1162d
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rdi
callq 0x45c0
incq %rbx
cmpl %ebx, (%r14)
jg 0x8664b
movq 0x8(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, (%r14)
jmp 0x86697
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq