#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, size_t, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *xmalloc(size_t);
char *read_file(const char *filename) {
  FILE *file = fopen(filename, "rb");
  if (file == NULL) {
    return NULL;
  }
  if (fseek(file, 0, SEEK_END) != 0) {
    fclose(file);
    return NULL;
  }
  long bufsize = ftell(file);
  if (bufsize == -1) {
    fclose(file);
    return NULL;
  }
  if (fseek(file, 0, SEEK_SET) != 0) {
    fclose(file);
    return NULL;
  }
  char *buf = xmalloc((size_t)bufsize + 1);
  size_t length = fread(buf, 1, (size_t)bufsize, file);
  fclose(file);
  buf[length + 1] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1198 <read_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1277 <read_file+0x117>
mov    -0x18(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     11cc <read_file+0x6c>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1277 <read_file+0x117>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11fa <read_file+0x9a>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1277 <read_file+0x117>
mov    -0x18(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     122e <read_file+0xce>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1277 <read_file+0x117>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,0x1(%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):
<read_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1199 <read_file+0x39>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
je     11a5 <read_file+0x45>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1060 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1191 <read_file+0x31>
mov    %rax,%r15
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
jne    1191 <read_file+0x31>
mov    %r15,%rdi
add    $0x1,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <fclose@plt>
movb   $0x0,0x1(%r15,%r14,1)
jmp    119c <read_file+0x3c>

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

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