#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*/ szBuf;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ readFile(char *);
int /*<<< orphan*/ runShellcode(char *, int);
int main(int argc, char **argv) {
  char ch;
  char szBuf[4096] = {
      0x00,
  };
  int cnt = 0;
  if (argc == 1) {
    memset(szBuf, 0x00, sizeof(szBuf));
    while (read(STDIN_FILENO, &ch, 1) > 0) {
      szBuf[cnt] = ch;
      cnt++;
      if (cnt > sizeof(szBuf)) {
        printf("Your shellcode is too big to load\n");
        return -1;
      }
    }
    szBuf[cnt] = '\0';
    runShellcode(szBuf, cnt);
  } else {
    readFile(argv[1]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
movl   $0x0,-0x1024(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    123a <main+0xea>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jle    1214 <main+0xc4>
mov    -0x11(%rbp),%cl
movslq -0x1024(%rbp),%rax
mov    %cl,-0x1020(%rbp,%rax,1)
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
movslq -0x1024(%rbp),%rax
cmp    $0x1000,%rax
jbe    120f <main+0xbf>
lea    0xe02(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <main+0xfe>
jmp    11a3 <main+0x53>
movslq -0x1024(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rdi
mov    -0x1024(%rbp),%esi
call   1060 <runShellcode@plt>
jmp    1247 <main+0xf7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <readFile@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x10(%rsp),%rdi
xor    %ebx,%ebx
mov    $0x1000,%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmp    $0x1,%ebp
jne    11de <main+0x8e>
lea    0x10(%rsp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    0x2e4e(%rip),%rbp
lea    0xf(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11e9 <main+0x99>
movzbl 0xf(%rsp),%eax
mov    %al,0x10(%rsp,%rbx,1)
add    $0x1,%rbx
cmp    $0x1001,%rbx
jne    11a0 <main+0x50>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    11fc <main+0xac>
mov    0x8(%r14),%rdi
call   1070 <readFile@plt>
jmp    11fc <main+0xac>
movb   $0x0,0x10(%rsp,%rbx,1)
lea    0x10(%rsp),%rdi
mov    %ebx,%esi
call   1060 <runShellcode@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [35 x i8] c"Your shellcode is too big to load\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca [4096 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [4096 x i8]* %7 to i8*
store i32 0, i32* %8
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %39
12: ; preds = %2
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 4096)
br label %15
15: ; preds = %31, %12
%16 = load i32, i32* @STDIN_FILENO
%17 = call i64 @read(i32 %16, i8* %6, i32 1)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = load i8, i8* %6
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %22
store i8 %20, i8* %23
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = icmp ugt i64 %27, 4096
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
31: ; preds = %19
br label %15
32: ; preds = %15
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %8
%38 = call i32 @runShellcode(i8* %36, i32 %37)
br label %44
39: ; preds = %2
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @readFile(i8* %42)
br label %44
44: ; preds = %39, %32
store i32 0, i32* %3
br label %45
45: ; preds = %44, %29
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@str = [34 x i8] c"Your shellcode is too big to load\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = icmp eq i32 %0, 1
br i1 %6, label %7, label %26
7: ; preds = %2
%8 = call i32 @memset(i8* %5, i32 0, i32 4096)
br label %9
9: ; preds = %14, %7
%10 = phi i64 [ %17, %14 ], [ 0, %7 ]
%11 = load i32, i32* @STDIN_FILENO
%12 = call i64 @read(i32 %11, i8* %3, i32 1)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %9
%15 = load i8, i8* %3
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %10
store i8 %15, i8* %16
%17 = add nuw nsw i64 %10, 1
%18 = icmp eq i64 %17, 4097
br i1 %18, label %19, label %9
19: ; preds = %14
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %30
21: ; preds = %9
%22 = trunc i64 %10 to i32
%23 = and i64 %10, 4294967295
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i32 @runShellcode(i8* %5, i32 %22)
br label %30
26: ; preds = %2
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = tail call i32 @readFile(i8* %28)
br label %30
30: ; preds = %21, %26, %19
%31 = phi i32 [ -1, %19 ], [ 0, %26 ], [ 0, %21 ]
ret i32 %31
}