#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 off_t;
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 *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseeko(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftello(int /*<<< orphan*/ *);
unsigned char *malloc(int);
int util_read_file(const char *filename, unsigned char **out,
                   size_t *bytes_read, int null_terminate) {
  FILE *file = NULL;
  off_t len = 0;
  size_t read = 0;
  *out = NULL;
  if ((file = fopen(filename, "r")) == NULL) {
    goto err;
  }
  if (fseeko(file, 0, SEEK_END)) {
    goto err_close;
  }
  if ((len = ftello(file)) == -1) {
    goto err_close;
  }
  if (fseeko(file, 0, SEEK_SET)) {
    goto err_close;
  }
  if ((*out = malloc(len + (null_terminate ? 1 : 0))) == NULL) {
    goto err_close;
  }
  read = fread(*out, 1, len, file);
  if (bytes_read != NULL) {
    *bytes_read = read;
  }
  if (null_terminate) {
    (*out)[read] = 0;
  }
  fclose(file);
  return 0;
err_close:
  fclose(file);
err:
  return 1;
}


x86 assembly (clang, -O0):
<util_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11bc <util_read_file+0x5c>
jmp    12b5 <util_read_file+0x155>
mov    -0x30(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseeko@plt>
cmp    $0x0,%rax
je     11df <util_read_file+0x7f>
jmp    12ac <util_read_file+0x14c>
mov    -0x30(%rbp),%rdi
call   1030 <ftello@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11f9 <util_read_file+0x99>
jmp    12ac <util_read_file+0x14c>
mov    -0x30(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseeko@plt>
cmp    $0x0,%rax
je     121c <util_read_file+0xbc>
jmp    12ac <util_read_file+0x14c>
mov    -0x34(%rbp),%edi
mov    -0x24(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
add    %eax,%edi
call   1060 <malloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    124c <util_read_file+0xec>
jmp    12ac <util_read_file+0x14c>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     127e <util_read_file+0x11e>
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x24(%rbp)
je     1297 <util_read_file+0x137>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x40(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bc <util_read_file+0x15c>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%r14
movq   $0x0,(%rsi)
lea    0xe7e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11b6 <util_read_file+0x56>
mov    %rax,%rbx
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseeko@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     11cc <util_read_file+0x6c>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11bb <util_read_file+0x5b>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <ftello@plt>
cmp    $0xffffffff,%eax
je     11ac <util_read_file+0x4c>
mov    %eax,%r13d
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseeko@plt>
test   %rax,%rax
jne    11ac <util_read_file+0x4c>
cmp    $0x1,%r15d
mov    %r13d,%edi
sbb    $0xffffffff,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11ac <util_read_file+0x4c>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %r12,%r12
je     1227 <util_read_file+0xc7>
mov    %rax,(%r12)
xor    %ebp,%ebp
test   %r15d,%r15d
je     11ac <util_read_file+0x4c>
mov    (%r14),%rcx
movb   $0x0,(%rcx,%rax,1)
jmp    11ac <util_read_file+0x4c>

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

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