// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rcx, %r14
movq %rsi, %r12
movq %rdi, %r13
testl %edx, %edx
js 0x2e0f3
movl %edx, %r15d
jmp 0x2e101
movq %r14, %rdi
callq 0x41d0
movq %rax, %r15
incq %r15
testq %r15, %r15
je 0x2e113
cmpb $0x0, -0x1(%r14,%r15)
setne 0x7(%rsp)
jmp 0x2e118
movb $0x1, 0x7(%rsp)
callq 0x4050
movq %rax, %rbp
movl (%rax), %ebx
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x2beb1
movq %rax, %r13
testq %rax, %rax
je 0x2e19e
movsbl (%r13), %ecx
cmpl $0x5a, %ecx
jne 0x2e222
movq %r12, 0x28(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x30(%rsp)
leaq 0x1(%r13), %rbx
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rcx
movslq 0x40(%rax), %r15
movq %r15, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
addq %rcx, %r15
subq %rbx, %r15
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x4550
movq %rax, %r14
subq %rbx, %r14
testq %rax, %rax
cmoveq %r15, %r14
incq %r14
leaq -0x2(%r13), %rbx
jmp 0x2e1e2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, (%rbp)
jne 0x2e2ea
movq %r12, 0x28(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x30(%rsp)
movl %ebx, (%rbp)
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rcx
movslq 0x40(%rax), %rbx
movq %rbx, 0x20(%rsp)
movq %rcx, 0x8(%rsp)
addq %rcx, %rbx
movl $0x3, %r12d
xorl %r14d, %r14d
movzbl 0x7(%rsp), %r15d
movq 0x18(%rsp), %rax
addq %r15, %rax
addq %rax, %r12
movq %r12, %rsi
subq %r14, %rsi
jbe 0x2e24d
movslq 0x20(%rsp), %rax
addq %rax, %rsi
setae %al
testq $-0x80000000, %rsi      # imm = 0x80000000
sete %cl
testb %al, %cl
movq 0x8(%rsp), %rdx
jne 0x2e254
movl $0xc, (%rbp)
jmp 0x2e243
leaq 0x69b99(%rip), %rsi     # 0x97dc2
leaq 0x69ba5(%rip), %rdx     # 0x97dd5
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movl $0x16, (%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2e2ea
movq 0x8(%rsp), %rax
jmp 0x2e27f
movq 0x10(%rsp), %rbp
movl 0x44(%rbp), %ecx
movq %rdx, %rax
cmpq %rcx, %rsi
jbe 0x2e279
movq %rbp, %rdi
callq 0x22a64
testl %eax, %eax
js 0x2e243
movq 0x38(%rbp), %rax
movq 0x8(%rsp), %rdx
subq %rdx, %rbx
addq %rax, %rbx
testq %r13, %r13
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rbp
je 0x2e2b0
leaq (%rbx,%r13), %rdi
addq $0x3, %rdi
addq %r15, %rdi
leaq (%rbx,%r14), %rsi
addq $0x3, %rsi
movslq 0x40(%rbp), %rdx
subq %rsi, %rdx
addq %rax, %rdx
callq 0x4830
subl %r14d, %r12d
addl %r12d, 0x40(%rbp)
movq 0x28(%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 0x30(%rsp), %rsi
movq %r13, %rdx
callq 0x4830
xorl %eax, %eax
cmpb $0x0, 0x7(%rsp)
je 0x2e2ea
movb $0x0, 0x3(%r13,%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq