#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*/ SEEK_SET;
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
unsigned char *load_image(const char *filename) {
  FILE *fp = fopen(filename, "r");
  if (fp == NULL) {
    printf("ERROR reading file %s", filename);
    return NULL;
  }
  if (fseek(fp, 0L, SEEK_END) != 0) {
    printf("ERROR seeking in file %s", filename);
    return NULL;
  }
  long bufsize = ftell(fp);
  if (bufsize == -1) {
    printf("ERROR getting file bufsize %s", filename);
    return NULL;
  }
  unsigned char *buf =
      (unsigned char *)malloc(sizeof(unsigned char) * bufsize + 1);
  if (buf == NULL) {
    printf("ERROR Could not allocate buffer %ld", bufsize);
    return NULL;
  }
  if (fseek(fp, 0L, SEEK_SET) != 0) {
    printf("ERROR seeking in file %s", filename);
    return NULL;
  }
  size_t readsize = fread(buf, sizeof(unsigned char), bufsize, fp);
  if (!(readsize > 0)) {
    printf("ERROR file is empty %s", filename);
    return NULL;
  }
  return buf;
}


x86 assembly (clang, -O0):
<load_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11aa <load_image+0x4a>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <load_image+0x18c>
mov    -0x18(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     11e9 <load_image+0x89>
mov    -0x10(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <load_image+0x18c>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1220 <load_image+0xc0>
mov    -0x10(%rbp),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <load_image+0x18c>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1261 <load_image+0x101>
mov    -0x20(%rbp),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <load_image+0x18c>
mov    -0x18(%rbp),%rdi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     12a0 <load_image+0x140>
mov    -0x10(%rbp),%rsi
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <load_image+0x18c>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     12e4 <load_image+0x184>
mov    -0x10(%rbp),%rsi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <load_image+0x18c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_image>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xe8e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11a0 <load_image+0x40>
mov    %rax,%rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
je     11c3 <load_image+0x63>
lea    0xe7a(%rip),%rdi
jmp    11a7 <load_image+0x47>
lea    0xe5b(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1050 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     121d <load_image+0xbd>
mov    %rax,%r12
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1226 <load_image+0xc6>
mov    %rax,%r14
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
jne    1197 <load_image+0x37>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11b4 <load_image+0x54>
lea    0xe58(%rip),%rdi
jmp    11a7 <load_image+0x47>
lea    0xe0d(%rip),%rdi
jmp    11a7 <load_image+0x47>
lea    0xe22(%rip),%rdi
xor    %r14d,%r14d
mov    %r12,%rsi
jmp    11ad <load_image+0x4d>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"ERROR reading file %s\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"ERROR seeking in file %s\00"
@.str.3 = [30 x i8] c"ERROR getting file bufsize %s\00"
@.str.4 = [36 x i8] c"ERROR Could not allocate buffer %ld\00"
@SEEK_SET = global i32 0
@.str.5 = [23 x i8] c"ERROR file is empty %s\00"
define i8* @load_image(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i8* null, i8** %2
br label %63
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = load i32, i32* @SEEK_END
%18 = call i64 @fseek(i32* %16, i64 0, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %21)
store i8* null, i8** %2
br label %63
23: ; preds = %15
%24 = load i32*, i32** %4
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %5
%26 = load i64, i64* %5
%27 = icmp eq i64 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i8*, i8** %3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %29)
store i8* null, i8** %2
br label %63
31: ; preds = %23
%32 = load i64, i64* %5
%33 = mul i64 1, %32
%34 = add i64 %33, 1
%35 = trunc i64 %34 to i32
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = load i64, i64* %5
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i64 %41)
store i8* null, i8** %2
br label %63
43: ; preds = %31
%44 = load i32*, i32** %4
%45 = load i32, i32* @SEEK_SET
%46 = call i64 @fseek(i32* %44, i64 0, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i8*, i8** %3
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %49)
store i8* null, i8** %2
br label %63
51: ; preds = %43
%52 = load i8*, i8** %6
%53 = load i64, i64* %5
%54 = load i32*, i32** %4
%55 = call i64 @fread(i8* %52, i32 1, i64 %53, i32* %54)
store i64 %55, i64* %7
%56 = load i64, i64* %7
%57 = icmp ugt i64 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %51
%59 = load i8*, i8** %3
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %59)
store i8* null, i8** %2
br label %63
61: ; preds = %51
%62 = load i8*, i8** %6
store i8* %62, i8** %2
br label %63
63: ; preds = %61, %58, %48, %40, %28, %20, %12
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"ERROR reading file %s\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"ERROR seeking in file %s\00"
@.str.3 = [30 x i8] c"ERROR getting file bufsize %s\00"
@.str.4 = [36 x i8] c"ERROR Could not allocate buffer %ld\00"
@SEEK_SET = global i32 0
@.str.5 = [23 x i8] c"ERROR file is empty %s\00"
define i8* @load_image(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %36
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i64 @fseek(i32* %2, i64 0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %36
12: ; preds = %6
%13 = tail call i64 @ftell(i32* %2)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %36
17: ; preds = %12
%18 = trunc i64 %13 to i32
%19 = add i32 %18, 1
%20 = tail call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i64 %13)
br label %36
25: ; preds = %17
%26 = load i32, i32* @SEEK_SET
%27 = tail call i64 @fseek(i32* %2, i64 0, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %36
31: ; preds = %25
%32 = tail call i64 @fread(i8* %21, i32 1, i64 %13, i32* %2)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %36
36: ; preds = %15, %34, %31, %29, %23, %10, %4
%37 = phi i8* [ null, %4 ], [ null, %10 ], [ null, %15 ], [ null, %23 ], [ null, %29 ], [ null, %34 ], [ %21, %31 ]
ret i8* %37
}