#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 */
typedef int /*<<< orphan*/ sizes;

/* Variables and functions */
int /*<<< orphan*/ assertEqual(size_t, int /*<<< orphan*/);
int /*<<< orphan*/ memoryUsage();
int /*<<< orphan*/ mfree(void *);
void *mmalloc(int);
void *mrealloc(void *, size_t);
int /*<<< orphan*/ msize(void *);
void testReallocMany(void) {
  void *ptr = mmalloc(1);
  size_t sizes[] = {1, 2, 4, 8, 36, 128, 512, 4096, 32864};
  for (int i = 0; i < (sizeof(sizes) / sizeof(sizes[0])); i++) {
    ptr = mrealloc(ptr, sizes[i]);
    assertEqual(sizes[i], msize(ptr));
    assertEqual(sizes[i], memoryUsage());
  }
  mfree(ptr);
}


x86 assembly (clang, -O0):
<testReallocMany>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    $0x1,%edi
call   1040 <mmalloc@plt>
mov    %rax,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe6f(%rip),%rsi
mov    $0x48,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmp    $0x9,%rax
jae    1214 <testReallocMany+0xa4>
mov    -0x8(%rbp),%rdi
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rsi
call   1030 <mrealloc@plt>
mov    %rax,-0x8(%rbp)
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    %rax,-0x68(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <msize@plt>
mov    -0x68(%rbp),%rdi
mov    %eax,%esi
call   1050 <assertEqual@plt>
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1070 <memoryUsage@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11a2 <testReallocMany+0x32>
mov    -0x8(%rbp),%rdi
call   1080 <mfree@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testReallocMany>:
push   %rbx
mov    $0x1,%edi
call   1040 <mmalloc@plt>
mov    $0x1,%esi
mov    %rax,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x1,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x1,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    $0x2,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x2,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x2,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    $0x4,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x4,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x4,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    $0x8,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x8,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x8,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    $0x24,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x24,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x24,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    $0x80,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x80,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x80,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    $0x200,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x200,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x200,%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    $0x8060,%esi
mov    %rbx,%rdi
call   1030 <mrealloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <msize@plt>
mov    $0x8060,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
xor    %eax,%eax
call   1060 <memoryUsage@plt>
mov    $0x8060,%edi
mov    %eax,%esi
call   1050 <assertEqual@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1070 <mfree@plt>

LLVM IR (-O0):
@__const.testReallocMany.sizes = [9 x i64] [i64 1, i64 2, i64 4, i64 8, i64 36, i64 128, i64 512, i64 4096, i64 32864]
define void @testReallocMany() {
%1 = alloca i8*
%2 = alloca [9 x i64]
%3 = alloca i32
%4 = call i8* @mmalloc(i32 1)
store i8* %4, i8** %1
%5 = bitcast [9 x i64]* %2 to i8*
store i32 0, i32* %3
br label %6
6: ; preds = %30, %0
%7 = load i32, i32* %3
%8 = sext i32 %7 to i64
%9 = icmp ult i64 %8, 9
br i1 %9, label %10, label %33
10: ; preds = %6
%11 = load i8*, i8** %1
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [9 x i64], [9 x i64]* %2, i64 0, i64 %13
%15 = load i64, i64* %14
%16 = call i8* @mrealloc(i8* %11, i64 %15)
store i8* %16, i8** %1
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [9 x i64], [9 x i64]* %2, i64 0, i64 %18
%20 = load i64, i64* %19
%21 = load i8*, i8** %1
%22 = call i32 @msize(i8* %21)
%23 = call i32 @assertEqual(i64 %20, i32 %22)
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [9 x i64], [9 x i64]* %2, i64 0, i64 %25
%27 = load i64, i64* %26
%28 = call i32 (...) @memoryUsage()
%29 = call i32 @assertEqual(i64 %27, i32 %28)
br label %30
30: ; preds = %10
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %6
33: ; preds = %6
%34 = load i8*, i8** %1
%35 = call i32 @mfree(i8* %34)
ret void
}

LLVM IR (-O2):
define void @testReallocMany() {
%1 = tail call i8* @mmalloc(i32 1)
%2 = tail call i8* @mrealloc(i8* %1, i64 1)
%3 = tail call i32 @msize(i8* %2)
%4 = tail call i32 @assertEqual(i64 1, i32 %3)
%5 = tail call i32 (...) @memoryUsage()
%6 = tail call i32 @assertEqual(i64 1, i32 %5)
%7 = tail call i8* @mrealloc(i8* %2, i64 2)
%8 = tail call i32 @msize(i8* %7)
%9 = tail call i32 @assertEqual(i64 2, i32 %8)
%10 = tail call i32 (...) @memoryUsage()
%11 = tail call i32 @assertEqual(i64 2, i32 %10)
%12 = tail call i8* @mrealloc(i8* %7, i64 4)
%13 = tail call i32 @msize(i8* %12)
%14 = tail call i32 @assertEqual(i64 4, i32 %13)
%15 = tail call i32 (...) @memoryUsage()
%16 = tail call i32 @assertEqual(i64 4, i32 %15)
%17 = tail call i8* @mrealloc(i8* %12, i64 8)
%18 = tail call i32 @msize(i8* %17)
%19 = tail call i32 @assertEqual(i64 8, i32 %18)
%20 = tail call i32 (...) @memoryUsage()
%21 = tail call i32 @assertEqual(i64 8, i32 %20)
%22 = tail call i8* @mrealloc(i8* %17, i64 36)
%23 = tail call i32 @msize(i8* %22)
%24 = tail call i32 @assertEqual(i64 36, i32 %23)
%25 = tail call i32 (...) @memoryUsage()
%26 = tail call i32 @assertEqual(i64 36, i32 %25)
%27 = tail call i8* @mrealloc(i8* %22, i64 128)
%28 = tail call i32 @msize(i8* %27)
%29 = tail call i32 @assertEqual(i64 128, i32 %28)
%30 = tail call i32 (...) @memoryUsage()
%31 = tail call i32 @assertEqual(i64 128, i32 %30)
%32 = tail call i8* @mrealloc(i8* %27, i64 512)
%33 = tail call i32 @msize(i8* %32)
%34 = tail call i32 @assertEqual(i64 512, i32 %33)
%35 = tail call i32 (...) @memoryUsage()
%36 = tail call i32 @assertEqual(i64 512, i32 %35)
%37 = tail call i8* @mrealloc(i8* %32, i64 4096)
%38 = tail call i32 @msize(i8* %37)
%39 = tail call i32 @assertEqual(i64 4096, i32 %38)
%40 = tail call i32 (...) @memoryUsage()
%41 = tail call i32 @assertEqual(i64 4096, i32 %40)
%42 = tail call i8* @mrealloc(i8* %37, i64 32864)
%43 = tail call i32 @msize(i8* %42)
%44 = tail call i32 @assertEqual(i64 32864, i32 %43)
%45 = tail call i32 (...) @memoryUsage()
%46 = tail call i32 @assertEqual(i64 32864, i32 %45)
%47 = tail call i32 @mfree(i8* %42)
ret void
}