#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_9__ TYPE_3__;
typedef struct TYPE_8__ TYPE_2__;
typedef struct TYPE_7__ TYPE_1__;

/* Type definitions */
struct TYPE_9__ {
  int e_shoff;
};
struct TYPE_8__ {
  unsigned int sh_link;
  int sh_offset;
  int sh_size;
  int sh_entsize;
};
struct TYPE_7__ {
  int st_name;
};
typedef TYPE_1__ Elf64_Sym;
typedef TYPE_2__ Elf64_Shdr;
typedef TYPE_3__ Elf64_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
Elf64_Sym *find_elf_symbol(Elf64_Ehdr *hdr, Elf64_Shdr *symtab, char *name) {
  Elf64_Sym *sym, *symtab_start;
  char *strtab, *symname;
  unsigned int link;
  Elf64_Shdr *x;
  int i, n;
  x = (void *)hdr + hdr->e_shoff;
  link = symtab->sh_link;
  symtab_start = (void *)hdr + symtab->sh_offset;
  n = symtab->sh_size / symtab->sh_entsize;
  strtab = (void *)hdr + x[link].sh_offset;
  for (i = 0; i < n; i++) {
    sym = &symtab_start[i];
    symname = strtab + sym->st_name;
    if (strcmp(symname, name) == 0)
      return sym;
  }
  fprintf(stderr,
          "ERROR:   "
          "Unable to find symbol: %s\n",
          name);
  return NULL;
}


x86 assembly (clang, -O0):
<find_elf_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq 0x4(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%eax
mov    -0x18(%rbp),%rcx
cltd
idivl  0xc(%rcx)
mov    %eax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    -0x44(%rbp),%edx
shl    $0x4,%rdx
add    %rdx,%rcx
movslq 0x4(%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x58(%rbp),%eax
jge    1200 <find_elf_symbol+0xe0>
mov    -0x30(%rbp),%rax
movslq -0x54(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    11ed <find_elf_symbol+0xcd>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1221 <find_elf_symbol+0x101>
jmp    11f2 <find_elf_symbol+0xd2>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1198 <find_elf_symbol+0x78>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdec(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_elf_symbol>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    0x8(%rsi),%eax
cltd
idivl  0xc(%rsi)
test   %eax,%eax
jle    117e <find_elf_symbol+0x5e>
movslq (%rdi),%rcx
add    %rdi,%rcx
mov    (%rsi),%edx
movslq 0x4(%rsi),%r12
shl    $0x4,%rdx
movslq 0x4(%rdx,%rcx,1),%r15
add    %rdi,%r15
mov    %eax,%ebx
add    %rdi,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq (%r12),%rdi
add    %r15,%rdi
mov    %r14,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
je     1199 <find_elf_symbol+0x79>
add    $0x4,%r12
add    $0xffffffffffffffff,%rbx
jne    1160 <find_elf_symbol+0x40>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32 }
%struct.TYPE_9__ = type { i32 }
%struct.TYPE_8__ = type { i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [36 x i8] c"ERROR: Unable to find symbol: %s\0A\00"
define %struct.TYPE_7__* @find_elf_symbol(%struct.TYPE_9__* %0, %struct.TYPE_8__* %1, i8* %2) {
%4 = alloca %struct.TYPE_7__*
%5 = alloca %struct.TYPE_9__*
%6 = alloca %struct.TYPE_8__*
%7 = alloca i8*
%8 = alloca %struct.TYPE_7__*
%9 = alloca %struct.TYPE_7__*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca %struct.TYPE_8__*
%14 = alloca i32
%15 = alloca i32
store %struct.TYPE_9__* %0, %struct.TYPE_9__** %5
store %struct.TYPE_8__* %1, %struct.TYPE_8__** %6
store i8* %2, i8** %7
%16 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%17 = bitcast %struct.TYPE_9__* %16 to i8*
%18 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%19 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = getelementptr i8, i8* %17, i64 %21
%23 = bitcast i8* %22 to %struct.TYPE_8__*
store %struct.TYPE_8__* %23, %struct.TYPE_8__** %13
%24 = load %struct.TYPE_8__*, %struct.TYPE_8__** %6
%25 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %24, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %12
%27 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%28 = bitcast %struct.TYPE_9__* %27 to i8*
%29 = load %struct.TYPE_8__*, %struct.TYPE_8__** %6
%30 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = getelementptr i8, i8* %28, i64 %32
%34 = bitcast i8* %33 to %struct.TYPE_7__*
store %struct.TYPE_7__* %34, %struct.TYPE_7__** %9
%35 = load %struct.TYPE_8__*, %struct.TYPE_8__** %6
%36 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = load %struct.TYPE_8__*, %struct.TYPE_8__** %6
%39 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %38, i32 0, i32 3
%40 = load i32, i32* %39
%41 = sdiv i32 %37, %40
store i32 %41, i32* %15
%42 = load %struct.TYPE_9__*, %struct.TYPE_9__** %5
%43 = bitcast %struct.TYPE_9__* %42 to i8*
%44 = load %struct.TYPE_8__*, %struct.TYPE_8__** %13
%45 = load i32, i32* %12
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %44, i64 %46
%48 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = sext i32 %49 to i64
%51 = getelementptr i8, i8* %43, i64 %50
store i8* %51, i8** %10
store i32 0, i32* %14
br label %52
52: ; preds = %74, %3
%53 = load i32, i32* %14
%54 = load i32, i32* %15
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %77
56: ; preds = %52
%57 = load %struct.TYPE_7__*, %struct.TYPE_7__** %9
%58 = load i32, i32* %14
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %57, i64 %59
store %struct.TYPE_7__* %60, %struct.TYPE_7__** %8
%61 = load i8*, i8** %10
%62 = load %struct.TYPE_7__*, %struct.TYPE_7__** %8
%63 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %61, i64 %65
store i8* %66, i8** %11
%67 = load i8*, i8** %11
%68 = load i8*, i8** %7
%69 = call i64 @strcmp(i8* %67, i8* %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %56
%72 = load %struct.TYPE_7__*, %struct.TYPE_7__** %8
store %struct.TYPE_7__* %72, %struct.TYPE_7__** %4
br label %81
73: ; preds = %56
br label %74
74: ; preds = %73
%75 = load i32, i32* %14
%76 = add nsw i32 %75, 1
store i32 %76, i32* %14
br label %52
77: ; preds = %52
%78 = load i32, i32* @stderr
%79 = load i8*, i8** %7
%80 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %79)
store %struct.TYPE_7__* null, %struct.TYPE_7__** %4
br label %81
81: ; preds = %77, %71
%82 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
ret %struct.TYPE_7__* %82
}

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