#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*/ compute(int);
int /*<<< orphan*/ debug(char *, int, int, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *realloc(char *, int);
void test_realloc() {
  int i, j;
  char *buffer[10];
  for (i = 0; i < 10; i++) {
    int alloc_size = (1 + i) * 1024;
    debug("\tloop %d/%d: allocating %d bytes\n", i, 10, alloc_size);
    buffer[i] = malloc(alloc_size);
    for (j = 0; j < (1 + i) * 1024; j++) {
      buffer[i][j] = 'a';
    }
    compute(20000);
    alloc_size *= 2;
    debug("\t\tloop %d/%d: reallocating %d bytes\n", i, 10, alloc_size);
    buffer[i] = realloc(buffer[i], alloc_size);
    compute(20000);
    free(buffer[i]);
    compute(10000);
  }
}


x86 assembly (clang, -O0):
<test_realloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0xa,-0x4(%rbp)
jge    1251 <test_realloc+0x101>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
shl    $0xa,%eax
mov    %eax,-0x64(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x64(%rbp),%ecx
lea    0xe7e(%rip),%rdi
mov    $0xa,%edx
call   1040 <debug@plt>
mov    -0x64(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
movslq -0x4(%rbp),%rax
mov    %rcx,-0x60(%rbp,%rax,8)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x4(%rbp),%ecx
add    $0x1,%ecx
shl    $0xa,%ecx
cmp    %ecx,%eax
jge    11da <test_realloc+0x8a>
movslq -0x4(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rax
movslq -0x8(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a7 <test_realloc+0x57>
mov    $0x4e20,%edi
call   1070 <compute@plt>
mov    -0x64(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x64(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x64(%rbp),%ecx
lea    0xe28(%rip),%rdi
mov    $0xa,%edx
call   1040 <debug@plt>
movslq -0x4(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rdi
mov    -0x64(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
movslq -0x4(%rbp),%rax
mov    %rcx,-0x60(%rbp,%rax,8)
mov    $0x4e20,%edi
call   1070 <compute@plt>
movslq -0x4(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rdi
call   1030 <free@plt>
mov    $0x2710,%edi
call   1070 <compute@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    115f <test_realloc+0xf>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_realloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x400,%ebx
mov    $0x800,%r12d
xor    %ebp,%ebp
lea    0xea3(%rip),%r15
nop
lea    0x1(%rbp),%r14
lea    0xe75(%rip),%rdi
mov    %ebp,%esi
mov    $0xa,%edx
mov    %ebx,%ecx
call   1040 <debug@plt>
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x61,%esi
mov    %rbx,%rdx
call   1050 <memset@plt>
mov    $0x4e20,%edi
call   1080 <compute@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0xa,%edx
mov    %r12d,%ecx
call   1040 <debug@plt>
mov    %r13,%rdi
mov    %r12d,%esi
call   1070 <realloc@plt>
mov    %rax,%rbp
mov    $0x4e20,%edi
call   1080 <compute@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0x2710,%edi
call   1080 <compute@plt>
add    $0x400,%rbx
add    $0x800,%r12d
mov    %r14,%rbp
cmp    $0xa,%r14
jne    1180 <test_realloc+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"\09loop %d/%d: allocating %d bytes\0A\00"
@.str.1 = [37 x i8] c"\09\09loop %d/%d: reallocating %d bytes\0A\00"
define void @test_realloc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [10 x i8*]
%4 = alloca i32
store i32 0, i32* %1
br label %5
5: ; preds = %60, %0
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 10
br i1 %7, label %8, label %63
8: ; preds = %5
%9 = load i32, i32* %1
%10 = add nsw i32 1, %9
%11 = mul nsw i32 %10, 1024
store i32 %11, i32* %4
%12 = load i32, i32* %1
%13 = load i32, i32* %4
%14 = call i32 @debug(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %12, i32 10, i32 %13)
%15 = load i32, i32* %4
%16 = call i8* @malloc(i32 %15)
%17 = load i32, i32* %1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %18
store i8* %16, i8** %19
store i32 0, i32* %2
br label %20
20: ; preds = %34, %8
%21 = load i32, i32* %2
%22 = load i32, i32* %1
%23 = add nsw i32 1, %22
%24 = mul nsw i32 %23, 1024
%25 = icmp slt i32 %21, %24
br i1 %25, label %26, label %37
26: ; preds = %20
%27 = load i32, i32* %1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = load i32, i32* %2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 97, i8* %33
br label %34
34: ; preds = %26
%35 = load i32, i32* %2
%36 = add nsw i32 %35, 1
store i32 %36, i32* %2
br label %20
37: ; preds = %20
%38 = call i32 @compute(i32 20000)
%39 = load i32, i32* %4
%40 = mul nsw i32 %39, 2
store i32 %40, i32* %4
%41 = load i32, i32* %1
%42 = load i32, i32* %4
%43 = call i32 @debug(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %41, i32 10, i32 %42)
%44 = load i32, i32* %1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %45
%47 = load i8*, i8** %46
%48 = load i32, i32* %4
%49 = call i8* @realloc(i8* %47, i32 %48)
%50 = load i32, i32* %1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %51
store i8* %49, i8** %52
%53 = call i32 @compute(i32 20000)
%54 = load i32, i32* %1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [10 x i8*], [10 x i8*]* %3, i64 0, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 @free(i8* %57)
%59 = call i32 @compute(i32 10000)
br label %60
60: ; preds = %37
%61 = load i32, i32* %1
%62 = add nsw i32 %61, 1
store i32 %62, i32* %1
br label %5
63: ; preds = %5
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"\09loop %d/%d: allocating %d bytes\0A\00"
@.str.1 = [37 x i8] c"\09\09loop %d/%d: reallocating %d bytes\0A\00"
define void @test_realloc() {
br label %1
1: ; preds = %0, %1
%2 = phi i64 [ 0, %0 ], [ %6, %1 ]
%3 = trunc i64 %2 to i32
%4 = shl nuw nsw i64 %2, 10
%5 = add nuw nsw i64 %4, 1024
%6 = add nuw nsw i64 %2, 1
%7 = trunc i64 %6 to i32
%8 = shl nuw nsw i32 %7, 10
%9 = tail call i32 @debug(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %3, i32 10, i32 %8)
%10 = tail call i8* @malloc(i32 %8)
%11 = tail call i32 @compute(i32 20000)
%12 = shl nsw i32 %7, 11
%13 = tail call i32 @debug(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 10, i32 %12)
%14 = tail call i8* @realloc(i8* %10, i32 %12)
%15 = tail call i32 @compute(i32 20000)
%16 = tail call i32 @free(i8* %14)
%17 = tail call i32 @compute(i32 10000)
%18 = icmp eq i64 %6, 10
br i1 %18, label %19, label %1
19: ; preds = %1
ret void
}