#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*/ clu_Exit(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(unsigned char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
unsigned char *clu_LoadProgBinary(const char *binary_file,
                                  size_t *binary_len_ret) {
  FILE *fp_binary = fopen(binary_file, "rb");
  if (fp_binary == NULL) {
    clu_Exit("ERROR: Failed to open %s\n", binary_file);
    return NULL;
  }
  fseek(fp_binary, 0, SEEK_END);
  size_t length = (size_t)ftell(fp_binary);
  *binary_len_ret = length;
  rewind(fp_binary);
  unsigned char *binary = (unsigned char *)malloc(length);
  if (fread(binary, length, 1, fp_binary) != 1) {
    fclose(fp_binary);
    free(binary);
    clu_Exit("ERROR: Failed to read %s\n", binary_file);
    return NULL;
  }
  return binary;
}


x86 assembly (clang, -O0):
<clu_LoadProgBinary>:
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   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <clu_LoadProgBinary+0x4c>
mov    -0x10(%rbp),%rsi
lea    0xe39(%rip),%rdi
call   1070 <clu_Exit@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <clu_LoadProgBinary+0xe6>
mov    -0x20(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x28(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     126e <clu_LoadProgBinary+0xde>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rsi
lea    0xdc1(%rip),%rdi
call   1070 <clu_Exit@plt>
movq   $0x0,-0x8(%rbp)
jmp    1276 <clu_LoadProgBinary+0xe6>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clu_LoadProgBinary>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe5b(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1217 <clu_LoadProgBinary+0x87>
mov    %rax,%rbx
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
movslq %eax,%r12
mov    %r12,(%r15)
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r12,%rdi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1229 <clu_LoadProgBinary+0x99>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xe08(%rip),%rdi
jmp    121e <clu_LoadProgBinary+0x8e>
lea    0xde5(%rip),%rdi
mov    %r14,%rsi
call   1070 <clu_Exit@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"ERROR: Failed to open %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [26 x i8] c"ERROR: Failed to read %s\0A\00"
define i8* @clu_LoadProgBinary(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %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
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @clu_Exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* null, i8** %3
br label %44
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %6
%21 = call i32 @ftell(i32* %20)
%22 = sext i32 %21 to i64
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = load i64*, i64** %5
store i64 %23, i64* %24
%25 = load i32*, i32** %6
%26 = call i32 @rewind(i32* %25)
%27 = load i64, i64* %7
%28 = call i64 @malloc(i64 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = load i64, i64* %7
%32 = load i32*, i32** %6
%33 = call i32 @fread(i8* %30, i64 %31, i32 1, i32* %32)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %42
35: ; preds = %16
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
%38 = load i8*, i8** %8
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %4
%41 = call i32 @clu_Exit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %40)
store i8* null, i8** %3
br label %44
42: ; preds = %16
%43 = load i8*, i8** %8
store i8* %43, i8** %3
br label %44
44: ; preds = %42, %35, %13
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"ERROR: Failed to open %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [26 x i8] c"ERROR: Failed to read %s\0A\00"
define i8* @clu_LoadProgBinary(i8* %0, i64* 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 %18, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i32 @ftell(i32* %3)
%9 = sext i32 %8 to i64
store i64 %9, i64* %1
%10 = tail call i32 @rewind(i32* %3)
%11 = tail call i64 @malloc(i64 %9)
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @fread(i8* %12, i64 %9, i32 1, i32* %3)
%14 = icmp eq i32 %13, 1
br i1 %14, label %21, label %15
15: ; preds = %5
%16 = tail call i32 @fclose(i32* %3)
%17 = tail call i32 @free(i8* %12)
br label %18
18: ; preds = %2, %15
%19 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), %15 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %2 ]
%20 = tail call i32 @clu_Exit(i8* %19, i8* %0)
br label %21
21: ; preds = %18, %5
%22 = phi i8* [ %12, %5 ], [ null, %18 ]
ret i8* %22
}