#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 *);
long fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
unsigned char *readFile(const char *name, long *length) {
  FILE *file;
  unsigned char *buffer;
  long fileLen;
  long returnCode;
  file = fopen(name, "rb");
  if (!file) {
    *length = -1;
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  fileLen = ftell(file);
  fseek(file, 0, SEEK_SET);
  buffer = (unsigned char *)malloc(fileLen + 1);
  if (!buffer) {
    *length = -2;
    fclose(file);
    return NULL;
  }
  returnCode = fread(buffer, 1, fileLen, file);
  if (returnCode != fileLen) {
    *length = -3;
  } else {
    *length = fileLen;
  }
  fclose(file);
  return buffer;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    11a7 <readFile+0x47>
mov    -0x18(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    126d <readFile+0x10d>
mov    -0x20(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1219 <readFile+0xb9>
mov    -0x18(%rbp),%rax
movq   $0xfffffffffffffffe,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    126d <readFile+0x10d>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1251 <readFile+0xf1>
mov    -0x18(%rbp),%rax
movq   $0xfffffffffffffffd,(%rax)
jmp    125c <readFile+0xfc>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe8e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f0 <readFile+0x90>
mov    %rax,%rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r12
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r12),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f9 <readFile+0x99>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r12,%rax
mov    $0xfffffffffffffffd,%rax
cmove  %r12,%rax
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    120b <readFile+0xab>
movq   $0xffffffffffffffff,(%r14)
jmp    1208 <readFile+0xa8>
movq   $0xfffffffffffffffe,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@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"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @readFile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = load i64*, i64** %5
store i64 -1, i64* %15
store i8* null, i8** %3
br label %52
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 i64 @ftell(i32* %20)
store i64 %21, i64* %8
%22 = load i32*, i32** %6
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i64, i64* %8
%26 = add nsw i64 %25, 1
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %35, label %31
31: ; preds = %16
%32 = load i64*, i64** %5
store i64 -2, i64* %32
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
store i8* null, i8** %3
br label %52
35: ; preds = %16
%36 = load i8*, i8** %7
%37 = load i64, i64* %8
%38 = load i32*, i32** %6
%39 = call i64 @fread(i8* %36, i32 1, i64 %37, i32* %38)
store i64 %39, i64* %9
%40 = load i64, i64* %9
%41 = load i64, i64* %8
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = load i64*, i64** %5
store i64 -3, i64* %44
br label %48
45: ; preds = %35
%46 = load i64, i64* %8
%47 = load i64*, i64** %5
store i64 %46, i64* %47
br label %48
48: ; preds = %45, %43
%49 = load i32*, i32** %6
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %7
store i8* %51, i8** %3
br label %52
52: ; preds = %48, %31, %14
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @readFile(i8* nocapture %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 %5, label %6
5: ; preds = %2
store i64 -1, i64* %1
br label %23
6: ; preds = %2
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %3, i32 0, i32 %7)
%9 = tail call i64 @ftell(i32* %3)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %3, i32 0, i32 %10)
%12 = add nsw i64 %9, 1
%13 = tail call i64 @malloc(i64 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %18
16: ; preds = %6
store i64 -2, i64* %1
%17 = tail call i32 @fclose(i32* %3)
br label %23
18: ; preds = %6
%19 = tail call i64 @fread(i8* %14, i32 1, i64 %9, i32* %3)
%20 = icmp eq i64 %19, %9
%21 = select i1 %20, i64 %9, i64 -3
store i64 %21, i64* %1
%22 = tail call i32 @fclose(i32* %3)
br label %23
23: ; preds = %18, %16, %5
%24 = phi i8* [ %14, %18 ], [ null, %16 ], [ null, %5 ]
ret i8* %24
}