#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ assertEqual(size_t, int /*<<< orphan*/);
int /*<<< orphan*/ memoryUsage();
int /*<<< orphan*/ mfree(void *);
void *mmalloc(size_t);
void *mrealloc(void *, size_t);
int /*<<< orphan*/ msize(void *);
void testRealloc(void) {
  size_t s1 = 1024, s2 = 4096;
  void *ptr = mmalloc(s1);
  assertEqual(s1, msize(ptr));
  assertEqual(s1, memoryUsage());
  ptr = mrealloc(ptr, s2);
  assertEqual(s2, msize(ptr));
  assertEqual(s2, memoryUsage());
  mfree(ptr);
}


x86 assembly (clang, -O0):
<testRealloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x400,-0x8(%rbp)
movq   $0x1000,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <mmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <msize@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <memoryUsage@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mrealloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <msize@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <memoryUsage@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    -0x18(%rbp),%rdi
call   1070 <mfree@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testRealloc>:
push   %rbx
mov    $0x400,%edi
call   1040 <mmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x400,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x400,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    $0x1000,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x1000,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x1000,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1070 <mfree@plt>

LLVM IR (-O0):
define void @testRealloc() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i8*
store i64 1024, i64* %1
store i64 4096, i64* %2
%4 = load i64, i64* %1
%5 = call i8* @mmalloc(i64 %4)
store i8* %5, i8** %3
%6 = load i64, i64* %1
%7 = load i8*, i8** %3
%8 = call i32 @msize(i8* %7)
%9 = call i32 @assertEqual(i64 %6, i32 %8)
%10 = load i64, i64* %1
%11 = call i32 (...) @memoryUsage()
%12 = call i32 @assertEqual(i64 %10, i32 %11)
%13 = load i8*, i8** %3
%14 = load i64, i64* %2
%15 = call i8* @mrealloc(i8* %13, i64 %14)
store i8* %15, i8** %3
%16 = load i64, i64* %2
%17 = load i8*, i8** %3
%18 = call i32 @msize(i8* %17)
%19 = call i32 @assertEqual(i64 %16, i32 %18)
%20 = load i64, i64* %2
%21 = call i32 (...) @memoryUsage()
%22 = call i32 @assertEqual(i64 %20, i32 %21)
%23 = load i8*, i8** %3
%24 = call i32 @mfree(i8* %23)
ret void
}

LLVM IR (-O2):
define void @testRealloc() {
%1 = tail call i8* @mmalloc(i64 1024)
%2 = tail call i32 @msize(i8* %1)
%3 = tail call i32 @assertEqual(i64 1024, i32 %2)
%4 = tail call i32 (...) @memoryUsage()
%5 = tail call i32 @assertEqual(i64 1024, i32 %4)
%6 = tail call i8* @mrealloc(i8* %1, i64 4096)
%7 = tail call i32 @msize(i8* %6)
%8 = tail call i32 @assertEqual(i64 4096, i32 %7)
%9 = tail call i32 (...) @memoryUsage()
%10 = tail call i32 @assertEqual(i64 4096, i32 %9)
%11 = tail call i32 @mfree(i8* %6)
ret void
}