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

/* Variables and functions */
int MAP_ANON;
int MAP_PRIVATE;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
size_t fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int stub1();
int main(int argc, char *argv[]) {
  FILE *fp;
  size_t file_sz;
  void *shellcode;
  if (argc < 2)
    return 1;
  fp = fopen(argv[1], "r");
  if (!fp) {
    fprintf(stderr, "%s: %s.\n", argv[1], strerror(errno));
    return 1;
  }
  fseek(fp, 0, SEEK_END);
  file_sz = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  shellcode = mmap(NULL, file_sz, PROT_READ | PROT_WRITE | PROT_EXEC,
                   MAP_ANON | MAP_PRIVATE, 0, 0);
  if (!shellcode) {
    fprintf(stderr, "mmap: %s.\n", strerror(errno));
    return 1;
  }
  if (fread(shellcode, sizeof(char), file_sz, fp) != file_sz) {
    fprintf(stderr, "fread: %s.\n", strerror(errno));
    return 1;
  }
  fclose(fp);
  return ((int (*)(void))shellcode)();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ac <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    1340 <main+0x1c0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe45(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1219 <main+0x99>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1340 <main+0x1c0>
mov    -0x18(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rsi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
mov    0x2d4e(%rip),%rax
or     (%rax),%edx
mov    0x2d6d(%rip),%rax
or     (%rax),%edx
mov    0x2d34(%rip),%rax
mov    (%rax),%ecx
mov    0x2d23(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d7 <main+0x157>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd47(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1340 <main+0x1c0>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x20(%rbp),%rax
je     1331 <main+0x1b1>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xcf8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1340 <main+0x1c0>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
call   *-0x28(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     12d1 <main+0x151>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1254 <main+0xd4>
mov    %rax,%r15
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %rax,%r14
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    0x2e11(%rip),%rax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ddf(%rip),%rax
or     (%rax),%edx
mov    0x2da6(%rip),%rax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     1287 <main+0x107>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %r14,%rax
jne    12a7 <main+0x127>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    *%rax
mov    0x2d6d(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rbx),%rbx
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd8c(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12d1 <main+0x151>
mov    0x2d3a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd66(%rip),%rsi
jmp    12c5 <main+0x145>
mov    0x2d1a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd51(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [9 x i8] c"%s: %s.\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [11 x i8] c"mmap: %s.\0A\00"
@.str.3 = [12 x i8] c"fread: %s.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %71
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %27, label %19
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %25)
store i32 1, i32* %3
br label %71
27: ; preds = %12
%28 = load i32*, i32** %6
%29 = load i32, i32* @SEEK_END
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %6
%32 = call i64 @ftell(i32* %31)
store i64 %32, i64* %7
%33 = load i32*, i32** %6
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %33, i32 0, i32 %34)
%36 = load i64, i64* %7
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @PROT_WRITE
%39 = or i32 %37, %38
%40 = load i32, i32* @PROT_EXEC
%41 = or i32 %39, %40
%42 = load i32, i32* @MAP_ANON
%43 = load i32, i32* @MAP_PRIVATE
%44 = or i32 %42, %43
%45 = call i8* @mmap(i32* null, i64 %36, i32 %41, i32 %44, i32 0, i32 0)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %53, label %48
48: ; preds = %27
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %51)
store i32 1, i32* %3
br label %71
53: ; preds = %27
%54 = load i8*, i8** %8
%55 = load i64, i64* %7
%56 = load i32*, i32** %6
%57 = call i64 @fread(i8* %54, i32 1, i64 %55, i32* %56)
%58 = load i64, i64* %7
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %65
60: ; preds = %53
%61 = load i32, i32* @stderr
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %63)
store i32 1, i32* %3
br label %71
65: ; preds = %53
%66 = load i32*, i32** %6
%67 = call i32 @fclose(i32* %66)
%68 = load i8*, i8** %8
%69 = bitcast i8* %68 to i32 ()*
%70 = call i32 %69()
store i32 %70, i32* %3
br label %71
71: ; preds = %65, %60, %48, %19, %11
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [9 x i8] c"%s: %s.\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [11 x i8] c"mmap: %s.\0A\00"
@.str.3 = [12 x i8] c"fread: %s.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %48, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %15
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %5
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %13)
br label %48
15: ; preds = %4
%16 = load i32, i32* @SEEK_END
%17 = tail call i32 @fseek(i32* %7, i32 0, i32 %16)
%18 = tail call i64 @ftell(i32* %7)
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %7, i32 0, i32 %19)
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @PROT_EXEC
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_ANON
%27 = load i32, i32* @MAP_PRIVATE
%28 = or i32 %27, %26
%29 = tail call i8* @mmap(i32* null, i64 %18, i32 %25, i32 %28, i32 0, i32 0)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %36
31: ; preds = %15
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = tail call i8* @strerror(i32 %33)
%35 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %48
36: ; preds = %15
%37 = tail call i64 @fread(i8* %29, i32 1, i64 %18, i32* %7)
%38 = icmp eq i64 %37, %18
br i1 %38, label %44, label %39
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = tail call i8* @strerror(i32 %41)
%43 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %48
44: ; preds = %36
%45 = tail call i32 @fclose(i32* %7)
%46 = bitcast i8* %29 to i32 ()*
%47 = tail call i32 %46()
br label %48
48: ; preds = %2, %44, %39, %31, %9
%49 = phi i32 [ 1, %39 ], [ %47, %44 ], [ 1, %31 ], [ 1, %9 ], [ 1, %2 ]
ret i32 %49
}