#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_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ getchar();
int getpid();
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ system(char *);
int main() {
  int i;
  int num_pages = ((100) << 20) / (4096);
  char str[100];
  snprintf(str, 100, "cat /proc/%d/statm", getpid());
  printf("When process starts:\n");
  system(str);
  printf("Press return to start mmap of %d MB\n", (100));
  getchar();
  char *p = mmap(NULL, (100) << 20, PROT_READ | PROT_WRITE,
                 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  printf("mmap() done @ VA %p. Total Program size should have increased by %d "
         "pages by now.\n",
         p, num_pages);
  system(str);
  printf("Press enter/return to write to each page\n");
  getchar();
  for (i = 0; i < num_pages; i++)
    p[i * (4096)] = 1;
  printf("Dirtied %d pages. Resident Pages and Shared pages should have "
         "increased by %d pages now.",
         num_pages, num_pages);
  printf("Press enter/return to exit:\n");
  system(str);
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x6400,-0xc(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x80(%rbp),%rdi
mov    %eax,%ecx
mov    $0x64,%esi
lea    0xe66(%rip),%rdx
call   1070 <snprintf@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1050 <system@plt>
lea    0xe6c(%rip),%rdi
mov    $0x64,%esi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2de8(%rip),%rax
or     (%rax),%edx
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    0x2df6(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x6400000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1050 <system@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1274 <main+0x114>
mov    -0x78(%rbp),%rax
mov    -0x8(%rbp),%ecx
shl    $0xc,%ecx
movslq %ecx,%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1249 <main+0xe9>
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1050 <system@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe7b(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x64,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
lea    0xf46(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1060 <system@plt>
lea    0xe61(%rip),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    0x2e2c(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dfa(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x6400000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
lea    0xe30(%rip),%rdi
mov    %rax,%rsi
mov    $0x6400,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1060 <system@plt>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    $0x4000,%eax
nopw   0x0(%rax,%rax,1)
movb   $0x1,-0x4000(%rbx,%rax,1)
01
movb   $0x1,-0x3000(%rbx,%rax,1)
01
movb   $0x1,-0x2000(%rbx,%rax,1)
01
movb   $0x1,-0x1000(%rbx,%rax,1)
01
movb   $0x1,(%rbx,%rax,1)
add    $0x5000,%rax
cmp    $0x6404000,%rax
jne    1240 <main+0xd0>
lea    0xe12(%rip),%rdi
mov    $0x6400,%esi
mov    $0x6400,%edx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1060 <system@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [19 x i8] c"cat /proc/%d/statm\00"
@.str.1 = [22 x i8] c"When process starts:\0A\00"
@.str.2 = [37 x i8] c"Press return to start mmap of %d MB\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [83 x i8] c"mmap() done @ VA %p. Total Program size should have increased by %d pages by now.\0A\00"
@.str.4 = [42 x i8] c"Press enter/return to write to each page\0A\00"
@.str.5 = [89 x i8] c"Dirtied %d pages. Resident Pages and Shared pages should have increased by %d pages now.\00"
@.str.6 = [29 x i8] c"Press enter/return to exit:\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i8*
store i32 0, i32* %1
store i32 25600, i32* %3
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = call i32 (...) @getpid()
%8 = call i32 @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%11 = call i32 @system(i8* %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 100)
%13 = call i32 (...) @getchar()
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_ANONYMOUS
%18 = load i32, i32* @MAP_SHARED
%19 = or i32 %17, %18
%20 = call i8* @mmap(i32* null, i32 104857600, i32 %16, i32 %19, i32 -1, i32 0)
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = load i32, i32* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i8* %21, i32 %22)
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%25 = call i32 @system(i8* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 (...) @getchar()
store i32 0, i32* %2
br label %28
28: ; preds = %38, %0
%29 = load i32, i32* %2
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %41
32: ; preds = %28
%33 = load i8*, i8** %5
%34 = load i32, i32* %2
%35 = mul nsw i32 %34, 4096
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8 1, i8* %37
br label %38
38: ; preds = %32
%39 = load i32, i32* %2
%40 = add nsw i32 %39, 1
store i32 %40, i32* %2
br label %28
41: ; preds = %28
%42 = load i32, i32* %3
%43 = load i32, i32* %3
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([89 x i8], [89 x i8]* @.str.5, i64 0, i64 0), i32 %42, i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%47 = call i32 @system(i8* %46)
%48 = call i32 (...) @getchar()
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"cat /proc/%d/statm\00"
@.str.2 = [37 x i8] c"Press return to start mmap of %d MB\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [83 x i8] c"mmap() done @ VA %p. Total Program size should have increased by %d pages by now.\0A\00"
@.str.5 = [89 x i8] c"Dirtied %d pages. Resident Pages and Shared pages should have increased by %d pages now.\00"
@str = [21 x i8] c"When process starts:\00"
@str.7 = [41 x i8] c"Press enter/return to write to each page\00"
@str.8 = [28 x i8] c"Press enter/return to exit:\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @snprintf(i8* %2, i32 100, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%6 = call i32 @system(i8* %2)
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 100)
%8 = call i32 (...) @getchar()
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = load i32, i32* @MAP_SHARED
%14 = or i32 %13, %12
%15 = call i8* @mmap(i32* null, i32 104857600, i32 %11, i32 %14, i32 -1, i32 0)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i8* %15, i32 25600)
%17 = call i32 @system(i8* %2)
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.7, i64 0, i64 0))
%19 = call i32 (...) @getchar()
br label %20
20: ; preds = %20, %0
%21 = phi i64 [ 0, %0 ], [ %36, %20 ]
%22 = shl nuw nsw i64 %21, 12
%23 = getelementptr inbounds i8, i8* %15, i64 %22
store i8 1, i8* %23
%24 = shl i64 %21, 12
%25 = add i64 %24, 4096
%26 = getelementptr inbounds i8, i8* %15, i64 %25
store i8 1, i8* %26
%27 = shl i64 %21, 12
%28 = add i64 %27, 8192
%29 = getelementptr inbounds i8, i8* %15, i64 %28
store i8 1, i8* %29
%30 = shl i64 %21, 12
%31 = add i64 %30, 12288
%32 = getelementptr inbounds i8, i8* %15, i64 %31
store i8 1, i8* %32
%33 = shl i64 %21, 12
%34 = add i64 %33, 16384
%35 = getelementptr inbounds i8, i8* %15, i64 %34
store i8 1, i8* %35
%36 = add nuw nsw i64 %21, 5
%37 = icmp eq i64 %36, 25600
br i1 %37, label %38, label %20
38: ; preds = %20
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([89 x i8], [89 x i8]* @.str.5, i64 0, i64 0), i32 25600, i32 25600)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.8, i64 0, i64 0))
%41 = call i32 @system(i8* %2)
%42 = call i32 (...) @getchar()
ret i32 0
}