#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ __NR_finit_module;
int /*<<< orphan*/ __NR_init_module;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fstat(int, struct stat *);
void *malloc(size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read(int, void *, size_t);
scalar_t__ syscall(int /*<<< orphan*/, void *, ...);
int main(int argc, char **argv) {
  const char *params;
  int fd, use_finit;
  size_t image_size;
  struct stat st;
  void *image;
  if (argc < 2) {
    puts("Usage ./prog mymodule.ko [args="
         " [use_finit=0]");
    return EXIT_FAILURE;
  }
  if (argc < 3) {
    params = "";
  } else {
    params = argv[2];
  }
  if (argc < 4) {
    use_finit = 0;
  } else {
    use_finit = (argv[3][0] != '0');
  }
  fd = open(argv[1], O_RDONLY);
  if (use_finit) {
    puts("finit");
    if (syscall(__NR_finit_module, fd, params, 0) != 0) {
      perror("finit_module");
      return EXIT_FAILURE;
    }
    close(fd);
  } else {
    puts("init");
    fstat(fd, &st);
    image_size = st.st_size;
    image = malloc(image_size);
    read(fd, image, image_size);
    close(fd);
    if (syscall(__NR_init_module, image, image_size, params) != 0) {
      perror("init_module");
      return EXIT_FAILURE;
    }
    free(image);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11cd <main+0x3d>
lea    0xe49(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <main+0x1ac>
cmpl   $0x3,-0x8(%rbp)
jge    11e7 <main+0x57>
lea    0xe4f(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11f3 <main+0x63>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1209 <main+0x79>
movl   $0x0,-0x20(%rbp)
jmp    1222 <main+0x92>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     129f <main+0x10f>
lea    0xde2(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
movslq -0x1c(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
mov    $0x0,%al
call   1070 <syscall@plt>
cmp    $0x0,%rax
je     1292 <main+0x102>
lea    0xdb8(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <main+0x1ac>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1330 <main+0x1a0>
lea    0xd89(%rip),%rdi
call   1040 <puts@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10b0 <fstat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <read@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x0,%al
call   1070 <syscall@plt>
cmp    $0x0,%rax
je     1327 <main+0x197>
lea    0xd24(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cb3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <main+0x1ac>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11b4 <main+0x24>
lea    0xe5d(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e21(%rip),%rax
jmp    124d <main+0xbd>
cmp    $0x2,%edi
jne    11c2 <main+0x32>
lea    0xe6d(%rip),%r14
jmp    11cf <main+0x3f>
mov    0x10(%rsi),%r14
cmp    $0x3,%edi
ja     125a <main+0xca>
mov    0x8(%rsi),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r15d
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
call   10b0 <fstat@plt>
mov    (%rsp),%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1060 <read@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <syscall@plt>
test   %rax,%rax
jne    12b1 <main+0x121>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsi),%rdi
mov    0x18(%rsi),%rax
mov    (%rax),%bl
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r15d
cmp    $0x30,%bl
je     11e4 <main+0x54>
lea    0xda9(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
movslq %r15d,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <syscall@plt>
test   %rax,%rax
jne    12c6 <main+0x136>
mov    %r15d,%edi
call   1050 <close@plt>
jmp    1246 <main+0xb6>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d0c(%rip),%rax
jmp    124d <main+0xbd>
lea    0xd67(%rip),%rdi
jmp    12b8 <main+0x128>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [46 x i8] c"Usage ./prog mymodule.ko [args= [use_finit=0]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [1 x i8] zeroinitializer
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"finit\00"
@__NR_finit_module = global i32 0
@.str.3 = [13 x i8] c"finit_module\00"
@.str.4 = [5 x i8] c"init\00"
@__NR_init_module = global i32 0
@.str.5 = [12 x i8] c"init_module\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca %struct.stat
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @puts(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %3
br label %89
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 3
br i1 %19, label %20, label %21
20: ; preds = %17
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %25
21: ; preds = %17
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
store i8* %24, i8** %6
br label %25
25: ; preds = %21, %20
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 4
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 0, i32* %8
br label %38
29: ; preds = %25
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 48
%37 = zext i1 %36 to i32
store i32 %37, i32* %8
br label %38
38: ; preds = %29, %28
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %7
%44 = load i32, i32* %8
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %61
46: ; preds = %38
%47 = call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @__NR_finit_module
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = inttoptr i64 %50 to i8*
%52 = load i8*, i8** %6
%53 = call i64 (i32, i8*, ...) @syscall(i32 %48, i8* %51, i8* %52, i32 0)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %46
%56 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
store i32 %57, i32* %3
br label %89
58: ; preds = %46
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
br label %87
61: ; preds = %38
%62 = call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%63 = load i32, i32* %7
%64 = call i32 @fstat(i32 %63, %struct.stat* %10)
%65 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%66 = load i64, i64* %65
store i64 %66, i64* %9
%67 = load i64, i64* %9
%68 = call i8* @malloc(i64 %67)
store i8* %68, i8** %11
%69 = load i32, i32* %7
%70 = load i8*, i8** %11
%71 = load i64, i64* %9
%72 = call i32 @read(i32 %69, i8* %70, i64 %71)
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* @__NR_init_module
%76 = load i8*, i8** %11
%77 = load i64, i64* %9
%78 = load i8*, i8** %6
%79 = call i64 (i32, i8*, ...) @syscall(i32 %75, i8* %76, i64 %77, i8* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %61
%82 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%83 = load i32, i32* @EXIT_FAILURE
store i32 %83, i32* %3
br label %89
84: ; preds = %61
%85 = load i8*, i8** %11
%86 = call i32 @free(i8* %85)
br label %87
87: ; preds = %84, %58
%88 = load i32, i32* @EXIT_SUCCESS
store i32 %88, i32* %3
br label %89
89: ; preds = %87, %81, %55, %14
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [46 x i8] c"Usage ./prog mymodule.ko [args= [use_finit=0]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [1 x i8] zeroinitializer
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"finit\00"
@__NR_finit_module = global i32 0
@.str.3 = [13 x i8] c"finit_module\00"
@.str.4 = [5 x i8] c"init\00"
@__NR_init_module = global i32 0
@.str.5 = [12 x i8] c"init_module\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %57
8: ; preds = %2
%9 = icmp eq i32 %0, 2
br i1 %9, label %14, label %10
10: ; preds = %8
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = icmp ult i32 %0, 4
br i1 %13, label %14, label %20
14: ; preds = %10, %8
%15 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %8 ]
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %17, i32 %18)
br label %40
20: ; preds = %10
%21 = getelementptr inbounds i8*, i8** %1, i64 3
%22 = load i8*, i8** %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 48
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 @open(i8* %26, i32 %27)
br i1 %24, label %40, label %29
29: ; preds = %20
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @__NR_finit_module
%32 = sext i32 %28 to i64
%33 = inttoptr i64 %32 to i8*
%34 = tail call i64 (i32, i8*, ...) @syscall(i32 %31, i8* %33, i8* %12, i32 0)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %29
%37 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %57
38: ; preds = %29
%39 = tail call i32 @close(i32 %28)
br label %57
40: ; preds = %14, %20
%41 = phi i32 [ %19, %14 ], [ %28, %20 ]
%42 = phi i8* [ %15, %14 ], [ %12, %20 ]
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @fstat(i32 %41, %struct.stat* %3)
%45 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%46 = load i64, i64* %45
%47 = tail call i8* @malloc(i64 %46)
%48 = tail call i32 @read(i32 %41, i8* %47, i64 %46)
%49 = tail call i32 @close(i32 %41)
%50 = load i32, i32* @__NR_init_module
%51 = tail call i64 (i32, i8*, ...) @syscall(i32 %50, i8* %47, i64 %46, i8* %42)
%52 = icmp eq i64 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %40
%54 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %57
55: ; preds = %40
%56 = tail call i32 @free(i8* %47)
br label %57
57: ; preds = %38, %55, %53, %36, %6
%58 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_FAILURE, %36 ], [ @EXIT_FAILURE, %53 ], [ @EXIT_SUCCESS, %55 ], [ @EXIT_SUCCESS, %38 ]
%59 = load i32, i32* %58
ret i32 %59
}