#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int getFileSize(char *);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
unsigned char *readBinaryFile(char *fileName, int *size) {
  FILE *f;
  unsigned char *data;
  f = fopen(fileName, "rb");
  if (!f) {
    printf("Error: couldn't open input file %s\n", fileName);
    return NULL;
  }
  *size = getFileSize(fileName);
  if (*size == 0) {
    printf("Error: empty file %s\n", fileName);
    return NULL;
  }
  data = malloc(*size);
  fread(data, 1, *size, f);
  fclose(f);
  return data;
}


x86 assembly (clang, -O0):
<readBinaryFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <readBinaryFile+0x4c>
mov    -0x10(%rbp),%rsi
lea    0xe69(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    121f <readBinaryFile+0xbf>
mov    -0x10(%rbp),%rdi
call   1060 <getFileSize@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11e7 <readBinaryFile+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    121f <readBinaryFile+0xbf>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readBinaryFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe8c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11b8 <readBinaryFile+0x58>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <getFileSize@plt>
mov    %eax,(%r15)
test   %eax,%eax
je     11c1 <readBinaryFile+0x61>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    11d2 <readBinaryFile+0x72>
lea    0xe44(%rip),%rdi
jmp    11c8 <readBinaryFile+0x68>
lea    0xe5f(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [36 x i8] c"Error: couldn't open input file %s\0A\00"
@.str.2 = [22 x i8] c"Error: empty file %s\0A\00"
define i8* @readBinaryFile(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i8* null, i8** %3
br label %37
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @getFileSize(i8* %16)
%18 = load i32*, i32** %5
store i32 %17, i32* %18
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %23)
store i8* null, i8** %3
br label %37
25: ; preds = %15
%26 = load i32*, i32** %5
%27 = load i32, i32* %26
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = load i32*, i32** %5
%31 = load i32, i32* %30
%32 = load i32*, i32** %6
%33 = call i32 @fread(i8* %29, i32 1, i32 %31, i32* %32)
%34 = load i32*, i32** %6
%35 = call i32 @fclose(i32* %34)
%36 = load i8*, i8** %7
store i8* %36, i8** %3
br label %37
37: ; preds = %25, %22, %12
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [36 x i8] c"Error: couldn't open input file %s\0A\00"
@.str.2 = [22 x i8] c"Error: empty file %s\0A\00"
define noalias i8* @readBinaryFile(i8* %0, i32* nocapture %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 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
7: ; preds = %2
%8 = tail call i32 @getFileSize(i8* %0)
store i32 %8, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %16
12: ; preds = %7
%13 = tail call i8* @malloc(i32 %8)
%14 = tail call i32 @fread(i8* %13, i32 1, i32 %8, i32* %3)
%15 = tail call i32 @fclose(i32* %3)
br label %16
16: ; preds = %12, %10, %5
%17 = phi i8* [ null, %10 ], [ %13, %12 ], [ null, %5 ]
ret i8* %17
}