// File: /workspace/github_c_makefile_star2_O2/0xADE1A1DE[P]MeasureSuite/lib/src/io/elf_parser.c
// Function: elf_load_symbol_mem
int elf_load_symbol_mem(measuresuite_t ms, void *dest, size_t dest_size,
                        const uint8_t *src, const char *symbol) {
  // read elf header
  // read section header table
  // find .text - section
  // find symbol
  // 	-- read all sections from table
  // 	-- skip sections which are not SHT_SYMTAB / SHT_DYNSYM
  // 	-- read SHT_SYMTAB / SHT_DYNSYM

  Elf64_Ehdr eh64; /* elf-header is fixed size */
  /* ELF header : at start of file */
  if (read_elf_header_mem(src, &eh64)) {
    ms->errorno = E_LOAD__ELF_MEM_INVALID_ELF;
    return 1;
  }

  /* Section header table :  */

  Elf64_Shdr *sh_tbl = malloc((long)eh64.e_shentsize * eh64.e_shnum);
  if (!sh_tbl || read_section_header_table64_mem(src, eh64, sh_tbl)) {
    ms->errorno = E_LOAD__ELF_FILE_MALLOC;
    return 1;
  }

  unsigned long index = 0; // get offset of .text first
  find_section_offset_mem(src, eh64, sh_tbl, ".text", &index);
  Elf64_Shdr text = sh_tbl[index];

  Elf64_Sym sym = {0};
  find_symbol_in_table_mem(&sym, src, sh_tbl, eh64.e_shnum, symbol);
  free(sh_tbl);

  // now we wwant to copy into dest from symbol-offset to the end of the section
  // size to copy is
  unsigned long size = text.sh_size - sym.st_value;

  // check if destination buffer is big enough
  assert(size <= dest_size);

  // calc offset in mem
  unsigned long offset_in_mem = text.sh_offset + sym.st_value;
  // then position in memory is
  const uint8_t *pos_in_mem = src + offset_in_mem;

  // read code into *dest
  memcpy(dest, pos_in_mem, size);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, %r12
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbp
leaq 0x48(%rsp), %rsi
movq %rcx, %rdi
callq 0x403e
testl %eax, %eax
je 0x3caf
movl $0x1f, 0x88(%rbp)
jmp 0x3d10
movzwl 0x82(%rsp), %eax
movzwl 0x84(%rsp), %edi
imulq %rax, %rdi
callq 0x21c0
testq %rax, %rax
je 0x3d06
movq %rax, %r13
movups 0x48(%rsp), %xmm0
movups 0x58(%rsp), %xmm1
movups 0x68(%rsp), %xmm2
movups 0x78(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3e04
testl %eax, %eax
je 0x3d25
movl $0x1d, 0x88(%rbp)
pushq $0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x88(%rsp), %rbp
andq $0x0, (%rbp)
movups 0x48(%rsp), %xmm0
movups 0x58(%rsp), %xmm1
movups 0x68(%rsp), %xmm2
movups 0x78(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x2180(%rip), %rdx      # 0x5ee0
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rcx
callq 0x3e2a
movq (%rbp), %rax
shlq $0x6, %rax
movq 0x18(%r13,%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x20(%r13,%rax), %rbp
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movzwl 0x84(%rsp), %ecx
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %r8
callq 0x3f07
movq %r13, %rdi
callq 0x2040
leaq 0x90(%rsp), %rax
movq 0x8(%rax), %rax
subq %rax, %rbp
cmpq %r15, %rbp
ja 0x3de7
addq 0x40(%rsp), %rbx
addq %rax, %rbx
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x21b0
xorl %eax, %eax
jmp 0x3d13
leaq 0x20f8(%rip), %rdi      # 0x5ee6
leaq 0x2103(%rip), %rsi      # 0x5ef8
leaq 0x2160(%rip), %rcx      # 0x5f5c
pushq $0x7a
popq %rdx
callq 0x2120