#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
unsigned long fread(void *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
int init_module(void *, unsigned long, char const *);
void *malloc(unsigned long);
int /*<<< orphan*/ stderr;
int manual_insmod_26(const char *fname, const char *opts) {
  unsigned long len, read_len;
  int ret = -1;
  void *buff;
  FILE *f;
  f = fopen(fname, "rb");
  if (f == NULL)
    return -1;
  fseek(f, 0, SEEK_END);
  len = ftell(f);
  fseek(f, 0, SEEK_SET);
  buff = malloc(len);
  if (buff == NULL)
    goto fail0;
  read_len = fread(buff, 1, len, f);
  if (read_len != len) {
    fprintf(stderr, "failed to read module\n");
    goto fail1;
  }
  ret = init_module(buff, len, opts);
fail1:
  free(buff);
fail0:
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<manual_insmod_26>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11d2 <manual_insmod_26+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <manual_insmod_26+0x102>
mov    -0x40(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x40(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1224 <manual_insmod_26+0x94>
jmp    1283 <manual_insmod_26+0xf3>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1266 <manual_insmod_26+0xd6>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
call   1060 <fprintf@plt>
jmp    127a <manual_insmod_26+0xea>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   10a0 <init_module@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manual_insmod_26>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
lea    0xe5e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     121a <manual_insmod_26+0x8a>
mov    %rax,%rbx
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1221 <manual_insmod_26+0x91>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    1228 <manual_insmod_26+0x98>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   10a0 <init_module@plt>
mov    %eax,%ebp
jmp    1242 <manual_insmod_26+0xb2>
mov    $0xffffffff,%ebp
jmp    1252 <manual_insmod_26+0xc2>
mov    $0xffffffff,%ebp
jmp    124a <manual_insmod_26+0xba>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"failed to read module\0A\00"
define i32 @manual_insmod_26(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %27, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i64 @ftell(i32* %3)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i8* @malloc(i64 %8)
%12 = icmp eq i8* %11, null
br i1 %12, label %24, label %13
13: ; preds = %5
%14 = tail call i64 @fread(i8* %11, i32 1, i64 %8, i32* %3)
%15 = icmp eq i64 %14, %8
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %21
19: ; preds = %13
%20 = tail call i32 @init_module(i8* %11, i64 %8, i8* %1)
br label %21
21: ; preds = %19, %16
%22 = phi i32 [ -1, %16 ], [ %20, %19 ]
%23 = tail call i32 @free(i8* %11)
br label %24
24: ; preds = %5, %21
%25 = phi i32 [ -1, %5 ], [ %22, %21 ]
%26 = tail call i32 @fclose(i32* %3)
br label %27
27: ; preds = %2, %24
%28 = phi i32 [ %25, %24 ], [ -1, %2 ]
ret i32 %28
}