#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EINVAL;
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ error(char *, char const *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fatal(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ mod_in_use(char const *, char *);
char *next_field(char *);
int sscanf(char *, char *, char *, unsigned long *, unsigned long *);
int /*<<< orphan*/ *strchr(char *, char);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strerror(scalar_t__);
int check_usage(const char *modname) {
  FILE *module_list;
  char line[10240], name[64];
  unsigned long size, refs;
  int scanned;
  module_list = fopen("/proc/modules", "r");
  if (!module_list) {
    if (errno == ENOENT)
      return 0;
    fatal("can't open /proc/modules: %s\n", strerror(errno));
  }
  while (fgets(line, sizeof(line) - 1, module_list) != NULL) {
    if (strchr(line, '\n') == NULL) {
      fatal("V. v. long line broke rmmod.\n");
      exit(1);
    }
    scanned = sscanf(line, "%s %lu %lu", name, &size, &refs);
    if (strcmp(name, modname) != 0)
      continue;
    if (scanned < 2)
      fatal("Unknown format in /proc/modules: %s\n", line);
    if (scanned == 2)
      fatal("Kernel does not have unload support.\n");
    if (refs != 0)
      mod_in_use(modname, next_field(next_field(next_field(line))));
    goto out;
  }
  error("Module %s does not exist in /proc/modules\n", modname);
  refs = 1;
out:
  fclose(module_list);
  return (refs == 0) ? 0 : -EINVAL;
}


x86 assembly (clang, -O0):
<check_usage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2890,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe2a(%rip),%rdi
lea    0xe31(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1236 <check_usage+0x76>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <check_usage+0x57>
movl   $0x0,-0x4(%rbp)
jmp    13b9 <check_usage+0x1f9>
mov    0x2dc2(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    %eax,%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   10e0 <fatal@plt>
jmp    123b <check_usage+0x7b>
lea    -0x2820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x27ff,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1360 <check_usage+0x1a0>
lea    -0x2820(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    128d <check_usage+0xcd>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   10e0 <fatal@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x2820(%rbp),%rdi
lea    -0x2860(%rbp),%rdx
lea    0xdaa(%rip),%rsi
lea    -0x2868(%rbp),%rcx
lea    -0x2870(%rbp),%r8
call   1080 <sscanf@plt>
mov    %eax,-0x2874(%rbp)
lea    -0x2860(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12da <check_usage+0x11a>
jmp    123b <check_usage+0x7b>
cmpl   $0x2,-0x2874(%rbp)
jge    12fc <check_usage+0x13c>
lea    -0x2820(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   10e0 <fatal@plt>
cmpl   $0x2,-0x2874(%rbp)
jne    1317 <check_usage+0x157>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   10e0 <fatal@plt>
cmpq   $0x0,-0x2870(%rbp)
je     135b <check_usage+0x19b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x2880(%rbp)
lea    -0x2820(%rbp),%rdi
call   10c0 <next_field@plt>
mov    %rax,%rdi
call   10c0 <next_field@plt>
mov    %rax,%rdi
call   10c0 <next_field@plt>
mov    -0x2880(%rbp),%rdi
mov    %rax,%rsi
call   1060 <mod_in_use@plt>
jmp    137b <check_usage+0x1bb>
mov    -0x10(%rbp),%rsi
lea    0xd37(%rip),%rdi
call   1090 <error@plt>
movq   $0x1,-0x2870(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x2870(%rbp)
jne    139f <check_usage+0x1df>
xor    %eax,%eax
mov    %eax,-0x2884(%rbp)
jmp    13b0 <check_usage+0x1f0>
xor    %eax,%eax
mov    0x2c30(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2884(%rbp)
mov    -0x2884(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2890,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_usage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2868,%rsp
mov    %rdi,(%rsp)
lea    0xe24(%rip),%rdi
lea    0xe2b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
jne    1223 <check_usage+0x63>
mov    0x2de7(%rip),%rax
mov    (%rax),%rdi
mov    0x2dc5(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%rdi
je     132d <check_usage+0x16d>
call   10d0 <strerror@plt>
lea    0xdf6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10e0 <fatal@plt>
lea    0x60(%rsp),%rbx
lea    0xe1d(%rip),%r12
lea    0x20(%rsp),%r13
lea    0x18(%rsp),%rbp
lea    0x8(%rsp),%r14
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x27ff,%esi
mov    0x10(%rsp),%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12b0 <check_usage+0xf0>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     133f <check_usage+0x17f>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
mov    %r14,%r8
call   1080 <sscanf@plt>
mov    %eax,%r15d
mov    %r13,%rdi
mov    (%rsp),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1240 <check_usage+0x80>
cmp    $0x1,%r15d
jg     12cb <check_usage+0x10b>
lea    0xdb5(%rip),%rdi
lea    0x60(%rsp),%rsi
xor    %eax,%eax
call   10e0 <fatal@plt>
jmp    12df <check_usage+0x11f>
lea    0xdeb(%rip),%rdi
mov    (%rsp),%rsi
call   1090 <error@plt>
movq   $0x1,0x8(%rsp)
jmp    130d <check_usage+0x14d>
cmp    $0x2,%r15d
jne    12df <check_usage+0x11f>
lea    0xda4(%rip),%rdi
xor    %eax,%eax
call   10e0 <fatal@plt>
cmpq   $0x0,0x8(%rsp)
je     130d <check_usage+0x14d>
lea    0x60(%rsp),%rdi
call   10c0 <next_field@plt>
mov    %rax,%rdi
call   10c0 <next_field@plt>
mov    %rax,%rdi
call   10c0 <next_field@plt>
mov    (%rsp),%rdi
mov    %rax,%rsi
call   1060 <mod_in_use@plt>
mov    0x10(%rsp),%rdi
call   1030 <fclose@plt>
mov    0x2cba(%rip),%rcx
xor    %edx,%edx
xor    %eax,%eax
sub    (%rcx),%eax
cmpq   $0x0,0x8(%rsp)
cmove  %edx,%eax
add    $0x2868,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xce8(%rip),%rdi
xor    %eax,%eax
call   10e0 <fatal@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [30 x i8] c"can't open /proc/modules: %s\0A\00"
@.str.3 = [30 x i8] c"V. v. long line broke rmmod.\0A\00"
@.str.4 = [11 x i8] c"%s %lu %lu\00"
@.str.5 = [37 x i8] c"Unknown format in /proc/modules: %s\0A\00"
@.str.6 = [38 x i8] c"Kernel does not have unload support.\0A\00"
@.str.7 = [43 x i8] c"Module %s does not exist in /proc/modules\0A\00"
@EINVAL = global i32 0
define i32 @check_usage(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [10240 x i8]
%6 = alloca [64 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
%10 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %22, label %13
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 0, i32* %2
br label %80
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = call i32 @strerror(i64 %19)
%21 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %1
br label %23
23: ; preds = %43, %22
%24 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%25 = load i32*, i32** %4
%26 = call i32* @fgets(i8* %24, i32 10239, i32* %25)
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %66
28: ; preds = %23
%29 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%30 = call i32* @strchr(i8* %29, i8 signext 10)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%38 = call i32 @sscanf(i8* %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %37, i64* %7, i64* %8)
store i32 %38, i32* %9
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%40 = load i8*, i8** %3
%41 = call i64 @strcmp(i8* %39, i8* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %35
br label %23
44: ; preds = %35
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 2
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%49 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* %9
%52 = icmp eq i32 %51, 2
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
%56 = load i64, i64* %8
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %65
58: ; preds = %55
%59 = load i8*, i8** %3
%60 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%61 = call i8* @next_field(i8* %60)
%62 = call i8* @next_field(i8* %61)
%63 = call i8* @next_field(i8* %62)
%64 = call i32 @mod_in_use(i8* %59, i8* %63)
br label %65
65: ; preds = %58, %55
br label %69
66: ; preds = %23
%67 = load i8*, i8** %3
%68 = call i32 @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* %67)
store i64 1, i64* %8
br label %69
69: ; preds = %66, %65
%70 = load i32*, i32** %4
%71 = call i32 @fclose(i32* %70)
%72 = load i64, i64* %8
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %69
br label %78
75: ; preds = %69
%76 = load i32, i32* @EINVAL
%77 = sub nsw i32 0, %76
br label %78
78: ; preds = %75, %74
%79 = phi i32 [ 0, %74 ], [ %77, %75 ]
store i32 %79, i32* %2
br label %80
80: ; preds = %78, %17
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [30 x i8] c"can't open /proc/modules: %s\0A\00"
@.str.3 = [30 x i8] c"V. v. long line broke rmmod.\0A\00"
@.str.4 = [11 x i8] c"%s %lu %lu\00"
@.str.5 = [37 x i8] c"Unknown format in /proc/modules: %s\0A\00"
@.str.6 = [38 x i8] c"Kernel does not have unload support.\0A\00"
@.str.7 = [43 x i8] c"Module %s does not exist in /proc/modules\0A\00"
@EINVAL = global i32 0
define i32 @check_usage(i8* %0) {
%2 = alloca [10240 x i8]
%3 = alloca [64 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = getelementptr inbounds [10240 x i8], [10240 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = bitcast i64* %4 to i8*
%9 = bitcast i64* %5 to i8*
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %19
12: ; preds = %1
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %58, label %16
16: ; preds = %12
%17 = tail call i32 @strerror(i64 %13)
%18 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %1
br label %20
20: ; preds = %19, %29
%21 = call i32* @fgets(i8* %6, i32 10239, i32* %10)
%22 = icmp eq i32* %21, null
br i1 %22, label %49, label %23
23: ; preds = %20
%24 = call i32* @strchr(i8* %6, i8 signext 10)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %7, i64* %4, i64* %5)
%31 = call i64 @strcmp(i8* %7, i8* %0)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %20
33: ; preds = %29
%34 = icmp slt i32 %30, 2
br i1 %34, label %35, label %37
35: ; preds = %33
%36 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %6)
br label %41
37: ; preds = %33
%38 = icmp eq i32 %30, 2
br i1 %38, label %39, label %41
39: ; preds = %37
%40 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %35, %39, %37
%42 = load i64, i64* %5
%43 = icmp eq i64 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %41
%45 = call i8* @next_field(i8* %6)
%46 = call i8* @next_field(i8* %45)
%47 = call i8* @next_field(i8* %46)
%48 = call i32 @mod_in_use(i8* %0, i8* %47)
br label %51
49: ; preds = %20
%50 = call i32 @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* %0)
store i64 1, i64* %5
br label %51
51: ; preds = %41, %44, %49
%52 = call i32 @fclose(i32* %10)
%53 = load i64, i64* %5
%54 = icmp eq i64 %53, 0
%55 = load i32, i32* @EINVAL
%56 = sub nsw i32 0, %55
%57 = select i1 %54, i32 0, i32 %56
br label %58
58: ; preds = %12, %51
%59 = phi i32 [ %57, %51 ], [ 0, %12 ]
ret i32 %59
}