#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*/ *);
scalar_t__ malloc(size_t);
scalar_t__ 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 = (uint8_t *)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 = (uint8_t *)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 %85
20: ; preds = %3
store i64 0, i64* %9
store i64 128, i64* %10
%21 = load i64, i64* %10
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i32*
store i32* %23, i32** %11
%24 = load i32*, i32** %11
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %20
br label %82
27: ; preds = %20
br label %28
28: ; preds = %27, %68
%29 = load i64, i64* %9
%30 = load i64, i64* %10
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %51
32: ; preds = %28
%33 = load i64, i64* @SIZE_MAX
%34 = load i64, i64* %10
%35 = udiv i64 %33, %34
%36 = icmp ugt i64 2, %35
br i1 %36, label %37, label %38
37: ; preds = %32
br label %79
38: ; preds = %32
%39 = load i64, i64* %10
%40 = mul i64 2, %39
store i64 %40, i64* %12
%41 = load i32*, i32** %11
%42 = load i64, i64* %12
%43 = call i64 @realloc(i32* %41, i64 %42)
%44 = inttoptr i64 %43 to i32*
store i32* %44, i32** %13
%45 = load i32*, i32** %13
%46 = icmp ne i32* %45, null
br i1 %46, label %48, label %47
47: ; preds = %38
br label %79
48: ; preds = %38
%49 = load i32*, i32** %13
store i32* %49, i32** %11
%50 = load i64, i64* %12
store i64 %50, i64* %10
br label %51
51: ; preds = %48, %28
%52 = load i32*, i32** %11
%53 = load i64, i64* %9
%54 = getelementptr inbounds i32, i32* %52, i64 %53
%55 = load i64, i64* %10
%56 = load i64, i64* %9
%57 = sub i64 %55, %56
%58 = load i32*, i32** %8
%59 = call i64 @fread(i32* %54, i32 1, i64 %57, i32* %58)
store i64 %59, i64* %14
%60 = load i64, i64* %14
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %51
%63 = load i32*, i32** %8
%64 = call i64 @feof(i32* %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %62
br label %72
67: ; preds = %62
br label %79
68: ; preds = %51
%69 = load i64, i64* %14
%70 = load i64, i64* %9
%71 = add i64 %70, %69
store i64 %71, i64* %9
br label %28
72: ; preds = %66
%73 = load i32*, i32** %8
%74 = call i32 @fclose(i32* %73)
%75 = load i32*, i32** %11
%76 = load i32**, i32*** %6
store i32* %75, i32** %76
%77 = load i64, i64* %9
%78 = load i64*, i64** %7
store i64 %77, i64* %78
store i32 1, i32* %4
br label %86
79: ; preds = %67, %47, %37
%80 = load i32*, i32** %11
%81 = call i32 @free(i32* %80)
br label %82
82: ; preds = %79, %26
%83 = load i32*, i32** %8
%84 = call i32 @fclose(i32* %83)
br label %85
85: ; preds = %82, %19
store i32 0, i32* %4
br label %86
86: ; preds = %85, %72
%87 = load i32, i32* %4
ret i32 %87
}

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 %43, label %6
6: ; preds = %3
%7 = tail call i64 @malloc(i64 128)
%8 = icmp eq i64 %7, 0
br i1 %8, label %41, label %9
9: ; preds = %6
%10 = inttoptr i64 %7 to i32*
br label %11
11: ; preds = %9, %25
%12 = phi i32* [ %26, %25 ], [ %10, %9 ]
%13 = phi i64 [ %27, %25 ], [ 128, %9 ]
%14 = phi i64 [ %32, %25 ], [ 0, %9 ]
%15 = icmp eq i64 %14, %13
br i1 %15, label %16, label %25
16: ; preds = %11
%17 = load i64, i64* @SIZE_MAX
%18 = udiv i64 %17, %13
%19 = icmp ult i64 %18, 2
br i1 %19, label %38, label %20
20: ; preds = %16
%21 = shl i64 %13, 1
%22 = tail call i64 @realloc(i32* %12, i64 %21)
%23 = icmp eq i64 %22, 0
%24 = inttoptr i64 %22 to i32*
br i1 %23, label %38, label %25
25: ; preds = %20, %11
%26 = phi i32* [ %24, %20 ], [ %12, %11 ]
%27 = phi i64 [ %21, %20 ], [ %13, %11 ]
%28 = getelementptr inbounds i32, i32* %26, i64 %14
%29 = sub i64 %27, %14
%30 = tail call i64 @fread(i32* %28, i32 1, i64 %29, i32* %4)
%31 = icmp eq i64 %30, 0
%32 = add i64 %30, %14
br i1 %31, label %33, label %11
33: ; preds = %25
%34 = tail call i64 @feof(i32* %4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = tail call i32 @fclose(i32* %4)
store i32* %26, i32** %1
store i64 %14, i64* %2
br label %43
38: ; preds = %20, %16, %33
%39 = phi i32* [ %26, %33 ], [ %12, %16 ], [ %12, %20 ]
%40 = tail call i32 @free(i32* %39)
br label %41
41: ; preds = %6, %38
%42 = tail call i32 @fclose(i32* %4)
br label %43
43: ; preds = %41, %3, %36
%44 = phi i32 [ 1, %36 ], [ 0, %3 ], [ 0, %41 ]
ret i32 %44
}