#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 */
struct kmod_module {
  int dummy;
};
struct kmod_list {
  int dummy;
};
struct kmod_ctx {
  int dummy;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ENOENT;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
struct kmod_list *kmod_list_append(struct kmod_list *, struct kmod_module *);
int /*<<< orphan*/ kmod_log_null(struct kmod_ctx *, char *, ...);
int kmod_module_new_from_name(struct kmod_ctx *, char *, struct kmod_module **);
int /*<<< orphan*/ kmod_module_unref(struct kmod_module *);
int /*<<< orphan*/ strerror(int);
char *strtok_r(char *, char *, char **);
__attribute__((visibility("default"))) int
kmod_module_new_from_loaded(struct kmod_ctx *ctx, struct kmod_list **list) {
  struct kmod_list *l = NULL;
  FILE *fp;
  char line[4096];
  if (ctx == NULL || list == NULL)
    return -ENOENT;
  fp = fopen("/proc/modules", "re");
  if (fp == NULL) {
    int err = -errno;
    kmod_log_null(ctx, "could not open /proc/modules: %s\n", strerror(errno));
    return err;
  }
  while (fgets(line, sizeof(line), fp)) {
    struct kmod_module *m;
    struct kmod_list *node;
    int err;
    char *saveptr, *name = strtok_r(line, " \t", &saveptr);
    err = kmod_module_new_from_name(ctx, name, &m);
    if (err < 0) {
      kmod_log_null(ctx, "could not get module from name '%s': %s\n", name,
                    strerror(-err));
      continue;
    }
    node = kmod_list_append(l, m);
    if (node)
      l = node;
    else {
      kmod_log_null(ctx, "out of memory\n");
      kmod_module_unref(m);
    }
  }
  fclose(fp);
  *list = l;
  return 0;
}


x86 assembly (clang, -O0):
<kmod_module_new_from_loaded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1080,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c1 <kmod_module_new_from_loaded+0x31>
cmpq   $0x0,-0x18(%rbp)
jne    11d4 <kmod_module_new_from_loaded+0x44>
xor    %eax,%eax
mov    0x2e06(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1370 <kmod_module_new_from_loaded+0x1e0>
lea    0xe25(%rip),%rdi
lea    0xe2c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1245 <kmod_module_new_from_loaded+0xb5>
xor    %eax,%eax
mov    0x2de1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1034(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1068(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1068(%rbp),%rdi
mov    %eax,%edx
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1040 <kmod_log_null@plt>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1370 <kmod_module_new_from_loaded+0x1e0>
jmp    124a <kmod_module_new_from_loaded+0xba>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1355 <kmod_module_new_from_loaded+0x1c5>
lea    -0x1030(%rbp),%rdi
lea    0xdbc(%rip),%rsi
lea    -0x1058(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x1060(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1060(%rbp),%rsi
lea    -0x1040(%rbp),%rdx
call   1090 <kmod_module_new_from_name@plt>
mov    %eax,-0x104c(%rbp)
cmpl   $0x0,-0x104c(%rbp)
jge    12fd <kmod_module_new_from_loaded+0x16d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1078(%rbp)
mov    -0x1060(%rbp),%rax
mov    %rax,-0x1070(%rbp)
xor    %edi,%edi
sub    -0x104c(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x1078(%rbp),%rdi
mov    -0x1070(%rbp),%rdx
mov    %eax,%ecx
lea    0xd45(%rip),%rsi
mov    $0x0,%al
call   1040 <kmod_log_null@plt>
jmp    124a <kmod_module_new_from_loaded+0xba>
mov    -0x20(%rbp),%rdi
mov    -0x1040(%rbp),%rsi
call   1030 <kmod_list_append@plt>
mov    %rax,-0x1048(%rbp)
cmpq   $0x0,-0x1048(%rbp)
je     1332 <kmod_module_new_from_loaded+0x1a2>
mov    -0x1048(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1350 <kmod_module_new_from_loaded+0x1c0>
mov    -0x10(%rbp),%rdi
lea    0xd22(%rip),%rsi
mov    $0x0,%al
call   1040 <kmod_log_null@plt>
mov    -0x1040(%rbp),%rdi
call   1060 <kmod_module_unref@plt>
jmp    124a <kmod_module_new_from_loaded+0xba>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1080,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmod_module_new_from_loaded>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
test   %rdi,%rdi
je     12ab <kmod_module_new_from_loaded+0x11b>
mov    %rsi,%rbp
test   %rsi,%rsi
je     12ab <kmod_module_new_from_loaded+0x11b>
mov    %rdi,%rbx
lea    0xe40(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12b8 <kmod_module_new_from_loaded+0x128>
mov    %rax,%r12
mov    %rbp,0x10(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12df <kmod_module_new_from_loaded+0x14f>
lea    0x20(%rsp),%r15
xor    %r14d,%r14d
jmp    124e <kmod_module_new_from_loaded+0xbe>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
neg    %eax
mov    %eax,%edi
call   10b0 <strerror@plt>
mov    %rbx,%rdi
lea    0xe13(%rip),%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <kmod_log_null@plt>
mov    %r14,%r13
mov    %r15,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1080 <fgets@plt>
mov    %r13,%r14
test   %rax,%rax
je     12e2 <kmod_module_new_from_loaded+0x152>
mov    %r15,%rdi
lea    0xddb(%rip),%rsi
lea    0x18(%rsp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %rax,%rsi
lea    0x8(%rsp),%rdx
call   1090 <kmod_module_new_from_name@plt>
test   %eax,%eax
js     1210 <kmod_module_new_from_loaded+0x80>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1030 <kmod_list_append@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1232 <kmod_module_new_from_loaded+0xa2>
mov    %rbx,%rdi
lea    0xdc7(%rip),%rsi
xor    %eax,%eax
call   1040 <kmod_log_null@plt>
mov    0x8(%rsp),%rdi
call   1060 <kmod_module_unref@plt>
jmp    122f <kmod_module_new_from_loaded+0x9f>
mov    0x2d1e(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12f4 <kmod_module_new_from_loaded+0x164>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebp
neg    %ebp
call   10b0 <strerror@plt>
lea    0xd40(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <kmod_log_null@plt>
jmp    12f4 <kmod_module_new_from_loaded+0x164>
xor    %r13d,%r13d
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rax
mov    %r13,(%rax)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.kmod_ctx = type { i32 }
%struct.kmod_list = type { i32 }
%struct.kmod_module = type { i32 }
@ENOENT = global i32 0
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [3 x i8] c"re\00"
@errno = global i32 0
@.str.2 = [34 x i8] c"could not open /proc/modules: %s\0A\00"
@.str.3 = [3 x i8] c" \09\00"
@.str.4 = [41 x i8] c"could not get module from name '%s': %s\0A\00"
@.str.5 = [15 x i8] c"out of memory\0A\00"
define i32 @kmod_module_new_from_loaded(%struct.kmod_ctx* %0, %struct.kmod_list** %1) {
%3 = alloca i32
%4 = alloca %struct.kmod_ctx*
%5 = alloca %struct.kmod_list**
%6 = alloca %struct.kmod_list*
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i32
%10 = alloca %struct.kmod_module*
%11 = alloca %struct.kmod_list*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store %struct.kmod_ctx* %0, %struct.kmod_ctx** %4
store %struct.kmod_list** %1, %struct.kmod_list*** %5
store %struct.kmod_list* null, %struct.kmod_list** %6
%15 = load %struct.kmod_ctx*, %struct.kmod_ctx** %4
%16 = icmp eq %struct.kmod_ctx* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load %struct.kmod_list**, %struct.kmod_list*** %5
%19 = icmp eq %struct.kmod_list** %18, null
br i1 %19, label %20, label %23
20: ; preds = %17, %2
%21 = load i32, i32* @ENOENT
%22 = sub nsw i32 0, %21
store i32 %22, i32* %3
br label %75
23: ; preds = %17
%24 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %35
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %9
%30 = load %struct.kmod_ctx*, %struct.kmod_ctx** %4
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (%struct.kmod_ctx*, i8*, ...) @kmod_log_null(%struct.kmod_ctx* %30, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %9
store i32 %34, i32* %3
br label %75
35: ; preds = %23
br label %36
36: ; preds = %69, %49, %35
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%38 = load i32*, i32** %7
%39 = call i64 @fgets(i8* %37, i32 4096, i32* %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %70
41: ; preds = %36
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%43 = call i8* @strtok_r(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %13)
store i8* %43, i8** %14
%44 = load %struct.kmod_ctx*, %struct.kmod_ctx** %4
%45 = load i8*, i8** %14
%46 = call i32 @kmod_module_new_from_name(%struct.kmod_ctx* %44, i8* %45, %struct.kmod_module** %10)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %41
%50 = load %struct.kmod_ctx*, %struct.kmod_ctx** %4
%51 = load i8*, i8** %14
%52 = load i32, i32* %12
%53 = sub nsw i32 0, %52
%54 = call i32 @strerror(i32 %53)
%55 = call i32 (%struct.kmod_ctx*, i8*, ...) @kmod_log_null(%struct.kmod_ctx* %50, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %51, i32 %54)
br label %36
56: ; preds = %41
%57 = load %struct.kmod_list*, %struct.kmod_list** %6
%58 = load %struct.kmod_module*, %struct.kmod_module** %10
%59 = call %struct.kmod_list* @kmod_list_append(%struct.kmod_list* %57, %struct.kmod_module* %58)
store %struct.kmod_list* %59, %struct.kmod_list** %11
%60 = load %struct.kmod_list*, %struct.kmod_list** %11
%61 = icmp ne %struct.kmod_list* %60, null
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = load %struct.kmod_list*, %struct.kmod_list** %11
store %struct.kmod_list* %63, %struct.kmod_list** %6
br label %69
64: ; preds = %56
%65 = load %struct.kmod_ctx*, %struct.kmod_ctx** %4
%66 = call i32 (%struct.kmod_ctx*, i8*, ...) @kmod_log_null(%struct.kmod_ctx* %65, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%67 = load %struct.kmod_module*, %struct.kmod_module** %10
%68 = call i32 @kmod_module_unref(%struct.kmod_module* %67)
br label %69
69: ; preds = %64, %62
br label %36
70: ; preds = %36
%71 = load i32*, i32** %7
%72 = call i32 @fclose(i32* %71)
%73 = load %struct.kmod_list*, %struct.kmod_list** %6
%74 = load %struct.kmod_list**, %struct.kmod_list*** %5
store %struct.kmod_list* %73, %struct.kmod_list** %74
store i32 0, i32* %3
br label %75
75: ; preds = %70, %27, %20
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.kmod_ctx = type { i32 }
%struct.kmod_list = type { i32 }
%struct.kmod_module = type { i32 }
@ENOENT = global i32 0
@.str = [14 x i8] c"/proc/modules\00"
@.str.1 = [3 x i8] c"re\00"
@errno = global i32 0
@.str.2 = [34 x i8] c"could not open /proc/modules: %s\0A\00"
@.str.3 = [3 x i8] c" \09\00"
@.str.4 = [41 x i8] c"could not get module from name '%s': %s\0A\00"
@.str.5 = [15 x i8] c"out of memory\0A\00"
define i32 @kmod_module_new_from_loaded(%struct.kmod_ctx* %0, %struct.kmod_list** writeonly %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.kmod_module*
%5 = alloca i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = icmp eq %struct.kmod_ctx* %0, null
%8 = icmp eq %struct.kmod_list** %1, null
%9 = or i1 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @ENOENT
%12 = sub nsw i32 0, %11
br label %51
13: ; preds = %2
%14 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = call i64 @fgets(i8* %6, i32 4096, i32* %14)
%18 = icmp eq i64 %17, 0
br i1 %18, label %48, label %19
19: ; preds = %16
%20 = bitcast %struct.kmod_module** %4 to i8*
%21 = bitcast i8** %5 to i8*
br label %27
22: ; preds = %13
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
%25 = tail call i32 @strerror(i32 %23)
%26 = tail call i32 (%struct.kmod_ctx*, i8*, ...) @kmod_log_null(%struct.kmod_ctx* %0, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %51
27: ; preds = %19, %44
%28 = phi %struct.kmod_list* [ null, %19 ], [ %45, %44 ]
%29 = call i8* @strtok_r(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %5)
%30 = call i32 @kmod_module_new_from_name(%struct.kmod_ctx* %0, i8* %29, %struct.kmod_module** %4)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = sub nsw i32 0, %30
%34 = call i32 @strerror(i32 %33)
%35 = call i32 (%struct.kmod_ctx*, i8*, ...) @kmod_log_null(%struct.kmod_ctx* %0, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %29, i32 %34)
br label %44
36: ; preds = %27
%37 = load %struct.kmod_module*, %struct.kmod_module** %4
%38 = call %struct.kmod_list* @kmod_list_append(%struct.kmod_list* %28, %struct.kmod_module* %37)
%39 = icmp eq %struct.kmod_list* %38, null
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = call i32 (%struct.kmod_ctx*, i8*, ...) @kmod_log_null(%struct.kmod_ctx* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%42 = load %struct.kmod_module*, %struct.kmod_module** %4
%43 = call i32 @kmod_module_unref(%struct.kmod_module* %42)
br label %44
44: ; preds = %40, %36, %32
%45 = phi %struct.kmod_list* [ %28, %32 ], [ %28, %40 ], [ %38, %36 ]
%46 = call i64 @fgets(i8* %6, i32 4096, i32* %14)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %27
48: ; preds = %44, %16
%49 = phi %struct.kmod_list* [ null, %16 ], [ %45, %44 ]
%50 = call i32 @fclose(i32* %14)
store %struct.kmod_list* %49, %struct.kmod_list** %1
br label %51
51: ; preds = %48, %22, %10
%52 = phi i32 [ %12, %10 ], [ %24, %22 ], [ 0, %48 ]
ret i32 %52
}