#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 fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(size_t);
unsigned char *realloc(unsigned char *, size_t);
int read_full_file(const char *fn, unsigned char **output, size_t *sz,
                   int max) {
  FILE *fd;
  unsigned char *buf, *buf2;
  size_t alloc = 1024, total, read;
  int ret;
  if (!output)
    return -1;
  fd = fopen(fn, "r");
  if (!fd)
    return -1;
  buf = malloc(alloc);
  if (!buf)
    return -1;
  total = 0;
  while (!feof(fd)) {
    read = max ? (*sz - total) : (alloc - total);
    if (read > (alloc - total))
      read = alloc - total;
    read = fread(buf + total, 1, read, fd);
    if (ferror(fd))
      goto err;
    total += read;
    if (feof(fd))
      break;
    if (total == alloc) {
      alloc <<= 2;
      buf2 = realloc(buf, alloc);
      if (!buf2)
        goto err;
      buf = buf2;
    }
    if (max && !(*sz - total))
      break;
  }
  ret = fclose(fd);
  if (ret < 0)
    goto err;
  *sz = total;
  *output = buf;
  return 0;
err:
  free(buf);
  return -1;
}


x86 assembly (clang, -O0):
<read_full_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x400,-0x48(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b6 <read_full_file+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <read_full_file+0x212>
mov    -0x10(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e1 <read_full_file+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <read_full_file+0x212>
mov    -0x48(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1205 <read_full_file+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <read_full_file+0x212>
movq   $0x0,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    122c <read_full_file+0xac>
jmp    1345 <read_full_file+0x1c5>
cmpl   $0x0,-0x24(%rbp)
je     124a <read_full_file+0xca>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
sub    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    1256 <read_full_file+0xd6>
mov    -0x48(%rbp),%rax
sub    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    -0x48(%rbp),%rcx
sub    -0x50(%rbp),%rcx
cmp    %rcx,%rax
jbe    127f <read_full_file+0xff>
mov    -0x48(%rbp),%rax
sub    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12b5 <read_full_file+0x135>
jmp    1382 <read_full_file+0x202>
mov    -0x58(%rbp),%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     12d9 <read_full_file+0x159>
jmp    1345 <read_full_file+0x1c5>
mov    -0x50(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    131c <read_full_file+0x19c>
mov    -0x48(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1090 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1314 <read_full_file+0x194>
jmp    1382 <read_full_file+0x202>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1340 <read_full_file+0x1c0>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
sub    -0x50(%rbp),%rax
cmp    $0x0,%rax
jne    1340 <read_full_file+0x1c0>
jmp    1345 <read_full_file+0x1c5>
jmp    120d <read_full_file+0x8d>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    1360 <read_full_file+0x1e0>
jmp    1382 <read_full_file+0x202>
mov    -0x50(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1392 <read_full_file+0x212>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_full_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x4(%rsp)
mov    %rdx,0x8(%rsp)
mov    $0xffffffff,%ebx
test   %rsi,%rsi
je     12e1 <read_full_file+0x161>
mov    %rsi,%rbp
lea    0xe51(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12e1 <read_full_file+0x161>
mov    %rax,%r12
mov    $0x400,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12e1 <read_full_file+0x161>
mov    %rax,%r15
mov    %rbp,0x10(%rsp)
mov    %r12,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     1212 <read_full_file+0x92>
xor    %r13d,%r13d
mov    %r12,%rdi
call   1060 <fclose@plt>
test   %eax,%eax
js     12d1 <read_full_file+0x151>
mov    0x8(%rsp),%rax
mov    %r13,(%rax)
mov    0x10(%rsp),%rax
mov    %r15,(%rax)
xor    %ebx,%ebx
jmp    12e1 <read_full_file+0x161>
mov    $0x400,%ebx
xor    %r13d,%r13d
mov    %r15,%r14
jmp    1230 <read_full_file+0xb0>
nop
mov    %r12,%rdi
call   1070 <feof@plt>
mov    %r15,%r14
test   %rax,%rax
jne    11eb <read_full_file+0x6b>
mov    %rbx,%rax
cmpl   $0x0,0x4(%rsp)
je     1242 <read_full_file+0xc2>
mov    0x8(%rsp),%rax
mov    (%rax),%rax
sub    %r13,%rax
mov    %rbx,%rdx
sub    %r13,%rdx
cmp    %rdx,%rax
cmovbe %rax,%rdx
lea    (%r14,%r13,1),%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    12d4 <read_full_file+0x154>
add    %rbp,%r13
mov    %r12,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    12f2 <read_full_file+0x172>
cmp    %rbx,%r13
jne    12b0 <read_full_file+0x130>
shl    $0x2,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     12d4 <read_full_file+0x154>
cmpl   $0x0,0x4(%rsp)
jne    12be <read_full_file+0x13e>
jmp    1220 <read_full_file+0xa0>
nopl   0x0(%rax,%rax,1)
mov    %r14,%r15
cmpl   $0x0,0x4(%rsp)
je     1220 <read_full_file+0xa0>
mov    0x8(%rsp),%rax
cmp    %r13,(%rax)
jne    1220 <read_full_file+0xa0>
jmp    11eb <read_full_file+0x6b>
mov    %r15,%r14
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%r15
jmp    11eb <read_full_file+0x6b>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @read_full_file(i8* %0, i8** %1, i64* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i64* %2, i64** %8
store i32 %3, i32* %9
store i64 1024, i64* %13
%17 = load i8**, i8*** %7
%18 = icmp ne i8** %17, null
br i1 %18, label %20, label %19
19: ; preds = %4
store i32 -1, i32* %5
br label %120
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %10
%23 = load i32*, i32** %10
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20
store i32 -1, i32* %5
br label %120
26: ; preds = %20
%27 = load i64, i64* %13
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %11
%29 = load i8*, i8** %11
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %26
store i32 -1, i32* %5
br label %120
32: ; preds = %26
store i64 0, i64* %14
br label %33
33: ; preds = %105, %32
%34 = load i32*, i32** %10
%35 = call i64 @feof(i32* %34)
%36 = icmp ne i64 %35, 0
%37 = xor i1 %36, true
br i1 %37, label %38, label %106
38: ; preds = %33
%39 = load i32, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i64*, i64** %8
%43 = load i64, i64* %42
%44 = load i64, i64* %14
%45 = sub i64 %43, %44
br label %50
46: ; preds = %38
%47 = load i64, i64* %13
%48 = load i64, i64* %14
%49 = sub i64 %47, %48
br label %50
50: ; preds = %46, %41
%51 = phi i64 [ %45, %41 ], [ %49, %46 ]
store i64 %51, i64* %15
%52 = load i64, i64* %15
%53 = load i64, i64* %13
%54 = load i64, i64* %14
%55 = sub i64 %53, %54
%56 = icmp ugt i64 %52, %55
br i1 %56, label %57, label %61
57: ; preds = %50
%58 = load i64, i64* %13
%59 = load i64, i64* %14
%60 = sub i64 %58, %59
store i64 %60, i64* %15
br label %61
61: ; preds = %57, %50
%62 = load i8*, i8** %11
%63 = load i64, i64* %14
%64 = getelementptr inbounds i8, i8* %62, i64 %63
%65 = load i64, i64* %15
%66 = load i32*, i32** %10
%67 = call i64 @fread(i8* %64, i32 1, i64 %65, i32* %66)
store i64 %67, i64* %15
%68 = load i32*, i32** %10
%69 = call i64 @ferror(i32* %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %61
br label %117
72: ; preds = %61
%73 = load i64, i64* %15
%74 = load i64, i64* %14
%75 = add i64 %74, %73
store i64 %75, i64* %14
%76 = load i32*, i32** %10
%77 = call i64 @feof(i32* %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %72
br label %106
80: ; preds = %72
%81 = load i64, i64* %14
%82 = load i64, i64* %13
%83 = icmp eq i64 %81, %82
br i1 %83, label %84, label %95
84: ; preds = %80
%85 = load i64, i64* %13
%86 = shl i64 %85, 2
store i64 %86, i64* %13
%87 = load i8*, i8** %11
%88 = load i64, i64* %13
%89 = call i8* @realloc(i8* %87, i64 %88)
store i8* %89, i8** %12
%90 = load i8*, i8** %12
%91 = icmp ne i8* %90, null
br i1 %91, label %93, label %92
92: ; preds = %84
br label %117
93: ; preds = %84
%94 = load i8*, i8** %12
store i8* %94, i8** %11
br label %95
95: ; preds = %93, %80
%96 = load i32, i32* %9
%97 = icmp ne i32 %96, 0
br i1 %97, label %98, label %105
98: ; preds = %95
%99 = load i64*, i64** %8
%100 = load i64, i64* %99
%101 = load i64, i64* %14
%102 = sub i64 %100, %101
%103 = icmp ne i64 %102, 0
br i1 %103, label %105, label %104
104: ; preds = %98
br label %106
105: ; preds = %98, %95
br label %33
106: ; preds = %104, %79, %33
%107 = load i32*, i32** %10
%108 = call i32 @fclose(i32* %107)
store i32 %108, i32* %16
%109 = load i32, i32* %16
%110 = icmp slt i32 %109, 0
br i1 %110, label %111, label %112
111: ; preds = %106
br label %117
112: ; preds = %106
%113 = load i64, i64* %14
%114 = load i64*, i64** %8
store i64 %113, i64* %114
%115 = load i8*, i8** %11
%116 = load i8**, i8*** %7
store i8* %115, i8** %116
store i32 0, i32* %5
br label %120
117: ; preds = %111, %92, %71
%118 = load i8*, i8** %11
%119 = call i32 @free(i8* %118)
store i32 -1, i32* %5
br label %120
120: ; preds = %117, %112, %31, %25, %19
%121 = load i32, i32* %5
ret i32 %121
}

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