#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 scalar_t__ off_t;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int, int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, void *, scalar_t__);
void stub1();
int main(int argc, char *argv[]) {
  void *p;
  int fd;
  off_t size;
  if (argc != 2) {
    printf("Usage:\n\t%s shellcode.bin\n", argv[0]);
    exit(-1);
  }
  fd = open(argv[1], O_RDONLY);
  assert(fd != -1);
  size = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  assert(size != 0);
  p = mmap(NULL, size, PROT_EXEC | PROT_READ | PROT_WRITE,
           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  printf("Memory@%p\n", p);
  assert(size == read(fd, p, size));
  close(fd);
  printf("Passing control to the shellcode...\n");
  ((void (*)())p)();
  printf("The shellcode has returned to main!\n");
  exit(-1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x1c(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x28(%rbp),%rsi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    0x2dbf(%rip),%rax
or     (%rax),%edx
mov    0x2d6e(%rip),%rax
or     (%rax),%edx
mov    0x2d5d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7c(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <read@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   *-0x18(%rbp)
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    1295 <main+0x105>
mov    0x8(%rsi),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%r14d
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1090 <assert@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rax,%rbx
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1090 <assert@plt>
mov    0x2dca(%rip),%rax
mov    0x2df3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da0(%rip),%rax
or     (%rax),%edx
mov    0x2d8f(%rip),%rax
mov    0x2db0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
lea    0xdd9(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <read@plt>
xor    %edi,%edi
cmp    %rax,%rbx
sete   %dil
call   1090 <assert@plt>
mov    %r14d,%edi
call   1070 <close@plt>
lea    0xdaf(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   *%rbp
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    (%rsi),%rsi
lea    0xd61(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Usage:\0A\09%s shellcode.bin\0A\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.1 = [11 x i8] c"Memory@%p\0A\00"
@.str.2 = [37 x i8] c"Passing control to the shellcode...\0A\00"
@.str.3 = [37 x i8] c"The shellcode has returned to main!\0A\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 i64
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 %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, -1
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i32, i32* %7
%28 = load i32, i32* @SEEK_END
%29 = call i64 @lseek(i32 %27, i32 0, i32 %28)
store i64 %29, i64* %8
%30 = load i32, i32* %7
%31 = load i32, i32* @SEEK_SET
%32 = call i64 @lseek(i32 %30, i32 0, i32 %31)
%33 = load i64, i64* %8
%34 = icmp ne i64 %33, 0
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i64, i64* %8
%38 = load i32, i32* @PROT_EXEC
%39 = load i32, i32* @PROT_READ
%40 = or i32 %38, %39
%41 = load i32, i32* @PROT_WRITE
%42 = or i32 %40, %41
%43 = load i32, i32* @MAP_PRIVATE
%44 = load i32, i32* @MAP_ANONYMOUS
%45 = or i32 %43, %44
%46 = call i8* @mmap(i32* null, i64 %37, i32 %42, i32 %45, i32 -1, i32 0)
store i8* %46, i8** %6
%47 = load i8*, i8** %6
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = load i64, i64* %8
%50 = load i32, i32* %7
%51 = load i8*, i8** %6
%52 = load i64, i64* %8
%53 = call i64 @read(i32 %50, i8* %51, i64 %52)
%54 = icmp eq i64 %49, %53
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%60 = load i8*, i8** %6
%61 = bitcast i8* %60 to void (...)*
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 -1)
unreachable
}

LLVM IR (-O2):
@.str = [26 x i8] c"Usage:\0A\09%s shellcode.bin\0A\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@PROT_EXEC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.1 = [11 x i8] c"Memory@%p\0A\00"
@str = [36 x i8] c"Passing control to the shellcode...\00"
@str.4 = [36 x i8] c"The shellcode has returned to main!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp ne i32 %12, -1
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = load i32, i32* @SEEK_END
%17 = tail call i64 @lseek(i32 %12, i32 0, i32 %16)
%18 = load i32, i32* @SEEK_SET
%19 = tail call i64 @lseek(i32 %12, i32 0, i32 %18)
%20 = icmp ne i64 %17, 0
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = load i32, i32* @PROT_EXEC
%24 = load i32, i32* @PROT_READ
%25 = or i32 %24, %23
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_PRIVATE
%29 = load i32, i32* @MAP_ANONYMOUS
%30 = or i32 %29, %28
%31 = tail call i8* @mmap(i32* null, i64 %17, i32 %27, i32 %30, i32 -1, i32 0)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = tail call i64 @read(i32 %12, i8* %31, i64 %17)
%34 = icmp eq i64 %17, %33
%35 = zext i1 %34 to i32
%36 = tail call i32 @assert(i32 %35)
%37 = tail call i32 @close(i32 %12)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%39 = bitcast i8* %31 to void (...)*
tail call void (...) %39()
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.4, i64 0, i64 0))
%41 = tail call i32 @exit(i32 -1)
unreachable
}