// File: /workspace/github_c_makefile_star2_O3/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 $0xb8, %rsp
movq %r8, %r12
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r13
leaq 0x60(%rsp), %rsi
movq %rcx, %rdi
callq 0x434d
testl %eax, %eax
je 0x3fbc
movl $0x1f, 0x88(%r13)
jmp 0x4024
movzwl 0x9a(%rsp), %eax
movzwl 0x9c(%rsp), %edi
imulq %rax, %rdi
callq 0x21c0
testq %rax, %rax
je 0x4019
movq %rax, %rbp
movups 0x60(%rsp), %xmm0
movups 0x70(%rsp), %xmm1
movups 0x80(%rsp), %xmm2
movups 0x90(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x412c
testl %eax, %eax
je 0x403f
movl $0x1d, 0x88(%r13)
movl $0x1, %r13d
movl %r13d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
leaq 0x58(%rsp), %rax
movq %r13, (%rax)
movq %rax, %rcx
movups 0x60(%rsp), %xmm0
movups 0x70(%rsp), %xmm1
movups 0x80(%rsp), %xmm2
movups 0x90(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x1e97(%rip), %rdx      # 0x5f18
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4152
leaq 0x58(%rsp), %rax
movq (%rax), %rax
shlq $0x6, %rax
movq 0x18(%rbp,%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0x20(%rbp,%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r13, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movzwl 0x9c(%rsp), %ecx
movq %rbx, %rsi
movq %rbp, %rdx
movq %r12, %r8
callq 0x41ec
movq %rbp, %rdi
callq 0x2040
movq 0x50(%rsp), %rdx
leaq 0xa0(%rsp), %rax
movq 0x8(%rax), %rax
subq %rax, %rdx
cmpq %r15, %rdx
ja 0x410d
addq 0x48(%rsp), %rbx
addq %rax, %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21b0
jmp 0x402a
leaq 0x1e0a(%rip), %rdi      # 0x5f1e
leaq 0x1e15(%rip), %rsi      # 0x5f30
leaq 0x1e72(%rip), %rcx      # 0x5f94
movl $0x7a, %edx
callq 0x2120