// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5570
cmpl $0x0, %eax
je 0x506b
movq -0x10(%rbp), %rax
movl $0x1f, 0x88(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x51fc
movzwl -0x36(%rbp), %eax
movl %eax, %edi
movzwl -0x34(%rbp), %eax
imulq %rax, %rdi
callq 0x21b0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x50bd
movq -0x28(%rbp), %rdi
movq -0x78(%rbp), %rsi
movups -0x70(%rbp), %xmm0
movups -0x60(%rbp), %xmm1
movups -0x50(%rbp), %xmm2
movups -0x40(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5210
cmpl $0x0, %eax
je 0x50d7
movq -0x10(%rbp), %rax
movl $0x1d, 0x88(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x51fc
movq $0x0, -0x80(%rbp)
movq -0x28(%rbp), %rdi
movq -0x78(%rbp), %rsi
movups -0x70(%rbp), %xmm0
movups -0x60(%rbp), %xmm1
movups -0x50(%rbp), %xmm2
movups -0x40(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq 0x2eb0(%rip), %rdx      # 0x7fc0
leaq -0x80(%rbp), %rcx
callq 0x5250
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
leaq -0xc0(%rbp), %rdi
movl $0x40, %edx
callq 0x21a0
leaq -0xd8(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq -0x28(%rbp), %rsi
movq -0x78(%rbp), %rdx
movw -0x34(%rbp), %ax
movq -0x30(%rbp), %r8
leaq -0xd8(%rbp), %rdi
movzwl %ax, %ecx
callq 0x53c0
movq -0x78(%rbp), %rdi
callq 0x2040
movq -0xa0(%rbp), %rax
subq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
cmpq -0x20(%rbp), %rax
ja 0x5198
jmp 0x51b7
leaq 0x2e27(%rip), %rdi      # 0x7fc6
leaq 0x2e32(%rip), %rsi      # 0x7fd8
movl $0x7a, %edx
leaq 0x2e8a(%rip), %rcx      # 0x803c
callq 0x2100
movq -0xa8(%rbp), %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x28(%rbp), %rax
addq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movq -0xe0(%rbp), %rdx
callq 0x21a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopl (%rax,%rax)