#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 kmod_module {
  int dummy;
};
struct kmod_list {
  int dummy;
};

/* Variables and functions */
int EBUSY;
int ENOENT;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/);
struct kmod_list *kmod_list_next(struct kmod_list *, struct kmod_list *);
struct kmod_list *kmod_module_get_holders(struct kmod_module *);
int kmod_module_get_initstate(struct kmod_module *);
struct kmod_module *kmod_module_get_module(struct kmod_list *);
char *kmod_module_get_name(struct kmod_module *);
scalar_t__ kmod_module_get_refcnt(struct kmod_module *);
int /*<<< orphan*/ kmod_module_unref(struct kmod_module *);
int /*<<< orphan*/ kmod_module_unref_list(struct kmod_list *);
int /*<<< orphan*/ stderr;
int check_module_inuse(struct kmod_module *mod) {
  struct kmod_list *holders;
  if (kmod_module_get_initstate(mod) == -ENOENT) {
    fprintf(stderr, "Error: Module %s is not currently loaded\n",
            kmod_module_get_name(mod));
    return -ENOENT;
  }
  holders = kmod_module_get_holders(mod);
  if (holders != NULL) {
    struct kmod_list *itr;
    fprintf(stderr,
            "Error: Module %s is in use by:", kmod_module_get_name(mod));
    for (itr = holders; itr != NULL; itr = kmod_list_next(holders, itr)) {
      struct kmod_module *hm = kmod_module_get_module(itr);
      fprintf(stderr, " %s", kmod_module_get_name(hm));
      kmod_module_unref(hm);
    }
    fputc('\n', stderr);
    kmod_module_unref_list(holders);
    return -EBUSY;
  }
  if (kmod_module_get_refcnt(mod) != 0) {
    fprintf(stderr, "Error: Module %s is in use\n", kmod_module_get_name(mod));
    return -EBUSY;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_module_inuse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <kmod_module_get_initstate@plt>
xor    %ecx,%ecx
mov    0x2e0a(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    1202 <check_module_inuse+0x62>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <kmod_module_get_name@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe16(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %eax,%eax
mov    0x2dd0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    132a <check_module_inuse+0x18a>
mov    -0x10(%rbp),%rdi
call   1060 <kmod_module_get_holders@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12d6 <check_module_inuse+0x136>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <kmod_module_get_name@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xdee(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12a7 <check_module_inuse+0x107>
mov    -0x20(%rbp),%rdi
call   1030 <kmod_module_get_module@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <kmod_module_get_name@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdc6(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <kmod_module_unref@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <kmod_list_next@plt>
mov    %rax,-0x20(%rbp)
jmp    1249 <check_module_inuse+0xa9>
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    $0xa,%edi
call   1090 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1050 <kmod_module_unref_list@plt>
xor    %eax,%eax
mov    0x2d0c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    132a <check_module_inuse+0x18a>
mov    -0x10(%rbp),%rdi
call   10c0 <kmod_module_get_refcnt@plt>
cmp    $0x0,%rax
je     1323 <check_module_inuse+0x183>
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <kmod_module_get_name@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd42(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %eax,%eax
mov    0x2cbf(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    132a <check_module_inuse+0x18a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_module_inuse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <kmod_module_get_initstate@plt>
mov    0x2e0e(%rip),%rbp
add    0x0(%rbp),%eax
jne    11eb <check_module_inuse+0x4b>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   10b0 <kmod_module_get_name@plt>
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
xor    %eax,%eax
sub    0x0(%rbp),%eax
jmp    12c1 <check_module_inuse+0x121>
mov    %r14,%rdi
call   1060 <kmod_module_get_holders@plt>
test   %rax,%rax
je     1287 <check_module_inuse+0xe7>
mov    %rax,%r15
mov    0x2dda(%rip),%r13
mov    0x0(%r13),%ebx
mov    %r14,%rdi
call   10b0 <kmod_module_get_name@plt>
lea    0xe11(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
lea    0xe1f(%rip),%r14
mov    %r15,%rbx
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <kmod_module_get_module@plt>
mov    %rax,%r12
mov    0x0(%r13),%ebp
mov    %rax,%rdi
call   10b0 <kmod_module_get_name@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    %r12,%rdi
call   1070 <kmod_module_unref@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <kmod_list_next@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1230 <check_module_inuse+0x90>
mov    0x0(%r13),%esi
mov    $0xa,%edi
call   1090 <fputc@plt>
mov    %r15,%rdi
call   1050 <kmod_module_unref_list@plt>
jmp    12b6 <check_module_inuse+0x116>
mov    %r14,%rdi
call   10c0 <kmod_module_get_refcnt@plt>
test   %rax,%rax
je     12d0 <check_module_inuse+0x130>
mov    0x2d45(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   10b0 <kmod_module_get_name@plt>
lea    0xda1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    0x2d1b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12c1 <check_module_inuse+0x121>

LLVM IR (-O0):
%struct.kmod_module = type { i32 }
%struct.kmod_list = type { i32 }
@ENOENT = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"Error: Module %s is not currently loaded\0A\00"
@.str.1 = [31 x i8] c"Error: Module %s is in use by:\00"
@.str.2 = [4 x i8] c" %s\00"
@EBUSY = global i32 0
@.str.3 = [28 x i8] c"Error: Module %s is in use\0A\00"
define i32 @check_module_inuse(%struct.kmod_module* %0) {
%2 = alloca i32
%3 = alloca %struct.kmod_module*
%4 = alloca %struct.kmod_list*
%5 = alloca %struct.kmod_list*
%6 = alloca %struct.kmod_module*
store %struct.kmod_module* %0, %struct.kmod_module** %3
%7 = load %struct.kmod_module*, %struct.kmod_module** %3
%8 = call i32 @kmod_module_get_initstate(%struct.kmod_module* %7)
%9 = load i32, i32* @ENOENT
%10 = sub nsw i32 0, %9
%11 = icmp eq i32 %8, %10
br i1 %11, label %12, label %19
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load %struct.kmod_module*, %struct.kmod_module** %3
%15 = call i8* @kmod_module_get_name(%struct.kmod_module* %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @ENOENT
%18 = sub nsw i32 0, %17
store i32 %18, i32* %2
br label %65
19: ; preds = %1
%20 = load %struct.kmod_module*, %struct.kmod_module** %3
%21 = call %struct.kmod_list* @kmod_module_get_holders(%struct.kmod_module* %20)
store %struct.kmod_list* %21, %struct.kmod_list** %4
%22 = load %struct.kmod_list*, %struct.kmod_list** %4
%23 = icmp ne %struct.kmod_list* %22, null
br i1 %23, label %24, label %53
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load %struct.kmod_module*, %struct.kmod_module** %3
%27 = call i8* @kmod_module_get_name(%struct.kmod_module* %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load %struct.kmod_list*, %struct.kmod_list** %4
store %struct.kmod_list* %29, %struct.kmod_list** %5
br label %30
30: ; preds = %42, %24
%31 = load %struct.kmod_list*, %struct.kmod_list** %5
%32 = icmp ne %struct.kmod_list* %31, null
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load %struct.kmod_list*, %struct.kmod_list** %5
%35 = call %struct.kmod_module* @kmod_module_get_module(%struct.kmod_list* %34)
store %struct.kmod_module* %35, %struct.kmod_module** %6
%36 = load i32, i32* @stderr
%37 = load %struct.kmod_module*, %struct.kmod_module** %6
%38 = call i8* @kmod_module_get_name(%struct.kmod_module* %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load %struct.kmod_module*, %struct.kmod_module** %6
%41 = call i32 @kmod_module_unref(%struct.kmod_module* %40)
br label %42
42: ; preds = %33
%43 = load %struct.kmod_list*, %struct.kmod_list** %4
%44 = load %struct.kmod_list*, %struct.kmod_list** %5
%45 = call %struct.kmod_list* @kmod_list_next(%struct.kmod_list* %43, %struct.kmod_list* %44)
store %struct.kmod_list* %45, %struct.kmod_list** %5
br label %30
46: ; preds = %30
%47 = load i32, i32* @stderr
%48 = call i32 @fputc(i8 signext 10, i32 %47)
%49 = load %struct.kmod_list*, %struct.kmod_list** %4
%50 = call i32 @kmod_module_unref_list(%struct.kmod_list* %49)
%51 = load i32, i32* @EBUSY
%52 = sub nsw i32 0, %51
store i32 %52, i32* %2
br label %65
53: ; preds = %19
%54 = load %struct.kmod_module*, %struct.kmod_module** %3
%55 = call i64 @kmod_module_get_refcnt(%struct.kmod_module* %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %53
%58 = load i32, i32* @stderr
%59 = load %struct.kmod_module*, %struct.kmod_module** %3
%60 = call i8* @kmod_module_get_name(%struct.kmod_module* %59)
%61 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %60)
%62 = load i32, i32* @EBUSY
%63 = sub nsw i32 0, %62
store i32 %63, i32* %2
br label %65
64: ; preds = %53
store i32 0, i32* %2
br label %65
65: ; preds = %64, %57, %46, %12
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
%struct.kmod_module = type { i32 }
%struct.kmod_list = type { i32 }
@ENOENT = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"Error: Module %s is not currently loaded\0A\00"
@.str.1 = [31 x i8] c"Error: Module %s is in use by:\00"
@.str.2 = [4 x i8] c" %s\00"
@EBUSY = global i32 0
@.str.3 = [28 x i8] c"Error: Module %s is in use\0A\00"
define i32 @check_module_inuse(%struct.kmod_module* %0) {
%2 = tail call i32 @kmod_module_get_initstate(%struct.kmod_module* %0)
%3 = load i32, i32* @ENOENT
%4 = sub nsw i32 0, %3
%5 = icmp eq i32 %2, %4
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i8* @kmod_module_get_name(%struct.kmod_module* %0)
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @ENOENT
%11 = sub nsw i32 0, %10
br label %43
12: ; preds = %1
%13 = tail call %struct.kmod_list* @kmod_module_get_holders(%struct.kmod_module* %0)
%14 = icmp eq %struct.kmod_list* %13, null
br i1 %14, label %34, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i8* @kmod_module_get_name(%struct.kmod_module* %0)
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %15, %19
%20 = phi %struct.kmod_list* [ %13, %15 ], [ %26, %19 ]
%21 = tail call %struct.kmod_module* @kmod_module_get_module(%struct.kmod_list* %20)
%22 = load i32, i32* @stderr
%23 = tail call i8* @kmod_module_get_name(%struct.kmod_module* %21)
%24 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = tail call i32 @kmod_module_unref(%struct.kmod_module* %21)
%26 = tail call %struct.kmod_list* @kmod_list_next(%struct.kmod_list* %13, %struct.kmod_list* %20)
%27 = icmp eq %struct.kmod_list* %26, null
br i1 %27, label %28, label %19
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = tail call i32 @fputc(i8 signext 10, i32 %29)
%31 = tail call i32 @kmod_module_unref_list(%struct.kmod_list* %13)
%32 = load i32, i32* @EBUSY
%33 = sub nsw i32 0, %32
br label %43
34: ; preds = %12
%35 = tail call i64 @kmod_module_get_refcnt(%struct.kmod_module* %0)
%36 = icmp eq i64 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = tail call i8* @kmod_module_get_name(%struct.kmod_module* %0)
%40 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @EBUSY
%42 = sub nsw i32 0, %41
br label %43
43: ; preds = %34, %37, %28, %6
%44 = phi i32 [ %11, %6 ], [ %33, %28 ], [ %42, %37 ], [ 0, %34 ]
ret i32 %44
}