#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *rf_loadfile(char *filename) {
  FILE *fdesc;
  if (filename == NULL)
    return NULL;
  fdesc = fopen(filename, "r");
  if (fdesc == NULL)
    return NULL;
  fseek(fdesc, 0L, SEEK_END);
  int flength = ftell(fdesc);
  if (flength == -1) {
    fclose(fdesc);
    return NULL;
  }
  rewind(fdesc);
  char *buf = malloc(sizeof(char) * (flength + 128));
  fread(buf, 1, flength, fdesc);
  fclose(fdesc);
  buf[flength] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<rf_loadfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <rf_loadfile+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1256 <rf_loadfile+0xe6>
mov    -0x10(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <rf_loadfile+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1256 <rf_loadfile+0xe6>
mov    -0x18(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1202 <rf_loadfile+0x92>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1256 <rf_loadfile+0xe6>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x1c(%rbp),%eax
add    $0x80,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rf_loadfile>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11f4 <rf_loadfile+0x84>
lea    0xe7f(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11f4 <rf_loadfile+0x84>
mov    %rax,%r14
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1060 <ftell@plt>
cmp    $0xffffffff,%eax
je     11ec <rf_loadfile+0x7c>
mov    %eax,%r15d
mov    %r14,%rdi
call   1050 <rewind@plt>
lea    0x80(%r15),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11f6 <rf_loadfile+0x86>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define i8* @rf_loadfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %47
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %10
store i8* null, i8** %2
br label %47
16: ; preds = %10
%17 = load i32*, i32** %4
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i64 0, i32 %18)
%20 = load i32*, i32** %4
%21 = call i32 @ftell(i32* %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
store i8* null, i8** %2
br label %47
27: ; preds = %16
%28 = load i32*, i32** %4
%29 = call i32 @rewind(i32* %28)
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 128
%32 = sext i32 %31 to i64
%33 = mul i64 1, %32
%34 = trunc i64 %33 to i32
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %6
%36 = load i8*, i8** %6
%37 = load i32, i32* %5
%38 = load i32*, i32** %4
%39 = call i32 @fread(i8* %36, i32 1, i32 %37, i32* %38)
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
%42 = load i8*, i8** %6
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %6
store i8* %46, i8** %2
br label %47
47: ; preds = %27, %24, %15, %9
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define noalias i8* @rf_loadfile(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %21, 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))
%5 = icmp eq i32* %4, null
br i1 %5, label %21, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i64 0, i32 %7)
%9 = tail call i32 @ftell(i32* %4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @fclose(i32* %4)
br label %21
13: ; preds = %6
%14 = tail call i32 @rewind(i32* %4)
%15 = add nsw i32 %9, 128
%16 = tail call i8* @malloc(i32 %15)
%17 = tail call i32 @fread(i8* %16, i32 1, i32 %9, i32* %4)
%18 = tail call i32 @fclose(i32* %4)
%19 = sext i32 %9 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %11, %13, %3, %1
%22 = phi i8* [ null, %1 ], [ null, %3 ], [ null, %11 ], [ %16, %13 ]
ret i8* %22
}