#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 */
struct WAD_node {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
struct WAD_node *WAD_createNode(char const *, unsigned int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
struct WAD_node *WAD_loadFile(const char *filename) {
  FILE *file = fopen(filename, "r");
  if (!file) {
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  unsigned int size = ftell(file);
  rewind(file);
  char *data = (char *)malloc(sizeof(char) * size);
  fread(data, sizeof(char), size, file);
  fclose(file);
  return WAD_createNode(filename, size, data);
}


x86 assembly (clang, -O0):
<WAD_loadFile>:
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    11b8 <WAD_loadFile+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1225 <WAD_loadFile+0xa5>
mov    -0x18(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x1c(%rbp),%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1050 <WAD_createNode@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WAD_loadFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6f(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11fe <WAD_loadFile+0x7e>
mov    %rax,%rbx
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <WAD_createNode@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.WAD_node = type { i32 }
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define %struct.WAD_node* @WAD_loadFile(i8* %0) {
%2 = alloca %struct.WAD_node*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 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 %12, label %11
11: ; preds = %1
store %struct.WAD_node* null, %struct.WAD_node** %2
br label %36
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 i32 @ftell(i32* %16)
store i32 %17, i32* %5
%18 = load i32*, i32** %4
%19 = call i32 @rewind(i32* %18)
%20 = load i32, i32* %5
%21 = zext i32 %20 to i64
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = load i32, i32* %5
%28 = load i32*, i32** %4
%29 = call i32 @fread(i8* %26, i32 1, i32 %27, i32* %28)
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %3
%33 = load i32, i32* %5
%34 = load i8*, i8** %6
%35 = call %struct.WAD_node* @WAD_createNode(i8* %32, i32 %33, i8* %34)
store %struct.WAD_node* %35, %struct.WAD_node** %2
br label %36
36: ; preds = %12, %11
%37 = load %struct.WAD_node*, %struct.WAD_node** %2
ret %struct.WAD_node* %37
}

LLVM IR (-O2):
%struct.WAD_node = type { i32 }
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define %struct.WAD_node* @WAD_loadFile(i8* %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 %14, 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 i32 @ftell(i32* %2)
%8 = tail call i32 @rewind(i32* %2)
%9 = tail call i64 @malloc(i32 %7)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @fread(i8* %10, i32 1, i32 %7, i32* %2)
%12 = tail call i32 @fclose(i32* %2)
%13 = tail call %struct.WAD_node* @WAD_createNode(i8* %0, i32 %7, i8* %10)
br label %14
14: ; preds = %1, %4
%15 = phi %struct.WAD_node* [ %13, %4 ], [ null, %1 ]
ret %struct.WAD_node* %15
}