#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 atoi(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  int max = -1;
  int mb = 0;
  char *buffer;
  if (argc > 1)
    max = atoi(argv[1]);
  while ((buffer = malloc(1024 * 1024)) != NULL && mb != max) {
    memset(buffer, 0, 1024 * 1024);
    mb++;
    printf("Allocated %d MB\n", mb);
    if (mb > 3708)
      sleep(600);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    118e <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    1193 <main+0x43>
mov    $0x100000,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     11bf <main+0x6f>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11cf <main+0x7f>
jmp    1213 <main+0xc3>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0xe7c,-0x18(%rbp)
jle    120e <main+0xbe>
mov    $0x258,%edi
call   1070 <sleep@plt>
jmp    1193 <main+0x43>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
cmp    $0x2,%edi
jl     116e <main+0x1e>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%r15d
mov    $0x100000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d7 <main+0x87>
test   %r15d,%r15d
je     11d7 <main+0x87>
mov    $0x1,%ebx
lea    0xe72(%rip),%r14
xchg   %ax,%ax
lea    -0x1(%rbx),%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
cmp    $0xe7c,%ebp
jb     11be <main+0x6e>
mov    $0x258,%edi
call   1070 <sleep@plt>
mov    $0x100000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d7 <main+0x87>
lea    0x1(%rbx),%ecx
cmp    %ebx,%r15d
mov    %ecx,%ebx
jne    1190 <main+0x40>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Allocated %d MB\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %7
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %6
br label %16
16: ; preds = %11, %2
br label %17
17: ; preds = %37, %16
%18 = call i8* @malloc(i32 1048576)
store i8* %18, i8** %8
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* %7
%22 = load i32, i32* %6
%23 = icmp ne i32 %21, %22
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %26, label %38
26: ; preds = %24
%27 = load i8*, i8** %8
%28 = call i32 @memset(i8* %27, i32 0, i32 1048576)
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %7
%34 = icmp sgt i32 %33, 3708
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 @sleep(i32 600)
br label %37
37: ; preds = %35, %26
br label %17
38: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"Allocated %d MB\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
br label %8
8: ; preds = %4, %2
%9 = phi i32 [ %7, %4 ], [ -1, %2 ]
%10 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%11 = icmp ne i8* %10, null
%12 = icmp ne i32 %9, 0
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %14, label %28
14: ; preds = %8, %23
%15 = phi i8* [ %24, %23 ], [ %10, %8 ]
%16 = phi i32 [ %18, %23 ], [ 0, %8 ]
%17 = tail call i32 @memset(i8* %15, i32 0, i32 1048576)
%18 = add nuw nsw i32 %16, 1
%19 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp ugt i32 %16, 3707
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 @sleep(i32 600)
br label %23
23: ; preds = %21, %14
%24 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%25 = icmp ne i8* %24, null
%26 = icmp ne i32 %18, %9
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %14, label %28
28: ; preds = %23, %8
ret i32 0
}