#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*/ flokivm_die(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *fopen(char *, char *);
long fread(void *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
void *malloc(long);
void load_default_image(void **outImage, long *outSize) {
  FILE *fp;
  fp = fopen("default.flokiimage", "rb");
  if (!fp)
    flokivm_die(NULL, "Couldn't open default VM image: %s",
                "default.flokiimage");
  fseek(fp, 0, SEEK_END);
  *outSize = (long)ftell(fp);
  fseek(fp, 0, SEEK_SET);
  if (*outSize == 0)
    flokivm_die(NULL, "VM image is empty.");
  *outImage = malloc(*outSize);
  if (*outImage == 0)
    flokivm_die(NULL, "Couln't allocate memory to load VM image.");
  long result = fread(*outImage, 1, *outSize, fp);
  if (result != *outSize)
    flokivm_die(NULL, "Couldn't read VM image contents.");
  fclose(fp);
}


x86 assembly (clang, -O0):
<load_default_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe79(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bb <load_default_image+0x4b>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe69(%rip),%rsi
lea    0xe4c(%rip),%rdx
mov    $0x0,%al
call   1060 <flokivm_die@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1216 <load_default_image+0xa6>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1060 <flokivm_die@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    124c <load_default_image+0xdc>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1060 <flokivm_die@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
je     128f <load_default_image+0x11f>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1060 <flokivm_die@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_default_image>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe7b(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b0 <load_default_image+0x40>
lea    0xe76(%rip),%rsi
lea    0xe59(%rip),%rdx
xor    %edi,%edi
xor    %eax,%eax
call   1060 <flokivm_die@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1050 <ftell@plt>
mov    %rax,(%r14)
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    (%r14),%rbx
test   %rbx,%rbx
jne    11fc <load_default_image+0x8c>
lea    0xe49(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1060 <flokivm_die@plt>
mov    (%r14),%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    1222 <load_default_image+0xb2>
lea    0xe39(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1060 <flokivm_die@plt>
mov    (%r15),%rax
mov    (%r14),%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    (%r14),%rax
je     124a <load_default_image+0xda>
lea    0xe35(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1060 <flokivm_die@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"default.flokiimage\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [35 x i8] c"Couldn't open default VM image: %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [19 x i8] c"VM image is empty.\00"
@.str.4 = [42 x i8] c"Couln't allocate memory to load VM image.\00"
@.str.5 = [33 x i8] c"Couldn't read VM image contents.\00"
define void @load_default_image(i8** %0, i64* %1) {
%3 = alloca i8**
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca i64
store i8** %0, i8*** %3
store i64* %1, i64** %4
%7 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32*, i32** %5
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i32 0, i32 %14)
%16 = load i32*, i32** %5
%17 = call i64 @ftell(i32* %16)
%18 = load i64*, i64** %4
store i64 %17, i64* %18
%19 = load i32*, i32** %5
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i64*, i64** %4
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %12
%28 = load i64*, i64** %4
%29 = load i64, i64* %28
%30 = call i8* @malloc(i64 %29)
%31 = load i8**, i8*** %3
store i8* %30, i8** %31
%32 = load i8**, i8*** %3
%33 = load i8*, i8** %32
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %35, %27
%38 = load i8**, i8*** %3
%39 = load i8*, i8** %38
%40 = load i64*, i64** %4
%41 = load i64, i64* %40
%42 = load i32*, i32** %5
%43 = call i64 @fread(i8* %39, i32 1, i64 %41, i32* %42)
store i64 %43, i64* %6
%44 = load i64, i64* %6
%45 = load i64*, i64** %4
%46 = load i64, i64* %45
%47 = icmp ne i64 %44, %46
br i1 %47, label %48, label %50
48: ; preds = %37
%49 = call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %48, %37
%51 = load i32*, i32** %5
%52 = call i32 @fclose(i32* %51)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"default.flokiimage\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [35 x i8] c"Couldn't open default VM image: %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [19 x i8] c"VM image is empty.\00"
@.str.4 = [42 x i8] c"Couln't allocate memory to load VM image.\00"
@.str.5 = [33 x i8] c"Couldn't read VM image contents.\00"
define void @load_default_image(i8** nocapture %0, i64* nocapture %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
store i64 %10, i64* %1
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = load i64, i64* %1
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = tail call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%17 = load i64, i64* %1
br label %18
18: ; preds = %15, %7
%19 = phi i64 [ %17, %15 ], [ %13, %7 ]
%20 = tail call i8* @malloc(i64 %19)
store i8* %20, i8** %0
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = tail call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%24 = load i8*, i8** %0
%25 = load i64, i64* %1
br label %26
26: ; preds = %22, %18
%27 = phi i64 [ %25, %22 ], [ %19, %18 ]
%28 = phi i8* [ %24, %22 ], [ %20, %18 ]
%29 = tail call i64 @fread(i8* %28, i32 1, i64 %27, i32* %3)
%30 = load i64, i64* %1
%31 = icmp eq i64 %29, %30
br i1 %31, label %34, label %32
32: ; preds = %26
%33 = tail call i32 (i32*, i8*, ...) @flokivm_die(i32* null, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %26
%35 = tail call i32 @fclose(i32* %3)
ret void
}