// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/sam.c
// Function: bam_aux_update_array
int bam_aux_update_array(bam1_t *b, const char tag[2],
                         uint8_t type, uint32_t items, void *data)
{
    uint8_t *s = bam_aux_get(b, tag);
    size_t old_sz = 0, new_sz;
    int new = 0;

    if (s) { // Tag present
        if (*s != 'B') { errno = EINVAL; return -1; }
        old_sz = aux_type2size(s[1]);
        if (old_sz < 1 || old_sz > 4) { errno = EINVAL; return -1; }
        old_sz *= le_to_u32(s + 2);
    } else {
        if (errno == ENOENT) {  // Tag doesn't exist - add a new one
            s = b->data + b->l_data;
            new = 1;
        }  else { // Invalid aux data, give up.
            return -1;
        }
    }

    new_sz = aux_type2size(type);
    if (new_sz < 1 || new_sz > 4) { errno = EINVAL; return -1; }
    if (items > INT32_MAX / new_sz) { errno = ENOMEM; return -1; }
    new_sz *= items;

    if (new || old_sz < new_sz) {
        // Make room for new tag
        ptrdiff_t s_offset = s - b->data;
        if (possibly_expand_bam_data(b, (new ? 8 : 0) + new_sz - old_sz) < 0)
            return -1;
        s =  b->data + s_offset;
    }
    if (new) { // Add tag id and type
        *s++ = tag[0];
        *s++ = tag[1];
        *s = 'B';
        b->l_data += 8 + new_sz;
    } else if (old_sz != new_sz) { // shift following data if necessary
        memmove(s + 6 + new_sz, s + 6 + old_sz,
                b->l_data - ((s + 6 + old_sz) - b->data));
        b->l_data -= old_sz;
        b->l_data += new_sz;
    }

    s[1] = type;
    u32_to_le(items, s + 2);
    if (new_sz > 0) {
#ifdef HTS_LITTLE_ENDIAN
        memcpy(s + 6, data, new_sz);
#else
        return aux_to_le(type, s + 6, data, new_sz);
#endif
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %rbx
movl %ecx, %ebp
movl %edx, %r14d
movq %rsi, %r13
movq %rdi, %r15
callq 0x24528
movq %rax, %r12
testq %rax, %rax
je 0x25f89
cmpb $0x42, (%r12)
jne 0x25fba
movzbl 0x1(%r12), %edi
callq 0x260de
leal -0x5(%rax), %ecx
cmpl $-0x5, %ecx
jbe 0x25fba
movq %rbx, 0x18(%rsp)
movl %eax, %eax
movl 0x2(%r12), %ecx
imulq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq %r12, %rbx
jmp 0x25fa9
callq 0x4050
cmpl $0x2, (%rax)
jne 0x25fc5
movq %rbx, 0x18(%rsp)
movslq 0x40(%r15), %rbx
addq 0x38(%r15), %rbx
movq $0x0, 0x8(%rsp)
movzbl %r14b, %edi
callq 0x260de
leal -0x5(%rax), %ecx
cmpl $-0x5, %ecx
ja 0x25fdb
callq 0x4050
movl $0x16, (%rax)
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
divl %ecx
cmpl %ebp, %eax
jae 0x25ff7
callq 0x4050
movl $0xc, (%rax)
jmp 0x25fc5
movl %ebp, %eax
imulq %rax, %rcx
testq %r12, %r12
je 0x26010
movq 0x8(%rsp), %rdx
cmpq %rdx, %rcx
jbe 0x26053
xorl %eax, %eax
jmp 0x26018
pushq $0x8
popq %rax
movq 0x8(%rsp), %rdx
movq 0x38(%r15), %rsi
movq %rsi, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
subq %rdx, %rcx
addq %rax, %rcx
movq %r15, %rdi
movq %rcx, %rsi
callq 0x2200f
testl %eax, %eax
js 0x25fc5
subq 0x20(%rsp), %rbx
addq 0x38(%r15), %rbx
testq %r12, %r12
je 0x2608c
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
cmpq %rdx, %rcx
je 0x260b3
leaq (%rbx,%rcx), %rdi
addq $0x6, %rdi
leaq (%rbx,%rdx), %rsi
addq $0x6, %rsi
movq %rdx, %r13
movslq 0x40(%r15), %rdx
subq %rsi, %rdx
addq 0x38(%r15), %rdx
movq %rcx, %r12
callq 0x4830
movq %r12, %rcx
movl %ecx, %eax
subl %r13d, %eax
addl %eax, 0x40(%r15)
jmp 0x260b3
movb (%r13), %al
movb %al, (%rbx)
movb 0x1(%r13), %al
movb %al, 0x1(%rbx)
movb $0x42, 0x2(%rbx)
addq $0x2, %rbx
movl 0x40(%r15), %eax
movq 0x10(%rsp), %rcx
addl %ecx, %eax
addl $0x8, %eax
movl %eax, 0x40(%r15)
movb %r14b, 0x1(%rbx)
movl %ebp, 0x2(%rbx)
xorl %r14d, %r14d
testl %ebp, %ebp
je 0x25fc9
addq $0x6, %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %rcx, %rdx
callq 0x43c0
jmp 0x25fc9