#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ message(int, char *, char *, ...);
int /*<<< orphan*/ mod_in_use(int, 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 *);
char const *strerror(scalar_t__);
int check_usage(int log, 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;
    do {
      message(log, "FATAL: ", "can't open /proc/modules: %s\n",
              strerror(errno));
      exit(1);
    } while (0);
  }
  while (fgets(line, sizeof(line) - 1, module_list) != NULL) {
    if (strchr(line, '\n') == NULL) {
      do {
        message(log, "FATAL: ", "V. v. long line broke rmmod.\n");
        exit(1);
      } while (0);
      exit(1);
    }
    scanned = sscanf(line, "%s %lu %lu", name, &size, &refs);
    if (strcmp(name, modname) != 0)
      continue;
    if (scanned < 2)
      do {
        message(log, "FATAL: ", "Unknown format in /proc/modules: %s\n", line);
        exit(1);
      } while (0);
    if (scanned == 2)
      do {
        message(log, "FATAL: ", "Kernel does not have unload support.\n");
        exit(1);
      } while (0);
    if (refs != 0)
      mod_in_use(log, modname, next_field(next_field(next_field(line))));
    goto out;
  }
  message(log, "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    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe37(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124f <check_usage+0x9f>
mov    0x2df5(%rip),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <check_usage+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    142e <check_usage+0x27e>
jmp    120f <check_usage+0x5f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2878(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x2878(%rbp),%edi
mov    %rax,%rcx
lea    0xdd9(%rip),%rsi
lea    0xdda(%rip),%rdx
mov    $0x0,%al
call   1030 <message@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1254 <check_usage+0xa4>
lea    -0x2820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x27ff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     13c9 <check_usage+0x219>
lea    -0x2820(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
jne    12b5 <check_usage+0x105>
jmp    1293 <check_usage+0xe3>
mov    -0x8(%rbp),%edi
lea    0xd73(%rip),%rsi
lea    0xd92(%rip),%rdx
mov    $0x0,%al
call   1030 <message@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x2820(%rbp),%rdi
lea    -0x2860(%rbp),%rdx
lea    0xd8a(%rip),%rsi
lea    -0x2868(%rbp),%rcx
lea    -0x2870(%rbp),%r8
call   1090 <sscanf@plt>
mov    %eax,-0x2874(%rbp)
lea    -0x2860(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1302 <check_usage+0x152>
jmp    1254 <check_usage+0xa4>
cmpl   $0x2,-0x2874(%rbp)
jge    133d <check_usage+0x18d>
jmp    1314 <check_usage+0x164>
mov    -0x8(%rbp),%edi
lea    -0x2820(%rbp),%rcx
lea    0xceb(%rip),%rsi
lea    0xd33(%rip),%rdx
mov    $0x0,%al
call   1030 <message@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x2,-0x2874(%rbp)
jne    1371 <check_usage+0x1c1>
jmp    134f <check_usage+0x19f>
mov    -0x8(%rbp),%edi
lea    0xcb7(%rip),%rsi
lea    0xd24(%rip),%rdx
mov    $0x0,%al
call   1030 <message@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x2870(%rbp)
je     13c4 <check_usage+0x214>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2884(%rbp)
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    -0x2884(%rbp),%edi
mov    -0x2880(%rbp),%rsi
mov    %rax,%rdx
call   1070 <mod_in_use@plt>
jmp    13f0 <check_usage+0x240>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rcx
lea    0xcd3(%rip),%rsi
lea    0xcd4(%rip),%rdx
mov    $0x0,%al
call   1030 <message@plt>
movq   $0x1,-0x2870(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x2870(%rbp)
jne    1414 <check_usage+0x264>
xor    %eax,%eax
mov    %eax,-0x2888(%rbp)
jmp    1425 <check_usage+0x275>
xor    %eax,%eax
mov    0x2bbb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2888(%rbp)
mov    -0x2888(%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    %rsi,%r15
mov    %edi,%ebx
lea    0xe33(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12a9 <check_usage+0xf9>
mov    %rax,%r13
mov    %ebx,0xc(%rsp)
lea    0x60(%rsp),%rbp
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x27ff,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12de <check_usage+0x12e>
mov    %rbp,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1333 <check_usage+0x183>
mov    %rbp,%rdi
lea    0xe1b(%rip),%rsi
mov    %rbx,%rdx
lea    0x18(%rsp),%rcx
mov    %r12,%r8
call   1090 <sscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1200 <check_usage+0x50>
cmp    $0x1,%r14d
jle    1356 <check_usage+0x1a6>
cmp    $0x2,%r14d
mov    0xc(%rsp),%ebx
je     137e <check_usage+0x1ce>
cmpq   $0x0,0x10(%rsp)
je     1303 <check_usage+0x153>
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    %ebx,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1070 <mod_in_use@plt>
jmp    1303 <check_usage+0x153>
mov    0x2d30(%rip),%rax
mov    (%rax),%rdi
mov    0x2d0e(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%rdi
je     1321 <check_usage+0x171>
call   10d0 <strerror@plt>
lea    0xd43(%rip),%rsi
lea    0xd44(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
jmp    136d <check_usage+0x1bd>
lea    0xdc5(%rip),%rsi
lea    0xdc6(%rip),%rdx
mov    0xc(%rsp),%edi
mov    %r15,%rcx
xor    %eax,%eax
call   1030 <message@plt>
movq   $0x1,0x10(%rsp)
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    0x2cc6(%rip),%rcx
xor    %edx,%edx
xor    %eax,%eax
sub    (%rcx),%eax
cmpq   $0x0,0x10(%rsp)
cmove  %edx,%eax
add    $0x2868,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd6(%rip),%rsi
lea    0xcf5(%rip),%rdx
mov    0xc(%rsp),%edi
xor    %eax,%eax
call   1030 <message@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xcb3(%rip),%rsi
lea    0xcfb(%rip),%rdx
lea    0x60(%rsp),%rcx
mov    0xc(%rsp),%edi
xor    %eax,%eax
call   1030 <message@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xc8b(%rip),%rsi
lea    0xcf8(%rip),%rdx
mov    %ebx,%edi
jmp    1345 <check_usage+0x195>

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

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