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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
scalar_t__ realloc(char *, int);
scalar_t__ strstr(char *, char const *);
int check_kmod(const char *mod) {
  int fd, size = 0, ret = 0;
  char *buf = NULL;
  if ((fd = open("/proc/modules", O_RDONLY, 0)) == -1) {
    perror("open /proc/modules");
    ret = -1;
    goto out;
  }
retry:
  size += 1000;
  if (!(buf = (char *)realloc(buf, size))) {
    perror("malloc");
    ret = -1;
    goto close;
  }
  ret = read(fd, buf + size - 1000, 1000);
  if (ret == -1) {
    perror("read");
    goto free;
  } else if (ret == 1000) {
    goto retry;
  }
  if (strstr(buf, mod)) {
    ret = 1;
  } else {
    ret = 0;
  }
free:
  free(buf);
close:
  close(fd);
out:
  return ret;
}


x86 assembly (clang, -O0):
<check_kmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe63(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11cd <check_kmod+0x5d>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    12a3 <check_kmod+0x133>
jmp    11d2 <check_kmod+0x62>
mov    -0x10(%rbp),%eax
add    $0x3e8,%eax
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    120f <check_kmod+0x9f>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    129b <check_kmod+0x12b>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
add    $0xfffffffffffffc18,%rsi
mov    $0x3e8,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    124c <check_kmod+0xdc>
lea    0xdd8(%rip),%rdi
call   1080 <perror@plt>
jmp    1292 <check_kmod+0x122>
cmpl   $0x3e8,-0x14(%rbp)
jne    125e <check_kmod+0xee>
jmp    11d2 <check_kmod+0x62>
jmp    1263 <check_kmod+0xf3>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1286 <check_kmod+0x116>
movl   $0x1,-0x14(%rbp)
jmp    128d <check_kmod+0x11d>
movl   $0x0,-0x14(%rbp)
jmp    1292 <check_kmod+0x122>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_kmod>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     120f <check_kmod+0x9f>
mov    %eax,%r15d
xor    %ebp,%ebp
xor    %ebx,%ebx
nopl   (%rax)
lea    0x3e8(%rbp),%esi
mov    %rbx,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     1223 <check_kmod+0xb3>
mov    %rax,%rbx
mov    %rax,%rsi
add    %rbp,%rsi
mov    %r15d,%edi
mov    $0x3e8,%edx
call   1050 <read@plt>
add    $0x3e8,%rbp
cmp    $0x3e8,%eax
je     11a0 <check_kmod+0x30>
cmp    $0xffffffff,%eax
je     1237 <check_kmod+0xc7>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strstr@plt>
xor    %r14d,%r14d
test   %rax,%rax
setne  %r14b
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1201 <check_kmod+0x91>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11f9 <check_kmod+0x89>
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11f1 <check_kmod+0x81>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/modules\00"
@O_RDONLY = global i32 0
@.str.1 = [19 x i8] c"open /proc/modules\00"
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @check_kmod(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %4
store i32 0, i32* %5
store i8* null, i8** %6
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7, i32 0)
store i32 %8, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %54
12: ; preds = %1
br label %13
13: ; preds = %38, %12
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1000
store i32 %15, i32* %4
%16 = load i8*, i8** %6
%17 = load i32, i32* %4
%18 = call i64 @realloc(i8* %16, i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %51
23: ; preds = %13
%24 = load i32, i32* %3
%25 = load i8*, i8** %6
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -1000
%30 = call i32 @read(i32 %24, i8* %29, i32 1000)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %48
35: ; preds = %23
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, 1000
br i1 %37, label %38, label %39
38: ; preds = %35
br label %13
39: ; preds = %35
br label %40
40: ; preds = %39
%41 = load i8*, i8** %6
%42 = load i8*, i8** %2
%43 = call i64 @strstr(i8* %41, i8* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 1, i32* %5
br label %47
46: ; preds = %40
store i32 0, i32* %5
br label %47
47: ; preds = %46, %45
br label %48
48: ; preds = %47, %33
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %48, %21
%52 = load i32, i32* %3
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51, %10
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/modules\00"
@O_RDONLY = global i32 0
@.str.1 = [19 x i8] c"open /proc/modules\00"
@.str.2 = [7 x i8] c"malloc\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @check_kmod(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %2, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %35
7: ; preds = %1, %17
%8 = phi i64 [ %10, %17 ], [ 0, %1 ]
%9 = phi i8* [ %13, %17 ], [ null, %1 ]
%10 = add nuw nsw i64 %8, 1000
%11 = trunc i64 %10 to i32
%12 = tail call i64 @realloc(i8* %9, i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %32
17: ; preds = %7
%18 = getelementptr inbounds i8, i8* %13, i64 %8
%19 = tail call i32 @read(i32 %3, i8* %18, i32 1000)
switch i32 %19, label %23 [
i32 -1, label %20
i32 1000, label %7
]
20: ; preds = %17
%21 = inttoptr i64 %12 to i8*
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %28
23: ; preds = %17
%24 = inttoptr i64 %12 to i8*
%25 = tail call i64 @strstr(i8* %24, i8* %0)
%26 = icmp ne i64 %25, 0
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %23, %20
%29 = phi i8* [ %21, %20 ], [ %24, %23 ]
%30 = phi i32 [ -1, %20 ], [ %27, %23 ]
%31 = tail call i32 @free(i8* %29)
br label %32
32: ; preds = %28, %15
%33 = phi i32 [ %30, %28 ], [ -1, %15 ]
%34 = tail call i32 @close(i32 %3)
br label %35
35: ; preds = %32, %5
%36 = phi i32 [ -1, %5 ], [ %33, %32 ]
ret i32 %36
}