#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 MAP_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fork();
int /*<<< orphan*/ gets(char *);
long memtotal();
scalar_t__ mmap(int /*<<< orphan*/, int const, int, int, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int const);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  const int PAGE_SZ = 4096;
  const int R_SZ = PAGE_SZ * 64000;
  long i, j;
  printf("Process Name: ");
  gets(argv[0]);
  while (1) {
    sleep(1);
    long m = memtotal();
    printf("mem = %ldM\n", m / 1024 / 1024);
    if (m > R_SZ) {
      if (!fork()) {
        char *p = (char *)mmap(0, R_SZ, PROT_READ | PROT_WRITE,
                               MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
        for (j = 0; j < R_SZ; j += PAGE_SZ)
          p[j] = 1;
        sleep(900);
        munmap(p, R_SZ);
        return 0;
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1000,-0x14(%rbp)
movl   $0xfa00000,-0x18(%rbp)
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <gets@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    $0x0,%al
call   1090 <memtotal@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0xfa00000,-0x30(%rbp)
0f
jle    12a2 <main+0x132>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
jne    129d <main+0x12d>
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    0x2db6(%rip),%rax
or     (%rax),%edx
mov    0x2dbd(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9c(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xfa00000,%esi
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0xfa00000,-0x28(%rbp)
0f
jge    127d <main+0x10d>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x28(%rbp)
jmp    1250 <main+0xe0>
mov    $0x384,%edi
call   1070 <sleep@plt>
mov    -0x38(%rbp),%rdi
mov    $0xfa00000,%esi
call   1060 <munmap@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret
jmp    12a2 <main+0x132>
jmp    11ae <main+0x3e>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    0xe82(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx),%rdi
call   1050 <gets@plt>
lea    0xe7b(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1090 <memtotal@plt>
mov    %rax,%rbx
lea    0xfffff(%rax),%rsi
test   %rax,%rax
cmovns %rax,%rsi
sar    $0x14,%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xfa00001,%rbx
jl     11a0 <main+0x30>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    11a0 <main+0x30>
mov    0x2e0d(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de3(%rip),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0xfa00000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    $0xfffffffffffff000,%rax
nopw   0x0(%rax,%rax,1)
movb   $0x1,0x1000(%rbx,%rax,1)
01
movb   $0x1,0x2000(%rbx,%rax,1)
01
movb   $0x1,0x3000(%rbx,%rax,1)
01
movb   $0x1,0x4000(%rbx,%rax,1)
01
movb   $0x1,0x5000(%rbx,%rax,1)
01
movb   $0x1,0x6000(%rbx,%rax,1)
01
movb   $0x1,0x7000(%rbx,%rax,1)
01
movb   $0x1,0x8000(%rbx,%rax,1)
01
add    $0x8000,%rax
cmp    $0xf9ff000,%rax
jb     1230 <main+0xc0>
mov    $0x384,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0xfa00000,%esi
call   1060 <munmap@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Process Name: \00"
@.str.1 = [12 x i8] c"mem = %ldM\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4096, i32* %6
store i32 262144000, i32* %7
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @gets(i8* %15)
br label %17
17: ; preds = %2, %53
%18 = call i32 @sleep(i32 1)
%19 = call i64 (...) @memtotal()
store i64 %19, i64* %10
%20 = load i64, i64* %10
%21 = sdiv i64 %20, 1024
%22 = sdiv i64 %21, 1024
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%24 = load i64, i64* %10
%25 = icmp sgt i64 %24, 262144000
br i1 %25, label %26, label %53
26: ; preds = %17
%27 = call i32 (...) @fork()
%28 = icmp ne i32 %27, 0
br i1 %28, label %52, label %29
29: ; preds = %26
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_ANONYMOUS
%34 = load i32, i32* @MAP_PRIVATE
%35 = or i32 %33, %34
%36 = call i64 @mmap(i32 0, i32 262144000, i32 %32, i32 %35, i32 -1, i32 0)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %11
store i64 0, i64* %9
br label %38
38: ; preds = %45, %29
%39 = load i64, i64* %9
%40 = icmp slt i64 %39, 262144000
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i8*, i8** %11
%43 = load i64, i64* %9
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 1, i8* %44
br label %45
45: ; preds = %41
%46 = load i64, i64* %9
%47 = add nsw i64 %46, 4096
store i64 %47, i64* %9
br label %38
48: ; preds = %38
%49 = call i32 @sleep(i32 900)
%50 = load i8*, i8** %11
%51 = call i32 @munmap(i8* %50, i32 262144000)
ret i32 0
52: ; preds = %26
br label %53
53: ; preds = %52, %17
br label %17
}

LLVM IR (-O2):
@.str = [15 x i8] c"Process Name: \00"
@.str.1 = [12 x i8] c"mem = %ldM\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = load i8*, i8** %1
%5 = tail call i32 @gets(i8* %4)
br label %6
6: ; preds = %12, %2
%7 = tail call i32 @sleep(i32 1)
%8 = tail call i64 (...) @memtotal()
%9 = sdiv i64 %8, 1048576
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %9)
%11 = icmp sgt i64 %8, 262144000
br i1 %11, label %13, label %12
12: ; preds = %6, %13
br label %6
13: ; preds = %6
%14 = tail call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %12
16: ; preds = %13
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_ANONYMOUS
%21 = load i32, i32* @MAP_PRIVATE
%22 = or i32 %21, %20
%23 = tail call i64 @mmap(i32 0, i32 262144000, i32 %19, i32 %22, i32 -1, i32 0)
%24 = inttoptr i64 %23 to i8*
br label %25
25: ; preds = %25, %16
%26 = phi i64 [ 0, %16 ], [ %42, %25 ]
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 1, i8* %27
%28 = or i64 %26, 4096
%29 = getelementptr inbounds i8, i8* %24, i64 %28
store i8 1, i8* %29
%30 = or i64 %26, 8192
%31 = getelementptr inbounds i8, i8* %24, i64 %30
store i8 1, i8* %31
%32 = or i64 %26, 12288
%33 = getelementptr inbounds i8, i8* %24, i64 %32
store i8 1, i8* %33
%34 = or i64 %26, 16384
%35 = getelementptr inbounds i8, i8* %24, i64 %34
store i8 1, i8* %35
%36 = or i64 %26, 20480
%37 = getelementptr inbounds i8, i8* %24, i64 %36
store i8 1, i8* %37
%38 = or i64 %26, 24576
%39 = getelementptr inbounds i8, i8* %24, i64 %38
store i8 1, i8* %39
%40 = or i64 %26, 28672
%41 = getelementptr inbounds i8, i8* %24, i64 %40
store i8 1, i8* %41
%42 = add nuw nsw i64 %26, 32768
%43 = icmp ult i64 %40, 262139904
br i1 %43, label %25, label %44
44: ; preds = %25
%45 = tail call i32 @sleep(i32 900)
%46 = tail call i32 @munmap(i8* %24, i32 262144000)
ret i32 0
}