#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*/ abort();
char *ecalloc(int const, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int getc(int /*<<< orphan*/ *);
char *realloc(char *, int);
char *fileToBuffer(const char *filename) {
  const int defaultSize = 1024;
  FILE *ifp;
  int c;
  int size = defaultSize;
  int i = 0;
  char *buffer = ecalloc(defaultSize, sizeof(char));
  if (!buffer)
    abort();
  ifp = fopen(filename, "rb");
  if (!ifp)
    abort();
  while ((c = getc(ifp)) != EOF) {
    buffer[i++] = c;
    if (i == size) {
      size += defaultSize;
      buffer = realloc(buffer, size);
      if (buffer == NULL) {
        abort();
      }
    }
  }
  fclose(ifp);
  buffer = realloc(buffer, i + 2);
  if (!buffer)
    abort();
  buffer[i] = '\n';
  buffer[i + 1] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<fileToBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x400,-0xc(%rbp)
movl   $0x400,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x400,%edi
mov    $0x1,%esi
call   1040 <ecalloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a6 <fileToBuffer+0x46>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cc <fileToBuffer+0x6c>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    11d1 <fileToBuffer+0x71>
mov    -0x18(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
je     1249 <fileToBuffer+0xe9>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    1244 <fileToBuffer+0xe4>
mov    -0x20(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123f <fileToBuffer+0xdf>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1244 <fileToBuffer+0xe4>
jmp    11d1 <fileToBuffer+0x71>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x2,%esi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1277 <fileToBuffer+0x117>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileToBuffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
mov    $0x400,%edi
mov    $0x1,%esi
call   1040 <ecalloc@plt>
test   %rax,%rax
je     1225 <fileToBuffer+0xc5>
mov    %rax,%r15
lea    0xe73(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1225 <fileToBuffer+0xc5>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <getc@plt>
mov    0x2e38(%rip),%r12
xor    %ebx,%ebx
cmp    (%r12),%eax
jne    11e1 <fileToBuffer+0x81>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0x2(%rbx),%esi
mov    %r15,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     1225 <fileToBuffer+0xc5>
mov    %ebx,%ecx
movw   $0xa,(%rax,%rcx,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x400,%ebp
xor    %ebx,%ebx
jmp    11fe <fileToBuffer+0x9e>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <getc@plt>
cmp    (%r12),%eax
je     11b8 <fileToBuffer+0x58>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %ebp,%eax
cmp    %rax,%rbx
jne    11f0 <fileToBuffer+0x90>
add    $0x400,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11f0 <fileToBuffer+0x90>
xor    %eax,%eax
call   1030 <abort@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@EOF = global i32 0
define i8* @fileToBuffer(i8* nocapture %0) {
%2 = tail call i8* @ecalloc(i32 1024, i32 1)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (...) @abort()
unreachable
6: ; preds = %1
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i32 @getc(i32* %7)
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %39, label %15
13: ; preds = %6
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %9, %31
%16 = phi i64 [ %21, %31 ], [ 0, %9 ]
%17 = phi i32 [ %34, %31 ], [ %10, %9 ]
%18 = phi i8* [ %33, %31 ], [ %2, %9 ]
%19 = phi i32 [ %32, %31 ], [ 1024, %9 ]
%20 = trunc i32 %17 to i8
%21 = add nuw i64 %16, 1
%22 = getelementptr inbounds i8, i8* %18, i64 %16
store i8 %20, i8* %22
%23 = zext i32 %19 to i64
%24 = icmp eq i64 %21, %23
br i1 %24, label %25, label %31
25: ; preds = %15
%26 = add nsw i32 %19, 1024
%27 = tail call i8* @realloc(i8* %18, i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 (...) @abort()
unreachable
31: ; preds = %25, %15
%32 = phi i32 [ %26, %25 ], [ %19, %15 ]
%33 = phi i8* [ %27, %25 ], [ %18, %15 ]
%34 = tail call i32 @getc(i32* %7)
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %15
37: ; preds = %31
%38 = trunc i64 %21 to i32
br label %39
39: ; preds = %37, %9
%40 = phi i32 [ 0, %9 ], [ %38, %37 ]
%41 = phi i8* [ %2, %9 ], [ %33, %37 ]
%42 = tail call i32 @fclose(i32* %7)
%43 = add nuw nsw i32 %40, 2
%44 = tail call i8* @realloc(i8* %41, i32 %43)
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = tail call i32 (...) @abort()
unreachable
48: ; preds = %39
%49 = zext i32 %40 to i64
%50 = getelementptr inbounds i8, i8* %44, i64 %49
store i8 10, i8* %50
%51 = add nuw nsw i32 %40, 1
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %44, i64 %52
store i8 0, i8* %53
ret i8* %44
}