#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*/ die(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char getc(int /*<<< orphan*/ *);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ *stdin;
char *readFile(char *fn) {
  FILE *fp = fn ? fopen(fn, "r") : stdin;
  if (!fp)
    die("Unable to open file");
  size_t used = 0, max = 1024;
  char *buf = malloc(sizeof(char) * max);
  if (!buf)
    die("Unable to allocate memory");
  for (char c; (c = getc(fp)) != EOF; buf[used++] = c) {
    if (used >= max) {
      buf = realloc(buf, sizeof(char) * (max *= 2));
      if (!buf)
        die("Unable to reallocate memory");
    }
  }
  if (!feof(fp))
    die("Error reading from file");
  fclose(fp);
  buf[used] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a0 <readFile+0x30>
mov    -0x8(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
jmp    11ae <readFile+0x3e>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cd <readFile+0x5d>
lea    0xe3a(%rip),%rdi
call   1080 <die@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x400,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1207 <readFile+0x97>
lea    0xe14(%rip),%rdi
call   1080 <die@plt>
jmp    120c <readFile+0x9c>
mov    -0x10(%rbp),%rdi
call   1090 <getc@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
mov    0x2dbe(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1299 <readFile+0x129>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1276 <readFile+0x106>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1271 <readFile+0x101>
lea    0xdc4(%rip),%rdi
call   1080 <die@plt>
jmp    1276 <readFile+0x106>
jmp    127b <readFile+0x10b>
mov    -0x29(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    120c <readFile+0x9c>
mov    -0x10(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%eax
jne    12b7 <readFile+0x147>
lea    0xd9a(%rip),%rdi
call   1080 <die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1265 <readFile+0xf5>
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
test   %r14,%r14
jne    11a4 <readFile+0x34>
lea    0xe63(%rip),%rdi
call   1080 <die@plt>
mov    $0x400,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11c2 <readFile+0x52>
lea    0xe59(%rip),%rdi
call   1080 <die@plt>
mov    %r14,%rdi
call   1090 <getc@plt>
mov    0x2e0f(%rip),%r13
cmp    0x0(%r13),%al
jne    1210 <readFile+0xa0>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1040 <feof@plt>
test   %eax,%eax
jne    11f1 <readFile+0x81>
lea    0xe60(%rip),%rdi
call   1080 <die@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
movb   $0x0,(%r15,%rbp,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    $0x400,%r12d
xor    %ebp,%ebp
jmp    1248 <readFile+0xd8>
lea    0xe0d(%rip),%rdi
call   1080 <die@plt>
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %bl,(%r15,%rbp,1)
add    $0x1,%rbp
mov    %r14,%rdi
call   1090 <getc@plt>
mov    %eax,%ebx
cmp    0x0(%r13),%al
je     11d9 <readFile+0x69>
cmp    %r12,%rbp
jb     1230 <readFile+0xc0>
add    %r12,%r12
mov    %r15,%rdi
mov    %r12d,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     121c <readFile+0xac>
mov    %rax,%r15
jmp    1230 <readFile+0xc0>
mov    0x2d7c(%rip),%rax
mov    (%rax),%r14
test   %r14,%r14
jne    11a4 <readFile+0x34>
jmp    1198 <readFile+0x28>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.1 = [20 x i8] c"Unable to open file\00"
@.str.2 = [26 x i8] c"Unable to allocate memory\00"
@EOF = global i8 0
@.str.3 = [28 x i8] c"Unable to reallocate memory\00"
@.str.4 = [24 x i8] c"Error reading from file\00"
define i8* @readFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %15
13: ; preds = %1
%14 = load i32*, i32** @stdin
br label %15
15: ; preds = %13, %10
%16 = phi i32* [ %12, %10 ], [ %14, %13 ]
store i32* %16, i32** %3
%17 = load i32*, i32** %3
%18 = icmp ne i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
store i64 0, i64* %4
store i64 1024, i64* %5
%22 = load i64, i64* %5
%23 = mul i64 1, %22
%24 = trunc i64 %23 to i32
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %21
%29 = call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %21
br label %31
31: ; preds = %55, %30
%32 = load i32*, i32** %3
%33 = call signext i8 @getc(i32* %32)
store i8 %33, i8* %7
%34 = sext i8 %33 to i32
%35 = load i8, i8* @EOF
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %34, %36
br i1 %37, label %38, label %61
38: ; preds = %31
%39 = load i64, i64* %4
%40 = load i64, i64* %5
%41 = icmp uge i64 %39, %40
br i1 %41, label %42, label %54
42: ; preds = %38
%43 = load i8*, i8** %6
%44 = load i64, i64* %5
%45 = mul i64 %44, 2
store i64 %45, i64* %5
%46 = mul i64 1, %45
%47 = trunc i64 %46 to i32
%48 = call i8* @realloc(i8* %43, i32 %47)
store i8* %48, i8** %6
%49 = load i8*, i8** %6
%50 = icmp ne i8* %49, null
br i1 %50, label %53, label %51
51: ; preds = %42
%52 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %42
br label %54
54: ; preds = %53, %38
br label %55
55: ; preds = %54
%56 = load i8, i8* %7
%57 = load i8*, i8** %6
%58 = load i64, i64* %4
%59 = add i64 %58, 1
store i64 %59, i64* %4
%60 = getelementptr inbounds i8, i8* %57, i64 %58
store i8 %56, i8* %60
br label %31
61: ; preds = %31
%62 = load i32*, i32** %3
%63 = call i32 @feof(i32* %62)
%64 = icmp ne i32 %63, 0
br i1 %64, label %67, label %65
65: ; preds = %61
%66 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %65, %61
%68 = load i32*, i32** %3
%69 = call i32 @fclose(i32* %68)
%70 = load i8*, i8** %6
%71 = load i64, i64* %4
%72 = getelementptr inbounds i8, i8* %70, i64 %71
store i8 0, i8* %72
%73 = load i8*, i8** %6
ret i8* %73
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stdin = global i32* null
@.str.1 = [20 x i8] c"Unable to open file\00"
@.str.2 = [26 x i8] c"Unable to allocate memory\00"
@EOF = global i8 0
@.str.3 = [28 x i8] c"Unable to reallocate memory\00"
@.str.4 = [24 x i8] c"Error reading from file\00"
define i8* @readFile(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %5, label %3
3: ; preds = %1
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %7
5: ; preds = %1
%6 = load i32*, i32** @stdin
br label %7
7: ; preds = %5, %3
%8 = phi i32* [ %4, %3 ], [ %6, %5 ]
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call signext i8 @getc(i32* %8)
%19 = load i8, i8* @EOF
%20 = icmp eq i8 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %39, %17
%22 = phi i8* [ %13, %17 ], [ %41, %39 ]
%23 = phi i64 [ 0, %17 ], [ %42, %39 ]
%24 = tail call i32 @feof(i32* %8)
%25 = icmp eq i32 %24, 0
br i1 %25, label %47, label %49
26: ; preds = %17, %39
%27 = phi i8 [ %44, %39 ], [ %18, %17 ]
%28 = phi i64 [ %42, %39 ], [ 0, %17 ]
%29 = phi i8* [ %41, %39 ], [ %13, %17 ]
%30 = phi i64 [ %40, %39 ], [ 1024, %17 ]
%31 = icmp ult i64 %28, %30
br i1 %31, label %39, label %32
32: ; preds = %26
%33 = shl i64 %30, 1
%34 = trunc i64 %33 to i32
%35 = tail call i8* @realloc(i8* %29, i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %26, %37, %32
%40 = phi i64 [ %33, %32 ], [ %33, %37 ], [ %30, %26 ]
%41 = phi i8* [ %35, %32 ], [ null, %37 ], [ %29, %26 ]
%42 = add i64 %28, 1
%43 = getelementptr inbounds i8, i8* %41, i64 %28
store i8 %27, i8* %43
%44 = tail call signext i8 @getc(i32* %8)
%45 = load i8, i8* @EOF
%46 = icmp eq i8 %44, %45
br i1 %46, label %21, label %26
47: ; preds = %21
%48 = tail call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %21
%50 = tail call i32 @fclose(i32* %8)
%51 = getelementptr inbounds i8, i8* %22, i64 %23
store i8 0, i8* %51
ret i8* %22
}