#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 */
typedef struct TYPE_7__ TYPE_3__;
typedef struct TYPE_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_7__ {
  int e_shoff;
};
struct TYPE_6__ {
  size_t sh_link;
  int sh_offset;
  int sh_size;
  int sh_entsize;
};
struct TYPE_5__ {
  int st_name;
  int st_value;
};
typedef TYPE_1__ Elf64_Sym;
typedef TYPE_2__ Elf64_Shdr;
typedef TYPE_3__ Elf64_Ehdr;

/* Variables and functions */
TYPE_2__ *elfi_find_section(void *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int elfi_find_symbol(void *data, char *name) {
  Elf64_Ehdr *elf_hdr = (Elf64_Ehdr *)data;
  Elf64_Shdr *s;
  Elf64_Shdr *shdr = (Elf64_Shdr *)(data + elf_hdr->e_shoff);
  char *sh_strtab_p = (char *)(data + shdr[s->sh_link].sh_offset);
  char *sh_symtab_p = data + s->sh_offset;
  Elf64_Sym *symbol;
  int n_entries, i;
  if ((s = elfi_find_section(data, ".symtab")) == NULL) {
    fprintf(stderr, "Cannot find symtable\n");
    return -1;
  }
  n_entries = s->sh_size / s->sh_entsize;
  shdr = (Elf64_Shdr *)(data + elf_hdr->e_shoff);
  sh_strtab_p = (char *)(data + shdr[s->sh_link].sh_offset);
  sh_symtab_p = data + s->sh_offset;
  for (i = 0; i < n_entries; i++) {
    symbol = &((Elf64_Sym *)sh_symtab_p)[i];
    if (symbol->st_name) {
      if (!strcmp(sh_strtab_p + symbol->st_name, name))
        return symbol->st_value;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<elfi_find_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rdx
imul   $0x18,(%rdx),%rdx
add    %rdx,%rcx
movslq 0x8(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq 0x8(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1050 <elfi_find_section@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ca <elfi_find_symbol+0x9a>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <elfi_find_symbol+0x170>
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%eax
mov    -0x28(%rbp),%rcx
cltd
idivl  0x10(%rcx)
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rdx
imul   $0x18,(%rdx),%rdx
add    %rdx,%rcx
movslq 0x8(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq 0x8(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    1299 <elfi_find_symbol+0x169>
mov    -0x40(%rbp),%rax
movslq -0x50(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmpl   $0x0,(%rax)
je     1286 <elfi_find_symbol+0x156>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1281 <elfi_find_symbol+0x151>
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <elfi_find_symbol+0x170>
jmp    1286 <elfi_find_symbol+0x156>
jmp    128b <elfi_find_symbol+0x15b>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1226 <elfi_find_symbol+0xf6>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elfi_find_symbol>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
lea    0xeb8(%rip),%rsi
call   1050 <elfi_find_section@plt>
test   %rax,%rax
je     11cf <elfi_find_symbol+0x9f>
mov    %rax,%rcx
mov    0xc(%rax),%eax
cltd
idivl  0x10(%rcx)
mov    $0xffffffff,%edx
test   %eax,%eax
jle    11e9 <elfi_find_symbol+0xb9>
movslq (%r14),%rdx
add    %r14,%rdx
mov    (%rcx),%rsi
lea    (%rsi,%rsi,2),%rsi
movslq 0x8(%rdx,%rsi,8),%rbp
add    %r14,%rbp
movslq 0x8(%rcx),%rcx
mov    %eax,%r15d
mov    %rcx,(%rsp)
lea    (%rcx,%r14,1),%r12
add    $0x4,%r12
xor    %ebx,%ebx
jmp    11a9 <elfi_find_symbol+0x79>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
cmp    %rbx,%r15
je     11e4 <elfi_find_symbol+0xb4>
movslq -0x4(%r12,%rbx,8),%rdi
test   %rdi,%rdi
je     11a0 <elfi_find_symbol+0x70>
add    %rbp,%rdi
mov    %r13,%rsi
call   1030 <strcmp@plt>
test   %eax,%eax
jne    11a0 <elfi_find_symbol+0x70>
mov    (%rsp),%rax
add    %r14,%rax
mov    0x4(%rax,%rbx,8),%edx
jmp    11e9 <elfi_find_symbol+0xb9>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%edx
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32 }
%struct.TYPE_6__ = type { i64, i32, i32, i32 }
%struct.TYPE_5__ = type { i32, i32 }
@.str = [8 x i8] c".symtab\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Cannot find symtable\0A\00"
define i32 @elfi_find_symbol(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.TYPE_7__*
%7 = alloca %struct.TYPE_6__*
%8 = alloca %struct.TYPE_6__*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.TYPE_5__*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%14 = load i8*, i8** %4
%15 = bitcast i8* %14 to %struct.TYPE_7__*
store %struct.TYPE_7__* %15, %struct.TYPE_7__** %6
%16 = load i8*, i8** %4
%17 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%18 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = sext i32 %19 to i64
%21 = getelementptr i8, i8* %16, i64 %20
%22 = bitcast i8* %21 to %struct.TYPE_6__*
store %struct.TYPE_6__* %22, %struct.TYPE_6__** %8
%23 = load i8*, i8** %4
%24 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%25 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%26 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %24, i64 %27
%29 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = getelementptr i8, i8* %23, i64 %31
store i8* %32, i8** %9
%33 = load i8*, i8** %4
%34 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%35 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = sext i32 %36 to i64
%38 = getelementptr i8, i8* %33, i64 %37
store i8* %38, i8** %10
%39 = load i8*, i8** %4
%40 = call %struct.TYPE_6__* @elfi_find_section(i8* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store %struct.TYPE_6__* %40, %struct.TYPE_6__** %7
%41 = icmp eq %struct.TYPE_6__* %40, null
br i1 %41, label %42, label %45
42: ; preds = %2
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %110
45: ; preds = %2
%46 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%47 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %46, i32 0, i32 2
%48 = load i32, i32* %47
%49 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%50 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %49, i32 0, i32 3
%51 = load i32, i32* %50
%52 = sdiv i32 %48, %51
store i32 %52, i32* %12
%53 = load i8*, i8** %4
%54 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%55 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = sext i32 %56 to i64
%58 = getelementptr i8, i8* %53, i64 %57
%59 = bitcast i8* %58 to %struct.TYPE_6__*
store %struct.TYPE_6__* %59, %struct.TYPE_6__** %8
%60 = load i8*, i8** %4
%61 = load %struct.TYPE_6__*, %struct.TYPE_6__** %8
%62 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%63 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %61, i64 %64
%66 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %65, i32 0, i32 1
%67 = load i32, i32* %66
%68 = sext i32 %67 to i64
%69 = getelementptr i8, i8* %60, i64 %68
store i8* %69, i8** %9
%70 = load i8*, i8** %4
%71 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%72 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %71, i32 0, i32 1
%73 = load i32, i32* %72
%74 = sext i32 %73 to i64
%75 = getelementptr i8, i8* %70, i64 %74
store i8* %75, i8** %10
store i32 0, i32* %13
br label %76
76: ; preds = %106, %45
%77 = load i32, i32* %13
%78 = load i32, i32* %12
%79 = icmp slt i32 %77, %78
br i1 %79, label %80, label %109
80: ; preds = %76
%81 = load i8*, i8** %10
%82 = bitcast i8* %81 to %struct.TYPE_5__*
%83 = load i32, i32* %13
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %82, i64 %84
store %struct.TYPE_5__* %85, %struct.TYPE_5__** %11
%86 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%87 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %86, i32 0, i32 0
%88 = load i32, i32* %87
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %105
90: ; preds = %80
%91 = load i8*, i8** %9
%92 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%93 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %92, i32 0, i32 0
%94 = load i32, i32* %93
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %91, i64 %95
%97 = load i8*, i8** %5
%98 = call i32 @strcmp(i8* %96, i8* %97)
%99 = icmp ne i32 %98, 0
br i1 %99, label %104, label %100
100: ; preds = %90
%101 = load %struct.TYPE_5__*, %struct.TYPE_5__** %11
%102 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %101, i32 0, i32 1
%103 = load i32, i32* %102
store i32 %103, i32* %3
br label %110
104: ; preds = %90
br label %105
105: ; preds = %104, %80
br label %106
106: ; preds = %105
%107 = load i32, i32* %13
%108 = add nsw i32 %107, 1
store i32 %108, i32* %13
br label %76
109: ; preds = %76
store i32 -1, i32* %3
br label %110
110: ; preds = %109, %100, %42
%111 = load i32, i32* %3
ret i32 %111
}

LLVM IR (-O2):
%struct.TYPE_6__ = type { i64, i32, i32, i32 }
%struct.TYPE_5__ = type { i32, i32 }
@.str = [8 x i8] c".symtab\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Cannot find symtable\0A\00"
define i32 @elfi_find_symbol(i8* %0, i8* nocapture %1) {
%3 = tail call %struct.TYPE_6__* @elfi_find_section(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq %struct.TYPE_6__* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %50
8: ; preds = %2
%9 = bitcast i8* %0 to i32*
%10 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %3, i64 0, i32 2
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %3, i64 0, i32 3
%13 = load i32, i32* %12
%14 = sdiv i32 %11, %13
%15 = load i32, i32* %9
%16 = sext i32 %15 to i64
%17 = getelementptr i8, i8* %0, i64 %16
%18 = bitcast i8* %17 to %struct.TYPE_6__*
%19 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %3, i64 0, i32 0
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %18, i64 %20, i32 1
%22 = load i32, i32* %21
%23 = sext i32 %22 to i64
%24 = getelementptr i8, i8* %0, i64 %23
%25 = icmp sgt i32 %14, 0
br i1 %25, label %26, label %50
26: ; preds = %8
%27 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %3, i64 0, i32 1
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = getelementptr i8, i8* %0, i64 %29
%31 = bitcast i8* %30 to %struct.TYPE_5__*
%32 = zext i32 %14 to i64
br label %33
33: ; preds = %26, %47
%34 = phi i64 [ 0, %26 ], [ %48, %47 ]
%35 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %31, i64 %34, i32 0
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 0
br i1 %37, label %47, label %38
38: ; preds = %33
%39 = sext i32 %36 to i64
%40 = getelementptr inbounds i8, i8* %24, i64 %39
%41 = tail call i32 @strcmp(i8* %40, i8* dereferenceable(1) %1)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = and i64 %34, 4294967295
%45 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %31, i64 %44, i32 1
%46 = load i32, i32* %45
br label %50
47: ; preds = %33, %38
%48 = add nuw nsw i64 %34, 1
%49 = icmp eq i64 %48, %32
br i1 %49, label %50, label %33
50: ; preds = %47, %8, %43, %5
%51 = phi i32 [ -1, %5 ], [ %46, %43 ], [ -1, %8 ], [ -1, %47 ]
ret i32 %51
}