#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_FAILURE;
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, size_t, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int munmap(char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[], char *envp[]) {
  int err;
  char *addr, *limit;
  size_t terabyte = (size_t)(1L << 42);
  printf("mmap'ing 1TB of anonymous memory.. ");
  addr = mmap(NULL, terabyte, PROT_READ | PROT_WRITE,
              MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  if (addr == MAP_FAILED)
    do {
      printf("mmap"
             ": %s\n",
             strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  printf("mapped 1TB at %p\n", addr);
  printf("Attempting to write to every at page...\n");
  limit = addr + terabyte - 0x1000;
  for (; addr < limit; addr += 0x1000) {
    *addr = 'A';
  }
  printf("\nmunmap'ing file...\n");
  err = munmap(addr, terabyte);
  if (err < 0)
    do {
      printf("munmap"
             ": %s\n",
             strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  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)
mov    %rdx,-0x18(%rbp)
movabs $0x40000000000,%rax
mov    %rax,-0x38(%rbp)
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rsi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
mov    0x2e15(%rip),%rax
mov    (%rax),%ecx
mov    0x2e34(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
jne    1207 <main+0xb7>
jmp    11da <main+0x8a>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
add    $0xfffffffffffff000,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1261 <main+0x111>
mov    -0x28(%rbp),%rax
movb   $0x41,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x28(%rbp)
jmp    1239 <main+0xe9>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <munmap@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12bb <main+0x16b>
jmp    128e <main+0x13e>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movabs $0x40000000000,%r14
lea    0xe8b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e75(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e23(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2df6(%rip),%rax
cmp    (%rax),%rbx
je     1270 <main+0x110>
lea    0xe64(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffff5000,%rax
lea    -0xc000(%r14),%rcx
xchg   %ax,%ax
movb   $0x41,(%rbx)
movb   $0x41,0x1000(%rbx)
movb   $0x41,0x2000(%rbx)
movb   $0x41,0x3000(%rbx)
movb   $0x41,0x4000(%rbx)
movb   $0x41,0x5000(%rbx)
movb   $0x41,0x6000(%rbx)
movb   $0x41,0x7000(%rbx)
movb   $0x41,0x8000(%rbx)
movb   $0x41,0x9000(%rbx)
movb   $0x41,0xa000(%rbx)
add    $0xb000,%rbx
add    $0xb000,%rax
cmp    %rcx,%rax
jb     11f0 <main+0x90>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <munmap@plt>
test   %eax,%eax
js     1287 <main+0x127>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd9f(%rip),%rdi
jmp    129c <main+0x13c>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xda4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"mmap'ing 1TB of anonymous memory.. \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [10 x i8] c"mmap: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"mapped 1TB at %p\0A\00"
@.str.3 = [41 x i8] c"Attempting to write to every at page...\0A\00"
@.str.4 = [21 x i8] c"\0Amunmap'ing file...\0A\00"
@.str.5 = [12 x i8] c"munmap: %s\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i64 4398046511104, i64* %11
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%13 = load i64, i64* %11
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_PRIVATE
%18 = load i32, i32* @MAP_ANONYMOUS
%19 = or i32 %17, %18
%20 = call i8* @mmap(i32* null, i64 %13, i32 %16, i32 %19, i32 0, i32 0)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %32
24: ; preds = %3
br label %25
25: ; preds = %24
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; No predecessors!
br label %32
32: ; preds = %31, %3
%33 = load i8*, i8** %9
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%36 = load i8*, i8** %9
%37 = load i64, i64* %11
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = getelementptr inbounds i8, i8* %38, i64 -4096
store i8* %39, i8** %10
br label %40
40: ; preds = %46, %32
%41 = load i8*, i8** %9
%42 = load i8*, i8** %10
%43 = icmp ult i8* %41, %42
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i8*, i8** %9
store i8 65, i8* %45
br label %46
46: ; preds = %44
%47 = load i8*, i8** %9
%48 = getelementptr inbounds i8, i8* %47, i64 4096
store i8* %48, i8** %9
br label %40
49: ; preds = %40
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%51 = load i8*, i8** %9
%52 = load i64, i64* %11
%53 = call i32 @munmap(i8* %51, i64 %52)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %49
br label %57
57: ; preds = %56
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; No predecessors!
br label %64
64: ; preds = %63, %49
ret i32 0
}

LLVM IR (-O2):
@.str = [36 x i8] c"mmap'ing 1TB of anonymous memory.. \00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [10 x i8] c"mmap: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [18 x i8] c"mapped 1TB at %p\0A\00"
@.str.5 = [12 x i8] c"munmap: %s\0A\00"
@str = [40 x i8] c"Attempting to write to every at page...\00"
@str.6 = [20 x i8] c"\0Amunmap'ing file...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_PRIVATE
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %9, %8
%11 = tail call i8* @mmap(i32* null, i64 4398046511104, i32 %7, i32 %10, i32 0, i32 0)
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %3
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %23, %20
%24 = phi i8* [ %11, %20 ], [ %47, %23 ]
%25 = phi i64 [ 0, %20 ], [ %46, %23 ]
store i8 65, i8* %24
%26 = add nuw nsw i64 %25, 4096
%27 = getelementptr inbounds i8, i8* %11, i64 %26
store i8 65, i8* %27
%28 = add nuw nsw i64 %25, 8192
%29 = getelementptr inbounds i8, i8* %11, i64 %28
store i8 65, i8* %29
%30 = add nuw nsw i64 %25, 12288
%31 = getelementptr inbounds i8, i8* %11, i64 %30
store i8 65, i8* %31
%32 = add nuw nsw i64 %25, 16384
%33 = getelementptr inbounds i8, i8* %11, i64 %32
store i8 65, i8* %33
%34 = add nuw nsw i64 %25, 20480
%35 = getelementptr inbounds i8, i8* %11, i64 %34
store i8 65, i8* %35
%36 = add nuw nsw i64 %25, 24576
%37 = getelementptr inbounds i8, i8* %11, i64 %36
store i8 65, i8* %37
%38 = add nuw nsw i64 %25, 28672
%39 = getelementptr inbounds i8, i8* %11, i64 %38
store i8 65, i8* %39
%40 = add nuw nsw i64 %25, 32768
%41 = getelementptr inbounds i8, i8* %11, i64 %40
store i8 65, i8* %41
%42 = add nuw nsw i64 %25, 36864
%43 = getelementptr inbounds i8, i8* %11, i64 %42
store i8 65, i8* %43
%44 = add nuw nsw i64 %25, 40960
%45 = getelementptr inbounds i8, i8* %11, i64 %44
store i8 65, i8* %45
%46 = add nuw nsw i64 %25, 45056
%47 = getelementptr inbounds i8, i8* %11, i64 %46
%48 = icmp ult i64 %25, 4398046461952
br i1 %48, label %23, label %49
49: ; preds = %23
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%51 = tail call i32 @munmap(i8* %47, i64 4398046511104)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %49
%54 = load i32, i32* @errno
%55 = tail call i8* @strerror(i32 %54)
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @EXIT_FAILURE
%58 = tail call i32 @exit(i32 %57)
unreachable
59: ; preds = %49
ret i32 0
}