#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;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
void *malloc(long);
void *load_file(char *path, long *size) {
  if (!size) {
    long local_size;
    size = &local_size;
  }
  void *mem = NULL;
  FILE *fp = fopen(path, "r");
  if (!fp)
    return NULL;
  if (fseek(fp, 0, SEEK_END) != 0)
    goto error;
  *size = ftell(fp);
  if (*size == -1)
    return NULL;
  if (fseek(fp, 0, SEEK_SET) != 0)
    goto error;
  mem = malloc(*size);
  if (!mem)
    goto error;
  if (fread(mem, *size, 1, fp) != 1)
    goto error;
  if (fclose(fp) != 0)
    goto error;
  return mem;
error:
  if (fp)
    fclose(fp);
  if (mem)
    free(mem);
  return NULL;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1193 <load_file+0x23>
lea    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c7 <load_file+0x57>
movq   $0x0,-0x8(%rbp)
jmp    12d7 <load_file+0x167>
mov    -0x30(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     11ea <load_file+0x7a>
jmp    12a7 <load_file+0x137>
mov    -0x30(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0xffffffffffffffff,(%rax)
jne    1218 <load_file+0xa8>
movq   $0x0,-0x8(%rbp)
jmp    12d7 <load_file+0x167>
mov    -0x30(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
je     123b <load_file+0xcb>
jmp    12a7 <load_file+0x137>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    125b <load_file+0xeb>
jmp    12a7 <load_file+0x137>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1282 <load_file+0x112>
jmp    12a7 <load_file+0x137>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     129a <load_file+0x12a>
jmp    12a7 <load_file+0x137>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d7 <load_file+0x167>
cmpq   $0x0,-0x30(%rbp)
je     12bb <load_file+0x14b>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     12cf <load_file+0x15f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
mov    %rsp,%rbx
cmovne %rsi,%rbx
lea    0xe77(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11b6 <load_file+0x46>
mov    %rax,%r12
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     11c8 <load_file+0x58>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r12,%rdi
call   1060 <ftell@plt>
mov    %rax,(%rbx)
cmp    $0xffffffffffffffff,%rax
je     11b6 <load_file+0x46>
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    11ae <load_file+0x3e>
mov    (%rbx),%r15
mov    %r15,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11ae <load_file+0x3e>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1229 <load_file+0xb9>
mov    %r12,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     11b9 <load_file+0x49>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11b6 <load_file+0x46>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @load_file(i8* nocapture %0, i64* %1) {
%3 = alloca i64
%4 = icmp eq i64* %1, null
%5 = select i1 %4, i64* %3, i64* %1
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %34, label %8
8: ; preds = %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i64 @fseek(i32* %6, i32 0, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %8
%13 = tail call i64 @ftell(i32* %6)
store i64 %13, i64* %5
%14 = icmp eq i64 %13, -1
br i1 %14, label %34, label %15
15: ; preds = %12
%16 = load i32, i32* @SEEK_SET
%17 = tail call i64 @fseek(i32* %6, i32 0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %29
19: ; preds = %15
%20 = load i64, i64* %5
%21 = tail call i8* @malloc(i64 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = tail call i32 @fread(i8* %21, i64 %20, i32 1, i32* %6)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = tail call i64 @fclose(i32* %6)
%28 = icmp eq i64 %27, 0
br i1 %28, label %34, label %31
29: ; preds = %8, %15, %19
%30 = tail call i64 @fclose(i32* %6)
br label %34
31: ; preds = %26, %23
%32 = tail call i64 @fclose(i32* %6)
%33 = tail call i32 @free(i8* %21)
br label %34
34: ; preds = %29, %31, %26, %12, %2
%35 = phi i8* [ null, %2 ], [ null, %12 ], [ %21, %26 ], [ null, %31 ], [ null, %29 ]
ret i8* %35
}