#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 */
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ _IONBF;
int atoi(char *);
int *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ pause();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "usage: balloon [megabytes]\n");
    exit(1);
  }
  setvbuf(stdout, NULL, _IONBF, 0);
  int size = atoi(argv[1]);
  printf("megabytes: %d\n", size);
  for (int x = 0; x < size; x++) {
    uint64_t *temp = calloc(1, 1024 * 1024);
    if (temp == NULL) {
      fprintf(stderr, "Failed to calloc chunk #%d\n", x);
      exit(1);
    }
    for (int i = 0; i < (1024 * 1024) / sizeof(uint64_t); i++) {
      temp[i] = i;
    }
  }
  printf("done.\n");
  pause();
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    11b1 <main+0x41>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1070 <setvbuf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    128a <main+0x11a>
mov    $0x1,%edi
mov    $0x100000,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1244 <main+0xd4>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x40000,%rax
jae    1277 <main+0x107>
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    124b <main+0xdb>
jmp    127c <main+0x10c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11f6 <main+0x86>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <pause@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jle    1305 <main+0x185>
mov    %rsi,%rbp
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <setvbuf@plt>
mov    0x8(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
lea    0xeee(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jle    12c6 <main+0x146>
nop
mov    $0x1,%edi
mov    $0x100000,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     12e2 <main+0x162>
xor    %ecx,%ecx
movdqa 0xe0e(%rip),%xmm0
movdqa 0xe16(%rip),%xmm3
movdqa 0xe1e(%rip),%xmm4
movdqa 0xe26(%rip),%xmm5
movdqa 0xe2e(%rip),%xmm6
movdqa 0xe36(%rip),%xmm7
movdqa 0xe3d(%rip),%xmm8
movdqa 0xe44(%rip),%xmm9
movdqa 0xe4b(%rip),%xmm10
cs nopw 0x0(%rax,%rax,1)
nop
movdqa %xmm0,%xmm1
paddd  %xmm3,%xmm1
movdqu %xmm0,(%rax,%rcx,4)
movdqu %xmm1,0x10(%rax,%rcx,4)
movdqa %xmm0,%xmm1
paddd  %xmm4,%xmm1
movdqa %xmm0,%xmm2
paddd  %xmm5,%xmm2
movdqu %xmm1,0x20(%rax,%rcx,4)
movdqu %xmm2,0x30(%rax,%rcx,4)
movdqa %xmm0,%xmm1
paddd  %xmm6,%xmm1
movdqa %xmm0,%xmm2
paddd  %xmm7,%xmm2
movdqu %xmm1,0x40(%rax,%rcx,4)
movdqu %xmm2,0x50(%rax,%rcx,4)
movdqa %xmm0,%xmm1
paddd  %xmm8,%xmm1
movdqa %xmm0,%xmm2
paddd  %xmm9,%xmm2
movdqu %xmm1,0x60(%rax,%rcx,4)
movdqu %xmm2,0x70(%rax,%rcx,4)
add    $0x20,%rcx
paddd  %xmm10,%xmm0
cmp    $0x40000,%rcx
jne    1240 <main+0xc0>
add    $0x1,%ebx
cmp    %ebp,%ebx
jne    11d0 <main+0x50>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <pause@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"usage: balloon [megabytes]\0A\00"
@stdout = global i32 0
@_IONBF = global i32 0
@.str.1 = [15 x i8] c"megabytes: %d\0A\00"
@.str.2 = [28 x i8] c"Failed to calloc chunk #%d\0A\00"
@.str.3 = [7 x i8] c"done.\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*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @stdout
%18 = load i32, i32* @_IONBF
%19 = call i32 @setvbuf(i32 %17, i32* null, i32 %18, i32 0)
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 0, i32* %7
br label %26
26: ; preds = %54, %16
%27 = load i32, i32* %7
%28 = load i32, i32* %6
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %57
30: ; preds = %26
%31 = call i32* @calloc(i32 1, i32 1048576)
store i32* %31, i32** %8
%32 = load i32*, i32** %8
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i32, i32* %7
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
store i32 0, i32* %9
br label %40
40: ; preds = %50, %39
%41 = load i32, i32* %9
%42 = sext i32 %41 to i64
%43 = icmp ult i64 %42, 262144
br i1 %43, label %44, label %53
44: ; preds = %40
%45 = load i32, i32* %9
%46 = load i32*, i32** %8
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 %45, i32* %49
br label %50
50: ; preds = %44
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %40
53: ; preds = %40
br label %54
54: ; preds = %53
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %26
57: ; preds = %26
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 (...) @pause()
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"usage: balloon [megabytes]\0A\00"
@stdout = global i32 0
@_IONBF = global i32 0
@.str.1 = [15 x i8] c"megabytes: %d\0A\00"
@.str.2 = [28 x i8] c"Failed to calloc chunk #%d\0A\00"
@str = [6 x i8] c"done.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @stdout
%10 = load i32, i32* @_IONBF
%11 = tail call i32 @setvbuf(i32 %9, i32* null, i32 %10, i32 0)
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp sgt i32 %14, 0
br i1 %16, label %20, label %17
17: ; preds = %60, %8
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 (...) @pause()
ret i32 0
20: ; preds = %8, %60
%21 = phi i32 [ %61, %60 ], [ 0, %8 ]
%22 = tail call i32* @calloc(i32 1, i32 1048576)
%23 = icmp eq i32* %22, null
br i1 %23, label %56, label %24
24: ; preds = %20, %24
%25 = phi i64 [ %53, %24 ], [ 0, %20 ]
%26 = phi <4 x i32> [ %54, %24 ], [ <i32 0, i32 1, i32 2, i32 3>, %20 ]
%27 = add <4 x i32> %26, <i32 4, i32 4, i32 4, i32 4>
%28 = getelementptr inbounds i32, i32* %22, i64 %25
%29 = bitcast i32* %28 to <4 x i32>*
store <4 x i32> %26, <4 x i32>* %29
%30 = getelementptr inbounds i32, i32* %28, i64 4
%31 = bitcast i32* %30 to <4 x i32>*
store <4 x i32> %27, <4 x i32>* %31
%32 = or i64 %25, 8
%33 = add <4 x i32> %26, <i32 8, i32 8, i32 8, i32 8>
%34 = add <4 x i32> %26, <i32 12, i32 12, i32 12, i32 12>
%35 = getelementptr inbounds i32, i32* %22, i64 %32
%36 = bitcast i32* %35 to <4 x i32>*
store <4 x i32> %33, <4 x i32>* %36
%37 = getelementptr inbounds i32, i32* %35, i64 4
%38 = bitcast i32* %37 to <4 x i32>*
store <4 x i32> %34, <4 x i32>* %38
%39 = or i64 %25, 16
%40 = add <4 x i32> %26, <i32 16, i32 16, i32 16, i32 16>
%41 = add <4 x i32> %26, <i32 20, i32 20, i32 20, i32 20>
%42 = getelementptr inbounds i32, i32* %22, i64 %39
%43 = bitcast i32* %42 to <4 x i32>*
store <4 x i32> %40, <4 x i32>* %43
%44 = getelementptr inbounds i32, i32* %42, i64 4
%45 = bitcast i32* %44 to <4 x i32>*
store <4 x i32> %41, <4 x i32>* %45
%46 = or i64 %25, 24
%47 = add <4 x i32> %26, <i32 24, i32 24, i32 24, i32 24>
%48 = add <4 x i32> %26, <i32 28, i32 28, i32 28, i32 28>
%49 = getelementptr inbounds i32, i32* %22, i64 %46
%50 = bitcast i32* %49 to <4 x i32>*
store <4 x i32> %47, <4 x i32>* %50
%51 = getelementptr inbounds i32, i32* %49, i64 4
%52 = bitcast i32* %51 to <4 x i32>*
store <4 x i32> %48, <4 x i32>* %52
%53 = add nuw nsw i64 %25, 32
%54 = add <4 x i32> %26, <i32 32, i32 32, i32 32, i32 32>
%55 = icmp eq i64 %53, 262144
br i1 %55, label %60, label %24
56: ; preds = %20
%57 = load i32, i32* @stderr
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%59 = tail call i32 @exit(i32 1)
unreachable
60: ; preds = %24
%61 = add nuw nsw i32 %21, 1
%62 = icmp eq i32 %61, %14
br i1 %62, label %17, label %20
}