#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 *);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ printf(char *);
int main() {
  malloc(1);
  int i;
  void *ptr = NULL;
  for (i = 0; i < 6000; i++) {
    ptr = malloc(1024 * 1024);
    if (ptr == NULL) {
      printf("Memory failed to allocate!\n");
      return 1;
    }
    memset(ptr, 0xab, 1024 * 1024);
    free(ptr);
  }
  printf("Memory was allocated and freed!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x1770,-0x8(%rbp)
jge    11d0 <main+0x90>
mov    $0x100000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <main+0x66>
lea    0xe6b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e3 <main+0xa3>
mov    -0x10(%rbp),%rdi
mov    $0xab,%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1168 <main+0x28>
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1770,%ebx
lea    0xecd(%rip),%r14
lea    0xea6(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    $0x100000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1198 <main+0x58>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0xab,%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
add    $0xffffffff,%ebx
jne    1160 <main+0x20>
xor    %ebp,%ebp
mov    %r15,%r14
jmp    119d <main+0x5d>
mov    $0x1,%ebp
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Memory failed to allocate!\0A\00"
@.str.1 = [33 x i8] c"Memory was allocated and freed!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i8* @malloc(i32 1)
store i8* null, i8** %3
store i32 0, i32* %2
br label %5
5: ; preds = %19, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 6000
br i1 %7, label %8, label %22
8: ; preds = %5
%9 = call i8* @malloc(i32 1048576)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %24
14: ; preds = %8
%15 = load i8*, i8** %3
%16 = call i32 @memset(i8* %15, i32 171, i32 1048576)
%17 = load i8*, i8** %3
%18 = call i32 @free(i8* %17)
br label %19
19: ; preds = %14
%20 = load i32, i32* %2
%21 = add nsw i32 %20, 1
store i32 %21, i32* %2
br label %5
22: ; preds = %5
%23 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %24
24: ; preds = %22, %12
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@str = [32 x i8] c"Memory was allocated and freed!\00"
@str.2 = [27 x i8] c"Memory failed to allocate!\00"
define i32 @main() {
br label %1
1: ; preds = %0, %5
%2 = phi i32 [ 0, %0 ], [ %8, %5 ]
%3 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%4 = icmp eq i8* %3, null
br i1 %4, label %10, label %5
5: ; preds = %1
%6 = tail call i32 @memset(i8* %3, i32 171, i32 1048576)
%7 = tail call i32 @free(i8* %3)
%8 = add nuw nsw i32 %2, 1
%9 = icmp eq i32 %8, 6000
br i1 %9, label %10, label %1
10: ; preds = %5, %1
%11 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.2, i64 0, i64 0), %1 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0), %5 ]
%12 = phi i32 [ 1, %1 ], [ 0, %5 ]
%13 = tail call i32 @puts(i8* dereferenceable(1) %11)
ret i32 %12
}