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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
bool load_to_buffer(char *fn, uint8_t **buf, int *len) {
  FILE *infile = NULL;
  bool result = false;
  infile = fopen(fn, "rb");
  if (infile == NULL) {
    printf("Error opening input file!\n");
    goto FINALLY;
  };
  fseek(infile, 0, SEEK_END);
  *len = ftell(infile);
  if (*len > 65536 || *len < 256) {
    printf("Input file is too big or too small, not looks like valid MCU FW "
           "image!\n");
    goto FINALLY;
  };
  fseek(infile, 0, SEEK_SET);
  *buf = (uint8_t *)malloc(*len);
  if (*buf == NULL) {
    printf("Error allocating buffer!\n");
    goto FINALLY;
  };
  if (fread(*buf, 1, *len, infile) != *len) {
    printf("Error reading input file!\n");
    goto FINALLY;
  };
  result = true;
FINALLY:
  if (infile != NULL)
    fclose(infile);
  return result;
}


x86 assembly (clang, -O0):
<load_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c3 <load_to_buffer+0x53>
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
jmp    12a0 <load_to_buffer+0x130>
mov    -0x20(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x10000,(%rax)
jg     1208 <load_to_buffer+0x98>
mov    -0x18(%rbp),%rax
cmpl   $0x100,(%rax)
jge    1219 <load_to_buffer+0xa9>
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
jmp    12a0 <load_to_buffer+0x130>
mov    -0x20(%rbp),%rdi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1261 <load_to_buffer+0xf1>
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
jmp    12a0 <load_to_buffer+0x130>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     1299 <load_to_buffer+0x129>
lea    0xdf1(%rip),%rdi
call   1050 <printf@plt>
jmp    12a0 <load_to_buffer+0x130>
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12b4 <load_to_buffer+0x144>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe7c(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11c4 <load_to_buffer+0x54>
mov    %rax,%rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,(%r14)
add    $0xfffeffff,%eax
cmp    $0xffff00ff,%eax
jae    11d4 <load_to_buffer+0x64>
lea    0xe74(%rip),%rdi
jmp    1224 <load_to_buffer+0xb4>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    1233 <load_to_buffer+0xc3>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    (%r14),%edi
call   1070 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     121d <load_to_buffer+0xad>
mov    (%r14),%edx
mov    $0x1,%ebp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    (%r14),%eax
je     122b <load_to_buffer+0xbb>
lea    0xde8(%rip),%rdi
jmp    1224 <load_to_buffer+0xb4>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [27 x i8] c"Error opening input file!\0A\00"
@SEEK_END = global i32 0
@.str.2 = [72 x i8] c"Input file is too big or too small, not looks like valid MCU FW image!\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [26 x i8] c"Error allocating buffer!\0A\00"
@.str.4 = [27 x i8] c"Error reading input file!\0A\00"
define i32 @load_to_buffer(i8* %0, i32** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i32** %1, i32*** %5
store i32* %2, i32** %6
store i32* null, i32** %7
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %58
15: ; preds = %3
%16 = load i32*, i32** %7
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %7
%20 = call i32 @ftell(i32* %19)
%21 = load i32*, i32** %6
store i32 %20, i32* %21
%22 = load i32*, i32** %6
%23 = load i32, i32* %22
%24 = icmp sgt i32 %23, 65536
br i1 %24, label %29, label %25
25: ; preds = %15
%26 = load i32*, i32** %6
%27 = load i32, i32* %26
%28 = icmp slt i32 %27, 256
br i1 %28, label %29, label %31
29: ; preds = %25, %15
%30 = call i32 @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.2, i64 0, i64 0))
br label %58
31: ; preds = %25
%32 = load i32*, i32** %7
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %32, i32 0, i32 %33)
%35 = load i32*, i32** %6
%36 = load i32, i32* %35
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i32*
%39 = load i32**, i32*** %5
store i32* %38, i32** %39
%40 = load i32**, i32*** %5
%41 = load i32*, i32** %40
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %58
45: ; preds = %31
%46 = load i32**, i32*** %5
%47 = load i32*, i32** %46
%48 = load i32*, i32** %6
%49 = load i32, i32* %48
%50 = load i32*, i32** %7
%51 = call i32 @fread(i32* %47, i32 1, i32 %49, i32* %50)
%52 = load i32*, i32** %6
%53 = load i32, i32* %52
%54 = icmp ne i32 %51, %53
br i1 %54, label %55, label %57
55: ; preds = %45
%56 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %58
57: ; preds = %45
store i32 1, i32* %8
br label %58
58: ; preds = %57, %55, %43, %29, %13
%59 = load i32*, i32** %7
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i32*, i32** %7
%63 = call i32 @fclose(i32* %62)
br label %64
64: ; preds = %61, %58
%65 = load i32, i32* %8
ret i32 %65
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [26 x i8] c"Error reading input file!\00"
@str.5 = [25 x i8] c"Error allocating buffer!\00"
@str.6 = [71 x i8] c"Input file is too big or too small, not looks like valid MCU FW image!\00"
@str.7 = [26 x i8] c"Error opening input file!\00"
define i32 @load_to_buffer(i8* nocapture %0, i32** nocapture writeonly %1, i32* nocapture %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %4)
store i32 %9, i32* %2
%10 = add i32 %9, -65537
%11 = icmp ult i32 %10, -65281
br i1 %11, label %26, label %12
12: ; preds = %6
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %4, i32 0, i32 %13)
%15 = load i32, i32* %2
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %1
%18 = icmp eq i64 %16, 0
br i1 %18, label %26, label %19
19: ; preds = %12
%20 = load i32, i32* %2
%21 = tail call i32 @fread(i32* %17, i32 1, i32 %20, i32* %4)
%22 = load i32, i32* %2
%23 = icmp eq i32 %21, %22
br i1 %23, label %29, label %26
24: ; preds = %3
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
br label %32
26: ; preds = %19, %12, %6
%27 = phi i8* [ getelementptr inbounds ([71 x i8], [71 x i8]* @str.6, i64 0, i64 0), %6 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0), %12 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0), %19 ]
%28 = tail call i32 @puts(i8* dereferenceable(1) %27)
br label %29
29: ; preds = %26, %19
%30 = phi i32 [ 1, %19 ], [ 0, %26 ]
%31 = tail call i32 @fclose(i32* %4)
br label %32
32: ; preds = %24, %29
%33 = phi i32 [ %30, %29 ], [ 0, %24 ]
ret i32 %33
}