#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ READ_IMPLIES_EXEC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int optind;
int /*<<< orphan*/ perror(char *);
int personality(int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  char *ptr;
  int fd, opt, prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  while ((opt = getopt(argc, argv, "r")) != -1) {
    switch (opt) {
    case 'r':
      if (personality(READ_IMPLIES_EXEC) == -1) {
        perror("personality");
        exit(1);
      }
      prot &= ~PROT_EXEC;
      break;
    default:
      usage(argv[0]);
      break;
    }
  }
  if ((argc - optind) != 1)
    usage(argv[0]);
  fd = open(argv[optind], O_RDWR);
  if (fd < 0) {
    perror(argv[optind]);
    exit(1);
  }
  ptr = mmap(NULL, 4096, prot, MAP_SHARED, fd, 0);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    close(fd);
    exit(1);
  }
  close(fd);
  exit(0);
}


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)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    0x2e12(%rip),%rcx
or     (%rcx),%eax
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe3e(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     1239 <main+0xb9>
mov    -0x20(%rbp),%eax
sub    $0x72,%eax
jne    1228 <main+0xa8>
jmp    11e4 <main+0x64>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1030 <personality@plt>
cmp    $0xffffffff,%eax
jne    1211 <main+0x91>
lea    0xe00(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1234 <main+0xb4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
jmp    11b4 <main+0x34>
mov    -0x8(%rbp),%eax
mov    0x2d8d(%rip),%rcx
sub    (%rcx),%eax
cmp    $0x1,%eax
je     125a <main+0xda>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x2d6b(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12a8 <main+0x128>
mov    -0x10(%rbp),%rax
mov    0x2d3e(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edx
mov    0x2d36(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2cd0(%rip),%rcx
cmp    (%rcx),%rax
jne    12ff <main+0x17f>
lea    0xd26(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e61(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%r14d
or     (%rax),%r14d
mov    0x2e1d(%rip),%r12
or     (%r12),%r14d
lea    0xe4a(%rip),%r15
mov    0x2df3(%rip),%r13
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <getopt@plt>
cmp    $0x72,%eax
je     11e1 <main+0x61>
cmp    $0xffffffff,%eax
je     11fe <main+0x7e>
mov    (%rbx),%rdi
call   1060 <usage@plt>
jmp    11c0 <main+0x40>
mov    0x0(%r13),%edi
call   1030 <personality@plt>
cmp    $0xffffffff,%eax
je     129f <main+0x11f>
mov    (%r12),%eax
not    %eax
and    %eax,%r14d
jmp    11c0 <main+0x40>
mov    0x2dcb(%rip),%r15
mov    (%r15),%eax
sub    %eax,%ebp
cmp    $0x1,%ebp
je     121a <main+0x9a>
mov    (%rbx),%rdi
call   1060 <usage@plt>
mov    (%r15),%eax
cltq
mov    (%rbx,%rax,8),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jns    1248 <main+0xc8>
movslq (%r15),%rax
mov    (%rbx,%rax,8),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%ebp
mov    0x2d97(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %r14d,%edx
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d39(%rip),%rcx
cmp    (%rcx),%rax
jne    1291 <main+0x111>
lea    0xd93(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd5c(%rip),%rdi
jmp    1239 <main+0xb9>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str = [2 x i8] c"r\00"
@READ_IMPLIES_EXEC = global i32 0
@.str.1 = [12 x i8] c"personality\00"
@optind = global i32 0
@O_RDWR = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @PROT_EXEC
%14 = or i32 %12, %13
store i32 %14, i32* %9
br label %15
15: ; preds = %39, %2
%16 = load i32, i32* %4
%17 = load i8**, i8*** %5
%18 = call i32 @getopt(i32 %16, i8** %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %18, i32* %8
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %40
20: ; preds = %15
%21 = load i32, i32* %8
switch i32 %21, label %34 [
i32 114, label %22
]
22: ; preds = %20
%23 = load i32, i32* @READ_IMPLIES_EXEC
%24 = call i32 @personality(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = load i32, i32* @PROT_EXEC
%31 = xor i32 %30, -1
%32 = load i32, i32* %9
%33 = and i32 %32, %31
store i32 %33, i32* %9
br label %39
34: ; preds = %20
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @usage(i8* %37)
br label %39
39: ; preds = %34, %29
br label %15
40: ; preds = %15
%41 = load i32, i32* %4
%42 = load i32, i32* @optind
%43 = sub nsw i32 %41, %42
%44 = icmp ne i32 %43, 1
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = call i32 @usage(i8* %48)
br label %50
50: ; preds = %45, %40
%51 = load i8**, i8*** %5
%52 = load i32, i32* @optind
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %51, i64 %53
%55 = load i8*, i8** %54
%56 = load i32, i32* @O_RDWR
%57 = call i32 @open(i8* %55, i32 %56)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %68
60: ; preds = %50
%61 = load i8**, i8*** %5
%62 = load i32, i32* @optind
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
%65 = load i8*, i8** %64
%66 = call i32 @perror(i8* %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %50
%69 = load i32, i32* %9
%70 = load i32, i32* @MAP_SHARED
%71 = load i32, i32* %7
%72 = call i8* @mmap(i32* null, i32 4096, i32 %69, i32 %70, i32 %71, i32 0)
store i8* %72, i8** %6
%73 = load i8*, i8** %6
%74 = load i8*, i8** @MAP_FAILED
%75 = icmp eq i8* %73, %74
br i1 %75, label %76, label %81
76: ; preds = %68
%77 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%78 = load i32, i32* %7
%79 = call i32 @close(i32 %78)
%80 = call i32 @exit(i32 1)
unreachable
81: ; preds = %68
%82 = load i32, i32* %7
%83 = call i32 @close(i32 %82)
%84 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str = [2 x i8] c"r\00"
@READ_IMPLIES_EXEC = global i32 0
@.str.1 = [12 x i8] c"personality\00"
@optind = global i32 0
@O_RDWR = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [5 x i8] c"mmap\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @PROT_EXEC
%7 = or i32 %5, %6
br label %8
8: ; preds = %19, %2
%9 = phi i32 [ %22, %19 ], [ %7, %2 ]
br label %10
10: ; preds = %8, %23
%11 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %11, label %23 [
i32 -1, label %26
i32 114, label %12
]
12: ; preds = %10
%13 = load i32, i32* @READ_IMPLIES_EXEC
%14 = tail call i32 @personality(i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = load i32, i32* @PROT_EXEC
%21 = xor i32 %20, -1
%22 = and i32 %9, %21
br label %8
23: ; preds = %10
%24 = load i8*, i8** %1
%25 = tail call i32 @usage(i8* %24)
br label %10
26: ; preds = %10
%27 = load i32, i32* @optind
%28 = sub nsw i32 %0, %27
%29 = icmp eq i32 %28, 1
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i8*, i8** %1
%32 = tail call i32 @usage(i8* %31)
%33 = load i32, i32* @optind
br label %34
34: ; preds = %30, %26
%35 = phi i32 [ %33, %30 ], [ %27, %26 ]
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %1, i64 %36
%38 = load i8*, i8** %37
%39 = load i32, i32* @O_RDWR
%40 = tail call i32 @open(i8* %38, i32 %39)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %34
%43 = load i32, i32* @optind
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8*, i8** %1, i64 %44
%46 = load i8*, i8** %45
%47 = tail call i32 @perror(i8* %46)
%48 = tail call i32 @exit(i32 1)
unreachable
49: ; preds = %34
%50 = load i32, i32* @MAP_SHARED
%51 = tail call i8* @mmap(i32* null, i32 4096, i32 %9, i32 %50, i32 %40, i32 0)
%52 = load i8*, i8** @MAP_FAILED
%53 = icmp eq i8* %51, %52
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%56 = tail call i32 @close(i32 %40)
%57 = tail call i32 @exit(i32 1)
unreachable
58: ; preds = %49
%59 = tail call i32 @close(i32 %40)
%60 = tail call i32 @exit(i32 0)
unreachable
}