// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/src/malloc_count.c
// Function: realloc
extern void* realloc(void* ptr, size_t size)
{
    void* newptr;
    size_t oldsize;

    if ((char*)ptr >= (char*)init_heap &&
        (char*)ptr <= (char*)init_heap + init_heap_use)
    {
        if (log_operations_init_heap) {
            fprintf(stderr, PPREFIX "realloc(%p) = on init heap\n", ptr);
        }

        ptr = (char*)ptr - alignment;

        if (*(size_t*)((char*)ptr + alignment - sizeof(size_t)) != sentinel) {
            fprintf(stderr, PPREFIX
                    "realloc(%p) has no sentinel !!! memory corruption?\n",
                    ptr);
        }

        oldsize = *(size_t*)ptr;

        if (oldsize >= size) {
            /* keep old area, just reduce the size */
            *(size_t*)ptr = size;
            return (char*)ptr + alignment;
        }
        else {
            /* allocate new area and copy data */
            ptr = (char*)ptr + alignment;
            newptr = malloc(size);
            memcpy(newptr, ptr, oldsize);
            free(ptr);
            return newptr;
        }
    }

    if (size == 0) { /* special case size == 0 -> free() */
        free(ptr);
        return NULL;
    }

    if (ptr == NULL) { /* special case ptr == 0 -> malloc() */
        return malloc(size);
    }

    ptr = (char*)ptr - alignment;

    if (*(size_t*)((char*)ptr + alignment - sizeof(size_t)) != sentinel) {
        fprintf(stderr, PPREFIX
                "free(%p) has no sentinel !!! memory corruption?\n", ptr);
    }

    oldsize = *(size_t*)ptr;

    dec_count(oldsize);
    inc_count(size);

    newptr = (*real_realloc)(ptr, alignment + size);

    if (log_operations && size >= log_operations_threshold)
    {
        if (newptr == ptr)
            fprintf(stderr, PPREFIX
                    "realloc(%'lld -> %'lld) = %p   (current %'lld)\n",
                   (long long)oldsize, (long long)size, newptr, curr);
        else
            fprintf(stderr, PPREFIX
                    "realloc(%'lld -> %'lld) = %p -> %p   (current %'lld)\n",
                   (long long)oldsize, (long long)size, ptr, newptr, curr);
    }

    *(size_t*)newptr = size;

    return (char*)newptr + alignment;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x4d1b(%rip), %rax      # 0xe1a0
cmpq %rax, %rdi
jb 0x94d7
addq 0x4d07(%rip), %rax      # 0xe198
cmpq %r14, %rax
jb 0x94d7
leaq -0x10(%r14), %r15
movl $0xdeadc0de, %eax       # imm = 0xDEADC0DE
cmpq %rax, -0x8(%r14)
jne 0x954b
movq (%r15), %r12
cmpq %rbx, %r12
jae 0x9519
movq %rbx, %rdi
callq 0x92c8
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x21b0
movq %r14, %rdi
callq 0x939e
movq %rbx, %r14
jmp 0x9529
testq %rbx, %rbx
je 0x951e
testq %r14, %r14
je 0x9538
leaq -0x10(%r14), %r15
movl $0xdeadc0de, %eax       # imm = 0xDEADC0DE
cmpq %rax, -0x8(%r14)
jne 0x956b
movq (%r15), %rdi
callq 0x9422
movq %rbx, %rdi
callq 0x9360
leaq 0x10(%rbx), %rsi
movq %r15, %rdi
callq *0x104c9b(%rip)         # 0x10e1a8
movq %rax, %r14
movq %rbx, (%rax)
addq $0x10, %r14
jmp 0x9529
movq %rbx, (%r15)
jmp 0x9529
movq %r14, %rdi
callq 0x939e
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x92c8
movq 0x4aa6(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1645(%rip), %rsi      # 0xaba1
movq %r15, %rdx
xorl %eax, %eax
callq 0x2190
jmp 0x94a9
movq 0x4a86(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x15e3(%rip), %rsi      # 0xab5f
movq %r15, %rdx
xorl %eax, %eax
callq 0x2190
jmp 0x94f0