#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ safe_malloc(long);
int /*<<< orphan*/ stderr;
char *ReadFile(const char *filename) {
  FILE *f;
  f = fopen(filename, "rb");
  if (!f) {
    fprintf(stderr, "warning: failed to read file '%s'\n", filename);
    return NULL;
  }
  fseek(f, 0, SEEK_END);
  long length = ftell(f);
  fseek(f, 0, SEEK_SET);
  char *contents = (char *)safe_malloc(length + 1);
  if (fread(contents, 1, length, f) != (size_t)length) {
    free(contents);
    fclose(f);
    fprintf(stderr, "warning: failed to read file '%s'\n", filename);
    return NULL;
  }
  contents[length] = '\0';
  fclose(f);
  return contents;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d1 <ReadFile+0x51>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe44(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    128c <ReadFile+0x10c>
mov    -0x18(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1040 <safe_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
cmp    -0x20(%rbp),%rax
je     126f <ReadFile+0xef>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda6(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    128c <ReadFile+0x10c>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1214 <ReadFile+0x94>
mov    %rax,%rbx
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r12
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    0x1(%r12),%rdi
call   1040 <safe_malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    %r12,%rax
jne    1204 <ReadFile+0x84>
movb   $0x0,(%r15,%r12,1)
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    122f <ReadFile+0xaf>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"warning: failed to read file '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @ReadFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i8* null, i8** %2
br label %49
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %4
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %5
%21 = load i32*, i32** %4
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64, i64* %5
%25 = add nsw i64 %24, 1
%26 = call i64 @safe_malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i64, i64* %5
%30 = load i32*, i32** %4
%31 = call i64 @fread(i8* %28, i32 1, i64 %29, i32* %30)
%32 = load i64, i64* %5
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %42
34: ; preds = %15
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
%37 = load i32*, i32** %4
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %3
%41 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %40)
store i8* null, i8** %2
br label %49
42: ; preds = %15
%43 = load i8*, i8** %6
%44 = load i64, i64* %5
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8 0, i8* %45
%46 = load i32*, i32** %4
%47 = call i32 @fclose(i32* %46)
%48 = load i8*, i8** %6
store i8* %48, i8** %2
br label %49
49: ; preds = %42, %34, %11
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"warning: failed to read file '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @ReadFile(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %26
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %2, i32 0, i32 %11)
%13 = add nsw i64 %10, 1
%14 = tail call i64 @safe_malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i64 @fread(i8* %15, i32 1, i64 %10, i32* %2)
%17 = icmp eq i64 %16, %10
br i1 %17, label %23, label %18
18: ; preds = %7
%19 = tail call i32 @free(i8* %15)
%20 = tail call i32 @fclose(i32* %2)
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %26
23: ; preds = %7
%24 = getelementptr inbounds i8, i8* %15, i64 %10
store i8 0, i8* %24
%25 = tail call i32 @fclose(i32* %2)
br label %26
26: ; preds = %18, %23, %4
%27 = phi i8* [ null, %4 ], [ null, %18 ], [ %15, %23 ]
ret i8* %27
}