#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 MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_READ;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
scalar_t__ mprotect(char *, int, int);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
void stub1();
int main(void) {
  char shellcode[0x100 + 1];
  char *map;
  int rc;
  map = mmap(NULL, 0x1000, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (map == MAP_FAILED) {
    error("mmap");
  }
  bzero(map, 0x1000);
  rc = read(0, shellcode, 0x100);
  strncpy(map, shellcode, rc);
  if (mprotect(map, 0x1000, PROT_READ | PROT_EXEC)) {
    error("mprotect");
  }
  ((void (*)())map)();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    11ce <main+0x6e>
lea    0xe37(%rip),%rdi
call   1060 <error@plt>
mov    -0x118(%rbp),%rdi
mov    $0x1000,%esi
call   1080 <bzero@plt>
lea    -0x110(%rbp),%rsi
xor    %edi,%edi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x11c(%rbp)
mov    -0x118(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    -0x11c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x118(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    0x2db0(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1070 <mprotect@plt>
cmp    $0x0,%rax
je     124a <main+0xea>
lea    0xdc0(%rip),%rdi
call   1060 <error@plt>
mov    $0x0,%al
call   *-0x118(%rbp)
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    0x2e46(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2e14(%rip),%rax
cmp    (%rax),%rbx
jne    11b5 <main+0x55>
lea    0xe50(%rip),%rdi
call   1060 <error@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1080 <bzero@plt>
mov    %rsp,%r14
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    0x2e10(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1070 <mprotect@plt>
test   %rax,%rax
je     1211 <main+0xb1>
lea    0xdf9(%rip),%rdi
call   1060 <error@plt>
xor    %eax,%eax
call   *%rbx
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@.str.1 = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [257 x i8]
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @PROT_WRITE
%6 = load i32, i32* @MAP_PRIVATE
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = or i32 %6, %7
%9 = call i8* @mmap(i32* null, i32 4096, i32 %5, i32 %8, i32 -1, i32 0)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i8*, i8** %3
%17 = call i32 @bzero(i8* %16, i32 4096)
%18 = getelementptr inbounds [257 x i8], [257 x i8]* %2, i64 0, i64 0
%19 = call i32 @read(i32 0, i8* %18, i32 256)
store i32 %19, i32* %4
%20 = load i8*, i8** %3
%21 = getelementptr inbounds [257 x i8], [257 x i8]* %2, i64 0, i64 0
%22 = load i32, i32* %4
%23 = call i32 @strncpy(i8* %20, i8* %21, i32 %22)
%24 = load i8*, i8** %3
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_EXEC
%27 = or i32 %25, %26
%28 = call i64 @mprotect(i8* %24, i32 4096, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %15
%31 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %15
%33 = load i8*, i8** %3
%34 = bitcast i8* %33 to void (...)*
ret i32 0
}

LLVM IR (-O2):
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@.str.1 = [9 x i8] c"mprotect\00"
define i32 @main() {
%1 = alloca [257 x i8]
%2 = getelementptr inbounds [257 x i8], [257 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @PROT_WRITE
%4 = load i32, i32* @MAP_PRIVATE
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i32* null, i32 4096, i32 %3, i32 %6, i32 -1, i32 0)
%8 = load i8*, i8** @MAP_FAILED
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = tail call i32 @bzero(i8* %7, i32 4096)
%14 = call i32 @read(i32 0, i8* %2, i32 256)
%15 = call i32 @strncpy(i8* %7, i8* %2, i32 %14)
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_EXEC
%18 = or i32 %17, %16
%19 = call i64 @mprotect(i8* %7, i32 4096, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %12
%24 = bitcast i8* %7 to void (...)*
ret i32 0
}