#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int getpid();
void *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int /*<<< orphan*/, void *, int);
int /*<<< orphan*/ stdout;
int main() {
  void *p;
  int megabytes = 512;
  int offset;
  printf("MEMORYHOG: MYPID: %d, gonna eat %d MB of mem.\n", getpid(),
         megabytes);
  for (; megabytes > 0; --megabytes) {
    p = malloc(1024 * 1024);
    if (!p) {
      printf("could not allocate new block\n");
      printf("%d megabytes left unallocated.\n", megabytes);
      exit(1);
    }
    offset = 0;
    while (offset < 1024 * 1024) {
      memset(p + offset, 1, 1);
      offset += 1024;
    }
  }
  printf("MEMORYHOG: I got it all.\n");
  printf("Press ENTER.\n");
  fflush(stdout);
  read(0, p, 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x200,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jle    123a <main+0xca>
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ec <main+0x7c>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x100000,-0x18(%rbp)
jge    1227 <main+0xb7>
mov    -0x10(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x18(%rbp)
jmp    11f3 <main+0x83>
jmp    122c <main+0xbc>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11a0 <main+0x30>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1060 <read@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe6d(%rip),%rdi
mov    $0x200,%r14d
mov    %eax,%esi
mov    $0x200,%edx
xor    %eax,%eax
call   1050 <printf@plt>
nopw   0x0(%rax,%rax,1)
mov    $0x100000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1244 <main+0xc4>
mov    %rax,%r15
mov    $0xfffffffffffffc00,%rbx
nopl   (%rax)
lea    (%r15,%rbx,1),%rdi
add    $0x400,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1060 <memset@plt>
add    $0x400,%rbx
cmp    $0xffc00,%rbx
jb     11d0 <main+0x50>
lea    -0x1(%r14),%eax
cmp    $0x1,%r14d
mov    %eax,%r14d
ja     11b0 <main+0x30>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
lea    0xdd8(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"MEMORYHOG: MYPID: %d, gonna eat %d MB of mem.\0A\00"
@.str.1 = [30 x i8] c"could not allocate new block\0A\00"
@.str.2 = [32 x i8] c"%d megabytes left unallocated.\0A\00"
@.str.3 = [26 x i8] c"MEMORYHOG: I got it all.\0A\00"
@.str.4 = [14 x i8] c"Press ENTER.\0A\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 512, i32* %3
%5 = call i32 (...) @getpid()
%6 = load i32, i32* %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
br label %8
8: ; preds = %33, %0
%9 = load i32, i32* %3
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %36
11: ; preds = %8
%12 = call i8* @malloc(i32 1048576)
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = icmp ne i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %11
store i32 0, i32* %4
br label %21
21: ; preds = %24, %20
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 1048576
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i8*, i8** %2
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr i8, i8* %25, i64 %27
%29 = call i32 @memset(i8* %28, i32 1, i32 1)
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1024
store i32 %31, i32* %4
br label %21
32: ; preds = %21
br label %33
33: ; preds = %32
%34 = load i32, i32* %3
%35 = add nsw i32 %34, -1
store i32 %35, i32* %3
br label %8
36: ; preds = %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
%41 = load i8*, i8** %2
%42 = call i32 @read(i32 0, i8* %41, i32 1)
ret i32 0
}

LLVM IR (-O2):
@.str = [47 x i8] c"MEMORYHOG: MYPID: %d, gonna eat %d MB of mem.\0A\00"
@.str.2 = [32 x i8] c"%d megabytes left unallocated.\0A\00"
@stdout = global i32 0
@str = [25 x i8] c"MEMORYHOG: I got it all.\00"
@str.5 = [13 x i8] c"Press ENTER.\00"
@str.6 = [29 x i8] c"could not allocate new block\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %1, i32 512)
br label %3
3: ; preds = %0, %17
%4 = phi i32 [ 512, %0 ], [ %18, %17 ]
%5 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.6, i64 0, i64 0))
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3, %11
%12 = phi i64 [ %15, %11 ], [ 0, %3 ]
%13 = getelementptr i8, i8* %5, i64 %12
%14 = tail call i32 @memset(i8* %13, i32 1, i32 1)
%15 = add nuw nsw i64 %12, 1024
%16 = icmp ult i64 %12, 1047552
br i1 %16, label %11, label %17
17: ; preds = %11
%18 = add nsw i32 %4, -1
%19 = icmp ugt i32 %4, 1
br i1 %19, label %3, label %20
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
%23 = load i32, i32* @stdout
%24 = tail call i32 @fflush(i32 %23)
%25 = tail call i32 @read(i32 0, i8* %5, i32 1)
ret i32 0
}