#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 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);
  }
  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    117e <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    1183 <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     11af <main+0x6f>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11bf <main+0x7f>
jmp    11ec <main+0xac>
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    0xe1e(%rip),%rdi
call   1030 <printf@plt>
jmp    1183 <main+0x43>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%ebp
cmp    $0x2,%edi
jl     1159 <main+0x19>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebp
mov    $0x100000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b1 <main+0x71>
test   %ebp,%ebp
je     11b1 <main+0x71>
mov    $0x1,%ebx
lea    0xe88(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0x100000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b1 <main+0x71>
lea    0x1(%rbx),%ecx
cmp    %ebx,%ebp
mov    %ecx,%ebx
jne    1180 <main+0x40>
xor    %eax,%eax
pop    %rbx
pop    %r14
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 = %26, %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 %33
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)
br label %17
33: ; 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 %24
14: ; preds = %8, %14
%15 = phi i8* [ %20, %14 ], [ %10, %8 ]
%16 = phi i32 [ %18, %14 ], [ 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 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%21 = icmp ne i8* %20, null
%22 = icmp ne i32 %18, %9
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %14, label %24
24: ; preds = %14, %8
ret i32 0
}