// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq 0x4539(%rip), %rax      # 0x151a0
cmpq %rax, -0x10(%rbp)
jb 0x10d34
movq -0x10(%rbp), %rax
leaq 0x4524(%rip), %rcx      # 0x151a0
addq 0x4515(%rip), %rcx      # 0x15198
cmpq %rcx, %rax
ja 0x10d34
movq -0x10(%rbp), %rax
addq $-0x10, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl $0xdeadc0de, %ecx       # imm = 0xDEADC0DE
cmpq %rcx, 0x8(%rax)
je 0x10cc3
movq 0x434a(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x1065(%rip), %rsi      # 0x11d21
movb $0x0, %al
callq 0x2190
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x18(%rbp), %rax
jb 0x10cf4
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x8(%rbp)
jmp 0x10dee
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x10(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10960
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x21b0
movq -0x10(%rbp), %rdi
callq 0x10ad0
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x10dee
cmpq $0x0, -0x18(%rbp)
jne 0x10d51
movq -0x10(%rbp), %rdi
callq 0x10ad0
movq $0x0, -0x8(%rbp)
jmp 0x10dee
cmpq $0x0, -0x10(%rbp)
jne 0x10d6a
movq -0x18(%rbp), %rdi
callq 0x10960
movq %rax, -0x8(%rbp)
jmp 0x10dee
movq -0x10(%rbp), %rax
addq $-0x10, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl $0xdeadc0de, %ecx       # imm = 0xDEADC0DE
cmpq %rcx, 0x8(%rax)
je 0x10da1
movq 0x426c(%rip), %rax      # 0x14ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0xf45(%rip), %rsi       # 0x11cdf
movb $0x0, %al
callq 0x2190
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x10ba0
movq -0x18(%rbp), %rdi
callq 0x10a60
movq 0x1043e3(%rip), %rax    # 0x1151a8
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x10, %rsi
callq *%rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)