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


x86 assembly (clang, -O0):
<get_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x134(%rbp)
lea    0xe90(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1198 <get_symbol+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1274 <get_symbol+0x124>
jmp    119d <get_symbol+0x4d>
mov    -0x134(%rbp),%eax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%eax
je     124e <get_symbol+0xfe>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rdx
lea    -0x130(%rbp),%r8
lea    0xe49(%rip),%rsi
lea    -0x21(%rbp),%rcx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jne    1204 <get_symbol+0xb4>
mov    -0x18(%rbp),%rdi
lea    -0x130(%rbp),%rdx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
jmp    119d <get_symbol+0x4d>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1249 <get_symbol+0xf9>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1274 <get_symbol+0x124>
jmp    119d <get_symbol+0x4d>
lea    -0x130(%rbp),%rsi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
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_symbol>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
lea    0xe95(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1244 <get_symbol+0xf4>
mov    %rax,%rbx
mov    0x2e5e(%rip),%rax
cmpl   $0x0,(%rax)
je     1225 <get_symbol+0xd5>
lea    0x8(%rsp),%r12
lea    0x7(%rsp),%r13
lea    0x10(%rsp),%rbp
jmp    11d0 <get_symbol+0x80>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
lea    0xe5d(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    0x2e1d(%rip),%rax
cmp    (%rax),%r15d
je     1225 <get_symbol+0xd5>
mov    %rbx,%rdi
lea    0xe37(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %eax,%r15d
test   %eax,%eax
je     11b0 <get_symbol+0x60>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    11c4 <get_symbol+0x74>
mov    0x8(%rsp),%rdx
lea    0xe0f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rbp
jmp    1246 <get_symbol+0xf6>
lea    0xe04(%rip),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1246 <get_symbol+0xf6>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x118,%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"
@.str.4 = [21 x i8] c"[+] Resolved %s: %p\0A\00"
@.str.5 = [27 x i8] c"[-] Couldn't resolve \22%s\22\0A\00"
define i64 @get_symbol(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 %13
12: ; preds = %1
store i64 0, i64* %2
br label %48
13: ; preds = %1
br label %14
14: ; preds = %42, %25, %13
%15 = load i32, i32* %8
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %43
18: ; preds = %14
%19 = load i32*, i32** %4
%20 = bitcast i64* %5 to i8**
%21 = getelementptr inbounds [256 x i8], [256 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 [256 x i8], [256 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 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%31 = load i8*, i8** %3
%32 = call i32 @strcmp(i8* %30, i8* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %29
%35 = load i8*, i8** %3
%36 = load i64, i64* %5
%37 = inttoptr i64 %36 to i8*
%38 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %35, i8* %37)
%39 = load i32*, i32** %4
%40 = call i32 @fclose(i32* %39)
%41 = load i64, i64* %5
store i64 %41, i64* %2
br label %48
42: ; preds = %29
br label %14
43: ; preds = %14
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%45 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = load i32*, i32** %4
%47 = call i32 @fclose(i32* %46)
store i64 0, i64* %2
br label %48
48: ; preds = %43, %34, %12
%49 = load i64, i64* %2
ret i64 %49
}

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"
@.str.4 = [21 x i8] c"[+] Resolved %s: %p\0A\00"
@.str.5 = [27 x i8] c"[-] Couldn't resolve \22%s\22\0A\00"
define i64 @get_symbol(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 %32, label %9
9: ; preds = %1
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %10, 0
br i1 %11, label %29, 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 %29, label %12
20: ; preds = %12
%21 = call i32 @strcmp(i8* %6, i8* dereferenceable(1) %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %17
23: ; preds = %20
%24 = load i64, i64* %2
%25 = inttoptr i64 %24 to i8*
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %25)
%27 = call i32 @fclose(i32* %7)
%28 = load i64, i64* %2
br label %32
29: ; preds = %17, %9
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%31 = call i32 @fclose(i32* %7)
br label %32
32: ; preds = %1, %29, %23
%33 = phi i64 [ %28, %23 ], [ 0, %29 ], [ 0, %1 ]
ret i64 %33
}