#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 scalar_t__ uint8_t;
typedef int uint64_t;
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, int *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strtok(char *, char *);
int strtoull(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
uint64_t get_address(char *identifier) {
  FILE *fp;
  char *line = NULL;
  ssize_t read;
  ssize_t len;
  char *tmp;
  uint64_t address = 0x0;
  uint8_t identifier_len = strlen(identifier);
  fp = fopen("/proc/kallsyms", "r");
  if (fp == NULL) {
    return address;
  }
  while ((read = getline(&line, &len, fp)) != -1) {
    if (strlen(line) > identifier_len &&
        !strcmp(line + strlen(line) - identifier_len, identifier)) {
      address = strtoull(strtok(line, " "), NULL, 16);
      break;
    }
  }
  fclose(fp);
  if (line) {
    free(line);
  }
  return address;
}


x86 assembly (clang, -O0):
<get_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
mov    %rax,-0x40(%rbp)
lea    0xe4f(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d7 <get_address+0x57>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <get_address+0x113>
jmp    11dc <get_address+0x5c>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1270 <get_address+0xf0>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    -0x40(%rbp),%rax
jle    126b <get_address+0xeb>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
cltq
add    %rax,%rdi
xor    %eax,%eax
sub    -0x40(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    126b <get_address+0xeb>
mov    -0x20(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1090 <strtok@plt>
mov    %eax,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtoull@plt>
mov    %eax,-0x34(%rbp)
jmp    1270 <get_address+0xf0>
jmp    11dc <get_address+0x5c>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     128d <get_address+0x10d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,(%rsp)
mov    %rdi,0x10(%rsp)
call   1050 <strlen@plt>
mov    %eax,%ebp
lea    0xe57(%rip),%rdi
lea    0xe5f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1258 <get_address+0xd8>
mov    %rax,%rbx
mov    %rsp,%rdi
lea    0xc(%rsp),%rsi
mov    %rax,%rdx
call   10a0 <getline@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
je     1260 <get_address+0xe0>
movslq %ebp,%r15
neg    %r15
mov    %rsp,%r12
lea    0xc(%rsp),%r13
jmp    1203 <get_address+0x83>
nopl   (%rax)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     125d <get_address+0xdd>
mov    (%rsp),%rdi
call   1050 <strlen@plt>
cmp    %ebp,%eax
jle    11f0 <get_address+0x70>
mov    (%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
add    %r15,%r14
add    %rax,%r14
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    11f0 <get_address+0x70>
mov    (%rsp),%rdi
lea    0xdd1(%rip),%rsi
call   1090 <strtok@plt>
mov    %eax,%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtoull@plt>
mov    %eax,%r14d
jmp    1260 <get_address+0xe0>
xor    %r14d,%r14d
jmp    1276 <get_address+0xf6>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1276 <get_address+0xf6>
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x18,%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"
@.str.2 = [2 x i8] c" \00"
define i32 @get_address(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %5
store i32 0, i32* %9
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
store i64 %13, i64* %10
%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** %4
%15 = load i32*, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = load i32, i32* %9
store i32 %18, i32* %2
br label %57
19: ; preds = %1
br label %20
20: ; preds = %46, %19
%21 = load i32*, i32** %4
%22 = call i32 @getline(i8** %5, i32* %7, i32* %21)
store i32 %22, i32* %6
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %47
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = sext i32 %26 to i64
%28 = load i64, i64* %10
%29 = icmp sgt i64 %27, %28
br i1 %29, label %30, label %46
30: ; preds = %24
%31 = load i8*, i8** %5
%32 = load i8*, i8** %5
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i64, i64* %10
%37 = sub i64 0, %36
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8*, i8** %3
%40 = call i32 @strcmp(i8* %38, i8* %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %30
%43 = load i8*, i8** %5
%44 = call i32 @strtok(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @strtoull(i32 %44, i32* null, i32 16)
store i32 %45, i32* %9
br label %47
46: ; preds = %30, %24
br label %20
47: ; preds = %42, %20
%48 = load i32*, i32** %4
%49 = call i32 @fclose(i32* %48)
%50 = load i8*, i8** %5
%51 = icmp ne i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i8*, i8** %5
%54 = call i32 @free(i8* %53)
br label %55
55: ; preds = %52, %47
%56 = load i32, i32* %9
store i32 %56, i32* %2
br label %57
57: ; preds = %55, %17
%58 = load i32, i32* %2
ret i32 %58
}

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