#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
char *strtok(char *, char *);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int kallsyms_parse(void *arg,
                   int (*process_symbol)(void *arg, const char *name, char type,
                                         unsigned long start)) {
  FILE *file;
  char *line = NULL;
  int ret = 0;
  int found = 0;
  file = fopen("/proc/kallsyms", "r");
  if (file == NULL)
    do {
      perror("open "
             "/proc/kallsyms"
             " failed");
      exit(-1);
    } while (0);
  while (!feof(file)) {
    char *symbol_addr, *symbol_name;
    char symbol_type;
    unsigned long start;
    int line_len;
    size_t n;
    line_len = getline(&line, &n, file);
    if (line_len < 0 || !line)
      break;
    line[--line_len] = '\0';
    symbol_addr = strtok(line, " \t");
    start = strtoul(symbol_addr, NULL, 16);
    symbol_type = *strtok(NULL, " \t");
    symbol_name = strtok(NULL, " \t");
    ret = process_symbol(arg, symbol_name, symbol_type, start);
    if (!ret)
      found = 1;
  }
  free(line);
  fclose(file);
  return found;
}


x86 assembly (clang, -O0):
<kallsyms_parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    0xe43(%rip),%rdi
lea    0xe4b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f3 <kallsyms_parse+0x63>
jmp    11dd <kallsyms_parse+0x4d>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    11f8 <kallsyms_parse+0x68>
mov    -0x18(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1216 <kallsyms_parse+0x86>
jmp    12dc <kallsyms_parse+0x14c>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jl     123f <kallsyms_parse+0xaf>
cmpq   $0x0,-0x20(%rbp)
jne    1244 <kallsyms_parse+0xb4>
jmp    12dc <kallsyms_parse+0x14c>
mov    -0x20(%rbp),%rax
mov    -0x4c(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %rax,-0x48(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd9f(%rip),%rsi
call   1080 <strtok@plt>
mov    (%rax),%al
mov    %al,-0x39(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd8a(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x39(%rbp),%dl
mov    -0x48(%rbp),%rcx
movsbl %dl,%edx
call   *%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12d7 <kallsyms_parse+0x147>
movl   $0x1,-0x28(%rbp)
jmp    11f8 <kallsyms_parse+0x68>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kallsyms_parse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r15
movq   $0x0,(%rsp)
lea    0xe4b(%rip),%rdi
lea    0xe53(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1292 <kallsyms_parse+0x102>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11e2 <kallsyms_parse+0x52>
xor    %edi,%edi
xor    %ebx,%ebx
jmp    1274 <kallsyms_parse+0xe4>
xor    %ebx,%ebx
lea    0xe41(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
mov    %r12,%rdx
call   10a0 <getline@plt>
mov    (%rsp),%rdi
test   %eax,%eax
js     1274 <kallsyms_parse+0xe4>
test   %rdi,%rdi
je     1274 <kallsyms_parse+0xe4>
cltq
movb   $0x0,-0x1(%rax,%rdi,1)
mov    (%rsp),%rdi
mov    %r13,%rsi
call   1080 <strtok@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %rax,%rbp
xor    %edi,%edi
mov    %r13,%rsi
call   1080 <strtok@plt>
movsbl (%rax),%r14d
xor    %edi,%edi
mov    %r13,%rsi
call   1080 <strtok@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r14d,%edx
mov    %rbp,%rcx
call   *0x8(%rsp)
test   %eax,%eax
mov    $0x1,%eax
cmove  %eax,%ebx
mov    %r12,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11f0 <kallsyms_parse+0x60>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd78(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/kallsyms\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"open /proc/kallsyms failed\00"
@.str.3 = [3 x i8] c" \09\00"
define i32 @kallsyms_parse(i8* %0, i32 (i8*, i8*, i8, i64)* %1) {
%3 = alloca i8*
%4 = alloca i32 (i8*, i8*, i8, i64)*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
store i8* %0, i8** %3
store i32 (i8*, i8*, i8, i64)* %1, i32 (i8*, i8*, i8, i64)** %4
store i8* null, i8** %6
store i32 0, i32* %7
store i32 0, i32* %8
%15 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %5
%16 = load i32*, i32** %5
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %23
18: ; preds = %2
br label %19
19: ; preds = %18
%20 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; No predecessors!
br label %23
23: ; preds = %22, %2
br label %24
24: ; preds = %60, %23
%25 = load i32*, i32** %5
%26 = call i32 @feof(i32* %25)
%27 = icmp ne i32 %26, 0
%28 = xor i1 %27, true
br i1 %28, label %29, label %61
29: ; preds = %24
%30 = load i32*, i32** %5
%31 = call i32 @getline(i8** %6, i64* %14, i32* %30)
store i32 %31, i32* %13
%32 = load i32, i32* %13
%33 = icmp slt i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %38, label %37
37: ; preds = %34, %29
br label %61
38: ; preds = %34
%39 = load i8*, i8** %6
%40 = load i32, i32* %13
%41 = add nsw i32 %40, -1
store i32 %41, i32* %13
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %6
%45 = call i8* @strtok(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %45, i8** %9
%46 = load i8*, i8** %9
%47 = call i64 @strtoul(i8* %46, i32* null, i32 16)
store i64 %47, i64* %12
%48 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%49 = load i8, i8* %48
store i8 %49, i8* %11
%50 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %50, i8** %10
%51 = load i32 (i8*, i8*, i8, i64)*, i32 (i8*, i8*, i8, i64)** %4
%52 = load i8*, i8** %3
%53 = load i8*, i8** %10
%54 = load i8, i8* %11
%55 = load i64, i64* %12
%56 = call i32 %51(i8* %52, i8* %53, i8 signext %54, i64 %55)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = icmp ne i32 %57, 0
br i1 %58, label %60, label %59
59: ; preds = %38
store i32 1, i32* %8
br label %60
60: ; preds = %59, %38
br label %24
61: ; preds = %37, %24
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
%64 = load i32*, i32** %5
%65 = call i32 @fclose(i32* %64)
%66 = load i32, i32* %8
ret i32 %66
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/kallsyms\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [27 x i8] c"open /proc/kallsyms failed\00"
@.str.3 = [3 x i8] c" \09\00"
define i32 @kallsyms_parse(i8* %0, i32 (i8*, i8*, i8, i64)* nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 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 %13, label %8
8: ; preds = %2
%9 = call i32 @feof(i32* %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %41
11: ; preds = %8
%12 = bitcast i64* %4 to i8*
br label %16
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %11, %24
%17 = phi i32 [ 0, %11 ], [ %36, %24 ]
%18 = call i32 @getline(i8** %3, i64* %4, i32* %6)
%19 = icmp sgt i32 %18, -1
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %24, label %23
23: ; preds = %16
br label %41
24: ; preds = %16
%25 = add nsw i32 %18, -1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %20, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %3
%29 = call i8* @strtok(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%30 = call i64 @strtoul(i8* nocapture %29, i32* null, i32 16)
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%32 = load i8, i8* %31
%33 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 %1(i8* %0, i8* %33, i8 signext %32, i64 %30)
%35 = icmp eq i32 %34, 0
%36 = select i1 %35, i32 1, i32 %17
%37 = call i32 @feof(i32* %6)
%38 = icmp eq i32 %37, 0
br i1 %38, label %16, label %39
39: ; preds = %24
%40 = load i8*, i8** %3
br label %41
41: ; preds = %39, %8, %23
%42 = phi i8* [ %20, %23 ], [ null, %8 ], [ %40, %39 ]
%43 = phi i32 [ %17, %23 ], [ 0, %8 ], [ %36, %39 ]
%44 = call i32 @free(i8* %42)
%45 = call i32 @fclose(i32* %6)
ret i32 %43
}