// File: /workspace/github_c_makefile_star2_O3/0xADE1A1DE[P]MeasureSuite/lib/src/io/elf_file.c
// Function: find_symbol_in_table
void find_symbol_in_table(int32_t file, Elf64_Ehdr hdr, Elf64_Shdr sh_table[],
                          const char *symbol, Elf64_Sym *dest) {

  size_t symbol_len = symbol != NULL ? strlen(symbol) : 0;

  for (int i = 0; i < hdr.e_shnum; i++) {
    if ((sh_table[i].sh_type != SHT_SYMTAB) &&
        (sh_table[i].sh_type != SHT_DYNSYM)) {
      continue;
    }

    Elf64_Sym *sym_tbl = {0};

    read_section(file, sh_table[i], (void *)&sym_tbl);

    /* Read linked string-table
     * Section containing the string table having names of
     * symbols of this section
     */
    Elf64_Word str_tbl_ndx = sh_table[i].sh_link;
    char *string_tbl = NULL;
    read_section(file, sh_table[str_tbl_ndx], (void *)&string_tbl);

    unsigned long symbol_count = sh_table[i].sh_size / sizeof(Elf64_Sym);

    for (unsigned long j = 0; j < symbol_count; j++) {

      Elf64_Sym cur_sym = sym_tbl[j];
      // st_name is the index in the string_table
      Elf64_Word index = cur_sym.st_name;

      // get the address of the string to compare
      const char *name = &(string_tbl[index]);
      // if we don't have a symbol(i.e. symbol len == 0),
      // or the current symbol is the required one
      if (symbol_len == 0 || strncmp(name, symbol, symbol_len) == 0) {
        memcpy(dest, &sym_tbl[j], sizeof(Elf64_Sym));
        break;
      }
    }
    free(string_tbl);
    free(sym_tbl);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x28(%rsp)
movq %rsi, %r14
movl %edi, 0x14(%rsp)
leaq 0x80(%rsp), %rbx
testq %rdx, %rdx
movq %rdx, 0x40(%rsp)
je 0x474f
movq %rdx, %rdi
callq 0x20a0
movq %rax, %r12
jmp 0x4752
xorl %r12d, %r12d
movzwl 0x3c(%rbx), %eax
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x4874
xorl %r15d, %r15d
movq %r14, 0x30(%rsp)
movq %r15, %rbx
shlq $0x6, %rbx
movl 0x4(%r14,%rbx), %eax
cmpl $0xb, %eax
je 0x4786
cmpl $0x2, %eax
jne 0x4866
addq %r14, %rbx
xorl %r13d, %r13d
movq %r13, 0x20(%rsp)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
movl 0x14(%rsp), %ebp
movl %ebp, %edi
leaq 0x20(%rsp), %rcx
callq 0x46c6
movl 0x28(%rbx), %eax
movq %r13, 0x18(%rsp)
shlq $0x6, %rax
movq 0x18(%r14,%rax), %rsi
movq 0x20(%r14,%rax), %rdx
movl %ebp, %edi
leaq 0x18(%rsp), %rcx
callq 0x46c6
movq 0x20(%rbx), %rcx
movq %rcx, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rdx
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x18, %rcx
jb 0x484f
movq %rdx, %rbp
shrq $0x4, %rbp
movq 0x8(%rsp), %r13
xorl %r14d, %r14d
testq %r12, %r12
je 0x482e
movl (%r13), %edi
addq %rbx, %rdi
movq 0x40(%rsp), %rsi
movq %r12, %rdx
callq 0x2080
testl %eax, %eax
je 0x4831
incq %r14
addq $0x18, %r13
cmpq %r14, %rbp
jne 0x4803
jmp 0x484f
xorl %r14d, %r14d
leaq (%r14,%r14,2), %rax
movq 0x8(%rsp), %rsi
movq 0x10(%rsi,%rax,8), %rcx
movq 0x28(%rsp), %rdx
movq %rcx, 0x10(%rdx)
movups (%rsi,%rax,8), %xmm0
movups %xmm0, (%rdx)
movq %rbx, %rdi
callq 0x2040
movq 0x8(%rsp), %rdi
callq 0x2040
movq 0x30(%rsp), %r14
incq %r15
cmpq 0x38(%rsp), %r15
jne 0x476c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq