#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 /*<<< orphan*/ buf;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_POPULATE;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  void *buf;
  if (argc < 2) {
    void *buf;
    printf("memoryhogger using malloc\n");
    while (1) {
      sleep(1);
      buf = malloc(1024 * 1024);
      printf("after malloc(), %p\n", buf);
      memset(buf, '2', 1024 * 1024);
    }
  } else {
    printf("memoryhogger using mmap\n");
    while (1) {
      sleep(1);
      buf = mmap(NULL, 1 * 1024 * 1024, PROT_READ | PROT_WRITE,
                 MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE, -1, 0);
      if (buf == (void *)-1)
        printf("%p\n", buf);
      else
        memset(buf, 1, sizeof(buf));
    }
  }
  return 0;
}


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   $0x2,-0x8(%rbp)
jge    11c0 <main+0x70>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    $0x100000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x32,%esi
mov    $0x100000,%edx
call   1050 <memset@plt>
jmp    117e <main+0x2e>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    0x2de0(%rip),%rax
or     (%rax),%edx
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc6(%rip),%rax
or     (%rax),%ecx
mov    0x2dd5(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    1248 <main+0xf8>
mov    -0x18(%rbp),%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125b <main+0x10b>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
jmp    11ce <main+0x7e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11c8 <main+0x68>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
mov    $0x100000,%edx
call   1060 <memset@plt>
jmp    1190 <main+0x30>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e1d(%rip),%r15
mov    0x2de6(%rip),%r12
mov    0x2df7(%rip),%r13
mov    0x2dd0(%rip),%rbx
mov    0x2de1(%rip),%rbp
lea    0xe12(%rip),%r14
jmp    1212 <main+0xb2>
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    (%r12),%edx
or     (%r15),%edx
mov    (%rbx),%ecx
or     0x0(%r13),%ecx
or     0x0(%rbp),%ecx
xor    %edi,%edi
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
jne    1200 <main+0xa0>
mov    %r14,%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1212 <main+0xb2>

LLVM IR (-O0):
@.str = [27 x i8] c"memoryhogger using malloc\0A\00"
@.str.1 = [20 x i8] c"after malloc(), %p\0A\00"
@.str.2 = [25 x i8] c"memoryhogger using mmap\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_POPULATE = global i32 0
@.str.3 = [4 x i8] c"%p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%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 slt i32 %8, 2
br i1 %9, label %10, label %19
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %12
%13 = call i32 @sleep(i32 1)
%14 = call i8* @malloc(i32 1048576)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %7
%18 = call i32 @memset(i8* %17, i32 50, i32 1048576)
br label %12
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %40
%22 = call i32 @sleep(i32 1)
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_ANONYMOUS
%27 = load i32, i32* @MAP_PRIVATE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_POPULATE
%30 = or i32 %28, %29
%31 = call i8* @mmap(i32* null, i32 1048576, i32 %25, i32 %30, i32 -1, i32 0)
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = icmp eq i8* %32, inttoptr (i64 -1 to i8*)
br i1 %33, label %34, label %37
34: ; preds = %21
%35 = load i8*, i8** %6
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %35)
br label %40
37: ; preds = %21
%38 = load i8*, i8** %6
%39 = call i32 @memset(i8* %38, i32 1, i32 8)
br label %40
40: ; preds = %37, %34
br label %21
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"after malloc(), %p\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_POPULATE = global i32 0
@.str.3 = [4 x i8] c"%p\0A\00"
@str = [24 x i8] c"memoryhogger using mmap\00"
@str.4 = [26 x i8] c"memoryhogger using malloc\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %11
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.4, i64 0, i64 0))
br label %6
6: ; preds = %6, %4
%7 = tail call i32 @sleep(i32 1)
%8 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = tail call i32 @memset(i8* %8, i32 50, i32 1048576)
br label %6
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %29, %11
%14 = tail call i32 @sleep(i32 1)
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_ANONYMOUS
%19 = load i32, i32* @MAP_PRIVATE
%20 = or i32 %19, %18
%21 = load i32, i32* @MAP_POPULATE
%22 = or i32 %20, %21
%23 = tail call i8* @mmap(i32* null, i32 1048576, i32 %17, i32 %22, i32 -1, i32 0)
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
br label %29
27: ; preds = %13
%28 = tail call i32 @memset(i8* %23, i32 1, i32 8)
br label %29
29: ; preds = %27, %25
br label %13
}