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

/* Variables and functions */
int LINE_MAX;
int /*<<< orphan*/ err(int, char *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ kfile_add(char *, scalar_t__);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ *stdin;
scalar_t__ strstr(char *, char *);
scalar_t__ strtoumax(char *, char **, int);
int tokenize(char *, char **, int);
void asf_prog(int run) {
  char buf[LINE_MAX];
  char *token[10];
  char *endp;
  FILE *kldstat;
  caddr_t base;
  int tokens;
  if (run) {
    if ((kldstat = popen("kldstat", "r")) == NULL)
      err(2, "can't start kldstat");
  } else
    kldstat = stdin;
  while (fgets(buf, sizeof(buf), kldstat)) {
    if (buf[0] == 'I' || strstr(buf, "kernel"))
      continue;
    tokens = tokenize(buf, token, 10);
    if (tokens < 4)
      continue;
    base = (caddr_t)(uintptr_t)strtoumax(token[2], &endp, 16);
    if (endp == NULL || *endp != '\0')
      continue;
    kfile_add(token[4], base);
  }
}


x86 assembly (clang, -O0):
<asf_prog>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0xa0(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x98(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11f5 <asf_prog+0x85>
lea    0xe49(%rip),%rdi
lea    0xe34(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x80(%rbp)
cmp    $0x0,%rax
jne    11f0 <asf_prog+0x80>
mov    $0x2,%edi
lea    0xe17(%rip),%rsi
call   1040 <err@plt>
jmp    1203 <asf_prog+0x93>
mov    0x2dec(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x80(%rbp)
jmp    1208 <asf_prog+0x98>
mov    -0x98(%rbp),%rdi
mov    -0xa0(%rbp),%rax
mov    %eax,%esi
mov    -0x80(%rbp),%rdx
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     12db <asf_prog+0x16b>
mov    -0x98(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x49,%eax
je     125b <asf_prog+0xeb>
mov    -0x98(%rbp),%rdi
lea    0xdca(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1260 <asf_prog+0xf0>
jmp    1208 <asf_prog+0x98>
mov    -0x98(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0xa,%edx
call   1050 <tokenize@plt>
mov    %eax,-0x8c(%rbp)
cmpl   $0x4,-0x8c(%rbp)
jge    128d <asf_prog+0x11d>
jmp    1208 <asf_prog+0x98>
mov    -0x60(%rbp),%rdi
lea    -0x78(%rbp),%rsi
mov    $0x10,%edx
call   1070 <strtoumax@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x78(%rbp)
je     12c1 <asf_prog+0x151>
mov    -0x78(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12c6 <asf_prog+0x156>
jmp    1208 <asf_prog+0x98>
mov    -0x50(%rbp),%rdi
mov    -0x88(%rbp),%rsi
call   1080 <kfile_add@plt>
jmp    1208 <asf_prog+0x98>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<asf_prog>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    0x2e58(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %edi,%edi
je     11d4 <asf_prog+0x64>
lea    0xe64(%rip),%rdi
lea    0xe4f(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11de <asf_prog+0x6e>
lea    0xe3d(%rip),%rsi
mov    $0x2,%edi
call   1040 <err@plt>
xor    %r15d,%r15d
jmp    11de <asf_prog+0x6e>
mov    0x2e0d(%rip),%rax
mov    (%rax),%r15
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
je     126b <asf_prog+0xfb>
lea    0xe1e(%rip),%r12
lea    -0x80(%rbp),%r13
jmp    1213 <asf_prog+0xa3>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
je     126b <asf_prog+0xfb>
cmpb   $0x49,(%rbx)
je     1200 <asf_prog+0x90>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
jne    1200 <asf_prog+0x90>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0xa,%edx
call   1050 <tokenize@plt>
cmp    $0x4,%eax
jl     1200 <asf_prog+0x90>
mov    -0x70(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1070 <strtoumax@plt>
mov    -0x30(%rbp),%rcx
test   %rcx,%rcx
je     1200 <asf_prog+0x90>
cmpb   $0x0,(%rcx)
jne    1200 <asf_prog+0x90>
mov    -0x60(%rbp),%rdi
mov    %rax,%rsi
call   1080 <kfile_add@plt>
jmp    1200 <asf_prog+0x90>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LINE_MAX = global i32 0
@.str = [8 x i8] c"kldstat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"can't start kldstat\00"
@stdin = global i32* null
@.str.3 = [7 x i8] c"kernel\00"
define void @asf_prog(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [10 x i8*]
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %2
%10 = load i32, i32* @LINE_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %3
%13 = alloca i8, i64 %11
store i64 %11, i64* %4
%14 = load i32, i32* %2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = call i32* @popen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @err(i32 2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
br label %24
22: ; preds = %1
%23 = load i32*, i32** @stdin
store i32* %23, i32** %7
br label %24
24: ; preds = %22, %21
br label %25
25: ; preds = %57, %56, %44, %38, %24
%26 = trunc i64 %11 to i32
%27 = load i32*, i32** %7
%28 = call i64 @fgets(i8* %13, i32 %26, i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %62
30: ; preds = %25
%31 = getelementptr inbounds i8, i8* %13, i64 0
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 73
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = call i64 @strstr(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35, %30
br label %25
39: ; preds = %35
%40 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
%41 = call i32 @tokenize(i8* %13, i8** %40, i32 10)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 4
br i1 %43, label %44, label %45
44: ; preds = %39
br label %25
45: ; preds = %39
%46 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 2
%47 = load i8*, i8** %46
%48 = call i64 @strtoumax(i8* %47, i8** %6, i32 16)
store i64 %48, i64* %8
%49 = load i8*, i8** %6
%50 = icmp eq i8* %49, null
br i1 %50, label %56, label %51
51: ; preds = %45
%52 = load i8*, i8** %6
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %51, %45
br label %25
57: ; preds = %51
%58 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 4
%59 = load i8*, i8** %58
%60 = load i64, i64* %8
%61 = call i32 @kfile_add(i8* %59, i64 %60)
br label %25
62: ; preds = %25
%63 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@LINE_MAX = global i32 0
@.str = [8 x i8] c"kldstat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"can't start kldstat\00"
@stdin = global i32* null
@.str.3 = [7 x i8] c"kernel\00"
define void @asf_prog(i32 %0) {
%2 = alloca [10 x i8*]
%3 = alloca i8*
%4 = load i32, i32* @LINE_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast [10 x i8*]* %2 to i8*
%8 = bitcast i8** %3 to i8*
%9 = icmp eq i32 %0, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = tail call i32* @popen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %17
15: ; preds = %1
%16 = load i32*, i32** @stdin
br label %17
17: ; preds = %10, %13, %15
%18 = phi i32* [ null, %13 ], [ %11, %10 ], [ %16, %15 ]
%19 = call i64 @fgets(i8* %6, i32 %4, i32* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %48, label %21
21: ; preds = %17
%22 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 0
%23 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 2
%24 = getelementptr inbounds [10 x i8*], [10 x i8*]* %2, i64 0, i64 4
br label %25
25: ; preds = %21, %31
%26 = load i8, i8* %6
%27 = icmp eq i8 %26, 73
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i64 @strstr(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %37, %42, %25, %28, %45, %34
%32 = call i64 @fgets(i8* %6, i32 %4, i32* %18)
%33 = icmp eq i64 %32, 0
br i1 %33, label %48, label %25
34: ; preds = %28
%35 = call i32 @tokenize(i8* %6, i8** %22, i32 10)
%36 = icmp slt i32 %35, 4
br i1 %36, label %31, label %37
37: ; preds = %34
%38 = load i8*, i8** %23
%39 = call i64 @strtoumax(i8* %38, i8** %3, i32 16)
%40 = load i8*, i8** %3
%41 = icmp eq i8* %40, null
br i1 %41, label %31, label %42
42: ; preds = %37
%43 = load i8, i8* %40
%44 = icmp eq i8 %43, 0
br i1 %44, label %45, label %31
45: ; preds = %42
%46 = load i8*, i8** %24
%47 = call i32 @kfile_add(i8* %46, i64 %39)
br label %31
48: ; preds = %31, %17
ret void
}