// File: /workspace/github_c_makefile_star2_O2/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 0x25c7b
callq 0x4050
movl $0x4b, (%rax)
pushq $-0x1
popq %rax
jmp 0x25e59
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
cmpq $-0x8000, %rdx          # imm = 0x8000
jge 0x25c94
movb $0x69, %dl
pushq $0x4
popq %rcx
jmp 0x25cd1
cmpq $-0x80, %r13
jge 0x25ca1
pushq $0x2
popq %rcx
movb $0x73, %dl
jmp 0x25cd1
pushq $0x1
popq %rcx
testq %r13, %r13
js 0x25ccf
movb $0x43, %dl
cmpq $0xff, %r13
jb 0x25cd1
xorl %eax, %eax
cmpq $0xffff, %r13           # imm = 0xFFFF
setae %al
leal 0x2(%rax,%rax), %ecx
pushq $0x53
popq %rax
pushq $0x49
popq %rdx
cmovbl %eax, %edx
jmp 0x25cd1
movb $0x63, %dl
movl %edx, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x24528
movq %rax, %r14
testq %rax, %rax
je 0x25d37
movzbl (%r14), %eax
addl $-0x43, %eax
cmpl $0x30, %eax
ja 0x25df9
movq %r14, %r12
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
jb 0x25d60
movabsq $0x4000000040, %rcx     # imm = 0x4000000040
btq %rax, %rcx
jb 0x25d5b
movabsq $0x1000000010000, %rcx  # imm = 0x1000000010000
btq %rax, %rcx
jae 0x25df9
pushq $0x2
jmp 0x25d62
callq 0x4050
cmpl $0x2, (%rax)
jne 0x25c73
movq 0x38(%rbx), %r13
movslq 0x40(%rbx), %r12
addq %r13, %r12
pushq $0x3
popq %rax
xorl %ebp, %ebp
movl 0x8(%rsp), %ecx
jmp 0x25d71
pushq $0x4
popq %rbp
jmp 0x25db6
pushq $0x1
popq %rbp
movl 0x8(%rsp), %ecx
cmpl %ecx, %ebp
jae 0x25db6
movq 0x38(%rbx), %r13
xorl %eax, %eax
movl %ecx, %esi
subl %ebp, %esi
addl %eax, %esi
movq %rbx, %rdi
callq 0x2200f
testl %eax, %eax
js 0x25c73
subq %r13, %r12
movq 0x38(%rbx), %r13
addq %r12, %r13
testq %r14, %r14
je 0x25e09
movl 0x8(%rsp), %r15d
leaq (%r15,%r13), %rdi
movl %ebp, %eax
leaq (%rax,%r13), %rsi
movslq 0x40(%rbx), %rdx
addq %rax, %r12
subq %r12, %rdx
callq 0x4830
jmp 0x25e21
testq %r13, %r13
leaq 0x536a0(%rip), %rax     # 0x79460
leaq 0x5369f(%rip), %rcx     # 0x79466
cmovsq %rax, %rcx
movl %ebp, %r15d
movb (%rcx,%r15), %al
movl %ebp, 0x8(%rsp)
testb %al, %al
jne 0x25e28
leaq 0x53f86(%rip), %rdi     # 0x79d67
leaq 0x53e99(%rip), %rsi     # 0x79c81
leaq 0x53f81(%rip), %rcx     # 0x79d70
movl $0x13d0, %edx           # imm = 0x13D0
callq 0x42f0
callq 0x4050
movl $0x16, (%rax)
jmp 0x25c73
movb (%r15), %al
movb %al, (%r13)
movb 0x1(%r15), %al
movb %al, 0x1(%r13)
addq $0x2, %r13
movl 0x8(%rsp), %r15d
movq %r13, %r12
movl 0xc(%rsp), %eax
xorl %r13d, %r13d
testq %r14, %r14
sete %r13b
movb %al, (%r12)
incq %r12
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x43c0
leal (%r13,%r13,2), %eax
subl %ebp, %eax
addl 0x8(%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