// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/sam.c
// Function: bam_aux_update_str
int bam_aux_update_str(bam1_t *b, const char tag[2], int len, const char *data)
{
    // FIXME: This is not at all efficient!
    size_t ln = len >= 0 ? len : strlen(data) + 1;
    size_t old_ln = 0;
    int need_nul = ln == 0 || data[ln - 1] != '\0';
    int save_errno = errno;
    int new_tag = 0;
    uint8_t *s = bam_aux_get(b,tag), *e;

    if (s) {  // Replacing existing tag
        char type = *s;
        if (type != 'Z') {
            hts_log_error("Called bam_aux_update_str for type '%c' instead of 'Z'", type);
            errno = EINVAL;
            return -1;
        }
        s++;
        e = memchr(s, '\0', b->data + b->l_data - s);
        old_ln = (e ? e - s : b->data + b->l_data - s) + 1;
        s -= 3;
    } else {
        if (errno != ENOENT) { // Invalid aux data, give up
            return -1;
        } else { // Tag doesn't exist - put it on the end
            errno = save_errno;
            s = b->data + b->l_data;
            new_tag = 3;
        }
    }

    if (old_ln < ln + need_nul + new_tag) {
        ptrdiff_t s_offset = s - b->data;
        if (possibly_expand_bam_data(b, ln + need_nul + new_tag - old_ln) < 0)
            return -1;
        s = b->data + s_offset;
    }
    if (!new_tag) {
        memmove(s + 3 + ln + need_nul,
                s + 3 + old_ln,
                b->l_data - (s + 3 - b->data) - old_ln);
    }
    b->l_data += new_tag + ln + need_nul - old_ln;

    s[0] = tag[0];
    s[1] = tag[1];
    s[2] = 'Z';
    memmove(s+3,data,ln);
    if (need_nul) s[3 + ln] = '\0';
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %rbx
movq %rsi, %r14
movq %rdi, %r12
testl %edx, %edx
js 0x2ed28
movl %edx, %eax
jmp 0x2ed33
movq %rbx, %rdi
callq 0x41d0
incq %rax
testq %rax, %rax
movq %rbx, 0x20(%rsp)
movq %rax, 0x10(%rsp)
je 0x2ed4e
cmpb $0x0, -0x1(%rbx,%rax)
setne 0x7(%rsp)
jmp 0x2ed53
movb $0x1, 0x7(%rsp)
callq 0x4050
movq %rax, %rbp
movl (%rax), %ebx
movq %r12, %rdi
movq %r14, %rsi
callq 0x2c960
movq %rax, %r13
testq %rax, %rax
movq %r12, 0x8(%rsp)
movq %r14, 0x18(%rsp)
je 0x2edba
movsbl (%r13), %ebx
cmpl $0x5a, %ebx
jne 0x2ede3
leaq 0x1(%r13), %r15
movq %r12, %rax
movslq 0x40(%r12), %r12
addq 0x38(%rax), %r12
subq %r15, %r12
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4550
movq %rax, %r14
subq %r15, %r14
testq %rax, %rax
cmoveq %r12, %r14
incq %r14
leaq -0x2(%r13), %r15
jmp 0x2ee0c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, (%rbp)
jne 0x2ef0b
movl %ebx, (%rbp)
movq 0x8(%rsp), %rax
movslq 0x40(%rax), %r15
addq 0x38(%rax), %r15
movl $0x3, %ebx
xorl %r14d, %r14d
jmp 0x2ee1c
leaq 0x6bfc5(%rip), %rsi     # 0x9adaf
leaq 0x6bfd1(%rip), %rdx     # 0x9adc2
xorl %r14d, %r14d
movl $0x1, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x14378
movl $0x16, (%rbp)
movq %r13, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x5a, %bl
jne 0x2ef0b
xorl %ebx, %ebx
movzbl 0x7(%rsp), %r12d
movq 0x10(%rsp), %rax
leaq (%rax,%r12), %rsi
addq %rbx, %rsi
subq %r14, %rsi
jbe 0x2ee97
movq 0x8(%rsp), %rdx
movq 0x38(%rdx), %rdi
movslq 0x40(%rdx), %rax
addq %rax, %rsi
setae %al
testq $-0x80000000, %rsi      # imm = 0x80000000
sete %cl
testb %al, %cl
jne 0x2ee5f
movl $0xc, (%rbp)
xorl %ecx, %ecx
jmp 0x2ee7e
movl 0x44(%rdx), %eax
movb $0x1, %cl
cmpq %rax, %rsi
jbe 0x2ee7e
movq %rdi, %rbp
movq 0x8(%rsp), %rdi
callq 0x23476
movq %rbp, %rdi
testl %eax, %eax
setns %cl
testb %cl, %cl
je 0x2ee8e
subq %rdi, %r15
movq 0x8(%rsp), %rax
addq 0x38(%rax), %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %cl, %cl
je 0x2ef0b
testq %r13, %r13
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
je 0x2eec9
leaq (%r15,%r13), %rdi
addq $0x3, %rdi
addq %r12, %rdi
leaq (%r15,%r14), %rsi
addq $0x3, %rsi
movslq 0x40(%rbp), %rdx
subq %rsi, %rdx
addq 0x38(%rbp), %rdx
callq 0x4830
addl %r13d, %r12d
addl %r12d, %ebx
subl %r14d, %ebx
addl %ebx, 0x40(%rbp)
movq 0x18(%rsp), %rcx
movb (%rcx), %al
movb %al, (%r15)
movb 0x1(%rcx), %al
movb %al, 0x1(%r15)
movb $0x5a, 0x2(%r15)
leaq 0x3(%r15), %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x4830
xorl %eax, %eax
cmpb $0x0, 0x7(%rsp)
je 0x2ef0b
movb $0x0, 0x3(%r13,%r15)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq