#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 pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
scalar_t__ execv(char *, char **);
int fork();
int waitpid(int, int *, int /*<<< orphan*/);
void remove_kmod(const char *mod) {
  pid_t pid;
  pid = fork();
  assert(pid != -1);
  if (pid > 0) {
    int stat;
    pid_t c;
    while ((c = waitpid(pid, &stat, 0)) == (pid_t)-1 && errno == EINTR)
      ;
    assert(c == pid);
    assert(stat == 0);
  } else {
    char *argv[5];
    argv[0] = "/sbin/rmmod";
    argv[1] = (char *)mod;
    argv[2] = NULL;
    assert(execv(argv[0], argv) == 0);
  }
}


x86 assembly (clang, -O0):
<remove_kmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
jle    11ef <remove_kmod+0xaf>
jmp    1176 <remove_kmod+0x36>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x41(%rbp)
jne    11b1 <remove_kmod+0x71>
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11c1 <remove_kmod+0x81>
jmp    11c6 <remove_kmod+0x86>
jmp    1176 <remove_kmod+0x36>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
jmp    1228 <remove_kmod+0xe8>
lea    0xe0a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <execv@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_kmod>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1030 <assert@plt>
test   %ebx,%ebx
jle    11af <remove_kmod+0x6f>
mov    %rsp,%r14
mov    0x2e6d(%rip),%r15
mov    0x2e5e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    119a <remove_kmod+0x5a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1180 <remove_kmod+0x40>
xor    %edi,%edi
cmp    %ebx,%eax
sete   %dil
call   1030 <assert@plt>
xor    %edi,%edi
cmpl   $0x0,(%rsp)
jmp    11d5 <remove_kmod+0x95>
lea    0xe4a(%rip),%rdi
mov    %rdi,(%rsp)
mov    %r14,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rsi
call   1050 <execv@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1030 <assert@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [12 x i8] c"/sbin/rmmod\00"
define void @remove_kmod(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [5 x i8*]
store i8* %0, i8** %2
%7 = call i32 (...) @fork()
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, -1
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i32, i32* %3
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %1
br label %15
15: ; preds = %25, %14
%16 = load i32, i32* %3
%17 = call i32 @waitpid(i32 %16, i32* %4, i32 0)
store i32 %17, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %15
%24 = phi i1 [ false, %15 ], [ %22, %19 ]
br i1 %24, label %25, label %26
25: ; preds = %23
br label %15
26: ; preds = %23
%27 = load i32, i32* %5
%28 = load i32, i32* %3
%29 = icmp eq i32 %27, %28
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
br label %48
36: ; preds = %1
%37 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %37
%38 = load i8*, i8** %2
%39 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 1
store i8* %38, i8** %39
%40 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 2
store i8* null, i8** %40
%41 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 0
%42 = load i8*, i8** %41
%43 = getelementptr inbounds [5 x i8*], [5 x i8*]* %6, i64 0, i64 0
%44 = call i64 @execv(i8* %42, i8** %43)
%45 = icmp eq i64 %44, 0
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
br label %48
48: ; preds = %36, %26
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [12 x i8] c"/sbin/rmmod\00"
define void @remove_kmod(i8* %0) {
%2 = alloca i32
%3 = alloca [5 x i8*]
%4 = tail call i32 (...) @fork()
%5 = icmp ne i32 %4, -1
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp sgt i32 %4, 0
br i1 %8, label %9, label %26
9: ; preds = %1
%10 = bitcast i32* %2 to i8*
br label %11
11: ; preds = %11, %9
%12 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%13 = icmp eq i32 %12, -1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %11, label %18
18: ; preds = %11
%19 = icmp eq i32 %12, %4
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
br label %35
26: ; preds = %1
%27 = bitcast [5 x i8*]* %3 to i8*
%28 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %28
%29 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 1
store i8* %0, i8** %29
%30 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %30
%31 = call i64 @execv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %28)
%32 = icmp eq i64 %31, 0
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
br label %35
35: ; preds = %26, %18
ret void
}