#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(int);
scalar_t__ realloc(char *, int);
char *read(char *path) {
  int n = 512;
  int i = 0;
  FILE *fh = fopen(path, "r");
  char *code = (char *)malloc(n);
  while ((code[i] = fgetc(fh)) != EOF) {
    i += 1;
    if (i == n) {
      n *= 2;
      code = (char *)realloc(code, n);
    }
  }
  fclose(fh);
  return code;
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x200,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    -0x20(%rbp),%rcx
movslq -0x10(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
mov    0x2e40(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11e6 <read+0x96>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11e1 <read+0x91>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    118a <read+0x3a>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    $0x200,%ebp
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %al,(%r12)
mov    0x2e5c(%rip),%r15
cmp    (%r15),%al
jne    11a5 <read+0x55>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebx
jmp    11c5 <read+0x75>
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    (%r15),%al
je     1191 <read+0x41>
mov    %ebp,%eax
cmp    %rax,%rbx
jne    11b0 <read+0x60>
add    %ebp,%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r12
jmp    11b0 <read+0x60>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
define i8* @read(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %2
store i32 512, i32* %3
store i32 0, i32* %4
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32, i32* %3
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
br label %12
12: ; preds = %36, %1
%13 = load i32*, i32** %5
%14 = call signext i8 @fgetc(i32* %13)
%15 = load i8*, i8** %6
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 %14, i8* %18
%19 = sext i8 %14 to i32
%20 = load i8, i8* @EOF
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %19, %21
br i1 %22, label %23, label %37
23: ; preds = %12
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = load i32, i32* %3
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %23
%30 = load i32, i32* %3
%31 = mul nsw i32 %30, 2
store i32 %31, i32* %3
%32 = load i8*, i8** %6
%33 = load i32, i32* %3
%34 = call i64 @realloc(i8* %32, i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %6
br label %36
36: ; preds = %29, %23
br label %12
37: ; preds = %12
%38 = load i32*, i32** %5
%39 = call i32 @fclose(i32* %38)
%40 = load i8*, i8** %6
ret i8* %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
define i8* @read(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 = tail call i64 @malloc(i32 512)
%4 = inttoptr i64 %3 to i8*
%5 = tail call signext i8 @fgetc(i32* %2)
store i8 %5, i8* %4
%6 = load i8, i8* @EOF
%7 = icmp eq i8 %5, %6
br i1 %7, label %26, label %8
8: ; preds = %1, %19
%9 = phi i64 [ %12, %19 ], [ 0, %1 ]
%10 = phi i8* [ %21, %19 ], [ %4, %1 ]
%11 = phi i32 [ %20, %19 ], [ 512, %1 ]
%12 = add nuw i64 %9, 1
%13 = zext i32 %11 to i64
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = shl nsw i32 %11, 1
%17 = tail call i64 @realloc(i8* %10, i32 %16)
%18 = inttoptr i64 %17 to i8*
br label %19
19: ; preds = %15, %8
%20 = phi i32 [ %16, %15 ], [ %11, %8 ]
%21 = phi i8* [ %18, %15 ], [ %10, %8 ]
%22 = tail call signext i8 @fgetc(i32* %2)
%23 = getelementptr inbounds i8, i8* %21, i64 %12
store i8 %22, i8* %23
%24 = load i8, i8* @EOF
%25 = icmp eq i8 %22, %24
br i1 %25, label %26, label %8
26: ; preds = %19, %1
%27 = phi i8* [ %4, %1 ], [ %21, %19 ]
%28 = tail call i32 @fclose(i32* %2)
ret i8* %27
}