#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);
  if (0 != filesize) {
    p = (unsigned char *)realloc(*out, filesize);
    if (!p) {
      puts("Failed to allocate memory");
      goto err;
    }
    *out = p;
    if (filesize != fread(p, sizeof(unsigned char), filesize, fp)) {
      perror("Failed to read file");
      goto err;
    }
  }
  *outlen = filesize;
  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    12db <read_file+0x15b>
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    12db <read_file+0x15b>
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    12db <read_file+0x15b>
mov    -0x20(%rbp),%rdi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
xor    %eax,%eax
cmp    -0x28(%rbp),%eax
je     12c9 <read_file+0x149>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1280 <read_file+0x100>
lea    0xdc9(%rip),%rdi
call   1030 <puts@plt>
jmp    12db <read_file+0x15b>
mov    -0x30(%rbp),%rcx
mov    -0x10(%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    12db <read_file+0x15b>
jmp    12c9 <read_file+0x149>
movslq -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12ef <read_file+0x16f>
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,%r12
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1225 <read_file+0xa5>
mov    %rax,%rbx
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
js     1239 <read_file+0xb9>
mov    %rbx,%rdi
call   1060 <ftell@plt>
test   %eax,%eax
js     1242 <read_file+0xc2>
mov    %eax,%ebp
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %ebp,%ebp
je     1210 <read_file+0x90>
mov    (%r12),%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
test   %rax,%rax
je     1217 <read_file+0x97>
mov    %rax,(%r12)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %eax,%ebp
jne    124b <read_file+0xcb>
mov    %ebp,%eax
mov    %rax,(%r14)
jmp    125d <read_file+0xdd>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
jmp    1257 <read_file+0xd7>
lea    0xdd7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r15d
jmp    1265 <read_file+0xe5>
lea    0xdd7(%rip),%rdi
jmp    1252 <read_file+0xd2>
lea    0xde2(%rip),%rdi
jmp    1252 <read_file+0xd2>
lea    0xe07(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r15d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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 %63
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 %63
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 %63
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 i32, i32* %9
%36 = icmp ne i32 0, %35
br i1 %36, label %37, label %59
37: ; preds = %31
%38 = load i8**, i8*** %5
%39 = load i8*, i8** %38
%40 = load i32, i32* %9
%41 = call i64 @realloc(i8* %39, i32 %40)
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = icmp ne i8* %43, null
br i1 %44, label %47, label %45
45: ; preds = %37
%46 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %63
47: ; preds = %37
%48 = load i8*, i8** %10
%49 = load i8**, i8*** %5
store i8* %48, i8** %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 = %47
%57 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %63
58: ; preds = %47
br label %59
59: ; preds = %58, %31
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = load i64*, i64** %6
store i64 %61, i64* %62
store i32 0, i32* %8
br label %63
63: ; preds = %59, %56, %45, %29, %22, %15
%64 = load i32*, i32** %7
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i32*, i32** %7
%68 = call i32 @fclose(i32* %67)
br label %69
69: ; preds = %66, %63
%70 = load i32, i32* %8
ret i32 %70
}

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 %35, 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 %37
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 %37
17: ; preds = %12
%18 = load i32, i32* @SEEK_SET
%19 = tail call i64 @fseek(i32* %4, i64 0, i32 %18)
%20 = icmp eq i32 %13, 0
br i1 %20, label %33, label %21
21: ; preds = %17
%22 = load i8*, i8** %1
%23 = tail call i64 @realloc(i8* %22, i32 %13)
%24 = inttoptr i64 %23 to i8*
%25 = icmp eq i64 %23, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %37
28: ; preds = %21
store i8* %24, i8** %1
%29 = tail call i32 @fread(i8* %24, i32 1, i32 %13, i32* %4)
%30 = icmp eq i32 %13, %29
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %37
33: ; preds = %28, %17
%34 = zext i32 %13 to i64
store i64 %34, i64* %2
br label %37
35: ; preds = %3
%36 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %40
37: ; preds = %33, %31, %26, %15, %10
%38 = phi i32 [ 1, %26 ], [ 0, %33 ], [ 1, %31 ], [ 1, %15 ], [ 1, %10 ]
%39 = tail call i32 @fclose(i32* %4)
br label %40
40: ; preds = %35, %37
%41 = phi i32 [ %38, %37 ], [ 1, %35 ]
ret i32 %41
}