#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcmp(char *, char *);
unsigned long get_kernel_sym(char *name) {
  FILE *f;
  unsigned long addr;
  char dummy;
  char sname[256];
  int ret = 0;
  f = fopen("/proc/kallsyms", "r");
  if (f == NULL) {
    printf("[-] Failed to open /proc/kallsyms\n");
    exit(-1);
  }
  printf("[+] Find %s...\n", name);
  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);
      printf("[+] Found %s at %lx\n", name, addr);
      return addr;
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<get_kernel_sym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x134(%rbp)
lea    0xe80(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <get_kernel_sym+0x53>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x134(%rbp),%eax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%eax
je     1276 <get_kernel_sym+0x116>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    -0x130(%rbp),%r8
lea    0xe54(%rip),%rsi
lea    -0x21(%rbp),%rcx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jne    122c <get_kernel_sym+0xcc>
mov    -0x18(%rbp),%rdi
lea    -0x130(%rbp),%rdx
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
jmp    11c5 <get_kernel_sym+0x65>
mov    -0x10(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1271 <get_kernel_sym+0x111>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1287 <get_kernel_sym+0x127>
jmp    11c5 <get_kernel_sym+0x65>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kernel_sym>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%rbp
lea    0xeb9(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1271 <get_kernel_sym+0x101>
mov    %rax,%rbx
lea    0xe58(%rip),%rdi
mov    %rbp,%r15
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e2a(%rip),%rax
cmpl   $0x0,(%rax)
je     1255 <get_kernel_sym+0xe5>
lea    0x8(%rsp),%r12
lea    0x7(%rsp),%r13
lea    0x10(%rsp),%rbp
jmp    1200 <get_kernel_sym+0x90>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
lea    0xe2e(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fscanf@plt>
mov    0x2ded(%rip),%rax
cmp    (%rax),%r14d
je     1255 <get_kernel_sym+0xe5>
mov    %rbx,%rdi
lea    0xe08(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1060 <fscanf@plt>
mov    %eax,%r14d
test   %eax,%eax
je     11e0 <get_kernel_sym+0x70>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    11f4 <get_kernel_sym+0x84>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdx
lea    0xdd8(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rax
jmp    125f <get_kernel_sym+0xef>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

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

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