#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 /*<<< orphan*/ printf(char *, int);
int *realloc(int *, int);
int main(void) {
  int *pa = malloc(10 * sizeof *pa);
  if (pa) {
    printf("%zu bytes allocated. Storing ints: ", 10 * sizeof(int));
    for (int n = 0; n < 10; ++n)
      printf("%d ", pa[n] = n);
  }
  int *pb = realloc(pa, 1000000 * sizeof *pb);
  if (pb) {
    printf("\n%zu bytes allocated, first 10 ints are: ", 1000000 * sizeof(int));
    for (int n = 0; n < 10; ++n)
      printf("%d ", pb[n]);
    free(pb);
  } else {
    free(pa);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x28,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b7 <main+0x77>
lea    0xe91(%rip),%rdi
mov    $0x28,%esi
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    11b2 <main+0x72>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %esi,(%rax,%rcx,4)
lea    0xe85(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1180 <main+0x40>
jmp    11b7 <main+0x77>
mov    -0x10(%rbp),%rdi
mov    $0x3d0900,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1229 <main+0xe9>
lea    0xe4d(%rip),%rdi
mov    $0x3d0900,%esi
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    121b <main+0xdb>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ec <main+0xac>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1232 <main+0xf2>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x28,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1240 <main+0x100>
lea    0xe9f(%rip),%rdi
mov    $0x28,%esi
call   1040 <printf@plt>
movl   $0x0,(%r14)
lea    0xeab(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
movl   $0x1,0x4(%r14)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
movl   $0x2,0x8(%r14)
mov    %rbx,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
movl   $0x3,0xc(%r14)
mov    %rbx,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
movl   $0x4,0x10(%r14)
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
movl   $0x5,0x14(%r14)
mov    %rbx,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
movl   $0x6,0x18(%r14)
mov    %rbx,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
movl   $0x7,0x1c(%r14)
mov    %rbx,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
movl   $0x8,0x20(%r14)
mov    %rbx,%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
movl   $0x9,0x24(%r14)
mov    %rbx,%rdi
mov    $0x9,%esi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x3d0900,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     12e3 <main+0x1a3>
mov    %rax,%rbx
lea    0xdc8(%rip),%rdi
mov    $0x3d0900,%esi
call   1040 <printf@plt>
mov    (%rbx),%esi
lea    0xdb1(%rip),%r14
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x8(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0xc(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x10(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x14(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x18(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x1c(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x20(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    0x24(%rbx),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
jmp    12e6 <main+0x1a6>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [36 x i8] c"%zu bytes allocated. Storing ints: \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [42 x i8] c"\0A%zu bytes allocated, first 10 ints are: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32* @malloc(i32 40)
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %25
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 40)
store i32 0, i32* %3
br label %11
11: ; preds = %21, %9
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 10
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = load i32, i32* %3
%16 = load i32*, i32** %2
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
store i32 %15, i32* %19
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %21
21: ; preds = %14
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
store i32 %23, i32* %3
br label %11
24: ; preds = %11
br label %25
25: ; preds = %24, %0
%26 = load i32*, i32** %2
%27 = call i32* @realloc(i32* %26, i32 4000000)
store i32* %27, i32** %4
%28 = load i32*, i32** %4
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %48
30: ; preds = %25
%31 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 4000000)
store i32 0, i32* %5
br label %32
32: ; preds = %42, %30
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 10
br i1 %34, label %35, label %45
35: ; preds = %32
%36 = load i32*, i32** %4
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %35
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %32
45: ; preds = %32
%46 = load i32*, i32** %4
%47 = call i32 @free(i32* %46)
br label %51
48: ; preds = %25
%49 = load i32*, i32** %2
%50 = call i32 @free(i32* %49)
br label %51
51: ; preds = %48, %45
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str = [36 x i8] c"%zu bytes allocated. Storing ints: \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [42 x i8] c"\0A%zu bytes allocated, first 10 ints are: \00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 40)
%2 = icmp eq i32* %1, null
br i1 %2, label %24, label %3
3: ; preds = %0
%4 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 40)
store i32 0, i32* %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 0)
%6 = getelementptr inbounds i32, i32* %1, i64 1
store i32 1, i32* %6
%7 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 1)
%8 = getelementptr inbounds i32, i32* %1, i64 2
store i32 2, i32* %8
%9 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 2)
%10 = getelementptr inbounds i32, i32* %1, i64 3
store i32 3, i32* %10
%11 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%12 = getelementptr inbounds i32, i32* %1, i64 4
store i32 4, i32* %12
%13 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%14 = getelementptr inbounds i32, i32* %1, i64 5
store i32 5, i32* %14
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 5)
%16 = getelementptr inbounds i32, i32* %1, i64 6
store i32 6, i32* %16
%17 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 6)
%18 = getelementptr inbounds i32, i32* %1, i64 7
store i32 7, i32* %18
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 7)
%20 = getelementptr inbounds i32, i32* %1, i64 8
store i32 8, i32* %20
%21 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 8)
%22 = getelementptr inbounds i32, i32* %1, i64 9
store i32 9, i32* %22
%23 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 9)
br label %24
24: ; preds = %3, %0
%25 = tail call i32* @realloc(i32* %1, i32 4000000)
%26 = icmp eq i32* %25, null
br i1 %26, label %59, label %27
27: ; preds = %24
%28 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 4000000)
%29 = load i32, i32* %25
%30 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds i32, i32* %25, i64 1
%32 = load i32, i32* %31
%33 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = getelementptr inbounds i32, i32* %25, i64 2
%35 = load i32, i32* %34
%36 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds i32, i32* %25, i64 3
%38 = load i32, i32* %37
%39 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds i32, i32* %25, i64 4
%41 = load i32, i32* %40
%42 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds i32, i32* %25, i64 5
%44 = load i32, i32* %43
%45 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds i32, i32* %25, i64 6
%47 = load i32, i32* %46
%48 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = getelementptr inbounds i32, i32* %25, i64 7
%50 = load i32, i32* %49
%51 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %50)
%52 = getelementptr inbounds i32, i32* %25, i64 8
%53 = load i32, i32* %52
%54 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %53)
%55 = getelementptr inbounds i32, i32* %25, i64 9
%56 = load i32, i32* %55
%57 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %56)
%58 = tail call i32 @free(i32* %25)
br label %61
59: ; preds = %24
%60 = tail call i32 @free(i32* %1)
br label %61
61: ; preds = %59, %27
ret i32 0
}