#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, long const, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long const);
char *ReadFile(const char *filename) {
  FILE *f = fopen(filename, "r");
  char *buf = NULL;
  if (f == NULL)
    goto bail;
  if (fseek(f, 0L, SEEK_END) != 0)
    goto bail;
  const long bufsize = ftell(f);
  if (bufsize == -1)
    goto bail;
  if (fseek(f, 0L, SEEK_SET) != 0)
    goto bail;
  buf = malloc(bufsize + 1);
  if (buf == NULL)
    goto bail;
  const size_t readlen = fread(buf, 1, bufsize, f);
  buf[readlen] = '\0';
bail:
  if (f) {
    fclose(f);
  }
  return buf;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1198 <ReadFile+0x38>
jmp    1246 <ReadFile+0xe6>
mov    -0x10(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     11bd <ReadFile+0x5d>
jmp    1246 <ReadFile+0xe6>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11da <ReadFile+0x7a>
jmp    1246 <ReadFile+0xe6>
mov    -0x10(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     11ff <ReadFile+0x9f>
jmp    1246 <ReadFile+0xe6>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1220 <ReadFile+0xc0>
jmp    1246 <ReadFile+0xe6>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x10(%rbp)
je     125a <ReadFile+0xfa>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadFile>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11eb <ReadFile+0x8b>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
jne    11f3 <ReadFile+0x93>
mov    %rbx,%rdi
call   1050 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     11f0 <ReadFile+0x90>
mov    %rax,%r15
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
jne    11f3 <ReadFile+0x93>
lea    0x1(%r15),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f0 <ReadFile+0x90>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
movb   $0x0,(%r14,%rax,1)
jmp    11f3 <ReadFile+0x93>
xor    %r14d,%r14d
jmp    11fb <ReadFile+0x9b>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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