#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ strcmp(char *, char *);
unsigned long get_kernel_sym(char *name) {
  FILE *f;
  unsigned long addr;
  char dummy;
  char sname[512];
  int ret;
  f = fopen("/proc/kallsyms", "r");
  if (f == NULL)
    return 0;
  ret = 0;
  while (ret != EOF) {
    ret = fscanf(f, "%p %c %s\n", (void **)&addr, &dummy, sname);
    if (ret == 0) {
      fscanf(f, "%s\n", sname);
      continue;
    }
    if (!strcmp(name, sname)) {
      fclose(f);
      return addr;
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<get_kernel_sym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117e <get_kernel_sym+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    1234 <get_kernel_sym+0xf4>
movl   $0x0,-0x234(%rbp)
mov    -0x234(%rbp),%eax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%eax
je     1223 <get_kernel_sym+0xe3>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    -0x230(%rbp),%r8
lea    0xe5e(%rip),%rsi
lea    -0x21(%rbp),%rcx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jne    11ef <get_kernel_sym+0xaf>
mov    -0x18(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
jmp    1188 <get_kernel_sym+0x48>
mov    -0x10(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    121e <get_kernel_sym+0xde>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <get_kernel_sym+0xf4>
jmp    1188 <get_kernel_sym+0x48>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kernel_sym>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%r14
lea    0xea5(%rip),%rdi
lea    0xead(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f7 <get_kernel_sym+0xb7>
mov    %rax,%rbx
mov    0x2e6e(%rip),%rax
cmpl   $0x0,(%rax)
je     11ef <get_kernel_sym+0xaf>
lea    0x8(%rsp),%r12
lea    0x7(%rsp),%r13
lea    0x10(%rsp),%rbp
jmp    11b0 <get_kernel_sym+0x70>
mov    %rbx,%rdi
lea    0xe7d(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    0x2e3d(%rip),%rax
cmp    (%rax),%r15d
je     11ef <get_kernel_sym+0xaf>
mov    %rbx,%rdi
lea    0xe57(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    %eax,%r15d
test   %eax,%eax
je     1190 <get_kernel_sym+0x50>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
jne    11a4 <get_kernel_sym+0x64>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rax
jmp    11f9 <get_kernel_sym+0xb9>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/kallsyms\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.2 = [10 x i8] c"%p %c %s\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i64 @get_kernel_sym(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8
%7 = alloca [512 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = 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* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i64 0, i64* %2
br label %42
13: ; preds = %1
store i32 0, i32* %8
br label %14
14: ; preds = %38, %25, %13
%15 = load i32, i32* %8
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = load i32*, i32** %4
%20 = bitcast i64* %5 to i8**
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%22 = call i32 (i32*, i8*, ...) @fscanf(i32* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %20, i8* %6, i8* %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i32*, i32** %4
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%28 = call i32 (i32*, i8*, ...) @fscanf(i32* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %14
29: ; preds = %18
%30 = load i8*, i8** %3
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%32 = call i32 @strcmp(i8* %30, i8* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %29
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
%37 = load i64, i64* %5
store i64 %37, i64* %2
br label %42
38: ; preds = %29
br label %14
39: ; preds = %14
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
store i64 0, i64* %2
br label %42
42: ; preds = %39, %34, %12
%43 = load i64, i64* %2
ret i64 %43
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/kallsyms\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.2 = [10 x i8] c"%p %c %s\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i64 @get_kernel_sym(i8* nocapture %0) {
%2 = alloca i64
%3 = alloca i8
%4 = alloca [512 x i8]
%5 = bitcast i64* %2 to i8*
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %28, label %9
9: ; preds = %1
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %9, %17
%13 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64* %2, i8* %3, i8* %6)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %17
17: ; preds = %15, %20
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %13, %18
br i1 %19, label %26, label %12
20: ; preds = %12
%21 = call i32 @strcmp(i8* dereferenceable(1) %0, i8* %6)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %17
23: ; preds = %20
%24 = call i32 @fclose(i32* %7)
%25 = load i64, i64* %2
br label %28
26: ; preds = %17, %9
%27 = call i32 @fclose(i32* %7)
br label %28
28: ; preds = %1, %26, %23
%29 = phi i64 [ %25, %23 ], [ 0, %26 ], [ 0, %1 ]
ret i64 %29
}