#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 */
scalar_t__ atol(char *);
int /*<<< orphan*/ exit(int);
char *malloc(unsigned long);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  unsigned long mem;
  if (argc == 1)
    mem = 1024 * 1024 * 512;
  else if (argc == 2)
    mem = (unsigned)atol(argv[1]);
  else {
    printf("Usage: loadmem <memory in bytes>");
    exit(1);
  }
  char *ptr = malloc(mem);
  while (1) {
    memset(ptr, 0, mem);
    sleep(120);
  }
}


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)
cmpl   $0x1,-0x8(%rbp)
jne    118d <main+0x2d>
movq   $0x20000000,-0x18(%rbp)
20
jmp    11ca <main+0x6a>
cmpl   $0x2,-0x8(%rbp)
jne    11af <main+0x4f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atol@plt>
mov    %eax,%eax
mov    %rax,-0x18(%rbp)
jmp    11c5 <main+0x65>
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11ca <main+0x6a>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    $0x78,%edi
call   1080 <sleep@plt>
jmp    11d7 <main+0x77>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x20000000,%r14d
cmp    $0x1,%edi
je     1180 <main+0x20>
cmp    $0x2,%edi
jne    11a9 <main+0x49>
mov    0x8(%rsi),%rdi
call   1060 <atol@plt>
mov    %eax,%r14d
mov    %r14,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
mov    $0x78,%edi
call   1080 <sleep@plt>
jmp    1190 <main+0x30>
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Usage: loadmem <memory in bytes>\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %2
store i64 536870912, i64* %6
br label %25
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @atol(i8* %17)
%19 = trunc i64 %18 to i32
%20 = zext i32 %19 to i64
store i64 %20, i64* %6
br label %24
21: ; preds = %11
%22 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
br label %25
25: ; preds = %24, %10
%26 = load i64, i64* %6
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %7
br label %28
28: ; preds = %25, %28
%29 = load i8*, i8** %7
%30 = load i64, i64* %6
%31 = call i32 @memset(i8* %29, i32 0, i64 %30)
%32 = call i32 @sleep(i32 120)
br label %28
}

LLVM IR (-O2):
@.str = [33 x i8] c"Usage: loadmem <memory in bytes>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %8 [
i32 1, label %11
i32 2, label %3
]
3: ; preds = %2
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i64 @atol(i8* %5)
%7 = and i64 %6, 4294967295
br label %11
8: ; preds = %2
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2, %3
%12 = phi i64 [ %7, %3 ], [ 536870912, %2 ]
%13 = tail call i8* @malloc(i64 %12)
br label %14
14: ; preds = %14, %11
%15 = tail call i32 @memset(i8* %13, i32 0, i64 %12)
%16 = tail call i32 @sleep(i32 120)
br label %14
}