#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 EOF;
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
char *realloc(char *, size_t);
char *read_file(char *path) {
  FILE *f;
  char *buf;
  size_t capa = 1024;
  size_t idx;
  int c;
  f = fopen(path, "r");
  if (!f) {
    perror(path);
    exit(1);
  }
  buf = malloc(capa);
  if (!buf)
    die("malloc");
  while ((c = getc(f)) != EOF) {
    if (idx + 1 >= capa) {
      capa *= 2;
      buf = realloc(buf, capa);
      if (!buf)
        die("realloc");
    }
    buf[idx++] = c;
  }
  buf[idx++] = '\0';
  fclose(f);
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x400,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c6 <read_file+0x46>
mov    -0x8(%rbp),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ea <read_file+0x6a>
lea    0xe1d(%rip),%rdi
call   1070 <die@plt>
jmp    11ef <read_file+0x6f>
mov    -0x10(%rbp),%rdi
call   10a0 <getc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%eax
je     1275 <read_file+0xf5>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
cmp    -0x20(%rbp),%rax
jb     1255 <read_file+0xd5>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1250 <read_file+0xd0>
lea    0xdbe(%rip),%rdi
call   1070 <die@plt>
jmp    1255 <read_file+0xd5>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    11ef <read_file+0x6f>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%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    %rdi,%rbx
lea    0xe6b(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     124d <read_file+0xcd>
mov    %rax,%r14
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11c4 <read_file+0x44>
lea    0xe43(%rip),%rdi
call   1070 <die@plt>
mov    %r14,%rdi
call   10a0 <getc@plt>
mov    %eax,%ebp
mov    0x2e13(%rip),%rax
cmp    (%rax),%ebp
jne    11f8 <read_file+0x78>
movb   $0x0,(%r15,%r13,1)
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x400,%ebx
jmp    1229 <read_file+0xa9>
lea    0xe03(%rip),%rdi
call   1070 <die@plt>
xor    %r15d,%r15d
xchg   %ax,%ax
mov    %bpl,(%r15,%r12,1)
mov    %r14,%rdi
call   10a0 <getc@plt>
mov    %eax,%ebp
mov    0x2dc3(%rip),%rax
cmp    (%rax),%ebp
je     11d9 <read_file+0x59>
mov    %r13,%r12
add    $0x1,%r13
cmp    %rbx,%r13
jb     1210 <read_file+0x90>
add    %rbx,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     11ff <read_file+0x7f>
mov    %rax,%r15
jmp    1210 <read_file+0x90>
mov    %rbx,%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [7 x i8] c"malloc\00"
@EOF = global i32 0
@.str.2 = [8 x i8] c"realloc\00"
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %2
store i64 1024, i64* %5
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i32 @perror(i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i64, i64* %5
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
br label %24
24: ; preds = %45, %23
%25 = load i32*, i32** %3
%26 = call i32 @getc(i32* %25)
store i32 %26, i32* %7
%27 = load i32, i32* @EOF
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %52
29: ; preds = %24
%30 = load i64, i64* %6
%31 = add i64 %30, 1
%32 = load i64, i64* %5
%33 = icmp uge i64 %31, %32
br i1 %33, label %34, label %45
34: ; preds = %29
%35 = load i64, i64* %5
%36 = mul i64 %35, 2
store i64 %36, i64* %5
%37 = load i8*, i8** %4
%38 = load i64, i64* %5
%39 = call i8* @realloc(i8* %37, i64 %38)
store i8* %39, i8** %4
%40 = load i8*, i8** %4
%41 = icmp ne i8* %40, null
br i1 %41, label %44, label %42
42: ; preds = %34
%43 = call i32 @die(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %34
br label %45
45: ; preds = %44, %29
%46 = load i32, i32* %7
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %4
%49 = load i64, i64* %6
%50 = add i64 %49, 1
store i64 %50, i64* %6
%51 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 %47, i8* %51
br label %24
52: ; preds = %24
%53 = load i8*, i8** %4
%54 = load i64, i64* %6
%55 = add i64 %54, 1
store i64 %55, i64* %6
%56 = getelementptr inbounds i8, i8* %53, i64 %54
store i8 0, i8* %56
%57 = load i32*, i32** %3
%58 = call i32 @fclose(i32* %57)
%59 = load i8*, i8** %4
ret i8* %59
}

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