#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 PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ READ_IMPLIES_EXEC;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
int mprotect(void *, long, int);
int /*<<< orphan*/ perror(char *);
int personality(int /*<<< orphan*/);
long sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  char buf[4096];
  int rc, opt, prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  void *ptr;
  long pagesize = sysconf(_SC_PAGESIZE);
  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;
    }
  }
  ptr = (void *)(((unsigned long)buf) & ~(pagesize - 1));
  rc = mprotect(ptr, pagesize, prot);
  if (rc < 0) {
    perror("mprotect");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    0x2e2f(%rip),%rcx
or     (%rcx),%eax
mov    0x2e36(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x101c(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x1030(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe33(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x1018(%rbp)
cmp    $0xffffffff,%eax
je     1250 <main+0xe0>
mov    -0x1018(%rbp),%eax
sub    $0x72,%eax
jne    123f <main+0xcf>
jmp    11f5 <main+0x85>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1030 <personality@plt>
cmp    $0xffffffff,%eax
jne    1222 <main+0xb2>
lea    0xdef(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x101c(%rbp),%eax
mov    %eax,-0x101c(%rbp)
jmp    124b <main+0xdb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <usage@plt>
jmp    11bf <main+0x4f>
lea    -0x1010(%rbp),%rax
mov    -0x1030(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x1028(%rbp)
mov    -0x1028(%rbp),%rdi
mov    -0x1030(%rbp),%rsi
mov    -0x101c(%rbp),%edx
call   1050 <mprotect@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    12b2 <main+0x142>
lea    0xd6b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e6b(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%r14d
or     (%rax),%r14d
mov    0x2e37(%rip),%r13
or     0x0(%r13),%r14d
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,0x8(%rsp)
lea    0xe41(%rip),%r12
mov    0x2dfa(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1080 <getopt@plt>
cmp    $0x72,%eax
je     11f1 <main+0x81>
cmp    $0xffffffff,%eax
je     1209 <main+0x99>
mov    (%rbx),%rdi
call   1040 <usage@plt>
jmp    11d0 <main+0x60>
mov    (%r15),%edi
call   1030 <personality@plt>
cmp    $0xffffffff,%eax
je     1245 <main+0xd5>
mov    0x0(%r13),%eax
not    %eax
and    %eax,%r14d
jmp    11d0 <main+0x60>
mov    0x8(%rsp),%rsi
mov    %rsi,%rax
neg    %rax
lea    0x10(%rsp),%rdi
and    %rax,%rdi
mov    %r14d,%edx
call   1050 <mprotect@plt>
test   %eax,%eax
jns    123e <main+0xce>
lea    0xddf(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdb6(%rip),%rdi
jmp    122f <main+0xbf>

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

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@_SC_PAGESIZE = global i32 0
@.str = [2 x i8] c"r\00"
@READ_IMPLIES_EXEC = global i32 0
@.str.1 = [12 x i8] c"personality\00"
@.str.2 = [9 x i8] c"mprotect\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, 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* @PROT_EXEC
%9 = or i32 %7, %8
%10 = load i32, i32* @_SC_PAGESIZE
%11 = tail call i64 @sysconf(i32 %10)
br label %12
12: ; preds = %23, %2
%13 = phi i32 [ %26, %23 ], [ %9, %2 ]
br label %14
14: ; preds = %12, %27
%15 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %15, label %27 [
i32 -1, label %30
i32 114, label %16
]
16: ; preds = %14
%17 = load i32, i32* @READ_IMPLIES_EXEC
%18 = tail call i32 @personality(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = load i32, i32* @PROT_EXEC
%25 = xor i32 %24, -1
%26 = and i32 %13, %25
br label %12
27: ; preds = %14
%28 = load i8*, i8** %1
%29 = tail call i32 @usage(i8* %28)
br label %14
30: ; preds = %14
%31 = ptrtoint [4096 x i8]* %3 to i64
%32 = sub i64 0, %11
%33 = and i64 %32, %31
%34 = inttoptr i64 %33 to i8*
%35 = call i32 @mprotect(i8* %34, i64 %11, i32 %13)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = call i32 @exit(i32 0)
unreachable
}