// File: /workspace/github_c_makefile_star2_O1/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 0x5039(%rip), %rax      # 0xf1a0
cmpq %rax, %rdi
jb 0xa1bd
addq 0x5025(%rip), %rax      # 0xf198
cmpq %r14, %rax
jb 0xa1bd
leaq -0x10(%r14), %r15
movl $0xdeadc0de, %eax       # imm = 0xDEADC0DE
cmpq %rax, -0x8(%r14)
jne 0xa253
movq (%r15), %r12
cmpq %rbx, %r12
jae 0xa221
movq %rbx, %rdi
callq 0x9fd2
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x21b0
movq %r14, %rdi
callq 0xa089
movq %rbx, %r14
jmp 0xa231
testq %rbx, %rbx
je 0xa226
testq %r14, %r14
je 0xa240
leaq -0x10(%r14), %r15
movl $0xdeadc0de, %eax       # imm = 0xDEADC0DE
cmpq %rax, -0x8(%r14)
jne 0xa273
movq 0x4f8f(%rip), %rsi      # 0xf170
subq (%r15), %rsi
movq %rsi, 0x4f85(%rip)      # 0xf170
movq 0x4f8e(%rip), %rax      # 0xf180
testq %rax, %rax
je 0xa200
movq 0x4f8a(%rip), %rdi      # 0xf188
callq *%rax
movq %rbx, %rdi
callq 0xa04b
leaq 0x10(%rbx), %rsi
movq %r15, %rdi
callq *0x104f93(%rip)         # 0x10f1a8
movq %rax, %r14
movq %rbx, (%rax)
addq $0x10, %r14
jmp 0xa231
movq %rbx, (%r15)
jmp 0xa231
movq %r14, %rdi
callq 0xa089
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 0x9fd2
movq 0x4d9e(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x1945(%rip), %rsi      # 0xbba9
movq %r15, %rdx
xorl %eax, %eax
callq 0x2190
jmp 0xa18b
movq 0x4d7e(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x18e3(%rip), %rsi      # 0xbb67
movq %r15, %rdx
xorl %eax, %eax
callq 0x2190
jmp 0xa1da