#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ stat(char *, struct stat *);
char *loadTextFile(char *filename) {
  FILE *fptr;
  int size;
  struct stat stbuf;
  char *buf;
  fptr = fopen(filename, "rb");
  if (!fptr)
    return 0;
  stat(filename, &stbuf);
  size = stbuf.st_size;
  buf = malloc(size + 1);
  fread(buf, size, 1, fptr);
  buf[size] = 0;
  fclose(fptr);
  return buf;
}


x86 assembly (clang, -O0):
<loadTextFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <loadTextFile+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11dc <loadTextFile+0x8c>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadTextFile>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b0 <loadTextFile+0x60>
mov    %rax,%r14
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
movslq 0x8(%rsp),%r15
lea    0x1(%r15),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    11b2 <loadTextFile+0x62>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
define i8* @loadTextFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %32
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @stat(i8* %14, %struct.stat* %6)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%17 = load i32, i32* %16
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = load i32, i32* %5
%23 = load i32*, i32** %4
%24 = call i32 @fread(i8* %21, i32 %22, i32 1, i32* %23)
%25 = load i8*, i8** %7
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load i8*, i8** %7
store i8* %31, i8** %2
br label %32
32: ; preds = %13, %12
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
define noalias i8* @loadTextFile(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %16, label %6
6: ; preds = %1
%7 = call i32 @stat(i8* %0, %struct.stat* %2)
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = add nsw i32 %9, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = tail call i32 @fread(i8* %11, i32 %9, i32 1, i32* %4)
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
store i8 0, i8* %14
%15 = tail call i32 @fclose(i32* %4)
br label %16
16: ; preds = %1, %6
%17 = phi i8* [ %11, %6 ], [ null, %1 ]
ret i8* %17
}