#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(int);
char *read_text_file(char *filename) {
  FILE *fp = fopen(filename, "r");
  char *buff = malloc(100);
  int c;
  int i = 0;
  while ((c = fgetc(fp)) != EOF) {
    buff[i++] = (char)c;
  }
  buff[i] = '\0';
  fclose(fp);
  return buff;
}


x86 assembly (clang, -O0):
<read_text_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    $0x64,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%eax
je     11af <read_text_file+0x6f>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1175 <read_text_file+0x35>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    $0x64,%edi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    0x2e75(%rip),%r15
cmp    (%r15),%eax
jne    117c <read_text_file+0x3c>
xor    %eax,%eax
jmp    1197 <read_text_file+0x57>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%eax
jne    1180 <read_text_file+0x40>
mov    %ebx,%eax
movb   $0x0,(%r12,%rax,1)
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @read_text_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%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** %3
%9 = call i8* @malloc(i32 100)
store i8* %9, i8** %4
store i32 0, i32* %6
br label %10
10: ; preds = %15, %1
%11 = load i32*, i32** %3
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %5
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %10
%16 = load i32, i32* %5
%17 = trunc i32 %16 to i8
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
store i8 %17, i8* %22
br label %10
23: ; preds = %10
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
%30 = load i8*, i8** %4
ret i8* %30
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define noalias i8* @read_text_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 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%4 = tail call i32 @fgetc(i32* %2)
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
br i1 %6, label %18, label %7
7: ; preds = %1, %7
%8 = phi i64 [ %11, %7 ], [ 0, %1 ]
%9 = phi i32 [ %13, %7 ], [ %4, %1 ]
%10 = trunc i32 %9 to i8
%11 = add nuw i64 %8, 1
%12 = getelementptr inbounds i8, i8* %3, i64 %8
store i8 %10, i8* %12
%13 = tail call i32 @fgetc(i32* %2)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %7
16: ; preds = %7
%17 = and i64 %11, 4294967295
br label %18
18: ; preds = %16, %1
%19 = phi i64 [ 0, %1 ], [ %17, %16 ]
%20 = getelementptr inbounds i8, i8* %3, i64 %19
store i8 0, i8* %20
%21 = tail call i32 @fclose(i32* %2)
ret i8* %3
}