// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %r8, %r14
movl %ecx, %ebx
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r15
callq 0x2c960
movq %rax, %rdi
testq %rax, %rax
je 0x2f31b
cmpb $0x42, (%rdi)
jne 0x2f42d
movzbl 0x1(%rdi), %ecx
movl $0x1, %eax
cmpl $0x59, %ecx
jg 0x2f35a
leal -0x41(%rcx), %edx
cmpl $0x8, %edx
ja 0x2f38e
leaq 0x6b00a(%rip), %rsi     # 0x9a31c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movl %ebx, %eax
movq %rdi, %rbx
movq %r12, %r13
movl %ebp, %r12d
movl %eax, %ebp
movq %r14, 0x8(%rsp)
callq 0x4050
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $0x2, (%rax)
jne 0x2f43e
movslq 0x40(%r15), %r8
addq 0x38(%r15), %r8
xorl %r9d, %r9d
movl %ebp, %r10d
movl %r12d, %ebp
movq %r13, %r12
movq %rbx, %rdi
jmp 0x2f3bd
leal -0x63(%rcx), %edx
cmpl $0x6, %edx
ja 0x2f379
leaq 0x6afd7(%rip), %rcx     # 0x9a340
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movl $0x4, %eax
jmp 0x2f39c
cmpl $0x5a, %ecx
je 0x2f38a
cmpl $0x73, %ecx
jne 0x2f393
movl $0x2, %eax
jmp 0x2f39c
movl %ecx, %eax
jmp 0x2f39c
cmpl $0x53, %ecx
je 0x2f383
xorl %eax, %eax
jmp 0x2f39c
movl $0x8, %eax
leal -0x5(%rax), %ecx
cmpl $-0x5, %ecx
jbe 0x2f42d
movl %ebx, %r10d
movq %r14, 0x8(%rsp)
movl %eax, %eax
movl 0x2(%rdi), %r9d
imulq %rax, %r9
movq %rdi, %r8
movzbl %bpl, %ecx
movl $0x1, %eax
cmpl $0x59, %ecx
jg 0x2f3e3
leal -0x41(%rcx), %edx
cmpl $0x8, %edx
ja 0x2f417
leaq 0x6af82(%rip), %rsi     # 0x9a35c
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
leal -0x63(%rcx), %edx
cmpl $0x6, %edx
ja 0x2f402
leaq 0x6af8e(%rip), %rcx     # 0x9a380
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
movl $0x4, %eax
jmp 0x2f425
cmpl $0x5a, %ecx
je 0x2f413
cmpl $0x73, %ecx
jne 0x2f41c
movl $0x2, %eax
jmp 0x2f425
movl %ecx, %eax
jmp 0x2f425
cmpl $0x53, %ecx
je 0x2f40c
xorl %eax, %eax
jmp 0x2f425
movl $0x8, %eax
leal -0x5(%rax), %ecx
cmpl $-0x5, %ecx
ja 0x2f450
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, 0x4(%rsp)
movl %eax, %ebp
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
divq %rbp
cmpl %r10d, %eax
jae 0x2f472
callq 0x4050
movl $0xc, (%rax)
jmp 0x2f438
movl %r10d, %eax
imulq %rax, %rbp
testq %rdi, %rdi
movl %r10d, (%rsp)
je 0x2f48f
cmpq %r9, %rbp
jbe 0x2f525
xorl %eax, %eax
jmp 0x2f494
movl $0x8, %eax
movq 0x38(%r15), %rcx
movq %rcx, 0x10(%rsp)
movq %rbp, %rcx
subq %r9, %rcx
addq %rax, %rcx
movslq 0x40(%r15), %rsi
addq %rcx, %rsi
setae %al
testq $-0x80000000, %rsi      # imm = 0x80000000
sete %cl
movq %rdi, %r13
testb %al, %cl
jne 0x2f4dc
movq %r8, %rbx
movq %r9, %r14
callq 0x4050
movq %r14, %r9
movq %rbx, %r8
movl $0xc, (%rax)
xorl %eax, %eax
jmp 0x2f500
movl 0x44(%r15), %ecx
movb $0x1, %al
cmpq %rcx, %rsi
jbe 0x2f500
movq %r15, %rdi
movq %r8, %rbx
movq %r9, %r14
callq 0x23476
movq %r14, %r9
movq %rbx, %r8
testl %eax, %eax
setns %al
testb %al, %al
je 0x2f512
subq 0x10(%rsp), %r8
movq %r15, %rcx
addq 0x38(%r15), %r8
jmp 0x2f515
movq %r15, %rcx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testb %al, %al
je 0x2f43e
jmp 0x2f52b
movq %rdi, %r13
movq %r15, %rcx
testq %r13, %r13
je 0x2f570
cmpq %r9, %rbp
movl (%rsp), %r14d
je 0x2f598
movq %rcx, %r15
leaq (%r8,%rbp), %rdi
addq $0x6, %rdi
leaq (%r8,%r9), %rsi
addq $0x6, %rsi
movslq 0x40(%rcx), %rdx
subq %rsi, %rdx
addq 0x38(%rcx), %rdx
movq %r8, %rbx
movq %r9, %r12
callq 0x4830
movq %rbx, %r8
movl %ebp, %eax
subl %r12d, %eax
addl %eax, 0x40(%r15)
jmp 0x2f598
movb (%r12), %al
movb %al, (%r8)
movb 0x1(%r12), %al
movb %al, 0x1(%r8)
movb $0x42, 0x2(%r8)
addq $0x2, %r8
movl 0x40(%rcx), %eax
addl %ebp, %eax
addl $0x8, %eax
movl %eax, 0x40(%rcx)
movl (%rsp), %r14d
movl 0x4(%rsp), %eax
movb %al, 0x1(%r8)
movl %r14d, 0x2(%r8)
movl %r14d, %eax
xorl %r14d, %r14d
testl %eax, %eax
je 0x2f43e
addq $0x6, %r8
movq %r8, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
callq 0x43c0
jmp 0x2f43e