#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ MADV_DONTNEED;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fstat(int, struct stat *);
int /*<<< orphan*/ helpAndExit(char *, int /*<<< orphan*/);
int madvise(char *, int /*<<< orphan*/, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int munlock(char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ peek(char *);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *);
long sysconf(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 2)
    helpAndExit(argv[0], EXIT_FAILURE);
  long pagesize;
  int fd, i;
  char *mem;
  struct stat st;
  pagesize = sysconf(_SC_PAGESIZE);
  if (pagesize == -1)
    pexit("sysconf");
  fd = open(argv[1], O_RDONLY);
  if (fd == -1)
    pexit("open");
  if (fstat(fd, &st) == -1)
    pexit("fstat");
  mem = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (mem == MAP_FAILED)
    pexit("mmap");
  printf("File loaded:\n");
  peek(mem);
  for (i = 0; i < (3); i++) {
    mem[i * pagesize] = 'X';
    mem[i * pagesize + 1] = 'X';
    mem[i * pagesize + 2] = 'X';
  }
  printf("\nModified file:\n");
  peek(mem);
  if (madvise(mem, st.st_size, MADV_DONTNEED) == -1)
    pexit("madvise");
  printf("\nmadvise() with MADV_DONTNEED:\n");
  peek(mem);
  if (munlock(mem, st.st_size) == -1)
    pexit("munlock");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e5 <main+0x35>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
call   10a0 <helpAndExit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    120e <main+0x5e>
lea    0xdf7(%rip),%rdi
call   10c0 <pexit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    123d <main+0x8d>
lea    0xdd0(%rip),%rdi
call   10c0 <pexit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0xae>
lea    0xdb4(%rip),%rdi
call   10c0 <pexit@plt>
mov    -0x30(%rbp),%esi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
mov    0x2d47(%rip),%rax
or     (%rax),%edx
mov    0x2d2e(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d05(%rip),%rcx
cmp    (%rcx),%rax
jne    12b0 <main+0x100>
lea    0xd68(%rip),%rdi
call   10c0 <pexit@plt>
lea    0xd61(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <peek@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jge    1319 <main+0x169>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
imul   -0x18(%rbp),%rcx
movb   $0x58,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
imul   -0x18(%rbp),%rcx
movb   $0x58,0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
imul   -0x18(%rbp),%rcx
movb   $0x58,0x2(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12cc <main+0x11c>
lea    0xd06(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <peek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    0x2c8c(%rip),%rax
mov    (%rax),%edx
call   1070 <madvise@plt>
cmp    $0xffffffff,%eax
jne    1358 <main+0x1a8>
lea    0xce4(%rip),%rdi
call   10c0 <pexit@plt>
lea    0xce0(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <peek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1030 <munlock@plt>
cmp    $0xffffffff,%eax
jne    138e <main+0x1de>
lea    0xcd6(%rip),%rdi
call   10c0 <pexit@plt>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
je     11d2 <main+0x22>
mov    0x0(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   10a0 <helpAndExit@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    11f5 <main+0x45>
lea    0xe10(%rip),%rdi
call   10c0 <pexit@plt>
mov    0x8(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    121a <main+0x6a>
lea    0xdf3(%rip),%rdi
call   10c0 <pexit@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1237 <main+0x87>
lea    0xddb(%rip),%rdi
call   10c0 <pexit@plt>
mov    0x8(%rsp),%r14d
mov    0x2db5(%rip),%rax
mov    0x2d6e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbp
mov    0x2d2f(%rip),%rax
cmp    (%rax),%rbp
jne    1282 <main+0xd2>
lea    0xd96(%rip),%rdi
call   10c0 <pexit@plt>
lea    0xd9f(%rip),%rdi
call   1050 <puts@plt>
mov    %rbp,%rdi
call   1040 <peek@plt>
movw   $0x5858,0x0(%rbp)
movb   $0x58,0x2(%rbp)
movw   $0x5858,0x0(%rbp,%rbx,1)
movb   $0x58,0x2(%rbp,%rbx,1)
movw   $0x5858,0x0(%rbp,%rbx,2)
movb   $0x58,0x2(%rbp,%rbx,2)
lea    0xd76(%rip),%rdi
call   1050 <puts@plt>
mov    %rbp,%rdi
call   1040 <peek@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r14d,%esi
call   1070 <madvise@plt>
cmp    $0xffffffff,%eax
jne    12f1 <main+0x141>
lea    0xd2c(%rip),%rdi
call   10c0 <pexit@plt>
lea    0xd4d(%rip),%rdi
call   1050 <puts@plt>
mov    %rbp,%rdi
call   1040 <peek@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1030 <munlock@plt>
cmp    $0xffffffff,%eax
jne    1321 <main+0x171>
lea    0xd04(%rip),%rdi
call   10c0 <pexit@plt>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EXIT_FAILURE = global i32 0
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [14 x i8] c"File loaded:\0A\00"
@.str.5 = [17 x i8] c"\0AModified file:\0A\00"
@MADV_DONTNEED = global i32 0
@.str.6 = [8 x i8] c"madvise\00"
@.str.7 = [32 x i8] c"\0Amadvise() with MADV_DONTNEED:\0A\00"
@.str.8 = [8 x i8] c"munlock\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @helpAndExit(i8* %16, i32 %17)
br label %19
19: ; preds = %13, %2
%20 = load i32, i32* @_SC_PAGESIZE
%21 = call i64 @sysconf(i32 %20)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %26
%37 = load i32, i32* %7
%38 = call i32 @fstat(i32 %37, %struct.stat* %10)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @PROT_READ
%46 = load i32, i32* @PROT_WRITE
%47 = or i32 %45, %46
%48 = load i32, i32* @MAP_PRIVATE
%49 = load i32, i32* %7
%50 = call i8* @mmap(i32* null, i32 %44, i32 %47, i32 %48, i32 %49, i32 0)
store i8* %50, i8** %9
%51 = load i8*, i8** %9
%52 = load i8*, i8** @MAP_FAILED
%53 = icmp eq i8* %51, %52
br i1 %53, label %54, label %56
54: ; preds = %42
%55 = call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %42
%57 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%58 = load i8*, i8** %9
%59 = call i32 @peek(i8* %58)
store i32 0, i32* %8
br label %60
60: ; preds = %84, %56
%61 = load i32, i32* %8
%62 = icmp slt i32 %61, 3
br i1 %62, label %63, label %87
63: ; preds = %60
%64 = load i8*, i8** %9
%65 = load i32, i32* %8
%66 = sext i32 %65 to i64
%67 = load i64, i64* %6
%68 = mul nsw i64 %66, %67
%69 = getelementptr inbounds i8, i8* %64, i64 %68
store i8 88, i8* %69
%70 = load i8*, i8** %9
%71 = load i32, i32* %8
%72 = sext i32 %71 to i64
%73 = load i64, i64* %6
%74 = mul nsw i64 %72, %73
%75 = add nsw i64 %74, 1
%76 = getelementptr inbounds i8, i8* %70, i64 %75
store i8 88, i8* %76
%77 = load i8*, i8** %9
%78 = load i32, i32* %8
%79 = sext i32 %78 to i64
%80 = load i64, i64* %6
%81 = mul nsw i64 %79, %80
%82 = add nsw i64 %81, 2
%83 = getelementptr inbounds i8, i8* %77, i64 %82
store i8 88, i8* %83
br label %84
84: ; preds = %63
%85 = load i32, i32* %8
%86 = add nsw i32 %85, 1
store i32 %86, i32* %8
br label %60
87: ; preds = %60
%88 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%89 = load i8*, i8** %9
%90 = call i32 @peek(i8* %89)
%91 = load i8*, i8** %9
%92 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%93 = load i32, i32* %92
%94 = load i32, i32* @MADV_DONTNEED
%95 = call i32 @madvise(i8* %91, i32 %93, i32 %94)
%96 = icmp eq i32 %95, -1
br i1 %96, label %97, label %99
97: ; preds = %87
%98 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %99
99: ; preds = %97, %87
%100 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
%101 = load i8*, i8** %9
%102 = call i32 @peek(i8* %101)
%103 = load i8*, i8** %9
%104 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%105 = load i32, i32* %104
%106 = call i32 @munlock(i8* %103, i32 %105)
%107 = icmp eq i32 %106, -1
br i1 %107, label %108, label %110
108: ; preds = %99
%109 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
br label %110
110: ; preds = %108, %99
%111 = load i32, i32* @EXIT_SUCCESS
%112 = call i32 @exit(i32 %111)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EXIT_FAILURE = global i32 0
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@MADV_DONTNEED = global i32 0
@.str.6 = [8 x i8] c"madvise\00"
@.str.8 = [8 x i8] c"munlock\00"
@EXIT_SUCCESS = global i32 0
@str = [13 x i8] c"File loaded:\00"
@str.9 = [16 x i8] c"\0AModified file:\00"
@str.10 = [31 x i8] c"\0Amadvise() with MADV_DONTNEED:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 2
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @helpAndExit(i8* %6, i32 %7)
br label %9
9: ; preds = %5, %2
%10 = bitcast %struct.stat* %3 to i8*
%11 = load i32, i32* @_SC_PAGESIZE
%12 = tail call i64 @sysconf(i32 %11)
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = call i32 @fstat(i32 %20, %struct.stat* %3)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @pexit(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @PROT_WRITE
%34 = or i32 %33, %32
%35 = load i32, i32* @MAP_PRIVATE
%36 = tail call i8* @mmap(i32* null, i32 %31, i32 %34, i32 %35, i32 %20, i32 0)
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp eq i8* %36, %37
br i1 %38, label %39, label %41
39: ; preds = %29
%40 = tail call i32 @pexit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %29
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%43 = tail call i32 @peek(i8* %36)
store i8 88, i8* %36
%44 = getelementptr inbounds i8, i8* %36, i64 1
store i8 88, i8* %44
%45 = getelementptr inbounds i8, i8* %36, i64 2
store i8 88, i8* %45
%46 = getelementptr inbounds i8, i8* %36, i64 %12
store i8 88, i8* %46
%47 = add nsw i64 %12, 1
%48 = getelementptr inbounds i8, i8* %36, i64 %47
store i8 88, i8* %48
%49 = add nsw i64 %12, 2
%50 = getelementptr inbounds i8, i8* %36, i64 %49
store i8 88, i8* %50
%51 = shl nsw i64 %12, 1
%52 = getelementptr inbounds i8, i8* %36, i64 %51
store i8 88, i8* %52
%53 = or i64 %51, 1
%54 = getelementptr inbounds i8, i8* %36, i64 %53
store i8 88, i8* %54
%55 = add nsw i64 %51, 2
%56 = getelementptr inbounds i8, i8* %36, i64 %55
store i8 88, i8* %56
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%58 = tail call i32 @peek(i8* %36)
%59 = load i32, i32* @MADV_DONTNEED
%60 = tail call i32 @madvise(i8* %36, i32 %31, i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %41
%63 = tail call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %64
64: ; preds = %62, %41
%65 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%66 = tail call i32 @peek(i8* %36)
%67 = tail call i32 @munlock(i8* %36, i32 %31)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %71
69: ; preds = %64
%70 = tail call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
br label %71
71: ; preds = %69, %64
%72 = load i32, i32* @EXIT_SUCCESS
%73 = tail call i32 @exit(i32 %72)
unreachable
}