// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/sam.c
// Function: bam_aux_update_int
int bam_aux_update_int(bam1_t *b, const char tag[2], int64_t val)
{
    uint32_t sz, old_sz = 0, new = 0;
    uint8_t *s, type;

    if (val < INT32_MIN || val > UINT32_MAX) {
        errno = EOVERFLOW;
        return -1;
    }
    if (val < INT16_MIN)       { type = 'i'; sz = 4; }
    else if (val < INT8_MIN)   { type = 's'; sz = 2; }
    else if (val < 0)          { type = 'c'; sz = 1; }
    else if (val < UINT8_MAX)  { type = 'C'; sz = 1; }
    else if (val < UINT16_MAX) { type = 'S'; sz = 2; }
    else                       { type = 'I'; sz = 4; }

    s = bam_aux_get(b, tag);
    if (s) {  // Tag present - how big was the old one?
        switch (*s) {
            case 'c': case 'C': old_sz = 1; break;
            case 's': case 'S': old_sz = 2; break;
            case 'i': case 'I': old_sz = 4; break;
            default: errno = EINVAL; return -1;  // Not an integer
        }
    } 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;
        }
    }

    if (new || old_sz < sz) {
        // Make room for new tag
        ptrdiff_t s_offset = s - b->data;
        if (possibly_expand_bam_data(b, (new ? 3 : 0) + sz - old_sz) < 0)
            return -1;
        s =  b->data + s_offset;
        if (new) { // Add tag id
            *s++ = tag[0];
            *s++ = tag[1];
        } else {   // Shift following data so we have space
            memmove(s + sz, s + old_sz, b->l_data - s_offset - old_sz);
        }
    } else {
        // Reuse old space.  Data value may be bigger than necessary but
        // we avoid having to move everything else
        sz = old_sz;
        type = (val < 0 ? "\0cs\0i" : "\0CS\0I")[old_sz];
        assert(type > 0);
    }
    *s++ = type;
#ifdef HTS_LITTLE_ENDIAN
    memcpy(s, &val, sz);
#else
    switch (sz) {
        case 4:  u32_to_le(val, s); break;
        case 2:  u16_to_le(val, s); break;
        default: *s = val; break;
    }
#endif
    b->l_data += (new ? 3 : 0) + sz - old_sz;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, 0x10(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rdx, %rax
movabsq $-0x180000001, %rcx     # imm = 0xFFFFFFFE7FFFFFFF
cmpq %rcx, %rax
ja 0x2e33d
callq 0x4050
movl $0x4b, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2e574
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
cmpq $-0x8000, %rdx          # imm = 0x8000
jge 0x2e358
movb $0x69, %dl
movl $0x4, %ecx
jmp 0x2e39d
cmpq $-0x80, %r13
jge 0x2e367
movb $0x73, %dl
movl $0x2, %ecx
jmp 0x2e39d
movl $0x1, %ecx
testq %r13, %r13
js 0x2e39b
movb $0x43, %dl
cmpq $0xff, %r13
jb 0x2e39d
xorl %eax, %eax
cmpq $0xffff, %r13           # imm = 0xFFFF
setae %al
leal 0x2(%rax,%rax), %ecx
movl $0x53, %eax
movl $0x49, %edx
cmovbl %eax, %edx
jmp 0x2e39d
movb $0x63, %dl
movl %edx, 0x4(%rsp)
movl %ecx, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2beb1
movq %rax, %r15
testq %rax, %rax
je 0x2e411
movzbl (%r15), %eax
addl $-0x43, %eax
cmpl $0x30, %eax
movl (%rsp), %edx
ja 0x2e517
movq %r15, %r12
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
jb 0x2e43f
movl $0x4, %ebp
movabsq $0x4000000040, %rcx     # imm = 0x4000000040
btq %rax, %rcx
jb 0x2e47c
movabsq $0x1000000010000, %rcx  # imm = 0x1000000010000
btq %rax, %rcx
jae 0x2e517
movl $0x2, %ebp
jmp 0x2e444
callq 0x4050
movq %rax, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, (%rcx)
jne 0x2e574
movq 0x38(%rbx), %rdi
movslq 0x40(%rbx), %rax
leaq (%rdi,%rax), %r12
movl $0x3, %ecx
xorl %ebp, %ebp
movl (%rsp), %edx
jmp 0x2e452
movl $0x1, %ebp
cmpl %edx, %ebp
jae 0x2e47c
movq 0x38(%rbx), %rdi
movslq 0x40(%rbx), %rax
xorl %ecx, %ecx
movl %edx, %esi
subl %ebp, %esi
addl %ecx, %esi
addq %rax, %rsi
setae %al
testq $-0x80000000, %rsi      # imm = 0x80000000
sete %cl
testb %al, %cl
jne 0x2e4c2
callq 0x4050
movl $0xc, (%rax)
jmp 0x2e333
testq %r13, %r13
leaq 0x6908a(%rip), %rax     # 0x97510
leaq 0x69089(%rip), %rcx     # 0x97516
cmovsq %rax, %rcx
movl %ebp, %r14d
movb (%rcx,%r14), %al
movl %ebp, (%rsp)
testb %al, %al
jne 0x2e544
leaq 0x69962(%rip), %rdi     # 0x97e0c
leaq 0x69875(%rip), %rsi     # 0x97d26
leaq 0x6995d(%rip), %rcx     # 0x97e15
movl $0x13d0, %edx           # imm = 0x13D0
callq 0x42f0
movl 0x44(%rbx), %eax
movq %rdi, %r13
cmpq %rax, %rsi
jbe 0x2e4ee
movq %rdi, 0x8(%rsp)
movq %rbx, %rdi
callq 0x22a64
testl %eax, %eax
js 0x2e333
movq 0x38(%rbx), %r13
movl (%rsp), %edx
movq 0x8(%rsp), %rdi
subq %rdi, %r12
addq %r12, %r13
testq %r15, %r15
je 0x2e527
movl %edx, %r14d
leaq (%r14,%r13), %rdi
movl %ebp, %eax
leaq (%rax,%r13), %rsi
movslq 0x40(%rbx), %rdx
addq %rax, %r12
subq %r12, %rdx
callq 0x4830
jmp 0x2e53d
callq 0x4050
movl $0x16, (%rax)
jmp 0x2e333
movb (%r14), %al
movb %al, (%r13)
movb 0x1(%r14), %al
movb %al, 0x1(%r13)
addq $0x2, %r13
movl %edx, %r14d
movq %r13, %r12
movl 0x4(%rsp), %eax
xorl %r13d, %r13d
testq %r15, %r15
sete %r13b
movb %al, (%r12)
incq %r12
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x43c0
leal (%r13,%r13,2), %eax
subl %ebp, %eax
addl (%rsp), %eax
addl %eax, 0x40(%rbx)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq