#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*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t load_binary(const char *file_path, char **out_buffer) {
  FILE *fp = fopen(file_path, "rb");
  size_t lSize = 0;
  char *buffer = NULL;
  if (fp == NULL) {
    fprintf(stderr,
            "[vpmu-control]  \033[1;31mFatal\033[0;00m: "
            "File '%s' not found\n"
            "\n",
            file_path);
    return 0;
  }
  fseek(fp, 0, SEEK_END);
  lSize = ftell(fp);
  rewind(fp);
  buffer = (char *)malloc(sizeof(char) * lSize);
  if (buffer == NULL) {
    fprintf(stderr, "[vpmu-control]  \033[1;31mFatal\033[0;00m: "
                    "Memory error"
                    "\n");
    exit(4);
  }
  fread(buffer, 1, lSize, fp);
  fclose(fp);
  *out_buffer = buffer;
  return lSize;
}


x86 assembly (clang, -O0):
<load_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f7 <load_binary+0x67>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1292 <load_binary+0x102>
mov    -0x20(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1260 <load_binary+0xd0>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_binary>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe5b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1208 <load_binary+0x78>
mov    %rax,%rbx
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r15d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1234 <load_binary+0xa4>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,(%r14)
jmp    1225 <load_binary+0x95>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
xor    %r15d,%r15d
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x4,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [59 x i8] c"[vpmu-control] \1B[1;31mFatal\1B[0;00m: File '%s' not found\0A\0A\00"
@SEEK_END = global i32 0
@.str.2 = [51 x i8] c"[vpmu-control] \1B[1;31mFatal\1B[0;00m: Memory error\0A\00"
define i64 @load_binary(i8* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%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** %6
store i64 0, i64* %7
store i8* null, i8** %8
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i64 0, i64* %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 = call i32 @rewind(i32* %23)
%25 = load i64, i64* %7
%26 = mul i64 1, %25
%27 = trunc i64 %26 to i32
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %36
32: ; preds = %17
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 4)
unreachable
36: ; preds = %17
%37 = load i8*, i8** %8
%38 = load i64, i64* %7
%39 = load i32*, i32** %6
%40 = call i32 @fread(i8* %37, i32 1, i64 %38, i32* %39)
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %8
%44 = load i8**, i8*** %5
store i8* %43, i8** %44
%45 = load i64, i64* %7
store i64 %45, i64* %3
br label %46
46: ; preds = %36, %13
%47 = load i64, i64* %3
ret i64 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [59 x i8] c"[vpmu-control] \1B[1;31mFatal\1B[0;00m: File '%s' not found\0A\0A\00"
@SEEK_END = global i32 0
@.str.2 = [51 x i8] c"[vpmu-control] \1B[1;31mFatal\1B[0;00m: Memory error\0A\00"
define i64 @load_binary(i8* %0, i8** nocapture writeonly %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 %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %24
8: ; preds = %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %3)
%12 = tail call i32 @rewind(i32* %3)
%13 = trunc i64 %11 to i32
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %21
17: ; preds = %8
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 4)
unreachable
21: ; preds = %8
%22 = tail call i32 @fread(i8* %15, i32 1, i64 %11, i32* %3)
%23 = tail call i32 @fclose(i32* %3)
store i8* %15, i8** %1
br label %24
24: ; preds = %21, %5
%25 = phi i64 [ 0, %5 ], [ %11, %21 ]
ret i64 %25
}