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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(unsigned char const *, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, size_t);
scalar_t__ xcalloc(size_t, int);
int /*<<< orphan*/ xfree(unsigned char *);
scalar_t__ xmalloc(int);
scalar_t__ xrealloc(unsigned char *, size_t);
int text_read_file(const unsigned char *path, int reserve, unsigned char **out,
                   size_t *out_len) {
  unsigned char read_buf[512];
  unsigned char *buf = 0;
  size_t buf_len = 0, read_len = 0;
  FILE *f = 0;
  if (reserve <= 0)
    reserve = 1;
  if (!(f = fopen(path, "r"))) {
    return -1;
  }
  while (1) {
    read_len = fread(read_buf, 1, sizeof(read_buf), f);
    if (!read_len)
      break;
    if (!buf_len) {
      buf = (unsigned char *)xcalloc(read_len + reserve, 1);
      memcpy(buf, read_buf, read_len);
      buf_len = read_len;
    } else {
      buf = (unsigned char *)xrealloc(buf, buf_len + reserve + read_len);
      memcpy(buf + buf_len, read_buf, read_len);
      buf_len += read_len;
      buf[buf_len] = 0;
    }
  }
  if (ferror(f)) {
    xfree(buf);
    fclose(f);
    return -1;
  }
  if (!buf_len) {
    buf = (unsigned char *)xmalloc(reserve);
    buf[0] = 0;
    buf_len = 0;
  }
  if (out)
    *out = buf;
  if (out_len)
    *out_len = buf_len;
  return (int)buf_len;
}


