#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(int *);
int *malloc(int);
int *realloc(int *, int);
int /*<<< orphan*/ test_assert(int);
int main(void) {
  int *p = malloc(4);
  if (!p)
    return 0;
  for (int i = sizeof(int); i < 10; ++i) {
    p = realloc(p, i);
    if (!p)
      return 0;
    *p = i;
  }
  test_assert(*p == 9);
  free(p);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <main+0x34>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <main+0xa0>
movl   $0x4,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    11c3 <main+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ac <main+0x6c>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <main+0xa0>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117b <main+0x3b>
mov    -0x10(%rbp),%rax
cmpl   $0x9,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <test_assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x4,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11fd <main+0xbd>
mov    %rax,%rdi
mov    $0x4,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11fd <main+0xbd>
movl   $0x4,(%rax)
mov    %rax,%rdi
mov    $0x5,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11fd <main+0xbd>
movl   $0x5,(%rax)
mov    %rax,%rdi
mov    $0x6,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11fd <main+0xbd>
movl   $0x6,(%rax)
mov    %rax,%rdi
mov    $0x7,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11fd <main+0xbd>
movl   $0x7,(%rax)
mov    %rax,%rdi
mov    $0x8,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11fd <main+0xbd>
movl   $0x8,(%rax)
mov    %rax,%rdi
mov    $0x9,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11fd <main+0xbd>
mov    %rax,%rbx
movl   $0x9,(%rax)
mov    $0x1,%edi
call   1060 <test_assert@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32* @malloc(i32 4)
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %0
store i32 0, i32* %1
br label %33
8: ; preds = %0
store i32 4, i32* %3
br label %9
9: ; preds = %22, %8
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = load i32*, i32** %2
%14 = load i32, i32* %3
%15 = call i32* @realloc(i32* %13, i32 %14)
store i32* %15, i32** %2
%16 = load i32*, i32** %2
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %12
store i32 0, i32* %1
br label %33
19: ; preds = %12
%20 = load i32, i32* %3
%21 = load i32*, i32** %2
store i32 %20, i32* %21
br label %22
22: ; preds = %19
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %9
25: ; preds = %9
%26 = load i32*, i32** %2
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 9
%29 = zext i1 %28 to i32
%30 = call i32 @test_assert(i32 %29)
%31 = load i32*, i32** %2
%32 = call i32 @free(i32* %31)
br label %33
33: ; preds = %25, %18, %7
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
define i32 @main() {
%1 = tail call i32* @malloc(i32 4)
%2 = icmp eq i32* %1, null
br i1 %2, label %24, label %3
3: ; preds = %0
%4 = tail call i32* @realloc(i32* %1, i32 4)
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %3
store i32 4, i32* %4
%7 = tail call i32* @realloc(i32* %4, i32 5)
%8 = icmp eq i32* %7, null
br i1 %8, label %24, label %9
9: ; preds = %6
store i32 5, i32* %7
%10 = tail call i32* @realloc(i32* %7, i32 6)
%11 = icmp eq i32* %10, null
br i1 %11, label %24, label %12
12: ; preds = %9
store i32 6, i32* %10
%13 = tail call i32* @realloc(i32* %10, i32 7)
%14 = icmp eq i32* %13, null
br i1 %14, label %24, label %15
15: ; preds = %12
store i32 7, i32* %13
%16 = tail call i32* @realloc(i32* %13, i32 8)
%17 = icmp eq i32* %16, null
br i1 %17, label %24, label %18
18: ; preds = %15
store i32 8, i32* %16
%19 = tail call i32* @realloc(i32* %16, i32 9)
%20 = icmp eq i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %18
store i32 9, i32* %19
%22 = tail call i32 @test_assert(i32 1)
%23 = tail call i32 @free(i32* %19)
br label %24
24: ; preds = %3, %6, %9, %12, %15, %18, %0, %21
ret i32 0
}