// File: Eba-M[P]E3DC-Control/cJSON.c
// Function: ensure(printbuffer*, unsigned long)
static unsigned char* ensure(printbuffer * const p, size_t needed)
{
    unsigned char *newbuffer = NULL;
    size_t newsize = 0;

    if ((p == NULL) || (p->buffer == NULL))
    {
        return NULL;
    }

    if ((p->length > 0) && (p->offset >= p->length))
    {
        /* make sure that offset is valid */
        return NULL;
    }

    if (needed > INT_MAX)
    {
        /* sizes bigger than INT_MAX are currently not supported */
        return NULL;
    }

    needed += p->offset + 1;
    if (needed <= p->length)
    {
        return p->buffer + p->offset;
    }

    if (p->noalloc) {
        return NULL;
    }

    /* calculate new buffer size */
    if (needed > (INT_MAX / 2))
    {
        /* overflow of int, use INT_MAX if possible */
        if (needed <= INT_MAX)
        {
            newsize = INT_MAX;
        }
        else
        {
            return NULL;
        }
    }
    else
    {
        newsize = needed * 2;
    }

    if (p->hooks.reallocate != NULL)
    {
        /* reallocate with realloc if available */
        newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
        if (newbuffer == NULL)
        {
            p->hooks.deallocate(p->buffer);
            p->length = 0;
            p->buffer = NULL;

            return NULL;
        }
    }
    else
    {
        /* otherwise reallocate manually */
        newbuffer = (unsigned char*)p->hooks.allocate(newsize);
        if (!newbuffer)
        {
            p->hooks.deallocate(p->buffer);
            p->length = 0;
            p->buffer = NULL;

            return NULL;
        }

        memcpy(newbuffer, p->buffer, p->offset + 1);
        p->hooks.deallocate(p->buffer);
    }
    p->length = newsize;
    p->buffer = newbuffer;

    return newbuffer + p->offset;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
cmpq $0x0, 0x18(%rsp)
je 0x5d782
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x5d790
movq $0x0, 0x20(%rsp)
jmp 0x5d98f
movq 0x18(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x5d7be
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
cmpq 0x8(%rcx), %rax
jb 0x5d7be
movq $0x0, 0x20(%rsp)
jmp 0x5d98f
cmpq $0x7fffffff, 0x10(%rsp) # imm = 0x7FFFFFFF
jbe 0x5d7d7
movq $0x0, 0x20(%rsp)
jmp 0x5d98f
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x1, %rax
addq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq 0x8(%rcx), %rax
ja 0x5d819
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rsp), %rcx
addq 0x10(%rcx), %rax
movq %rax, 0x20(%rsp)
jmp 0x5d98f
movq 0x18(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0x5d832
movq $0x0, 0x20(%rsp)
jmp 0x5d98f
cmpq $0x3fffffff, 0x10(%rsp) # imm = 0x3FFFFFFF
jbe 0x5d862
cmpq $0x7fffffff, 0x10(%rsp) # imm = 0x7FFFFFFF
ja 0x5d852
movq $0x7fffffff, (%rsp)     # imm = 0x7FFFFFFF
jmp 0x5d860
movq $0x0, 0x20(%rsp)
jmp 0x5d98f
jmp 0x5d86e
movq 0x10(%rsp), %rax
shlq %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x38(%rax)
je 0x5d8dd
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rdi
movq (%rsp), %rsi
callq *%rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x5d8d8
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rdi
callq *%rax
movq 0x18(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
movq $0x0, 0x20(%rsp)
jmp 0x5d98f
jmp 0x5d962
movq 0x18(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rsp), %rdi
callq *%rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x5d930
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rdi
callq *%rax
movq 0x18(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
movq $0x0, 0x20(%rsp)
jmp 0x5d98f
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdx
addq $0x1, %rdx
callq 0x2220
movq 0x18(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rdi
callq *%rax
movq (%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
addq 0x10(%rcx), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq $0x28, %rsp
retq
nopl (%rax)