// File: /workspace/github_c_makefile_star2_O1/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 0x2ef5e
callq 0x4050
movl $0x4b, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2f195
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
cmpq $-0x8000, %rdx          # imm = 0x8000
jge 0x2ef7b
movb $0x69, %bpl
movl $0x4, %r14d
jmp 0x2efc6
cmpq $-0x80, %r13
jge 0x2ef8c
movb $0x73, %bpl
movl $0x2, %r14d
jmp 0x2efc6
movl $0x1, %r14d
testq %r13, %r13
js 0x2efc3
movb $0x43, %bpl
cmpq $0xff, %r13
jl 0x2efc6
xorl %eax, %eax
cmpq $0xffff, %r13           # imm = 0xFFFF
setge %al
leal 0x2(%rax,%rax), %r14d
movl $0x53, %eax
movl $0x49, %ebp
cmovll %eax, %ebp
jmp 0x2efc6
movb $0x63, %bpl
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2c960
movq %rax, %r15
testq %rax, %rax
movl %ebp, 0x4(%rsp)
je 0x2f02d
movzbl (%r15), %eax
addl $-0x43, %eax
cmpl $0x30, %eax
ja 0x2f1a4
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
movq %r12, 0x8(%rsp)
jb 0x2f05b
movabsq $0x4000000040, %rcx     # imm = 0x4000000040
btq %rax, %rcx
jb 0x2f054
movabsq $0x1000000010000, %rcx  # imm = 0x1000000010000
btq %rax, %rcx
jae 0x2f1a4
movl $0x2, %ebp
jmp 0x2f060
callq 0x4050
movq %rax, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, (%rcx)
jne 0x2f195
movq %r12, 0x8(%rsp)
movslq 0x40(%rbx), %r12
addq 0x38(%rbx), %r12
xorl %ebp, %ebp
jmp 0x2f063
movl $0x4, %ebp
jmp 0x2f060
movl $0x1, %ebp
movq %r15, %r12
testq %r15, %r15
je 0x2f0b7
xorl %eax, %eax
cmpl %ebp, %r14d
ja 0x2f0bc
testq %r13, %r13
leaq 0x6b487(%rip), %rax     # 0x9a500
leaq 0x6b486(%rip), %rcx     # 0x9a506
cmovsq %rax, %rcx
movl %ebp, %eax
movb (%rcx,%rax), %al
movl %ebp, (%rsp)
movl %eax, 0x4(%rsp)
testb %al, %al
jne 0x2f15e
leaq 0x6bd5a(%rip), %rdi     # 0x9adf9
leaq 0x6bc6d(%rip), %rsi     # 0x9ad13
leaq 0x6bd55(%rip), %rcx     # 0x9ae02
movl $0x13d0, %edx           # imm = 0x13D0
callq 0x42f0
movl $0x3, %eax
movq %r12, %r13
subq 0x38(%rbx), %r13
movl %r14d, (%rsp)
movl %r14d, %ecx
subl %ebp, %ecx
addl %eax, %ecx
movslq 0x40(%rbx), %rsi
addq %rcx, %rsi
setae %al
testq $-0x80000000, %rsi      # imm = 0x80000000
sete %cl
testb %al, %cl
jne 0x2f0f6
callq 0x4050
movl $0xc, (%rax)
xorl %r14d, %r14d
jmp 0x2f10f
movl 0x44(%rbx), %eax
movb $0x1, %r14b
cmpq %rax, %rsi
jbe 0x2f10f
movq %rbx, %rdi
callq 0x23476
testl %eax, %eax
setns %r14b
testb %r14b, %r14b
je 0x2f154
movq 0x38(%rbx), %r12
addq %r13, %r12
testq %r15, %r15
je 0x2f13d
movl (%rsp), %edi
addq %r12, %rdi
movl %ebp, %eax
leaq (%r12,%rax), %rsi
movslq 0x40(%rbx), %rdx
addq %rax, %r13
subq %r13, %rdx
callq 0x4830
jmp 0x2f154
movq 0x8(%rsp), %rcx
movb (%rcx), %al
movb %al, (%r12)
movb 0x1(%rcx), %al
movb %al, 0x1(%r12)
addq $0x2, %r12
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %r14b, %r14b
je 0x2f195
xorl %r14d, %r14d
testq %r15, %r15
sete %r14b
movl 0x4(%rsp), %eax
movb %al, (%r12)
incq %r12
movl (%rsp), %r15d
movl %r15d, %edx
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x43c0
leal (%r14,%r14,2), %eax
subl %ebp, %eax
addl %r15d, %eax
addl %eax, 0x40(%rbx)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4050
movl $0x16, (%rax)
jmp 0x2ef54