#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(char *, int, size_t, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
char *read_file(const char *fname, size_t *len_out) {
  FILE *fp;
  char *buf;
  size_t capacity = 1000;
  size_t len = 0;
  if (fname) {
    fp = fopen(fname, "r");
    if (!fp) {
      perror("opening");
      exit(1);
    }
  } else {
    fp = stdin;
  }
  buf = malloc(capacity);
  if (!buf) {
    fprintf(stderr, "malloc failed\n");
    exit(1);
  }
  for (;;) {
    len += fread(buf + len, 1, capacity - len, fp);
    if (feof(fp))
      break;
    if (ferror(fp)) {
      perror("reading");
      exit(1);
    }
    if (len == capacity) {
      capacity *= 2;
      buf = realloc(buf, capacity);
      if (!buf) {
        fprintf(stderr, "realloc failed\n");
        exit(1);
      }
    }
  }
  if (fname)
    fclose(fp);
  *len_out = len;
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x3e8,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1205 <read_file+0x65>
mov    -0x8(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1200 <read_file+0x60>
lea    0xe11(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1213 <read_file+0x73>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    124a <read_file+0xaa>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    124f <read_file+0xaf>
mov    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     128d <read_file+0xed>
jmp    1315 <read_file+0x175>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12b6 <read_file+0x116>
lea    0xd72(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1310 <read_file+0x170>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    130b <read_file+0x16b>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1310 <read_file+0x170>
jmp    124f <read_file+0xaf>
cmpq   $0x0,-0x8(%rbp)
je     1329 <read_file+0x189>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
je     11d9 <read_file+0x39>
lea    0xe43(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11e3 <read_file+0x43>
lea    0xe2e(%rip),%rdi
jmp    12b8 <read_file+0x118>
mov    0x2e08(%rip),%rax
mov    (%rax),%r12
mov    $0x3e8,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12c7 <read_file+0x127>
mov    %rax,%r13
mov    $0x3e8,%ebp
mov    $0x3e8,%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
jmp    123a <read_file+0x9a>
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%r13,1),%rdi
mov    %rbp,%rdx
sub    %rbx,%rdx
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
add    %rax,%rbx
mov    %r12,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    128f <read_file+0xef>
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    12b1 <read_file+0x111>
cmp    %rbp,%rbx
jne    1220 <read_file+0x80>
lea    (%rbx,%rbx,1),%rbp
mov    %r13,%rdi
mov    %rbp,%rsi
call   1090 <realloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1220 <read_file+0x80>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
test   %r15,%r15
je     129c <read_file+0xfc>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %rbx,(%r14)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
jmp    1280 <read_file+0xe0>

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

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