#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ runShellcode(char *, int);
int readFile(char *fn) {
  char szBuf[4096] = {
      0x00,
  };
  char *p = szBuf;
  int f, rv, cnt;
  FILE *fp;
  fp = fopen(fn, "r");
  cnt = 0;
  while (1) {
    rv = fread(p, 1, 1, fp);
    p++;
    if (!rv)
      break;
    cnt++;
  }
  szBuf[cnt] = '\0';
  fclose(fp);
  runShellcode(szBuf, cnt);
  return 0;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1030(%rbp)
movl   $0x0,-0x1024(%rbp)
mov    -0x1018(%rbp),%rdi
mov    -0x1030(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,-0x1020(%rbp)
mov    -0x1018(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1018(%rbp)
cmpl   $0x0,-0x1020(%rbp)
jne    11e5 <readFile+0x95>
jmp    11f9 <readFile+0xa9>
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
jmp    11a1 <readFile+0x51>
movslq -0x1024(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
mov    -0x1030(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x1024(%rbp),%esi
call   1060 <runShellcode@plt>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rdi,%r14
mov    %rsp,%r15
xor    %ebx,%ebx
mov    $0x1000,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
lea    0xe86(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     11ce <readFile+0x7e>
lea    0x1(%rsp),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r15,%rbx,1),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
add    $0x1,%rbx
test   %eax,%eax
jne    11b0 <readFile+0x60>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rsp,%rdi
mov    %ebx,%esi
call   1060 <runShellcode@plt>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @readFile(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %2
%9 = bitcast [4096 x i8]* %3 to i8*
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
store i8* %10, i8** %4
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
store i32 0, i32* %7
br label %13
13: ; preds = %1, %22
%14 = load i8*, i8** %4
%15 = load i32*, i32** %8
%16 = call i32 @fread(i8* %14, i32 1, i32 1, i32* %15)
store i32 %16, i32* %6
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %4
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %13
br label %25
22: ; preds = %13
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
br label %13
25: ; preds = %21
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %27
store i8 0, i8* %28
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%32 = load i32, i32* %7
%33 = call i32 @runShellcode(i8* %31, i32 %32)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @readFile(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %1, %7
%8 = phi i8* [ %10, %7 ], [ %3, %1 ]
%9 = phi i32 [ %11, %7 ], [ 0, %1 ]
%10 = getelementptr inbounds i8, i8* %8, i64 1
%11 = add nuw nsw i32 %9, 1
%12 = call i32 @fread(i8* %10, i32 1, i32 1, i32* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %7
14: ; preds = %7, %1
%15 = phi i32 [ 0, %1 ], [ %11, %7 ]
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %16
store i8 0, i8* %17
%18 = tail call i32 @fclose(i32* %4)
%19 = call i32 @runShellcode(i8* %3, i32 %15)
ret i32 0
}