#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_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int mprotect(char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int personality(int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char *ptr;
  int rc;
  int fd;
  if (argc != 2) {
    fprintf(stderr, "usage: %s file\n", argv[0]);
    exit(1);
  }
  rc = personality(0xffffffff);
  if ((rc != -1) && (rc & 0x0400000)) {
    rc &= ~0x0400000;
    personality(rc);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror(argv[1]);
    exit(1);
  }
  ptr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  *((char *)ptr) = 42;
  rc = mprotect(ptr, 4096, PROT_READ | PROT_EXEC);
  if (rc < 0) {
    perror("mprotect");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b6 <main+0x46>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0xffffffff,%edi
call   1030 <personality@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11f1 <main+0x81>
mov    -0x1c(%rbp),%eax
and    $0x400000,%eax
cmp    $0x0,%eax
je     11f1 <main+0x81>
mov    -0x1c(%rbp),%eax
and    $0xffbfffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <personality@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    122b <main+0xbb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    0x2d85(%rip),%rax
or     (%rax),%edx
mov    0x2d74(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%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    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
jne    1289 <main+0x119>
lea    0xd96(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
movb   $0x2a,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
mov    0x2d34(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1060 <mprotect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12d3 <main+0x163>
lea    0xd51(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11bd <main+0x4d>
mov    $0xffffffff,%edi
call   1030 <personality@plt>
cmp    $0xffffffff,%eax
je     119e <main+0x2e>
mov    %eax,%ecx
and    $0x400000,%ecx
je     119e <main+0x2e>
and    $0xffbfffff,%eax
mov    %eax,%edi
call   1030 <personality@plt>
mov    0x8(%rbx),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
jns    11df <main+0x6f>
mov    0x8(%rbx),%rdi
jmp    1247 <main+0xd7>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe30(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e12(%rip),%rbx
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%edx
or     (%rbx),%edx
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <main+0xb1>
lea    0xdf1(%rip),%rdi
jmp    1247 <main+0xd7>
movb   $0x2a,(%rax)
mov    0x2dad(%rip),%rcx
mov    (%rcx),%edx
or     (%rbx),%edx
mov    %rax,%rdi
mov    $0x1000,%esi
call   1060 <mprotect@plt>
test   %eax,%eax
jns    1256 <main+0xe6>
lea    0xdce(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"usage: %s file\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@PROT_EXEC = global i32 0
@.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 i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = call i32 @personality(i32 -1)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i32, i32* %7
%24 = and i32 %23, 4194304
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* %7
%28 = and i32 %27, -4194305
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = call i32 @personality(i32 %29)
br label %31
31: ; preds = %26, %22, %18
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %31
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @perror(i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %31
%46 = load i32, i32* @PROT_READ
%47 = load i32, i32* @PROT_WRITE
%48 = or i32 %46, %47
%49 = load i32, i32* @MAP_PRIVATE
%50 = load i32, i32* %8
%51 = call i8* @mmap(i32* null, i32 4096, i32 %48, i32 %49, i32 %50, i32 0)
store i8* %51, i8** %6
%52 = load i8*, i8** %6
%53 = load i8*, i8** @MAP_FAILED
%54 = icmp eq i8* %52, %53
br i1 %54, label %55, label %58
55: ; preds = %45
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %45
%59 = load i8*, i8** %6
store i8 42, i8* %59
%60 = load i8*, i8** %6
%61 = load i32, i32* @PROT_READ
%62 = load i32, i32* @PROT_EXEC
%63 = or i32 %61, %62
%64 = call i32 @mprotect(i8* %60, i32 4096, i32 %63)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %58
%68 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %58
%71 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"usage: %s file\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@PROT_EXEC = global i32 0
@.str.2 = [9 x i8] c"mprotect\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = tail call i32 @personality(i32 -1)
%11 = icmp eq i32 %10, -1
%12 = and i32 %10, 4194304
%13 = icmp eq i32 %12, 0
%14 = or i1 %11, %13
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = and i32 %10, -4194305
%17 = tail call i32 @personality(i32 %16)
br label %18
18: ; preds = %15, %9
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 @open(i8* %20, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i8*, i8** %19
%26 = tail call i32 @perror(i8* %25)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %30, %29
%32 = load i32, i32* @MAP_PRIVATE
%33 = tail call i8* @mmap(i32* null, i32 4096, i32 %31, i32 %32, i32 %22, i32 0)
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %28
store i8 42, i8* %33
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @PROT_EXEC
%42 = or i32 %41, %40
%43 = tail call i32 @mprotect(i8* %33, i32 4096, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %39
%49 = tail call i32 @exit(i32 0)
unreachable
}