#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int moduleLoaded(char *modName) {
  int len = strlen(modName);
  int found = false;
  FILE *fd = fopen("/proc/modules", "r");
  char line[80];
  if (fd == NULL) {
    fprintf(stderr, "Unable to check modules: %s\n", strerror(errno));
    exit(1);
  }
  while (fgets(line, 80, fd) != NULL) {
    if (strncmp(line, modName, len) != 0)
      continue;
    found = true;
    break;
  }
  fclose(fd);
  return found;
}


x86 assembly (clang, -O0):
<moduleLoaded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xe57(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fa <moduleLoaded+0x7a>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xe25(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11ff <moduleLoaded+0x7f>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x50,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1241 <moduleLoaded+0xc1>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     123a <moduleLoaded+0xba>
jmp    11ff <moduleLoaded+0x7f>
movl   $0x1,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<moduleLoaded>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %eax,%r15d
lea    0xe64(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1206 <moduleLoaded+0x86>
mov    %rax,%rbx
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11ef <moduleLoaded+0x6f>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11c0 <moduleLoaded+0x40>
mov    $0x1,%ebp
jmp    11f1 <moduleLoaded+0x71>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd3(%rip),%rax
mov    (%rax),%ebx
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdec(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Unable to check modules: %s\0A\00"
@errno = global i32 0
define i32 @moduleLoaded(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [80 x i8]
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %3
store i32 0, i32* %4
%9 = 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* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
br label %19
19: ; preds = %30, %18
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%21 = load i32*, i32** %5
%22 = call i32* @fgets(i8* %20, i32 80, i32* %21)
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%26 = load i8*, i8** %2
%27 = load i32, i32* %3
%28 = call i64 @strncmp(i8* %25, i8* %26, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %19
31: ; preds = %24
store i32 1, i32* %4
br label %32
32: ; preds = %31, %19
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Unable to check modules: %s\0A\00"
@errno = global i32 0
define i32 @moduleLoaded(i8* %0) {
%2 = alloca [80 x i8]
%3 = tail call i32 @strlen(i8* %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 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%6 = icmp eq i32* %4, null
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %1, %16
%14 = call i32* @fgets(i8* %5, i32 80, i32* %4)
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = call i64 @strncmp(i8* %5, i8* %0, i32 %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %13
19: ; preds = %16, %13
%20 = phi i32 [ 0, %13 ], [ 1, %16 ]
%21 = call i32 @fclose(i32* %4)
ret i32 %20
}