#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*/ dod_exit(char *, char const *, ...);
int /*<<< orphan*/ errno;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t dod_loadFile(char const *path, char *buffer, size_t length) {
  FILE *file = fopen(path, "rb");
  if (file == NULL)
    dod_exit("dod:fopen(%s):%s", path, strerror(errno));
  size_t nread = fread(buffer, 1, length, file);
  if (ferror(file))
    dod_exit("dod:fread(%s):an error occurred", path);
  if (!feof(file))
    dod_exit("dod:fread(%s):file too large", path);
  int result = fclose(file);
  if (result != 0)
    dod_exit("dod:fclose(%s):%s", path, strerror(errno));
  return nread;
}


x86 assembly (clang, -O0):
<dod_loadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cd <dod_loadFile+0x5d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <dod_exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     120c <dod_loadFile+0x9c>
mov    -0x8(%rbp),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <dod_exit@plt>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
jne    1230 <dod_loadFile+0xc0>
mov    -0x8(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <dod_exit@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1270 <dod_loadFile+0x100>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <dod_exit@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dod_loadFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b6 <dod_loadFile+0x46>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe59(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <dod_exit@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11ea <dod_loadFile+0x7a>
lea    0xe34(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <dod_exit@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1207 <dod_loadFile+0x97>
lea    0xe37(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <dod_exit@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %eax,%eax
je     1234 <dod_loadFile+0xc4>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe29(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <dod_exit@plt>
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 = [17 x i8] c"dod:fopen(%s):%s\00"
@errno = global i32 0
@.str.2 = [32 x i8] c"dod:fread(%s):an error occurred\00"
@.str.3 = [29 x i8] c"dod:fread(%s):file too large\00"
@.str.4 = [18 x i8] c"dod:fclose(%s):%s\00"
define i64 @dod_loadFile(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%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** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %17)
br label %19
19: ; preds = %14, %3
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = load i32*, i32** %7
%23 = call i64 @fread(i8* %20, i32 1, i64 %21, i32* %22)
store i64 %23, i64* %8
%24 = load i32*, i32** %7
%25 = call i64 @ferror(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i8*, i8** %4
%29 = call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %19
%31 = load i32*, i32** %7
%32 = call i32 @feof(i32* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = load i8*, i8** %4
%36 = call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34, %30
%38 = load i32*, i32** %7
%39 = call i32 @fclose(i32* %38)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i8*, i8** %4
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 %45)
br label %47
47: ; preds = %42, %37
%48 = load i64, i64* %8
ret i64 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [17 x i8] c"dod:fopen(%s):%s\00"
@errno = global i32 0
@.str.2 = [32 x i8] c"dod:fread(%s):an error occurred\00"
@.str.3 = [29 x i8] c"dod:fread(%s):file too large\00"
@.str.4 = [18 x i8] c"dod:fclose(%s):%s\00"
define i64 @dod_loadFile(i8* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %10
10: ; preds = %6, %3
%11 = tail call i64 @fread(i8* %1, i32 1, i64 %2, i32* %4)
%12 = tail call i64 @ferror(i32* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %16
16: ; preds = %14, %10
%17 = tail call i32 @feof(i32* %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %19, %16
%22 = tail call i32 @fclose(i32* %4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = tail call i32 @strerror(i32 %25)
%27 = tail call i32 (i8*, i8*, ...) @dod_exit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %26)
br label %28
28: ; preds = %24, %21
ret i64 %11
}