#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
size_t SIZE_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, size_t);
int read_file(const char *file, uint8_t **out_data, size_t *out_len) {
  FILE *f = fopen(file, "r");
  if (!f) {
    goto fail0;
  }
  size_t buf_len = 0;
  size_t buf_size = 128;
  uint8_t *buf = malloc(buf_size);
  if (!buf) {
    goto fail1;
  }
  while (1) {
    if (buf_len == buf_size) {
      if (2 > SIZE_MAX / buf_size) {
        goto fail;
      }
      size_t newsize = 2 * buf_size;
      uint8_t *newbuf = realloc(buf, newsize);
      if (!newbuf) {
        goto fail;
      }
      buf = newbuf;
      buf_size = newsize;
    }
    size_t bytes = fread(buf + buf_len, 1, buf_size - buf_len, f);
    if (bytes == 0) {
      if (feof(f)) {
        break;
      }
      goto fail;
    }
    buf_len += bytes;
  }
  fclose(f);
  *out_data = buf;
  *out_len = buf_len;
  return 1;
fail:
  free(buf);
fail1:
  fclose(f);
fail0:
  return 0;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a8 <read_file+0x38>
jmp    12ec <read_file+0x17c>
movq   $0x0,-0x30(%rbp)
movq   $0x80,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11d5 <read_file+0x65>
jmp    12e3 <read_file+0x173>
jmp    11da <read_file+0x6a>
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    124d <read_file+0xdd>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x38(%rbp)
mov    %rax,%rcx
mov    $0x2,%eax
cmp    %rcx,%rax
jbe    1210 <read_file+0xa0>
jmp    12da <read_file+0x16a>
mov    -0x38(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    123d <read_file+0xcd>
jmp    12da <read_file+0x16a>
mov    -0x50(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    129e <read_file+0x12e>
mov    -0x28(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     1299 <read_file+0x129>
jmp    12af <read_file+0x13f>
jmp    12da <read_file+0x16a>
mov    -0x58(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11da <read_file+0x6a>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12f3 <read_file+0x183>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     127e <read_file+0x10e>
mov    %rax,%r13
mov    $0x80,%ebp
mov    $0x80,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1276 <read_file+0x106>
mov    %rax,%r14
mov    %r12,0x8(%rsp)
mov    %r15,0x10(%rsp)
xor    %ecx,%ecx
mov    0x2e0c(%rip),%r12
jmp    11f6 <read_file+0x86>
xchg   %ax,%ax
mov    %r14,%rbx
lea    (%rbx,%r15,4),%rdi
mov    %rbp,%rdx
sub    %r15,%rdx
mov    $0x1,%esi
mov    %r13,%rcx
call   1040 <fread@plt>
lea    (%rax,%r15,1),%rcx
mov    %rbx,%r14
test   %rax,%rax
je     123d <read_file+0xcd>
mov    %rcx,%r15
cmp    %rbp,%rcx
jne    11d0 <read_file+0x60>
mov    (%r12),%rax
mov    %rax,%rcx
or     %rbp,%rcx
shr    $0x20,%rcx
je     121b <read_file+0xab>
xor    %edx,%edx
div    %rbp
cmp    $0x2,%rax
jae    1225 <read_file+0xb5>
jmp    126c <read_file+0xfc>
xor    %edx,%edx
div    %ebp
cmp    $0x2,%rax
jb     126c <read_file+0xfc>
add    %rbp,%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
call   1080 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d3 <read_file+0x63>
jmp    126c <read_file+0xfc>
mov    %r13,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     1269 <read_file+0xf9>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax)
mov    0x10(%rsp),%rax
mov    %r15,(%rax)
mov    $0x1,%ebx
jmp    127e <read_file+0x10e>
mov    %rbx,%r14
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SIZE_MAX = global i64 0
define i32 @read_file(i8* nocapture %0, i32** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %40, label %6
6: ; preds = %3
%7 = tail call i32* @malloc(i64 128)
%8 = icmp eq i32* %7, null
br i1 %8, label %38, label %9
9: ; preds = %6, %22
%10 = phi i32* [ %23, %22 ], [ %7, %6 ]
%11 = phi i64 [ %24, %22 ], [ 128, %6 ]
%12 = phi i64 [ %29, %22 ], [ 0, %6 ]
%13 = icmp eq i64 %12, %11
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = load i64, i64* @SIZE_MAX
%16 = udiv i64 %15, %11
%17 = icmp ult i64 %16, 2
br i1 %17, label %35, label %18
18: ; preds = %14
%19 = shl i64 %11, 1
%20 = tail call i32* @realloc(i32* %10, i64 %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %35, label %22
22: ; preds = %18, %9
%23 = phi i32* [ %20, %18 ], [ %10, %9 ]
%24 = phi i64 [ %19, %18 ], [ %11, %9 ]
%25 = getelementptr inbounds i32, i32* %23, i64 %12
%26 = sub i64 %24, %12
%27 = tail call i64 @fread(i32* %25, i32 1, i64 %26, i32* %4)
%28 = icmp eq i64 %27, 0
%29 = add i64 %27, %12
br i1 %28, label %30, label %9
30: ; preds = %22
%31 = tail call i64 @feof(i32* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 @fclose(i32* %4)
store i32* %23, i32** %1
store i64 %12, i64* %2
br label %40
35: ; preds = %18, %14, %30
%36 = phi i32* [ %23, %30 ], [ %10, %14 ], [ %10, %18 ]
%37 = tail call i32 @free(i32* %36)
br label %38
38: ; preds = %6, %35
%39 = tail call i32 @fclose(i32* %4)
br label %40
40: ; preds = %38, %3, %33
%41 = phi i32 [ 1, %33 ], [ 0, %3 ], [ 0, %38 ]
ret i32 %41
}