// File: /workspace/github_c_makefile_star2_O3/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 0x5025(%rip), %rax      # 0xf1a0
cmpq %rax, %rdi
jb 0xa1d1
addq 0x5011(%rip), %rax      # 0xf198
cmpq %r14, %rax
jb 0xa1d1
leaq -0x10(%r14), %r15
movl $0xdeadc0de, %eax       # imm = 0xDEADC0DE
cmpq %rax, -0x8(%r14)
jne 0xa267
movq (%r15), %r12
cmpq %rbx, %r12
jae 0xa235
movq %rbx, %rdi
callq 0x9fe6
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x21b0
movq %r14, %rdi
callq 0xa09d
movq %rbx, %r14
jmp 0xa245
testq %rbx, %rbx
je 0xa23a
testq %r14, %r14
je 0xa254
leaq -0x10(%r14), %r15
movl $0xdeadc0de, %eax       # imm = 0xDEADC0DE
cmpq %rax, -0x8(%r14)
jne 0xa287
movq 0x4f7b(%rip), %rsi      # 0xf170
subq (%r15), %rsi
movq %rsi, 0x4f71(%rip)      # 0xf170
movq 0x4f7a(%rip), %rax      # 0xf180
testq %rax, %rax
je 0xa214
movq 0x4f76(%rip), %rdi      # 0xf188
callq *%rax
movq %rbx, %rdi
callq 0xa05f
leaq 0x10(%rbx), %rsi
movq %r15, %rdi
callq *0x104f7f(%rip)         # 0x10f1a8
movq %rax, %r14
movq %rbx, (%rax)
addq $0x10, %r14
jmp 0xa245
movq %rbx, (%r15)
jmp 0xa245
movq %r14, %rdi
callq 0xa09d
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 0x9fe6
movq 0x4d8a(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x1961(%rip), %rsi      # 0xbbd9
movq %r15, %rdx
xorl %eax, %eax
callq 0x2190
jmp 0xa19f
movq 0x4d6a(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x18ff(%rip), %rsi      # 0xbb97
movq %r15, %rdx
xorl %eax, %eax
callq 0x2190
jmp 0xa1ee