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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int bruteforceDump(int /*<<< orphan*/ *, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(int /*<<< orphan*/ *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(long);
int /*<<< orphan*/ prnlog(char *, ...);
int bruteforceFile(const char *filename, uint16_t keytable[]) {
  FILE *f = fopen(filename, "rb");
  if (!f) {
    prnlog("Failed to read from file '%s'", filename);
    return 1;
  }
  fseek(f, 0, SEEK_END);
  long fsize = ftell(f);
  fseek(f, 0, SEEK_SET);
  uint8_t *dump = malloc(fsize);
  size_t bytes_read = fread(dump, fsize, 1, f);
  fclose(f);
  if (bytes_read < fsize) {
    prnlog("Error, could only read %d bytes (should be %d)", bytes_read, fsize);
  }
  return bruteforceDump(dump, fsize, keytable);
}


x86 assembly (clang, -O0):
<bruteforceFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cd <bruteforceFile+0x4d>
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <prnlog@plt>
movl   $0x1,-0x4(%rbp)
jmp    126a <bruteforceFile+0xea>
mov    -0x20(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1256 <bruteforceFile+0xd6>
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1040 <prnlog@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1090 <bruteforceDump@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bruteforceFile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1231 <bruteforceFile+0xb1>
mov    %rax,%rbx
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    %r15,%r13
jae    121a <bruteforceFile+0x9a>
lea    0xe14(%rip),%rdi
mov    %r13,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <prnlog@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1090 <bruteforceDump@plt>
lea    0xdcb(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <prnlog@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [30 x i8] c"Failed to read from file '%s'\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [47 x i8] c"Error, could only read %d bytes (should be %d)\00"
define i32 @bruteforceFile(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 (i8*, ...) @prnlog(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %46
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %6
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %7
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i64, i64* %7
%27 = call i32* @malloc(i64 %26)
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = load i64, i64* %7
%30 = load i32*, i32** %6
%31 = call i64 @fread(i32* %28, i64 %29, i32 1, i32* %30)
store i64 %31, i64* %9
%32 = load i32*, i32** %6
%33 = call i32 @fclose(i32* %32)
%34 = load i64, i64* %9
%35 = load i64, i64* %7
%36 = icmp ult i64 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %17
%38 = load i64, i64* %9
%39 = load i64, i64* %7
%40 = call i32 (i8*, ...) @prnlog(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %38, i64 %39)
br label %41
41: ; preds = %37, %17
%42 = load i32*, i32** %8
%43 = load i64, i64* %7
%44 = load i32*, i32** %5
%45 = call i32 @bruteforceDump(i32* %42, i64 %43, i32* %44)
store i32 %45, i32* %3
br label %46
46: ; preds = %41, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [30 x i8] c"Failed to read from file '%s'\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [47 x i8] c"Error, could only read %d bytes (should be %d)\00"
define i32 @bruteforceFile(i8* %0, i32* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @prnlog(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = tail call i32* @malloc(i64 %10)
%14 = tail call i64 @fread(i32* %13, i64 %10, i32 1, i32* %3)
%15 = tail call i32 @fclose(i32* %3)
%16 = icmp ult i64 %14, %10
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = tail call i32 (i8*, ...) @prnlog(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i64 %14, i64 %10)
br label %19
19: ; preds = %17, %7
%20 = tail call i32 @bruteforceDump(i32* %13, i64 %10, i32* %1)
br label %21
21: ; preds = %19, %5
%22 = phi i32 [ %20, %19 ], [ 1, %5 ]
ret i32 %22
}