#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 *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ realloc(unsigned char *, int);
int read_file(const char *filename, unsigned char **out, size_t *outlen) {
  FILE *fp = NULL;
  int fail = 1;
  int filesize;
  unsigned char *p;
  fp = fopen(filename, "rb");
  if (!fp) {
    perror("Could not open file");
    goto err;
  }
  if (0 > fseek(fp, 0L, SEEK_END)) {
    perror("count not seek file");
    goto err;
  }
  filesize = ftell(fp);
  if (0 > filesize) {
    perror("count not tell file");
    goto err;
  }
  fseek(fp, 0L, SEEK_SET);
  p = (unsigned char *)realloc(*out, filesize);
  if (!p) {
    puts("Failed to allocate memory");
    goto err;
  }
  *out = p;
  *outlen = filesize;
  if (filesize != fread(p, sizeof(unsigned char), filesize, fp)) {
    perror("Failed to read file");
    goto err;
  }
  fail = 0;
err:
  if (fp)
    fclose(fp);
  return fail;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d3 <read_file+0x53>
lea    0xe3a(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cb <read_file+0x14b>
mov    -0x20(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    1208 <read_file+0x88>
lea    0xe19(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cb <read_file+0x14b>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%eax
jle    1230 <read_file+0xb0>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cb <read_file+0x14b>
mov    -0x20(%rbp),%rdi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1275 <read_file+0xf5>
lea    0xdd4(%rip),%rdi
call   1030 <puts@plt>
jmp    12cb <read_file+0x14b>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movslq -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
je     12c4 <read_file+0x144>
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cb <read_file+0x14b>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12df <read_file+0x15f>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     124b <read_file+0xcb>
mov    %rax,%rbx
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
js     125f <read_file+0xdf>
mov    %rbx,%rdi
call   1060 <ftell@plt>
test   %eax,%eax
js     126d <read_file+0xed>
mov    %eax,%ebp
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    (%r15),%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
test   %rax,%rax
je     1225 <read_file+0xa5>
mov    %rax,(%r15)
mov    %ebp,%ecx
mov    %rcx,(%r14)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %eax,%ebp
je     1237 <read_file+0xb7>
lea    0xe3b(%rip),%rdi
call   10a0 <perror@plt>
jmp    1231 <read_file+0xb1>
lea    0xe13(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r12d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r12d
jmp    123f <read_file+0xbf>
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
jmp    1231 <read_file+0xb1>
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
jmp    1231 <read_file+0xb1>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"Could not open file\00"
@SEEK_END = global i32 0
@.str.2 = [20 x i8] c"count not seek file\00"
@.str.3 = [20 x i8] c"count not tell file\00"
@SEEK_SET = global i32 0
@.str.4 = [26 x i8] c"Failed to allocate memory\00"
@.str.5 = [20 x i8] c"Failed to read file\00"
define i32 @read_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
store i32* null, i32** %7
store i32 1, i32* %8
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %59
17: ; preds = %3
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i64 @fseek(i32* %18, i64 0, i32 %19)
%21 = icmp sgt i64 0, %20
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %59
24: ; preds = %17
%25 = load i32*, i32** %7
%26 = call i32 @ftell(i32* %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp sgt i32 0, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %59
31: ; preds = %24
%32 = load i32*, i32** %7
%33 = load i32, i32* @SEEK_SET
%34 = call i64 @fseek(i32* %32, i64 0, i32 %33)
%35 = load i8**, i8*** %5
%36 = load i8*, i8** %35
%37 = load i32, i32* %9
%38 = call i64 @realloc(i8* %36, i32 %37)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = icmp ne i8* %40, null
br i1 %41, label %44, label %42
42: ; preds = %31
%43 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %59
44: ; preds = %31
%45 = load i8*, i8** %10
%46 = load i8**, i8*** %5
store i8* %45, i8** %46
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = load i64*, i64** %6
store i64 %48, i64* %49
%50 = load i32, i32* %9
%51 = load i8*, i8** %10
%52 = load i32, i32* %9
%53 = load i32*, i32** %7
%54 = call i32 @fread(i8* %51, i32 1, i32 %52, i32* %53)
%55 = icmp ne i32 %50, %54
br i1 %55, label %56, label %58
56: ; preds = %44
%57 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %59
58: ; preds = %44
store i32 0, i32* %8
br label %59
59: ; preds = %58, %56, %42, %29, %22, %15
%60 = load i32*, i32** %7
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32*, i32** %7
%64 = call i32 @fclose(i32* %63)
br label %65
65: ; preds = %62, %59
%66 = load i32, i32* %8
ret i32 %66
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"Could not open file\00"
@SEEK_END = global i32 0
@.str.2 = [20 x i8] c"count not seek file\00"
@.str.3 = [20 x i8] c"count not tell file\00"
@SEEK_SET = global i32 0
@.str.4 = [26 x i8] c"Failed to allocate memory\00"
@.str.5 = [20 x i8] c"Failed to read file\00"
define i32 @read_file(i8* nocapture %0, i8** nocapture %1, i64* nocapture writeonly %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 %32, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i64 @fseek(i32* %4, i64 0, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %34
12: ; preds = %6
%13 = tail call i32 @ftell(i32* %4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %34
17: ; preds = %12
%18 = load i32, i32* @SEEK_SET
%19 = tail call i64 @fseek(i32* %4, i64 0, i32 %18)
%20 = load i8*, i8** %1
%21 = tail call i64 @realloc(i8* %20, i32 %13)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %34
26: ; preds = %17
store i8* %22, i8** %1
%27 = zext i32 %13 to i64
store i64 %27, i64* %2
%28 = tail call i32 @fread(i8* %22, i32 1, i32 %13, i32* %4)
%29 = icmp eq i32 %13, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %34
32: ; preds = %3
%33 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %37
34: ; preds = %26, %30, %24, %15, %10
%35 = phi i32 [ 0, %26 ], [ 1, %24 ], [ 1, %30 ], [ 1, %15 ], [ 1, %10 ]
%36 = tail call i32 @fclose(i32* %4)
br label %37
37: ; preds = %32, %34
%38 = phi i32 [ %35, %34 ], [ 1, %32 ]
ret i32 %38
}