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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ *strstr(char *, char *);
int check_modules(char *driver_name) {
  FILE *fd;
  const char *proc_modules = "/proc/modules";
  char buffer[256];
  fd = fopen(proc_modules, "r");
  if (!fd)
    return -1;
  while (fgets(buffer, sizeof(buffer), fd)) {
    if (strstr(buffer, driver_name) == NULL)
      continue;
    else {
      fclose(fd);
      return 0;
    }
  }
  fclose(fd);
  return -1;
}


x86 assembly (clang, -O0):
<check_modules>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xeaa(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1185 <check_modules+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <check_modules+0xad>
jmp    118a <check_modules+0x4a>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11dd <check_modules+0x9d>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
cmp    $0x0,%rax
jne    11c8 <check_modules+0x88>
jmp    118a <check_modules+0x4a>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ed <check_modules+0xad>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_modules>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r14
lea    0xeaa(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1199 <check_modules+0x59>
mov    %rax,%r15
mov    %rsp,%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11a0 <check_modules+0x60>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     1170 <check_modules+0x30>
xor    %ebx,%ebx
jmp    11a5 <check_modules+0x65>
mov    $0xffffffff,%ebx
jmp    11ad <check_modules+0x6d>
mov    $0xffffffff,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @check_modules(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [256 x i8]
store i8* %0, i8** %3
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = load i8*, i8** %5
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i32 -1, i32* %2
br label %30
12: ; preds = %1
br label %13
13: ; preds = %23, %12
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i64 @fgets(i8* %14, i32 256, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %13
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = load i8*, i8** %3
%21 = call i32* @strstr(i8* %19, i8* %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18
br label %13
24: ; preds = %18
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
store i32 0, i32* %2
br label %30
27: ; preds = %13
%28 = load i32*, i32** %4
%29 = call i32 @fclose(i32* %28)
store i32 -1, i32* %2
br label %30
30: ; preds = %27, %24, %11
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @check_modules(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = 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))
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %1, %9
%7 = call i64 @fgets(i8* %3, i32 256, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = call i32* @strstr(i8* %3, i8* dereferenceable(1) %0)
%11 = icmp eq i32* %10, null
br i1 %11, label %6, label %12
12: ; preds = %6, %9
%13 = phi i32 [ 0, %9 ], [ -1, %6 ]
%14 = call i32 @fclose(i32* %4)
br label %15
15: ; preds = %12, %1
%16 = phi i32 [ -1, %1 ], [ %13, %12 ]
ret i32 %16
}