#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 stat {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ init_module(void *, size_t, char *);
void *malloc(size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, void *, size_t);
const void load_module(void) {
  int fd = open("driver.ko", O_RDONLY);
  struct stat st;
  fstat(fd, &st);
  size_t image_size = st.st_size;
  void *image = malloc(image_size);
  read(fd, image, image_size);
  close(fd);
  if (init_module(image, image_size, "") != 0) {
    printf("VISH\n");
  }
}


x86 assembly (clang, -O0):
<load_module>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xe37(%rip),%rdx
call   1070 <init_module@plt>
cmp    $0x0,%rax
je     11f3 <load_module+0x83>
lea    0xe1c(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_module>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %ebp,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %ebp,%edi
call   1040 <close@plt>
lea    0xe48(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <init_module@plt>
test   %rax,%rax
je     11ea <load_module+0x7a>
lea    0xe2d(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <puts@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [10 x i8] c"driver.ko\00"
@O_RDONLY = global i32 0
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [6 x i8] c"VISH\0A\00"
define void @load_module() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca i64
%4 = alloca i8*
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = call i32 @fstat(i32 %7, %struct.stat* %2)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%10 = load i64, i64* %9
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %4
%13 = load i32, i32* %1
%14 = load i8*, i8** %4
%15 = load i64, i64* %3
%16 = call i32 @read(i32 %13, i8* %14, i64 %15)
%17 = load i32, i32* %1
%18 = call i32 @close(i32 %17)
%19 = load i8*, i8** %4
%20 = load i64, i64* %3
%21 = call i64 @init_module(i8* %19, i64 %20, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %0
%24 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %0
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [10 x i8] c"driver.ko\00"
@O_RDONLY = global i32 0
@.str.1 = [1 x i8] zeroinitializer
@str = [5 x i8] c"VISH\00"
define void @load_module() {
%1 = alloca %struct.stat
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = bitcast %struct.stat* %1 to i8*
%5 = call i32 @fstat(i32 %3, %struct.stat* %1)
%6 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%7 = load i64, i64* %6
%8 = tail call i8* @malloc(i64 %7)
%9 = tail call i32 @read(i32 %3, i8* %8, i64 %7)
%10 = tail call i32 @close(i32 %3)
%11 = tail call i64 @init_module(i8* %8, i64 %7, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
ret void
}