#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*/ assert(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
int /*<<< orphan*/ fread(unsigned char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
unsigned char *malloc(long);
int load_file(const char *const fname, unsigned char **pdata,
              size_t *const psize) {
  FILE *fp;
  long size;
  unsigned char *data;
  assert(fname != NULL);
  assert(pdata != NULL);
  assert(psize != NULL);
  *pdata = NULL;
  *psize = 0;
  fp = fopen(fname, "rb");
  if (fp == NULL)
    return errno;
  if (fseek(fp, 0, SEEK_END) < 0) {
    int err = errno;
    fclose(fp);
    return err;
  }
  size = ftell(fp);
  if (size == -1) {
    int err = errno;
    fclose(fp);
    return err;
  }
  if (fseek(fp, 0, SEEK_SET) < 0) {
    int err = errno;
    fclose(fp);
    return err;
  }
  data = malloc(size);
  if (data == NULL) {
    int err = errno;
    fclose(fp);
    return err;
  }
  *psize = size;
  *pdata = data;
  fread(data, 1, size, fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1200 <load_file+0x90>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1328 <load_file+0x1b8>
mov    -0x28(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    123e <load_file+0xce>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1328 <load_file+0x1b8>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1276 <load_file+0x106>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1328 <load_file+0x1b8>
mov    -0x28(%rbp),%rdi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    12b4 <load_file+0x144>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1328 <load_file+0x1b8>
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12ec <load_file+0x17c>
mov    0x2d05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1328 <load_file+0x1b8>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
movq   $0x0,(%r15)
movq   $0x0,(%r14)
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     125a <load_file+0xea>
mov    %rax,%rbx
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
js     1247 <load_file+0xd7>
mov    %rbx,%rdi
call   1060 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1247 <load_file+0xd7>
mov    %rax,%r12
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
js     1247 <load_file+0xd7>
mov    %r12,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1247 <load_file+0xd7>
mov    %r12,(%r14)
mov    %rax,(%r15)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    1263 <load_file+0xf3>
mov    0x2d8a(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1263 <load_file+0xf3>
mov    0x2d77(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @load_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8**, i8*** %6
%20 = icmp ne i8** %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i64*, i64** %7
%24 = icmp ne i64* %23, null
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i8**, i8*** %6
store i8* null, i8** %27
%28 = load i64*, i64** %7
store i64 0, i64* %28
%29 = load i8*, i8** %5
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %30, i32** %8
%31 = load i32*, i32** %8
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %3
%34 = load i32, i32* @errno
store i32 %34, i32* %4
br label %86
35: ; preds = %3
%36 = load i32*, i32** %8
%37 = load i32, i32* @SEEK_END
%38 = call i64 @fseek(i32* %36, i32 0, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32, i32* @errno
store i32 %41, i32* %11
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %11
store i32 %44, i32* %4
br label %86
45: ; preds = %35
%46 = load i32*, i32** %8
%47 = call i64 @ftell(i32* %46)
store i64 %47, i64* %9
%48 = load i64, i64* %9
%49 = icmp eq i64 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = load i32, i32* @errno
store i32 %51, i32* %12
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
%54 = load i32, i32* %12
store i32 %54, i32* %4
br label %86
55: ; preds = %45
%56 = load i32*, i32** %8
%57 = load i32, i32* @SEEK_SET
%58 = call i64 @fseek(i32* %56, i32 0, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i32, i32* @errno
store i32 %61, i32* %13
%62 = load i32*, i32** %8
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %13
store i32 %64, i32* %4
br label %86
65: ; preds = %55
%66 = load i64, i64* %9
%67 = call i8* @malloc(i64 %66)
store i8* %67, i8** %10
%68 = load i8*, i8** %10
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %75
70: ; preds = %65
%71 = load i32, i32* @errno
store i32 %71, i32* %14
%72 = load i32*, i32** %8
%73 = call i32 @fclose(i32* %72)
%74 = load i32, i32* %14
store i32 %74, i32* %4
br label %86
75: ; preds = %65
%76 = load i64, i64* %9
%77 = load i64*, i64** %7
store i64 %76, i64* %77
%78 = load i8*, i8** %10
%79 = load i8**, i8*** %6
store i8* %78, i8** %79
%80 = load i8*, i8** %10
%81 = load i64, i64* %9
%82 = load i32*, i32** %8
%83 = call i32 @fread(i8* %80, i32 1, i64 %81, i32* %82)
%84 = load i32*, i32** %8
%85 = call i32 @fclose(i32* %84)
store i32 0, i32* %4
br label %86
86: ; preds = %75, %70, %60, %50, %40, %33
%87 = load i32, i32* %4
ret i32 %87
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @load_file(i8* %0, i8** writeonly %1, i64* writeonly %2) {
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8** %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i64* %2, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
store i8* null, i8** %1
store i64 0, i64* %2
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* @errno
br label %46
17: ; preds = %3
%18 = load i32, i32* @SEEK_END
%19 = tail call i64 @fseek(i32* %13, i32 0, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @errno
%23 = tail call i32 @fclose(i32* %13)
br label %46
24: ; preds = %17
%25 = tail call i64 @ftell(i32* %13)
%26 = icmp eq i64 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = tail call i32 @fclose(i32* %13)
br label %46
30: ; preds = %24
%31 = load i32, i32* @SEEK_SET
%32 = tail call i64 @fseek(i32* %13, i32 0, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* @errno
%36 = tail call i32 @fclose(i32* %13)
br label %46
37: ; preds = %30
%38 = tail call i8* @malloc(i64 %25)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = tail call i32 @fclose(i32* %13)
br label %46
43: ; preds = %37
store i64 %25, i64* %2
store i8* %38, i8** %1
%44 = tail call i32 @fread(i8* %38, i32 1, i64 %25, i32* %13)
%45 = tail call i32 @fclose(i32* %13)
br label %46
46: ; preds = %43, %40, %34, %27, %21, %15
%47 = phi i32 [ %16, %15 ], [ %22, %21 ], [ %28, %27 ], [ %35, %34 ], [ %41, %40 ], [ 0, %43 ]
ret i32 %47
}