#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*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *const, char *);
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *read_file(const char *const filename) {
  if (filename == NULL)
    return NULL;
  FILE *fp = fopen(filename, "r");
  if (fp == NULL)
    return NULL;
  assert(!(fseek(fp, 0, SEEK_END)));
  long file_size = ftell(fp);
  rewind(fp);
  size_t code_size = sizeof(char) * file_size;
  char *code = malloc(code_size);
  if (code == NULL)
    return NULL;
  fread(code, 1, file_size, fp);
  assert(!(fclose(fp)));
  return code;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <read_file+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_file+0xf6>
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <read_file+0x50>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_file+0xf6>
mov    -0x18(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123d <read_file+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    1276 <read_file+0xf6>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
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   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1218 <read_file+0x98>
lea    0xe68(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1218 <read_file+0x98>
mov    %rax,%rbx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r12,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     121b <read_file+0x9b>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    %r15,%r14
jmp    121b <read_file+0x9b>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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