#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 fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
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 + 1));
  if (fread(buf, 1, flength, fdesc) != flength) {
    free(buf);
    fclose(fdesc);
    return NULL;
  }
  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    11a4 <rf_loadfile+0x24>
movq   $0x0,-0x8(%rbp)
jmp    128c <rf_loadfile+0x10c>
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <rf_loadfile+0x50>
movq   $0x0,-0x8(%rbp)
jmp    128c <rf_loadfile+0x10c>
mov    -0x18(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1212 <rf_loadfile+0x92>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    128c <rf_loadfile+0x10c>
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x1c(%rbp),%eax
je     126f <rf_loadfile+0xef>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    128c <rf_loadfile+0x10c>
mov    -0x18(%rbp),%rdi
call   1050 <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     1212 <rf_loadfile+0x92>
lea    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1212 <rf_loadfile+0x92>
mov    %rax,%r14
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
cmp    $0xffffffff,%eax
je     120a <rf_loadfile+0x8a>
mov    %eax,%r15d
mov    %r14,%rdi
call   1060 <rewind@plt>
lea    0x1(%r15),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %r15d,%eax
jne    1202 <rf_loadfile+0x82>
mov    %r14,%rdi
call   1050 <fclose@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1214 <rf_loadfile+0x94>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <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 %55
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 %55
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 %55
27: ; preds = %16
%28 = load i32*, i32** %4
%29 = call i32 @rewind(i32* %28)
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
%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* %5
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %47
42: ; preds = %27
%43 = load i8*, i8** %6
%44 = call i32 @free(i8* %43)
%45 = load i32*, i32** %4
%46 = call i32 @fclose(i32* %45)
store i8* null, i8** %2
br label %55
47: ; preds = %27
%48 = load i32*, i32** %4
%49 = call i32 @fclose(i32* %48)
%50 = load i8*, i8** %6
%51 = load i32, i32* %5
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %6
store i8* %54, i8** %2
br label %55
55: ; preds = %47, %42, %24, %15, %9
%56 = load i8*, i8** %2
ret i8* %56
}

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 %26, 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 %26, 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 %26
13: ; preds = %6
%14 = tail call i32 @rewind(i32* %4)
%15 = add nuw nsw i32 %9, 1
%16 = tail call i8* @malloc(i32 %15)
%17 = tail call i32 @fread(i8* %16, i32 1, i32 %9, i32* %4)
%18 = icmp eq i32 %17, %9
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = tail call i32 @free(i8* %16)
%21 = tail call i32 @fclose(i32* %4)
br label %26
22: ; preds = %13
%23 = tail call i32 @fclose(i32* %4)
%24 = sext i32 %9 to i64
%25 = getelementptr inbounds i8, i8* %16, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %11, %22, %19, %3, %1
%27 = phi i8* [ null, %1 ], [ null, %3 ], [ null, %11 ], [ null, %19 ], [ %16, %22 ]
ret i8* %27
}