x86 assembly (clang, -O0):
<text_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x238(%rbp)
movq   $0x0,-0x240(%rbp)
movq   $0x0,-0x248(%rbp)
movq   $0x0,-0x250(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11e7 <text_read_file+0x57>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x250(%rbp)
cmp    $0x0,%rax
jne    1214 <text_read_file+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <text_read_file+0x237>
jmp    1219 <text_read_file+0x89>
lea    -0x230(%rbp),%rdi
mov    -0x250(%rbp),%rcx
mov    $0x1,%esi
mov    $0x200,%edx
call   1040 <fread@plt>
mov    %rax,-0x248(%rbp)
cmpq   $0x0,-0x248(%rbp)
jne    1250 <text_read_file+0xc0>
jmp    131f <text_read_file+0x18f>
cmpq   $0x0,-0x240(%rbp)
jne    12aa <text_read_file+0x11a>
mov    -0x248(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    $0x1,%esi
call   10b0 <xcalloc@plt>
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rdi
lea    -0x230(%rbp),%rsi
mov    -0x248(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x248(%rbp),%rax
mov    %rax,-0x240(%rbp)
jmp    131a <text_read_file+0x18a>
mov    -0x238(%rbp),%rdi
mov    -0x240(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
add    -0x248(%rbp),%rsi
call   1070 <xrealloc@plt>
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rdi
add    -0x240(%rbp),%rdi
lea    -0x230(%rbp),%rsi
mov    -0x248(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x248(%rbp),%rax
add    -0x240(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x238(%rbp),%rax
mov    -0x240(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1219 <text_read_file+0x89>
mov    -0x250(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1359 <text_read_file+0x1c9>
mov    -0x238(%rbp),%rdi
call   1060 <xfree@plt>
mov    -0x250(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <text_read_file+0x237>
cmpq   $0x0,-0x240(%rbp)
jne    138b <text_read_file+0x1fb>
mov    -0x14(%rbp),%edi
call   1080 <xmalloc@plt>
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x240(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     13a4 <text_read_file+0x214>
mov    -0x238(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     13bd <text_read_file+0x22d>
mov    -0x240(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x240(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<text_read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
test   %esi,%esi
mov    $0x1,%r13d
cmovg  %esi,%r13d
lea    0xe46(%rip),%rsi
call   10a0 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1305 <text_read_file+0x175>
mov    %rax,%r12
lea    0x20(%rsp),%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
mov    %r12,0x8(%rsp)
je     12b0 <text_read_file+0x120>
mov    %rax,%r14
mov    %r15,0x18(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r13d,0x4(%rsp)
mov    %r13d,%r13d
xor    %ebp,%ebp
lea    0x20(%rsp),%r15
xor    %ebx,%ebx
jmp    126e <text_read_file+0xde>
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r14,%rbp,1),%r12
lea    (%r12,%r13,1),%rsi
mov    %rbx,%rdi
call   1070 <xrealloc@plt>
mov    %rax,%rbx
add    %rax,%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1090 <memcpy@plt>
movb   $0x0,(%rbx,%r12,1)
mov    %r12,%rbp
mov    0x8(%rsp),%r12
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x200,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
test   %rax,%rax
je     1297 <text_read_file+0x107>
test   %rbp,%rbp
jne    1220 <text_read_file+0x90>
lea    (%r14,%r13,1),%rdi
mov    $0x1,%esi
call   10b0 <xcalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1090 <memcpy@plt>
mov    %r14,%rbp
jmp    1251 <text_read_file+0xc1>
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
mov    0x18(%rsp),%r15
je     12d6 <text_read_file+0x146>
mov    $0xffffffff,%ebp
jmp    12c2 <text_read_file+0x132>
mov    %rbx,%r14
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     12e5 <text_read_file+0x155>
xor    %ebx,%ebx
mov    %rbx,%rdi
call   1060 <xfree@plt>
mov    0x8(%rsp),%rdi
call   1050 <fclose@plt>
jmp    1305 <text_read_file+0x175>
test   %rbp,%rbp
mov    0x10(%rsp),%r14
mov    0x4(%rsp),%r13d
jne    12f5 <text_read_file+0x165>
mov    %r13d,%edi
call   1080 <xmalloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
xor    %ebp,%ebp
test   %r15,%r15
je     12fd <text_read_file+0x16d>
mov    %rbx,(%r15)
test   %r14,%r14
je     1305 <text_read_file+0x175>
mov    %rbp,(%r14)
mov    %ebp,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @text_read_file(i8* nocapture %0, i32 %1, i8** writeonly %2, i64* writeonly %3) {
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%7 = icmp sgt i32 %1, 1
%8 = select i1 %7, i32 %1, i32 1
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %64, label %11
11: ; preds = %4
%12 = call i64 @fread(i8* %6, i32 1, i32 512, i32* %9)
%13 = icmp eq i64 %12, 0
br i1 %13, label %42, label %14
14: ; preds = %11
%15 = zext i32 %8 to i64
br label %16
16: ; preds = %14, %34
%17 = phi i64 [ %12, %14 ], [ %37, %34 ]
%18 = phi i64 [ 0, %14 ], [ %36, %34 ]
%19 = phi i8* [ null, %14 ], [ %35, %34 ]
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = add i64 %17, %15
%23 = call i64 @xcalloc(i64 %22, i32 1)
%24 = inttoptr i64 %23 to i8*
%25 = call i32 @memcpy(i8* %24, i8* %6, i64 %17)
br label %34
26: ; preds = %16
%27 = add i64 %17, %18
%28 = add i64 %27, %15
%29 = call i64 @xrealloc(i8* %19, i64 %28)
%30 = inttoptr i64 %29 to i8*
%31 = getelementptr inbounds i8, i8* %30, i64 %18
%32 = call i32 @memcpy(i8* %31, i8* %6, i64 %17)
%33 = getelementptr inbounds i8, i8* %30, i64 %27
store i8 0, i8* %33
br label %34
34: ; preds = %26, %21
%35 = phi i8* [ %30, %26 ], [ %24, %21 ]
%36 = phi i64 [ %27, %26 ], [ %17, %21 ]
%37 = call i64 @fread(i8* %6, i32 1, i32 512, i32* %9)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %16
39: ; preds = %34
%40 = call i64 @ferror(i32* %9)
%41 = icmp eq i64 %40, 0
br i1 %41, label %49, label %45
42: ; preds = %11
%43 = call i64 @ferror(i32* %9)
%44 = icmp eq i64 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %42, %39
%46 = phi i8* [ null, %42 ], [ %35, %39 ]
%47 = call i32 @xfree(i8* %46)
%48 = call i32 @fclose(i32* %9)
br label %64
49: ; preds = %39
%50 = icmp eq i64 %36, 0
br i1 %50, label %51, label %54
51: ; preds = %42, %49
%52 = call i64 @xmalloc(i32 %8)
%53 = inttoptr i64 %52 to i8*
store i8 0, i8* %53
br label %54
54: ; preds = %51, %49
%55 = phi i64 [ %36, %49 ], [ 0, %51 ]
%56 = phi i8* [ %35, %49 ], [ %53, %51 ]
%57 = icmp eq i8** %2, null
br i1 %57, label %59, label %58
58: ; preds = %54
store i8* %56, i8** %2
br label %59
59: ; preds = %58, %54
%60 = icmp eq i64* %3, null
br i1 %60, label %62, label %61
61: ; preds = %59
store i64 %55, i64* %3
br label %62
62: ; preds = %61, %59
%63 = trunc i64 %55 to i32
br label %64
64: ; preds = %4, %62, %45
%65 = phi i32 [ -1, %45 ], [ %63, %62 ], [ -1, %4 ]
ret i32 %65
}