#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *malloc(size_t);
char *realloc(char *, size_t);
char *read_file(const char *file_name) {
  int ch;
  char *buf;
  size_t buf_size;
  size_t length;
  FILE *fp;
  fp = fopen(file_name, "r");
  if (fp == NULL) {
    return NULL;
  }
  buf_size = 10;
  length = 0;
  buf = malloc(buf_size);
  while ((ch = fgetc(fp)) != EOF && !feof(fp)) {
    if (length + 1 >= buf_size - 1) {
      buf_size *= 2;
      buf = realloc(buf, buf_size);
    }
    buf[length] = ch;
    length++;
  }
  buf[length] = '\0';
  fclose(fp);
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1198 <read_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    126f <read_file+0x10f>
movq   $0xa,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
mov    0x2e1c(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x39(%rbp)
je     11eb <read_file+0x8b>
mov    -0x38(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11fb <read_file+0x9b>
jmp    1252 <read_file+0xf2>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     1231 <read_file+0xd1>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11b5 <read_file+0x55>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11a7 <read_file+0x47>
mov    %rax,%r13
mov    $0xa,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1040 <fgetc@plt>
mov    0x2e4a(%rip),%r14
cmp    (%r14),%eax
jne    11ae <read_file+0x4e>
xor    %eax,%eax
jmp    1222 <read_file+0xc2>
xor    %eax,%eax
jmp    1232 <read_file+0xd2>
mov    %eax,%ebp
mov    $0xa,%eax
mov    %rax,0x8(%rsp)
xor    %ebx,%ebx
jmp    11f1 <read_file+0x91>
xchg   %ax,%ax
add    %rsi,%rsi
mov    %r15,%rdi
mov    %rsi,0x8(%rsp)
call   1070 <realloc@plt>
mov    %rax,%r15
mov    %bpl,(%rax,%rbx,1)
mov    %r12,%r13
mov    %r12,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%rax
mov    %rax,%rbx
cmp    (%r14),%ebp
je     1222 <read_file+0xc2>
mov    %r13,%r12
mov    %r13,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    121c <read_file+0xbc>
lea    0x1(%rbx),%rcx
mov    0x8(%rsp),%rsi
lea    -0x1(%rsi),%rax
mov    %rcx,0x10(%rsp)
cmp    %rax,%rcx
jae    11c0 <read_file+0x60>
mov    %r15,%rax
jmp    11d0 <read_file+0x70>
mov    %rbx,%rax
mov    %r12,%r13
movb   $0x0,(%r15,%rax,1)
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
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 %36, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(10) i8* @malloc(i64 10)
%6 = tail call i32 @fgetc(i32* %2)
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %6, %7
br i1 %8, label %31, label %9
9: ; preds = %4, %23
%10 = phi i32 [ %28, %23 ], [ %6, %4 ]
%11 = phi i64 [ %17, %23 ], [ 0, %4 ]
%12 = phi i64 [ %25, %23 ], [ 10, %4 ]
%13 = phi i8* [ %24, %23 ], [ %5, %4 ]
%14 = tail call i32 @feof(i32* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %9
%17 = add i64 %11, 1
%18 = add i64 %12, -1
%19 = icmp ult i64 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = shl i64 %12, 1
%22 = tail call i8* @realloc(i8* %13, i64 %21)
br label %23
23: ; preds = %20, %16
%24 = phi i8* [ %22, %20 ], [ %13, %16 ]
%25 = phi i64 [ %21, %20 ], [ %12, %16 ]
%26 = trunc i32 %10 to i8
%27 = getelementptr inbounds i8, i8* %24, i64 %11
store i8 %26, i8* %27
%28 = tail call i32 @fgetc(i32* %2)
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %9
31: ; preds = %9, %23, %4
%32 = phi i8* [ %5, %4 ], [ %24, %23 ], [ %13, %9 ]
%33 = phi i64 [ 0, %4 ], [ %17, %23 ], [ %11, %9 ]
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %34
%35 = tail call i32 @fclose(i32* %2)
br label %36
36: ; preds = %1, %31
%37 = phi i8* [ %32, %31 ], [ null, %1 ]
ret i8* %37
}