// File: /workspace/github_c_makefile_star2_O3/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 0x84128
movl %edx, %r15d
xorl %ebp, %ebp
testl %edx, %edx
js 0x8412d
movq %rdi, %r14
movl 0x28(%rsi), %eax
cmpl %eax, (%rdi)
movq %rsi, 0x8(%rsp)
jge 0x83f30
movslq %eax, %rsi
movq 0x20(%r14), %rdi
shlq $0x3, %rsi
callq 0x4840
testq %rax, %rax
je 0x84128
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rdi
movq 0x8(%rsp), %rbx
movslq 0x28(%rbx), %rsi
shlq $0x2, %rsi
callq 0x4840
testq %rax, %rax
je 0x84128
movq %rax, 0x10(%r14)
movl 0x28(%rbx), %eax
movq %rbx, %rsi
movq 0x30(%r14), %r13
cmpl %r15d, %eax
jle 0x840d9
movl %r15d, %r15d
leaq (,%r15,8), %rax
leaq (,%r15,4), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x18(%rsp)
leaq (%rax,%rax,2), %r12
xorl %ebx, %ebx
movq %r15, 0x10(%rsp)
movq %r13, 0x30(%rsp)
leal (%r15,%rbx), %eax
movq 0x30(%rsi), %r13
movq 0x20(%r14), %r15
cmpl %eax, (%r14)
jle 0x83fb1
movq 0x18(%rsp), %rax
addq %r15, %rax
movq (%rax,%rbx,8), %rbp
movq (%r13,%r12), %rsi
movq %rbp, %rdi
callq 0x46c0
testl %eax, %eax
je 0x840b1
movq %rbp, %rdi
callq 0x45c0
movq 0x8(%rsp), %rax
movq 0x30(%rax), %r13
movq 0x20(%r14), %r15
movq (%r13,%r12), %rdi
callq 0x4930
movq 0x18(%rsp), %rcx
addq %rcx, %r15
movq %rax, (%r15,%rbx,8)
movq 0x20(%r14), %r15
leaq (%r15,%rcx), %rax
movq (%rax,%rbx,8), %rbp
testq %rbp, %rbp
je 0x84128
movq 0x8(%rsp), %rsi
movq 0x30(%rsi), %r13
movq 0x8(%r13,%r12), %rax
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
cmpq %rcx, %rax
jg 0x84038
movq 0x10(%r14), %rcx
addq 0x28(%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
movq 0x30(%rsp), %r13
testq %r13, %r13
movq 0x10(%rsp), %r15
je 0x840bb
movq %r13, %rdi
movq %rbp, %rsi
callq 0x10e38
cmpl (%r13), %eax
jae 0x8402e
movq %r13, %rdi
movl %eax, %esi
callq 0x113d6
movq 0x8(%rsp), %rsi
jmp 0x840be
movq 0x10(%r14), %rax
addq 0x28(%rsp), %rax
movl $0xffffffff, (%rax,%rbx,4) # imm = 0xFFFFFFFF
cmpq %rsi, 0x38(%r14)
movq 0x30(%rsp), %r13
je 0x840aa
testq %r13, %r13
jne 0x84071
callq 0x10dc0
movq %rax, 0x30(%r14)
testq %rax, %rax
je 0x84128
movq %rax, %r13
movq 0x20(%r14), %r15
addq 0x18(%rsp), %r15
movq (%r15,%rbx,8), %rsi
movq %r13, %rdi
leaq 0x24(%rsp), %rdx
callq 0x111c4
cmpl $0x0, 0x24(%rsp)
js 0x84128
movq 0x8(%rsp), %rsi
movq 0x30(%rsi), %rcx
movq 0x8(%rcx,%r12), %rcx
movq 0x20(%r13), %rdx
movl %eax, %eax
movq %rcx, (%rdx,%rax,8)
movq 0x10(%rsp), %r15
jmp 0x840be
movq 0x8(%rsp), %rsi
jmp 0x83fe5
xorl %r13d, %r13d
movl 0x28(%rsi), %eax
incq %rbx
addq $0x18, %r12
leal (%r15,%rbx), %ecx
cmpl %ecx, %eax
jg 0x83f65
addl %ebx, %r15d
xorl %ebp, %ebp
cmpl (%r14), %r15d
jge 0x84123
movl %r15d, %ebx
testq %r13, %r13
je 0x84106
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq %r13, %rdi
callq 0x10e38
cmpl (%r13), %eax
jae 0x84106
movq %r13, %rdi
movl %eax, %esi
callq 0x113d6
movq 0x20(%r14), %rax
movq (%rax,%rbx,8), %rdi
callq 0x45c0
incq %rbx
cmpl %ebx, (%r14)
jg 0x840e1
movq 0x8(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, (%r14)
jmp 0x8412d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq