#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*/ sys_cmd;
struct stat {
  int dummy;
};

/* Variables and functions */
int WEXITSTATUS(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ os_snprintf(char *, int, char *, char const *, ...);
int stat(char *, struct stat *);
int /*<<< orphan*/ system(char *);
int unlink(char *);
int os_kmod_load(const char *module_name) {
  char sys_cmd[512];
  struct stat sstat;
  int ret;
  os_snprintf(sys_cmd, sizeof(sys_cmd), "lsmod | grep -q %s", module_name);
  ret = WEXITSTATUS(system(sys_cmd));
  if (ret != 0) {
    os_snprintf(sys_cmd, sizeof(sys_cmd), "modprobe %s", module_name);
    ret = WEXITSTATUS(system(sys_cmd));
    if (ret != 0)
      return ret;
  }
  os_snprintf(sys_cmd, sizeof(sys_cmd), "/dev/%s", module_name);
  ret = stat(sys_cmd, &sstat);
  if (ret == 0) {
    ret = unlink(sys_cmd);
    if (ret != 0)
      return -errno;
  }
  os_snprintf(sys_cmd, sizeof(sys_cmd),
              "mknod /dev/%s c `cat /proc/devices "
              "| grep %s | cut -d ' ' -f 1` 0",
              module_name, module_name);
  ret = WEXITSTATUS(system(sys_cmd));
  return ret;
}


x86 assembly (clang, -O0):
<os_kmod_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
lea    0xe8a(%rip),%rdx
mov    $0x0,%al
call   1050 <os_snprintf@plt>
lea    -0x210(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
je     11fa <os_kmod_load+0xaa>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
lea    0xe59(%rip),%rdx
mov    $0x0,%al
call   1050 <os_snprintf@plt>
lea    -0x210(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
je     11f5 <os_kmod_load+0xa5>
mov    -0x21c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12b9 <os_kmod_load+0x169>
jmp    11fa <os_kmod_load+0xaa>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
lea    0xe0e(%rip),%rdx
mov    $0x0,%al
call   1050 <os_snprintf@plt>
lea    -0x210(%rbp),%rdi
lea    -0x218(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
jne    1275 <os_kmod_load+0x125>
lea    -0x210(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
je     1270 <os_kmod_load+0x120>
xor    %eax,%eax
mov    0x2d7a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12b9 <os_kmod_load+0x169>
jmp    1275 <os_kmod_load+0x125>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xd97(%rip),%rdx
mov    $0x0,%al
call   1050 <os_snprintf@plt>
lea    -0x210(%rbp),%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x21c(%rbp)
mov    -0x21c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_kmod_load>:
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%r14
lea    0xe9c(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <os_snprintf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     11bf <os_kmod_load+0x6f>
lea    0xe7e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <os_snprintf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
jne    1239 <os_kmod_load+0xe9>
lea    0xe59(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <os_snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %eax,%eax
je     1220 <os_kmod_load+0xd0>
lea    0xe32(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r14,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1050 <os_snprintf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %eax,%edi
call   1070 <WEXITSTATUS@plt>
jmp    1239 <os_kmod_load+0xe9>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
je     11ee <os_kmod_load+0x9e>
mov    0x2dab(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x218,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [19 x i8] c"lsmod | grep -q %s\00"
@.str.1 = [12 x i8] c"modprobe %s\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@errno = global i32 0
@.str.3 = [66 x i8] c"mknod /dev/%s c `cat /proc/devices | grep %s | cut -d ' ' -f 1` 0\00"
define i32 @os_kmod_load(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [512 x i8]
%5 = alloca %struct.stat
%6 = alloca i32
store i8* %0, i8** %3
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %7, i32 512, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%11 = call i32 @system(i8* %10)
%12 = call i32 @WEXITSTATUS(i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %1
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %16, i32 512, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%20 = call i32 @system(i8* %19)
%21 = call i32 @WEXITSTATUS(i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = load i32, i32* %6
store i32 %25, i32* %2
br label %53
26: ; preds = %15
br label %27
27: ; preds = %26, %1
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%29 = load i8*, i8** %3
%30 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %28, i32 512, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%32 = call i32 @stat(i8* %31, %struct.stat* %5)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %27
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%37 = call i32 @unlink(i8* %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* @errno
%42 = sub nsw i32 0, %41
store i32 %42, i32* %2
br label %53
43: ; preds = %35
br label %44
44: ; preds = %43, %27
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%46 = load i8*, i8** %3
%47 = load i8*, i8** %3
%48 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %45, i32 512, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i8* %46, i8* %47)
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%50 = call i32 @system(i8* %49)
%51 = call i32 @WEXITSTATUS(i32 %50)
store i32 %51, i32* %6
%52 = load i32, i32* %6
store i32 %52, i32* %2
br label %53
53: ; preds = %44, %40, %24
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [19 x i8] c"lsmod | grep -q %s\00"
@.str.1 = [12 x i8] c"modprobe %s\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
@errno = global i32 0
@.str.3 = [66 x i8] c"mknod /dev/%s c `cat /proc/devices | grep %s | cut -d ' ' -f 1` 0\00"
define i32 @os_kmod_load(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32 @system(i8* %4)
%8 = call i32 @WEXITSTATUS(i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = call i32 @system(i8* %4)
%13 = call i32 @WEXITSTATUS(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %10, %1
%16 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%17 = call i32 @stat(i8* %4, %struct.stat* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = call i32 @unlink(i8* %4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
br label %29
25: ; preds = %19, %15
%26 = call i32 (i8*, i32, i8*, i8*, ...) @os_snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %0)
%27 = call i32 @system(i8* %4)
%28 = call i32 @WEXITSTATUS(i32 %27)
br label %29
29: ; preds = %10, %25, %22
%30 = phi i32 [ %24, %22 ], [ %28, %25 ], [ %13, %10 ]
ret i32 %30
}