#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *modNameMunge(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strdup(char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int /*<<< orphan*/);
int isLoaded(char *module) {
  FILE *pm;
  char *mod = NULL;
  char line[256];
  char path[256];
  pm = fopen("/proc/modules", "r");
  if (!pm)
    return 0;
  mod = strdup(module);
  mod = modNameMunge(mod);
  snprintf(path, 255, "%s ", mod);
  while (fgets(line, sizeof(line), pm)) {
    if (!strncmp(line, path, strlen(path))) {
      free(mod);
      fclose(pm);
      return 1;
    }
  }
  free(mod);
  fclose(pm);
  return 0;
}


x86 assembly (clang, -O0):
<isLoaded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe52(%rip),%rdi
lea    0xe59(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d5 <isLoaded+0x45>
movl   $0x0,-0x4(%rbp)
jmp    12ac <isLoaded+0x11c>
mov    -0x10(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <modNameMunge@plt>
mov    %rax,-0x20(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0xff,%esi
lea    0xe0a(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1293 <isLoaded+0x103>
lea    -0x120(%rbp),%rax
mov    %rax,-0x230(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x230(%rbp),%rdi
mov    -0x228(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    128e <isLoaded+0xfe>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ac <isLoaded+0x11c>
jmp    120b <isLoaded+0x7b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isLoaded>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%rbx
lea    0xe58(%rip),%rdi
lea    0xe5f(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1235 <isLoaded+0xa5>
mov    %rax,%r15
mov    %rbx,%rdi
call   10b0 <strdup@plt>
mov    %rax,%rdi
call   1080 <modNameMunge@plt>
mov    %rax,%r14
lea    0xe3a(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xff,%esi
mov    %rax,%rcx
call   1070 <snprintf@plt>
lea    0x100(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1239 <isLoaded+0xa9>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    1200 <isLoaded+0x70>
mov    $0x1,%ebx
jmp    123b <isLoaded+0xab>
xor    %ebx,%ebx
jmp    124b <isLoaded+0xbb>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%s \00"
define i32 @isLoaded(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca [256 x i8]
store i8* %0, i8** %3
store i8* null, i8** %5
%8 = 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* %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 0, i32* %2
br label %43
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = call i8* @modNameMunge(i8* %15)
store i8* %16, i8** %5
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = call i32 @snprintf(i8* %17, i32 255, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %37, %12
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = load i32*, i32** %4
%23 = call i64 @fgets(i8* %21, i32 256, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %20
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @strncmp(i8* %26, i8* %27, i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %25
%33 = load i8*, i8** %5
%34 = call i32 @free(i8* %33)
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
store i32 1, i32* %2
br label %43
37: ; preds = %25
br label %20
38: ; preds = %20
%39 = load i8*, i8** %5
%40 = call i32 @free(i8* %39)
%41 = load i32*, i32** %4
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %2
br label %43
43: ; preds = %38, %32, %11
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%s \00"
define i32 @isLoaded(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = 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))
%7 = icmp eq i32* %6, null
br i1 %7, label %23, label %8
8: ; preds = %1
%9 = tail call i8* @strdup(i8* %0)
%10 = tail call i8* @modNameMunge(i8* %9)
%11 = call i32 @snprintf(i8* %5, i32 255, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %15, %8
%13 = call i64 @fgets(i8* %4, i32 256, i32* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = call i32 @strlen(i8* %5)
%17 = call i32 @strncmp(i8* %4, i8* %5, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %12
19: ; preds = %12, %15
%20 = phi i32 [ 1, %15 ], [ 0, %12 ]
%21 = call i32 @free(i8* %10)
%22 = call i32 @fclose(i32* %6)
br label %23
23: ; preds = %19, %1
%24 = phi i32 [ 0, %1 ], [ %20, %19 ]
ret i32 %24
}