#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 const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *read_file(const char *fname) {
  FILE *f;
  size_t len, r;
  char *bytes;
  if (!(f = fopen(fname, "rb")))
    return NULL;
  fseek(f, 0, SEEK_END);
  len = ftell(f);
  rewind(f);
  if (!(bytes = malloc(len + 2)))
    return NULL;
  r = fread(bytes, 1, len, f);
  if (r != len) {
    free(bytes);
    return NULL;
  }
  fclose(f);
  bytes[len] = '\0';
  return bytes;
}


x86 assembly (clang, -O0):
<read_file>:
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)
cmp    $0x0,%rax
jne    11b7 <read_file+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1264 <read_file+0xe4>
mov    -0x18(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x2,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1209 <read_file+0x89>
movq   $0x0,-0x8(%rbp)
jmp    1264 <read_file+0xe4>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1247 <read_file+0xc7>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1264 <read_file+0xe4>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11ff <read_file+0x7f>
mov    %rax,%rbx
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <rewind@plt>
lea    0x2(%r15),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11ff <read_file+0x7f>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jne    11f7 <read_file+0x77>
mov    %rbx,%rdi
call   1050 <fclose@plt>
movb   $0x0,(%r14,%r15,1)
jmp    1202 <read_file+0x82>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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