#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 ssize_t;
typedef int off_t;

/* Variables and functions */
int get_file_size(int);
int hack_frame(int, char *, unsigned char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, unsigned char *, int);
int hack_code(int f, int o) {
  off_t filesize;
  ssize_t gotbytes;
  unsigned char buf[64];
  filesize = get_file_size(o);
  if (filesize <= 0)
    return 0;
  if (filesize > 64) {
    printf("ERROR: Simple code upload limited to %i bytes.\n", 64);
    return 0;
  }
  gotbytes = read(o, buf, filesize);
  if (gotbytes != filesize) {
    printf("ERROR: File read returned %i.\n", (int)gotbytes);
    return 0;
  }
  return hack_frame(f, "HACKCODE", buf);
}


x86 assembly (clang, -O0):
<hack_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <get_file_size@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     116f <hack_code+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <hack_code+0xa5>
cmpl   $0x40,-0x10(%rbp)
jle    1196 <hack_code+0x56>
lea    0xe80(%rip),%rdi
mov    $0x40,%esi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <hack_code+0xa5>
mov    -0xc(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     11cf <hack_code+0x8f>
mov    -0x14(%rbp),%esi
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <hack_code+0xa5>
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rdx
lea    0xe72(%rip),%rsi
call   1050 <hack_frame@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hack_code>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
mov    %esi,%edi
call   1060 <get_file_size@plt>
xor    %r15d,%r15d
test   %eax,%eax
jle    11a7 <hack_code+0x67>
mov    %eax,%ebx
cmp    $0x41,%eax
jb     1172 <hack_code+0x32>
lea    0xe95(%rip),%rdi
mov    $0x40,%esi
jmp    11a2 <hack_code+0x62>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1040 <read@plt>
cmp    %ebx,%eax
jne    1199 <hack_code+0x59>
lea    0xec6(%rip),%rsi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1050 <hack_frame@plt>
mov    %eax,%r15d
jmp    11a7 <hack_code+0x67>
lea    0xe90(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %r15d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [48 x i8] c"ERROR: Simple code upload limited to %i bytes.\0A\00"
@.str.1 = [31 x i8] c"ERROR: File read returned %i.\0A\00"
@.str.2 = [9 x i8] c"HACKCODE\00"
define i32 @hack_code(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [64 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = call i32 @get_file_size(i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 0, i32* %3
br label %34
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 64
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 64)
store i32 0, i32* %3
br label %34
19: ; preds = %14
%20 = load i32, i32* %5
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%22 = load i32, i32* %6
%23 = call i32 @read(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i32, i32* %7
%29 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 0, i32* %3
br label %34
30: ; preds = %19
%31 = load i32, i32* %4
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%33 = call i32 @hack_frame(i32 %31, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 %33, i32* %3
br label %34
34: ; preds = %30, %27, %17, %13
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [48 x i8] c"ERROR: Simple code upload limited to %i bytes.\0A\00"
@.str.1 = [31 x i8] c"ERROR: File read returned %i.\0A\00"
@.str.2 = [9 x i8] c"HACKCODE\00"
define i32 @hack_code(i32 %0, i32 %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @get_file_size(i32 %1)
%6 = icmp slt i32 %5, 1
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = icmp ugt i32 %5, 64
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 64)
br label %18
11: ; preds = %7
%12 = call i32 @read(i32 %1, i8* %4, i32 %5)
%13 = icmp eq i32 %12, %5
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %18
16: ; preds = %11
%17 = call i32 @hack_frame(i32 %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %18
18: ; preds = %2, %16, %14, %9
%19 = phi i32 [ 0, %9 ], [ 0, %14 ], [ %17, %16 ], [ 0, %2 ]
ret i32 %19
}