#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strcspn(char *, char *);
char const *strerror(int /*<<< orphan*/);
int strspn(char *, char *);
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int);
const char *lookup_sym(void *addr) {
  static char ret_sym[256] = "???";
  FILE *fh = fopen("/symbol_table", "r");
  if (!fh) {
    return strerror(errno);
  }
  char buf[256];
  void *cptr = NULL, *ptr;
  while (fgets(buf, 256, fh)) {
    ptr = (void *)strtoul(buf, NULL, 16);
    if (addr >= ptr && ptr > cptr) {
      char *s = buf + strspn(buf, "1234567890ABCDEFabcdef");
      strcpy(ret_sym, s + strspn(s, "\t"));
      ret_sym[strcspn(ret_sym, "\n")] = '\0';
      cptr = ptr;
    }
  }
  fclose(fh);
  return ret_sym;
}


x86 assembly (clang, -O0):
<lookup_sym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <lookup_sym+0x48>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,-0x8(%rbp)
jmp    12eb <lookup_sym+0x16b>
movq   $0x0,-0x128(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12d7 <lookup_sym+0x157>
lea    -0x120(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %rax,-0x130(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x130(%rbp),%rax
jb     12d2 <lookup_sym+0x152>
mov    -0x130(%rbp),%rax
cmp    -0x128(%rbp),%rax
jbe    12d2 <lookup_sym+0x152>
lea    -0x120(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x120(%rbp),%rdi
lea    0xdc1(%rip),%rsi
call   1050 <strspn@plt>
mov    %eax,%ecx
mov    -0x148(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    -0x138(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1050 <strspn@plt>
mov    -0x140(%rbp),%rsi
cltq
add    %rax,%rsi
lea    0x2dd2(%rip),%rdi
call   1030 <strcpy@plt>
lea    0x2dc6(%rip),%rdi
lea    0xd78(%rip),%rsi
call   1060 <strcspn@plt>
mov    %rax,%rcx
lea    0x2db0(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x130(%rbp),%rax
mov    %rax,-0x128(%rbp)
jmp    11d3 <lookup_sym+0x53>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0x2d89(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_sym>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
lea    0xe65(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1286 <lookup_sym+0x106>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1265 <lookup_sym+0xe5>
xor    %ebp,%ebp
mov    %rsp,%rbx
jmp    11f5 <lookup_sym+0x75>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1265 <lookup_sym+0xe5>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
cmp    %r14,%rax
ja     11e0 <lookup_sym+0x60>
mov    %rax,%r13
cmp    %rax,%rbp
jae    11e0 <lookup_sym+0x60>
mov    %rbx,%rdi
lea    0xdf5(%rip),%rsi
call   1050 <strspn@plt>
cltq
lea    (%rsp,%rax,1),%r12
mov    %r12,%rdi
lea    0xdf7(%rip),%rsi
call   1050 <strspn@plt>
movslq %eax,%rsi
add    %r12,%rsi
lea    0x2e2e(%rip),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
lea    0xdd5(%rip),%rsi
call   1060 <strcspn@plt>
movb   $0x0,(%rax,%rbp,1)
mov    %r13,%rbp
jmp    11e0 <lookup_sym+0x60>
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    0x2dfc(%rip),%rax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <strerror@plt>

LLVM IR (-O0):
@lookup_sym.ret_sym = internal global [256 x i8] c"???\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [14 x i8] c"/symbol_table\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"1234567890ABCDEFabcdef\00"
@.str.3 = [2 x i8] c"\09\00"
@.str.4 = [2 x i8] c"\0A\00"
define i8* @lookup_sym(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [256 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
%9 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 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 ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
store i8* %14, i8** %2
br label %51
15: ; preds = %1
store i8* null, i8** %6
br label %16
16: ; preds = %47, %15
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i64 @fgets(i8* %17, i32 256, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %48
21: ; preds = %16
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%23 = call i64 @strtoul(i8* %22, i32* null, i32 16)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %7
%25 = load i8*, i8** %3
%26 = load i8*, i8** %7
%27 = icmp uge i8* %25, %26
br i1 %27, label %28, label %47
28: ; preds = %21
%29 = load i8*, i8** %7
%30 = load i8*, i8** %6
%31 = icmp ugt i8* %29, %30
br i1 %31, label %32, label %47
32: ; preds = %28
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = call i32 @strspn(i8* %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = load i8*, i8** %8
%40 = call i32 @strspn(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = call i32 @strcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 0), i8* %42)
%44 = call i64 @strcspn(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = getelementptr inbounds [256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %7
store i8* %46, i8** %6
br label %47
47: ; preds = %32, %28, %21
br label %16
48: ; preds = %16
%49 = load i32*, i32** %4
%50 = call i32 @fclose(i32* %49)
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 0), i8** %2
br label %51
51: ; preds = %48, %12
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@lookup_sym.ret_sym = internal global [256 x i8] c"???\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [14 x i8] c"/symbol_table\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"1234567890ABCDEFabcdef\00"
@.str.3 = [2 x i8] c"\09\00"
@.str.4 = [2 x i8] c"\0A\00"
define i8* @lookup_sym(i8* readnone %0) {
%2 = alloca [256 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
br label %35
8: ; preds = %1
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%10 = call i64 @fgets(i8* %9, i32 256, i32* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %8, %29
%13 = phi i8* [ %30, %29 ], [ null, %8 ]
%14 = call i64 @strtoul(i8* nocapture %9, i32* null, i32 16)
%15 = inttoptr i64 %14 to i8*
%16 = icmp ule i8* %15, %0
%17 = icmp ult i8* %13, %15
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %19, label %29
19: ; preds = %12
%20 = call i32 @strspn(i8* %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %21
%23 = call i32 @strspn(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = call i32 @strcpy(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 0), i8* %25)
%27 = call i64 @strcspn(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%28 = getelementptr inbounds [256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %19, %12
%30 = phi i8* [ %15, %19 ], [ %13, %12 ]
%31 = call i64 @fgets(i8* %9, i32 256, i32* %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %12
33: ; preds = %29, %8
%34 = call i32 @fclose(i32* %3)
br label %35
35: ; preds = %33, %5
%36 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @lookup_sym.ret_sym, i64 0, i64 0), %33 ], [ %7, %5 ]
ret i8* %36
}