#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, char *, unsigned long *, unsigned long *);
int /*<<< orphan*/ *strchr(char *, char);
scalar_t__ strcmp(char *, char const *);
char *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;
    printf("can't open /proc/modules: %s\n", strerror(errno));
  }
  while (fgets(line, sizeof(line) - 1, module_list) != NULL) {
    if (strchr(line, '\n') == NULL) {
      printf("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)
      printf("Unknown format in /proc/modules: %s\n", line);
    if (scanned == 2)
      printf("Kernel does not have unload support.\n");
    if (refs != 0) {
      printf("Cannot remove the module, %s is in use\n", 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    $0x2880,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4a(%rip),%rdi
lea    0xe51(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1217 <check_usage+0x77>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%rax
jne    11f7 <check_usage+0x57>
movl   $0x0,-0x4(%rbp)
jmp    1379 <check_usage+0x1d9>
mov    0x2de2(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    121c <check_usage+0x7c>
lea    -0x2820(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x27ff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1320 <check_usage+0x180>
lea    -0x2820(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    126e <check_usage+0xce>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x2820(%rbp),%rdi
lea    -0x2860(%rbp),%rdx
lea    0xdc9(%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     12bb <check_usage+0x11b>
jmp    121c <check_usage+0x7c>
cmpl   $0x2,-0x2874(%rbp)
jge    12dd <check_usage+0x13d>
lea    -0x2820(%rbp),%rsi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x2,-0x2874(%rbp)
jne    12f8 <check_usage+0x158>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x2870(%rbp)
je     131b <check_usage+0x17b>
lea    -0x2820(%rbp),%rsi
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    133b <check_usage+0x19b>
mov    -0x10(%rbp),%rsi
lea    0xd9f(%rip),%rdi
call   1090 <error@plt>
movq   $0x1,-0x2870(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x2870(%rbp)
jne    135f <check_usage+0x1bf>
xor    %eax,%eax
mov    %eax,-0x2878(%rbp)
jmp    1370 <check_usage+0x1d0>
xor    %eax,%eax
mov    0x2c70(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2878(%rbp)
mov    -0x2878(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2880,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_usage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2858,%rsp
mov    %rdi,%r15
lea    0xe35(%rip),%rdi
lea    0xe3c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1211 <check_usage+0x61>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rdi
mov    0x2dd8(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%rdi
je     12f2 <check_usage+0x142>
call   10d0 <strerror@plt>
lea    0xe09(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x50(%rsp),%rbx
lea    0x10(%rsp),%r13
mov    %rsp,%r12
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x27ff,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1291 <check_usage+0xe1>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1304 <check_usage+0x154>
mov    %rbx,%rdi
lea    0xdd9(%rip),%rsi
mov    %r13,%rdx
lea    0x8(%rsp),%rcx
mov    %r12,%r8
call   1090 <sscanf@plt>
mov    %eax,%ebp
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1220 <check_usage+0x70>
cmp    $0x1,%ebp
jg     12aa <check_usage+0xfa>
lea    0xdb6(%rip),%rdi
lea    0x50(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12bb <check_usage+0x10b>
lea    0xdee(%rip),%rdi
mov    %r15,%rsi
call   10a0 <error@plt>
movq   $0x1,(%rsp)
jmp    12d5 <check_usage+0x125>
cmp    $0x2,%ebp
jne    12bb <check_usage+0x10b>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
cmpq   $0x0,(%rsp)
je     12d5 <check_usage+0x125>
lea    0xd95(%rip),%rdi
lea    0x50(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2cf4(%rip),%rcx
xor    %edx,%edx
xor    %eax,%eax
sub    (%rcx),%eax
cmpq   $0x0,(%rsp)
cmove  %edx,%eax
add    $0x2858,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <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 = [40 x i8] c"Cannot remove the module, %s is in use\0A\00"
@.str.8 = [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 %76
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = call i8* @strerror(i64 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %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 %62
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*, ...) @printf(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*, ...) @printf(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*, ...) @printf(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 %61
58: ; preds = %55
%59 = getelementptr inbounds [10240 x i8], [10240 x i8]* %5, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %58, %55
br label %65
62: ; preds = %23
%63 = load i8*, i8** %3
%64 = call i32 @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), i8* %63)
store i64 1, i64* %8
br label %65
65: ; preds = %62, %61
%66 = load i32*, i32** %4
%67 = call i32 @fclose(i32* %66)
%68 = load i64, i64* %8
%69 = icmp eq i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %65
br label %74
71: ; preds = %65
%72 = load i32, i32* @EINVAL
%73 = sub nsw i32 0, %72
br label %74
74: ; preds = %71, %70
%75 = phi i32 [ 0, %70 ], [ %73, %71 ]
store i32 %75, i32* %2
br label %76
76: ; preds = %74, %17
%77 = load i32, i32* %2
ret i32 %77
}

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.4 = [11 x i8] c"%s %lu %lu\00"
@.str.5 = [37 x i8] c"Unknown format in /proc/modules: %s\0A\00"
@.str.7 = [40 x i8] c"Cannot remove the module, %s is in use\0A\00"
@.str.8 = [43 x i8] c"Module %s does not exist in /proc/modules\0A\00"
@EINVAL = global i32 0
@str = [37 x i8] c"Kernel does not have unload support.\00"
@str.9 = [29 x i8] c"V. v. long line broke rmmod.\00"
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 %55, label %16
16: ; preds = %12
%17 = tail call i8* @strerror(i64 %13)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %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 %46, 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.9, 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*, ...) @printf(i8* dereferenceable(1) 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, 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 %48, label %44
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* %6)
br label %48
46: ; preds = %20
%47 = call i32 @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0), i8* %0)
store i64 1, i64* %5
br label %48
48: ; preds = %41, %44, %46
%49 = call i32 @fclose(i32* %10)
%50 = load i64, i64* %5
%51 = icmp eq i64 %50, 0
%52 = load i32, i32* @EINVAL
%53 = sub nsw i32 0, %52
%54 = select i1 %51, i32 0, i32 %53
br label %55
55: ; preds = %12, %48
%56 = phi i32 [ %54, %48 ], [ 0, %12 ]
ret i32 %56
}