#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*/ exit(int);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  (void)argc;
  (void)argv;
  printf("a3 malloc \n");
  int *ar;
  ar = malloc(sizeof(int) * 2000);
  if (ar == NULL) {
    printf("malloc did not work \n");
    exit(1);
  }
  for (int i = 0; i < 2000; i++) {
    ar[i] = i;
  }
  for (int i = 0; i < 2000; i++) {
    printf("%d \n", ar[i]);
  }
  free(ar);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1f40,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1195 <main+0x55>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x7d0,-0x1c(%rbp)
jge    11c5 <main+0x85>
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119c <main+0x5c>
movl   $0x0,-0x20(%rbp)
cmpl   $0x7d0,-0x20(%rbp)
jge    1200 <main+0xc0>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11cc <main+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xf59(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1f40,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12cb <main+0x17b>
mov    %rax,%r15
movdqa 0xe81(%rip),%xmm0
mov    $0x24,%eax
movdqa 0xe83(%rip),%xmm8
movdqa 0xe8a(%rip),%xmm9
movdqa 0xe91(%rip),%xmm10
movdqa 0xe98(%rip),%xmm11
movdqa 0xe9f(%rip),%xmm12
movdqa 0xea7(%rip),%xmm6
movdqa 0xeaf(%rip),%xmm7
movdqa 0xeb7(%rip),%xmm1
movdqa 0xebf(%rip),%xmm2
movdqa 0xec7(%rip),%xmm3
nopl   0x0(%rax)
movdqa %xmm0,%xmm4
paddd  %xmm8,%xmm4
movdqu %xmm0,-0x90(%r15,%rax,4)
ff ff ff
movdqu %xmm4,-0x80(%r15,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm9,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm10,%xmm5
movdqu %xmm4,-0x70(%r15,%rax,4)
movdqu %xmm5,-0x60(%r15,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm11,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm12,%xmm5
movdqu %xmm4,-0x50(%r15,%rax,4)
movdqu %xmm5,-0x40(%r15,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm6,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm7,%xmm5
movdqu %xmm4,-0x30(%r15,%rax,4)
movdqu %xmm5,-0x20(%r15,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm1,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm2,%xmm5
movdqu %xmm4,-0x10(%r15,%rax,4)
movdqu %xmm5,(%r15,%rax,4)
paddd  %xmm3,%xmm0
add    $0x28,%rax
cmp    $0x7f4,%rax
jne    11e0 <main+0x90>
lea    0xe20(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15,%rbx,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    $0x7d0,%rbx
jne    12a0 <main+0x150>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdee(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str.2 = [5 x i8] c"%d \0A\00"
@str = [11 x i8] c"a3 malloc \00"
@str.3 = [21 x i8] c"malloc did not work \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%4 = tail call i32* @malloc(i32 8000)
%5 = icmp eq i32* %4, null
br i1 %5, label %45, label %6
6: ; preds = %2, %6
%7 = phi i64 [ %42, %6 ], [ 0, %2 ]
%8 = phi <4 x i32> [ %43, %6 ], [ <i32 0, i32 1, i32 2, i32 3>, %2 ]
%9 = add <4 x i32> %8, <i32 4, i32 4, i32 4, i32 4>
%10 = getelementptr inbounds i32, i32* %4, i64 %7
%11 = bitcast i32* %10 to <4 x i32>*
store <4 x i32> %8, <4 x i32>* %11
%12 = getelementptr inbounds i32, i32* %10, i64 4
%13 = bitcast i32* %12 to <4 x i32>*
store <4 x i32> %9, <4 x i32>* %13
%14 = add nuw nsw i64 %7, 8
%15 = add <4 x i32> %8, <i32 8, i32 8, i32 8, i32 8>
%16 = add <4 x i32> %8, <i32 12, i32 12, i32 12, i32 12>
%17 = getelementptr inbounds i32, i32* %4, i64 %14
%18 = bitcast i32* %17 to <4 x i32>*
store <4 x i32> %15, <4 x i32>* %18
%19 = getelementptr inbounds i32, i32* %17, i64 4
%20 = bitcast i32* %19 to <4 x i32>*
store <4 x i32> %16, <4 x i32>* %20
%21 = add nuw nsw i64 %7, 16
%22 = add <4 x i32> %8, <i32 16, i32 16, i32 16, i32 16>
%23 = add <4 x i32> %8, <i32 20, i32 20, i32 20, i32 20>
%24 = getelementptr inbounds i32, i32* %4, i64 %21
%25 = bitcast i32* %24 to <4 x i32>*
store <4 x i32> %22, <4 x i32>* %25
%26 = getelementptr inbounds i32, i32* %24, i64 4
%27 = bitcast i32* %26 to <4 x i32>*
store <4 x i32> %23, <4 x i32>* %27
%28 = add nuw nsw i64 %7, 24
%29 = add <4 x i32> %8, <i32 24, i32 24, i32 24, i32 24>
%30 = add <4 x i32> %8, <i32 28, i32 28, i32 28, i32 28>
%31 = getelementptr inbounds i32, i32* %4, i64 %28
%32 = bitcast i32* %31 to <4 x i32>*
store <4 x i32> %29, <4 x i32>* %32
%33 = getelementptr inbounds i32, i32* %31, i64 4
%34 = bitcast i32* %33 to <4 x i32>*
store <4 x i32> %30, <4 x i32>* %34
%35 = add nuw nsw i64 %7, 32
%36 = add <4 x i32> %8, <i32 32, i32 32, i32 32, i32 32>
%37 = add <4 x i32> %8, <i32 36, i32 36, i32 36, i32 36>
%38 = getelementptr inbounds i32, i32* %4, i64 %35
%39 = bitcast i32* %38 to <4 x i32>*
store <4 x i32> %36, <4 x i32>* %39
%40 = getelementptr inbounds i32, i32* %38, i64 4
%41 = bitcast i32* %40 to <4 x i32>*
store <4 x i32> %37, <4 x i32>* %41
%42 = add nuw nsw i64 %7, 40
%43 = add <4 x i32> %8, <i32 40, i32 40, i32 40, i32 40>
%44 = icmp eq i64 %42, 2000
br i1 %44, label %50, label %6
45: ; preds = %2
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.3, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %50
%49 = tail call i32 @free(i32* %4)
ret i32 0
50: ; preds = %6, %50
%51 = phi i64 [ %55, %50 ], [ 0, %6 ]
%52 = getelementptr inbounds i32, i32* %4, i64 %51
%53 = load i32, i32* %52
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %53)
%55 = add nuw nsw i64 %51, 1
%56 = icmp eq i64 %55, 2000
br i1 %56, label %48, label %50
}