#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *readfile(const char *filename, size_t max_size, size_t *size_out) {
  FILE *fp;
  size_t size, pos, n, _out;
  char *buf;
  size_out = size_out ? size_out : &_out;
  fp = fopen(filename, "rb");
  size = 0;
  buf = 0;
  if (!fp) {
    goto fail;
  }
  fseek(fp, 0L, SEEK_END);
  size = ftell(fp);
  *size_out = size;
  if (max_size > 0 && size > max_size) {
    goto fail;
  }
  rewind(fp);
  buf = malloc(size ? size : 1);
  if (!buf) {
    goto fail;
  }
  pos = 0;
  while ((n = fread(buf + pos, 1, size - pos, fp))) {
    pos += n;
  }
  if (pos != size) {
    goto fail;
  }
  fclose(fp);
  *size_out = size;
  return buf;
fail:
  if (fp) {
    fclose(fp);
  }
  if (buf) {
    free(buf);
  }
  *size_out = size;
  return 0;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ac <readfile+0x2c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    11b9 <readfile+0x39>
lea    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    11b9 <readfile+0x39>
mov    -0x58(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x50(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f5 <readfile+0x75>
jmp    1306 <readfile+0x186>
mov    -0x28(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x18(%rbp)
jbe    1241 <readfile+0xc1>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1241 <readfile+0xc1>
jmp    1306 <readfile+0x186>
mov    -0x28(%rbp),%rdi
call   1060 <rewind@plt>
cmpq   $0x0,-0x30(%rbp)
je     1262 <readfile+0xe2>
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1270 <readfile+0xf0>
mov    $0x1,%eax
mov    %rax,-0x60(%rbp)
jmp    1270 <readfile+0xf0>
mov    -0x60(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    128d <readfile+0x10d>
jmp    1306 <readfile+0x186>
movq   $0x0,-0x38(%rbp)
mov    -0x50(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
sub    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     12d2 <readfile+0x152>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1295 <readfile+0x115>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     12e5 <readfile+0x165>
jmp    1306 <readfile+0x186>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1341 <readfile+0x1c1>
cmpq   $0x0,-0x28(%rbp)
je     131a <readfile+0x19a>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x50(%rbp)
je     132e <readfile+0x1ae>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
test   %rdx,%rdx
lea    0x8(%rsp),%r12
cmovne %rdx,%r12
lea    0xe5d(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1254 <readfile+0xd4>
mov    %rax,%r15
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
mov    %rax,%r14
mov    %rax,(%r12)
test   %rbx,%rbx
je     11e0 <readfile+0x60>
cmp    %rbx,%r14
ja     124a <readfile+0xca>
mov    %r15,%rdi
call   1060 <rewind@plt>
test   %r14,%r14
mov    $0x1,%edi
cmovne %r14,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     124a <readfile+0xca>
mov    %rax,%r13
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rcx,%rbx
lea    (%rcx,%r13,1),%rdi
mov    %r14,%rdx
sub    %rcx,%rdx
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
lea    (%rax,%rbx,1),%rcx
test   %rax,%rax
jne    1210 <readfile+0x90>
mov    %r15,%rdi
call   1050 <fclose@plt>
cmp    %rbx,%r14
je     125a <readfile+0xda>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    1257 <readfile+0xd7>
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    1257 <readfile+0xd7>
xor    %r14d,%r14d
xor    %r13d,%r13d
mov    %r14,(%r12)
mov    %r13,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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