// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rsi, %r12
movq %rdi, %r13
testl %edx, %edx
js 0x25a97
movl %edx, %r14d
jmp 0x25aa5
movq %r15, %rdi
callq 0x41d0
movq %rax, %r14
incq %r14
testq %r14, %r14
je 0x25ab7
cmpb $0x0, -0x1(%r15,%r14)
setne 0x7(%rsp)
jmp 0x25abc
movb $0x1, 0x7(%rsp)
callq 0x4050
movq %rax, %rbp
movl (%rax), %ebx
movq %r13, 0x8(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x24528
movq %rax, %r13
testq %rax, %rax
je 0x25b38
movsbl (%r13), %ecx
cmpl $0x5a, %ecx
jne 0x25b9b
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
leaq 0x1(%r13), %rbp
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rcx
movslq 0x40(%rax), %rbx
movq %rcx, 0x10(%rsp)
addq %rcx, %rbx
subq %rbp, %rbx
xorl %r15d, %r15d
movq %rbp, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x4550
movq %rax, %r12
subq %rbp, %r12
testq %rax, %rax
cmoveq %rbx, %r12
incq %r12
leaq -0x2(%r13), %rbx
jmp 0x25b67
cmpl $0x2, (%rbp)
jne 0x25bba
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movl %ebx, (%rbp)
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rcx
movslq 0x40(%rax), %rbx
movq %rcx, 0x10(%rsp)
addq %rcx, %rbx
pushq $0x3
popq %r15
xorl %r12d, %r12d
movzbl 0x7(%rsp), %ebp
addq %r14, %r15
addq %rbp, %r15
movq %r15, %rsi
subq %r12, %rsi
jbe 0x25bbf
movq 0x8(%rsp), %rdi
callq 0x2200f
testl %eax, %eax
js 0x25bba
subq 0x10(%rsp), %rbx
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rax
addq %rax, %rbx
jmp 0x25bc4
leaq 0x5417b(%rip), %rsi     # 0x79d1d
leaq 0x54187(%rip), %rdx     # 0x79d30
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movl $0x16, (%rbp)
pushq $-0x1
popq %rax
jmp 0x25c2a
movq 0x10(%rsp), %rax
testq %r13, %r13
movq 0x8(%rsp), %r13
je 0x25bf0
leaq (%rbx,%r14), %rdi
addq $0x3, %rdi
addq %rbp, %rdi
leaq (%rbx,%r12), %rsi
addq $0x3, %rsi
movslq 0x40(%r13), %rdx
subq %rsi, %rdx
addq %rax, %rdx
callq 0x4830
subl %r12d, %r15d
addl %r15d, 0x40(%r13)
movq 0x18(%rsp), %rcx
movb (%rcx), %al
movb %al, (%rbx)
movb 0x1(%rcx), %al
movb %al, 0x1(%rbx)
movb $0x5a, 0x2(%rbx)
leaq 0x3(%rbx), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x4830
xorl %eax, %eax
cmpb $0x0, 0x7(%rsp)
je 0x25c2a
movb $0x0, 0x3(%r14,%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq