#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fread(void *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
void *malloc(size_t);
int /*<<< orphan*/ stderr;
void *load_module(const char *filename, size_t *outsize) {
  FILE *mbf = fopen(filename, "r");
  if (mbf == NULL) {
    fprintf(stderr, "file %s not found\n", filename);
    exit(EXIT_FAILURE);
  }
  fseek(mbf, 0, SEEK_END);
  size_t fsize = ftell(mbf);
  fseek(mbf, 0, SEEK_SET);
  void *dstbuf = malloc(fsize);
  fread(dstbuf, fsize, 1, mbf);
  fclose(mbf);
  *outsize = fsize;
  return dstbuf;
}


x86 assembly (clang, -O0):
<load_module>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d6 <load_module+0x56>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3f(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_module>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120b <load_module+0x8b>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"file %s not found\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @load_module(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i32*, i32** %5
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %5
%23 = call i64 @ftell(i32* %22)
store i64 %23, i64* %6
%24 = load i32*, i32** %5
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %24, i32 0, i32 %25)
%27 = load i64, i64* %6
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = load i64, i64* %6
%31 = load i32*, i32** %5
%32 = call i32 @fread(i8* %29, i64 %30, i32 1, i32* %31)
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
%35 = load i64, i64* %6
%36 = load i64*, i64** %4
store i64 %35, i64* %36
%37 = load i8*, i8** %7
ret i8* %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"file %s not found\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @load_module(i8* %0, i64* nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %3)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %3, i32 0, i32 %14)
%16 = tail call i8* @malloc(i64 %13)
%17 = tail call i32 @fread(i8* %16, i64 %13, i32 1, i32* %3)
%18 = tail call i32 @fclose(i32* %3)
store i64 %13, i64* %1
ret i8* %16
}