#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 st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_SHARED;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
void *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int /*<<< orphan*/, void *, int);
int stat(char *, struct stat *);
void stub1();
int main(int argc, char *argv[]) {
  struct stat statbuf;
  if (stat(argv[1], &statbuf) == -1) {
    printf("Usage: %s <code.bin>\n", argv[0]);
    exit(0);
  }
  void *a = mmap(0, statbuf.st_size, PROT_EXEC | PROT_READ | PROT_WRITE,
                 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  printf("allocated %d bytes of executable memory at: %p\n", statbuf.st_size,
         a);
  FILE *file = fopen(argv[1], "rb");
  read(fileno(file), a, statbuf.st_size);
  ((void (*)(void))a)();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    0x2e30(%rip),%rax
or     (%rax),%edx
mov    0x2def(%rip),%rax
or     (%rax),%edx
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
mov    0x2e05(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe28(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <read@plt>
call   *-0x20(%rbp)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     121c <main+0xac>
mov    0x8(%rsp),%ebp
mov    0x2e33(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e11(%rip),%rax
or     (%rax),%edx
mov    0x2e20(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
lea    0xe37(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%r14),%rdi
lea    0xe50(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <read@plt>
call   *%rbx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xdda(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [22 x i8] c"Usage: %s <code.bin>\0A\00"
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [48 x i8] c"allocated %d bytes of executable memory at: %p\0A\00"
@.str.2 = [3 x i8] c"rb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i32*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i8**, i8*** %4
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @stat(i8* %10, %struct.stat* %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %4
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @PROT_EXEC
%23 = load i32, i32* @PROT_READ
%24 = or i32 %22, %23
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_ANONYMOUS
%28 = load i32, i32* @MAP_SHARED
%29 = or i32 %27, %28
%30 = call i8* @mmap(i32 0, i32 %21, i32 %26, i32 %29, i32 -1, i32 0)
store i8* %30, i8** %6
%31 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 %32, i8* %33)
%35 = load i8**, i8*** %4
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %38, i32** %7
%39 = load i32*, i32** %7
%40 = call i32 @fileno(i32* %39)
%41 = load i8*, i8** %6
%42 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @read(i32 %40, i8* %41, i32 %43)
%45 = load i8*, i8** %6
%46 = bitcast i8* %45 to void ()*
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [22 x i8] c"Usage: %s <code.bin>\0A\00"
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [48 x i8] c"allocated %d bytes of executable memory at: %p\0A\00"
@.str.2 = [3 x i8] c"rb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = call i32 @stat(i8* %6, %struct.stat* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @PROT_EXEC
%17 = load i32, i32* @PROT_READ
%18 = or i32 %17, %16
%19 = load i32, i32* @PROT_WRITE
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_ANONYMOUS
%22 = load i32, i32* @MAP_SHARED
%23 = or i32 %22, %21
%24 = tail call i8* @mmap(i32 0, i32 %15, i32 %20, i32 %23, i32 -1, i32 0)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %24)
%26 = load i8*, i8** %5
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @fileno(i32* %27)
%29 = tail call i32 @read(i32 %28, i8* %24, i32 %15)
%30 = bitcast i8* %24 to void ()*
tail call void %30()
ret i32 0
}