#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(unsigned char *, long, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
unsigned char *ReadFile(const char *fileName) {
  FILE *fd;
  long fileSize;
  unsigned char *data = NULL;
  fd = fopen(fileName, "rb");
  if (!fd) {
    perror("Could not open file");
    goto Cleanup;
  }
  if (fseek(fd, 0, SEEK_END)) {
    fprintf(stderr, "fseek failed\n");
    goto Cleanup;
  }
  fileSize = ftell(fd);
  if (fseek(fd, 0, SEEK_SET)) {
    fprintf(stderr, "fseek failed\n");
    goto Cleanup;
  }
  data = (unsigned char *)malloc(fileSize);
  if (!data) {
    fprintf(stderr, "Out of memory\n");
    goto Cleanup;
  }
  fread(data, fileSize, 1, fd);
Cleanup:
  if (fd) {
    fclose(fd);
  }
  return data;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <ReadFile+0x44>
lea    0xe49(%rip),%rdi
call   10a0 <perror@plt>
jmp    1289 <ReadFile+0x109>
mov    -0x10(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11fc <ReadFile+0x7c>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1289 <ReadFile+0x109>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     1241 <ReadFile+0xc1>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1289 <ReadFile+0x109>
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1273 <ReadFile+0xf3>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1289 <ReadFile+0x109>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmpq   $0x0,-0x10(%rbp)
je     129d <ReadFile+0x11d>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadFile>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1226 <ReadFile+0xa6>
mov    %rax,%rbx
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    11d8 <ReadFile+0x58>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     1201 <ReadFile+0x81>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1050 <fprintf@plt>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1237 <ReadFile+0xb7>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
jmp    11f0 <ReadFile+0x70>
lea    0xdd6(%rip),%rdi
call   10a0 <perror@plt>
xor    %r14d,%r14d
jmp    11f8 <ReadFile+0x78>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
jmp    11e8 <ReadFile+0x68>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"Could not open file\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.2 = [14 x i8] c"fseek failed\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [15 x i8] c"Out of memory\0A\00"
define i8* @ReadFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %5
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %44
12: ; preds = %1
%13 = load i32*, i32** %3
%14 = load i32, i32* @SEEK_END
%15 = call i64 @fseek(i32* %13, i32 0, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %44
20: ; preds = %12
%21 = load i32*, i32** %3
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %4
%23 = load i32*, i32** %3
%24 = load i32, i32* @SEEK_SET
%25 = call i64 @fseek(i32* %23, i32 0, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %44
30: ; preds = %20
%31 = load i64, i64* %4
%32 = call i64 @malloc(i64 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %5
%34 = load i8*, i8** %5
%35 = icmp ne i8* %34, null
br i1 %35, label %39, label %36
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %44
39: ; preds = %30
%40 = load i8*, i8** %5
%41 = load i64, i64* %4
%42 = load i32*, i32** %3
%43 = call i32 @fread(i8* %40, i64 %41, i32 1, i32* %42)
br label %44
44: ; preds = %39, %36, %27, %17, %10
%45 = load i32*, i32** %3
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32*, i32** %3
%49 = call i32 @fclose(i32* %48)
br label %50
50: ; preds = %47, %44
%51 = load i8*, i8** %5
ret i8* %51
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"Could not open file\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.2 = [14 x i8] c"fseek failed\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [15 x i8] c"Out of memory\0A\00"
define i8* @ReadFile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %28, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i32 0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %30
11: ; preds = %4
%12 = tail call i64 @ftell(i32* %2)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @fseek(i32* %2, i32 0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %30
19: ; preds = %11
%20 = tail call i64 @malloc(i64 %12)
%21 = inttoptr i64 %20 to i8*
%22 = icmp eq i64 %20, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %30
26: ; preds = %19
%27 = tail call i32 @fread(i8* %21, i64 %12, i32 1, i32* %2)
br label %30
28: ; preds = %1
%29 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %33
30: ; preds = %26, %23, %16, %8
%31 = phi i8* [ null, %23 ], [ %21, %26 ], [ null, %16 ], [ null, %8 ]
%32 = tail call i32 @fclose(i32* %2)
br label %33
33: ; preds = %28, %30
%34 = phi i8* [ %31, %30 ], [ null, %28 ]
ret i8* %34
}