#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)) {
  int ret = 0;
  FILE *file;
  char *line = NULL;
  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)
      break;
  }
  free(line);
  fclose(file);
  return ret;
}


x86 assembly (clang, -O0):
<kallsyms_parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe4a(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ec <kallsyms_parse+0x5c>
jmp    11d6 <kallsyms_parse+0x46>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    11f1 <kallsyms_parse+0x61>
mov    -0x20(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    120f <kallsyms_parse+0x7f>
jmp    12d3 <kallsyms_parse+0x143>
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jl     1238 <kallsyms_parse+0xa8>
cmpq   $0x0,-0x28(%rbp)
jne    123d <kallsyms_parse+0xad>
jmp    12d3 <kallsyms_parse+0x143>
mov    -0x28(%rbp),%rax
mov    -0x4c(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
lea    0xdd0(%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    0xda6(%rip),%rsi
call   1080 <strtok@plt>
mov    (%rax),%al
mov    %al,-0x39(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd91(%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,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12ce <kallsyms_parse+0x13e>
jmp    12d3 <kallsyms_parse+0x143>
jmp    11f1 <kallsyms_parse+0x61>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%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,%r14
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
lea    0xe4c(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1294 <kallsyms_parse+0x104>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    1271 <kallsyms_parse+0xe1>
lea    0xe47(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
test   %eax,%eax
js     126b <kallsyms_parse+0xdb>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     126b <kallsyms_parse+0xdb>
cltq
movb   $0x0,-0x1(%rax,%rcx,1)
mov    0x8(%rsp),%rdi
mov    %r12,%rsi
call   1080 <strtok@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %rax,%r13
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
movsbl (%rax),%ebp
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
mov    %r13,%rcx
call   *%r14
test   %eax,%eax
jne    126f <kallsyms_parse+0xdf>
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     11f0 <kallsyms_parse+0x60>
xor    %ebp,%ebp
jmp    1271 <kallsyms_parse+0xe1>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd76(%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 i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
store i8* %0, i8** %3
store i32 (i8*, i8*, i8, i64)* %1, i32 (i8*, i8*, i8, i64)** %4
store i32 0, i32* %5
store i8* null, i8** %7
%14 = 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* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %2
br label %18
18: ; preds = %17
%19 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; No predecessors!
br label %22
22: ; preds = %21, %2
br label %23
23: ; preds = %59, %22
%24 = load i32*, i32** %6
%25 = call i32 @feof(i32* %24)
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br i1 %27, label %28, label %60
28: ; preds = %23
%29 = load i32*, i32** %6
%30 = call i32 @getline(i8** %7, i64* %13, i32* %29)
store i32 %30, i32* %12
%31 = load i32, i32* %12
%32 = icmp slt i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %37, label %36
36: ; preds = %33, %28
br label %60
37: ; preds = %33
%38 = load i8*, i8** %7
%39 = load i32, i32* %12
%40 = add nsw i32 %39, -1
store i32 %40, i32* %12
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %7
%44 = call i8* @strtok(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = call i64 @strtoul(i8* %45, i32* null, i32 16)
store i64 %46, i64* %11
%47 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%48 = load i8, i8* %47
store i8 %48, i8* %10
%49 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %49, i8** %9
%50 = load i32 (i8*, i8*, i8, i64)*, i32 (i8*, i8*, i8, i64)** %4
%51 = load i8*, i8** %3
%52 = load i8*, i8** %9
%53 = load i8, i8* %10
%54 = load i64, i64* %11
%55 = call i32 %50(i8* %51, i8* %52, i8 signext %53, i64 %54)
store i32 %55, i32* %5
%56 = load i32, i32* %5
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %37
br label %60
59: ; preds = %37
br label %23
60: ; preds = %58, %36, %23
%61 = load i8*, i8** %7
%62 = call i32 @free(i8* %61)
%63 = load i32*, i32** %6
%64 = call i32 @fclose(i32* %63)
%65 = load i32, i32* %5
ret i32 %65
}

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 %39
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, %36
%17 = call i32 @getline(i8** %3, i64* %4, i32* %6)
%18 = icmp sgt i32 %17, -1
%19 = load i8*, i8** %3
%20 = icmp ne i8* %19, null
%21 = select i1 %18, i1 %20, i1 false
br i1 %21, label %22, label %34
22: ; preds = %16
%23 = add nsw i32 %17, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %19, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %3
%27 = call i8* @strtok(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%28 = call i64 @strtoul(i8* nocapture %27, i32* null, i32 16)
%29 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%30 = load i8, i8* %29
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 %1(i8* %0, i8* %31, i8 signext %30, i64 %28)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %16, %22
%35 = phi i32 [ %32, %22 ], [ 0, %16 ]
br label %39
36: ; preds = %22
%37 = call i32 @feof(i32* %6)
%38 = icmp eq i32 %37, 0
br i1 %38, label %16, label %39
39: ; preds = %36, %8, %34
%40 = phi i32 [ %35, %34 ], [ 0, %8 ], [ 0, %36 ]
%41 = load i8*, i8** %3
%42 = call i32 @free(i8* %41)
%43 = call i32 @fclose(i32* %6)
ret i32 %40
}