#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*/ free(void *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t, void *, size_t);
void *realloc(void *, size_t);
int main(void) {
  void *p = NULL;
  void *pnew = NULL;
  size_t s = 1024;
  for (size_t i = 0; i < 100; i++) {
    pnew = realloc(p, s);
    if (!pnew) {
      perror("realloc");
      free(p);
      return 0;
    }
    p = pnew;
    printf("%zu: p = %p  s = %zu\n", i, p, s);
    s *= 2;
  }
  free(p);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x400,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x64,-0x28(%rbp)
jae    11f4 <main+0xb4>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <main+0x77>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1204 <main+0xc4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116f <main+0x2f>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    $0x400,%r14d
xor    %ebx,%ebx
lea    0xeb0(%rip),%r15
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r14,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     11a8 <main+0x68>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %r14,%rcx
call   1040 <printf@plt>
add    %r14,%r14
add    $0x1,%rbx
mov    %r12,%r13
cmp    $0x64,%rbx
jne    1160 <main+0x20>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
mov    %r13,%rdi
jmp    1197 <main+0x57>

LLVM IR (-O0):
@.str = [8 x i8] c"realloc\00"
@.str.1 = [22 x i8] c"%zu: p = %p s = %zu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i32 0, i32* %1
store i8* null, i8** %2
store i8* null, i8** %3
store i64 1024, i64* %4
store i64 0, i64* %5
br label %6
6: ; preds = %27, %0
%7 = load i64, i64* %5
%8 = icmp ult i64 %7, 100
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = load i8*, i8** %2
%11 = load i64, i64* %4
%12 = call i8* @realloc(i8* %10, i64 %11)
store i8* %12, i8** %3
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %2
%18 = call i32 @free(i8* %17)
store i32 0, i32* %1
br label %33
19: ; preds = %9
%20 = load i8*, i8** %3
store i8* %20, i8** %2
%21 = load i64, i64* %5
%22 = load i8*, i8** %2
%23 = load i64, i64* %4
%24 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 %21, i8* %22, i64 %23)
%25 = load i64, i64* %4
%26 = mul i64 %25, 2
store i64 %26, i64* %4
br label %27
27: ; preds = %19
%28 = load i64, i64* %5
%29 = add i64 %28, 1
store i64 %29, i64* %5
br label %6
30: ; preds = %6
%31 = load i8*, i8** %2
%32 = call i32 @free(i8* %31)
store i32 0, i32* %1
br label %33
33: ; preds = %30, %15
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [8 x i8] c"realloc\00"
@.str.1 = [22 x i8] c"%zu: p = %p s = %zu\0A\00"
define i32 @main() {
br label %1
1: ; preds = %0, %10
%2 = phi i64 [ 0, %0 ], [ %13, %10 ]
%3 = phi i64 [ 1024, %0 ], [ %12, %10 ]
%4 = phi i8* [ null, %0 ], [ %5, %10 ]
%5 = tail call i8* @realloc(i8* %4, i64 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @free(i8* %4)
br label %17
10: ; preds = %1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 %2, i8* %5, i64 %3)
%12 = shl i64 %3, 1
%13 = add nuw nsw i64 %2, 1
%14 = icmp eq i64 %13, 100
br i1 %14, label %15, label %1
15: ; preds = %10
%16 = tail call i32 @free(i8* %5)
br label %17
17: ; preds = %7, %15
ret i32 0
}