// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movl %edx, 0x4(%rsp)
movq %rsi, %r13
movq %rdi, %r15
callq 0x2beb1
movq %rax, %r12
testq %rax, %rax
je 0x2e6e9
cmpb $0x42, (%r12)
jne 0x2e7b0
movq %r12, %rbx
movzbl 0x1(%r12), %eax
addl $-0x41, %eax
cmpl $0x32, %eax
ja 0x2e7b0
movabsq $0x400000005, %rcx      # imm = 0x400000005
btq %rax, %rcx
jb 0x2e708
movabsq $0x12000000100, %rcx    # imm = 0x12000000100
btq %rax, %rcx
jae 0x2e70f
movl $0x4, %eax
jmp 0x2e728
callq 0x4050
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x2, (%rax)
jne 0x2e7f3
movslq 0x40(%r15), %rbx
addq 0x38(%r15), %rbx
xorl %ebp, %ebp
jmp 0x2e72f
movl $0x1, %eax
jmp 0x2e728
movabsq $0x4000000040000, %rcx  # imm = 0x4000000040000
btq %rax, %rcx
jae 0x2e7b0
movl $0x2, %eax
movl 0x2(%rbx), %ebp
imulq %rax, %rbp
movzbl 0x4(%rsp), %eax
addl $-0x41, %eax
cmpl $0x32, %eax
ja 0x2e7b0
movabsq $0x400000005, %rcx      # imm = 0x400000005
btq %rax, %rcx
movq %r14, 0x10(%rsp)
jb 0x2e769
movabsq $0x12000000100, %rcx    # imm = 0x12000000100
btq %rax, %rcx
jae 0x2e771
movl $0x4, %r14d
jmp 0x2e787
movl $0x1, %r14d
jmp 0x2e787
movabsq $0x4000000040000, %rcx  # imm = 0x4000000040000
btq %rax, %rcx
jae 0x2e7b0
movl $0x2, %r14d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
divl %r14d
movl (%rsp), %edx
cmpl %edx, %eax
jb 0x2e7e3
movl %edx, %eax
imulq %rax, %r14
testq %r12, %r12
je 0x2e7bd
cmpq %rbp, %r14
jbe 0x2e83c
xorl %eax, %eax
jmp 0x2e7c2
callq 0x4050
movl $0x16, (%rax)
jmp 0x2e7ee
movl $0x8, %eax
movq %r14, %rcx
subq %rbp, %rcx
addq %rax, %rcx
movslq 0x40(%r15), %rsi
addq %rcx, %rsi
setae %al
testq $-0x80000000, %rsi      # imm = 0x80000000
sete %cl
testb %al, %cl
jne 0x2e804
callq 0x4050
movl $0xc, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%r15), %rdi
movl 0x44(%r15), %ecx
movq %rdi, %rax
cmpq %rcx, %rsi
jbe 0x2e831
movq %rdi, 0x8(%rsp)
movq %r15, %rdi
callq 0x22a64
testl %eax, %eax
js 0x2e7ee
movq 0x38(%r15), %rax
movl (%rsp), %edx
movq 0x8(%rsp), %rdi
subq %rdi, %rbx
addq %rax, %rbx
testq %r12, %r12
je 0x2e86f
cmpq %rbp, %r14
je 0x2e892
leaq (%rbx,%r14), %rdi
addq $0x6, %rdi
leaq (%rbx,%rbp), %rsi
addq $0x6, %rsi
movslq 0x40(%r15), %rdx
subq %rsi, %rdx
addq 0x38(%r15), %rdx
callq 0x4830
movl (%rsp), %edx
movl %r14d, %eax
subl %ebp, %eax
addl %eax, 0x40(%r15)
jmp 0x2e892
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
addl %r14d, %eax
addl $0x8, %eax
movl %eax, 0x40(%r15)
movl 0x4(%rsp), %eax
movb %al, 0x1(%rbx)
movl %edx, 0x2(%rbx)
xorl %ebp, %ebp
testl %edx, %edx
je 0x2e7f3
addq $0x6, %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x43c0
jmp 0x2e7f3