#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*/ add_symbol(char *, unsigned int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ finish_symbol();
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ init_symbol();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
unsigned int strtol(char *, int /*<<< orphan*/ *, int);
int load_syms(char *file) {
  FILE *fp;
  char line[256];
  int n = 0;
  char val[9];
  char name[256];
  unsigned int addr;
  fp = fopen(file, "r");
  if (fp == NULL) {
    fprintf(stderr, "Cannot read symbol file: %s\n", file);
    exit(1);
  }
  init_symbol();
  while (fgets(line, 256, fp) != NULL) {
    line[strlen(line) - 1] = '\0';
    strncpy(val, line, 8);
    val[8] = '\0';
    addr = strtol(val, NULL, 16);
    strcpy(name, &line[9]);
    add_symbol(name, addr);
    n++;
  }
  finish_symbol();
  return n;
}


x86 assembly (clang, -O0):
<load_syms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x114(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120b <load_syms+0x5b>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe06(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10d0 <init_symbol@plt>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12c1 <load_syms+0x111>
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x11d(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x115(%rbp)
lea    -0x11d(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1090 <strtol@plt>
mov    %eax,-0x224(%rbp)
lea    -0x220(%rbp),%rdi
lea    -0x110(%rbp),%rsi
add    $0x9,%rsi
call   1040 <strcpy@plt>
lea    -0x220(%rbp),%rdi
mov    -0x224(%rbp),%esi
call   1060 <add_symbol@plt>
mov    -0x114(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x114(%rbp)
jmp    1212 <load_syms+0x62>
mov    $0x0,%al
call   10a0 <finish_symbol@plt>
mov    -0x114(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_syms>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%rbx
lea    0xe35(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1292 <load_syms+0xe2>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   10d0 <init_symbol@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1277 <load_syms+0xc7>
xor    %ebx,%ebx
lea    0x7(%rsp),%r13
lea    0x110(%rsp),%rbp
xchg   %ax,%ax
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r15,1)
mov    %r13,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xf(%rsp)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <strtol@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
lea    0x19(%rsp),%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
mov    %r12d,%esi
call   1060 <add_symbol@plt>
add    $0x1,%ebx
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1210 <load_syms+0x60>
xor    %eax,%eax
call   10a0 <finish_symbol@plt>
mov    %ebx,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xd60(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Cannot read symbol file: %s\0A\00"
define i32 @load_syms(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca [9 x i8]
%7 = alloca [256 x i8]
%8 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = call i32 (...) @init_symbol()
br label %20
20: ; preds = %25, %18
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%22 = load i32*, i32** %3
%23 = call i32* @fgets(i8* %21, i32 256, i32* %22)
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %45
25: ; preds = %20
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%33 = call i32 @strncpy(i8* %31, i8* %32, i32 8)
%34 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 8
store i8 0, i8* %34
%35 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%36 = call i32 @strtol(i8* %35, i32* null, i32 16)
store i32 %36, i32* %8
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 9
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%41 = load i32, i32* %8
%42 = call i32 @add_symbol(i8* %40, i32 %41)
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %20
45: ; preds = %20
%46 = call i32 (...) @finish_symbol()
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Cannot read symbol file: %s\0A\00"
define i32 @load_syms(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [9 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = tail call i32 (...) @init_symbol()
%16 = call i32* @fgets(i8* %5, i32 256, i32* %8)
%17 = icmp eq i32* %16, null
br i1 %17, label %34, label %18
18: ; preds = %14
%19 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 8
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 9
br label %21
21: ; preds = %18, %21
%22 = phi i32 [ 0, %18 ], [ %31, %21 ]
%23 = call i32 @strlen(i8* %5)
%24 = add nsw i32 %23, -1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 @strncpy(i8* %6, i8* %5, i32 8)
store i8 0, i8* %19
%28 = call i32 @strtol(i8* nocapture %6, i32* null, i32 16)
%29 = call i32 @strcpy(i8* %7, i8* %20)
%30 = call i32 @add_symbol(i8* %7, i32 %28)
%31 = add nuw nsw i32 %22, 1
%32 = call i32* @fgets(i8* %5, i32 256, i32* %8)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %21
34: ; preds = %21, %14
%35 = phi i32 [ 0, %14 ], [ %31, %21 ]
%36 = call i32 (...) @finish_symbol()
ret i32 %35
}