// File: /workspace/github_c_makefile_star2_O1/0xADE1A1DE[P]MeasureSuite/lib/src/io/elf_parser.c
// Function: elf_load_symbol
int elf_load_symbol(measuresuite_t ms, void *dest, size_t dest_size,
                    const char *filename, const char *symbol) {

  // open file
  int file = open(filename, O_RDONLY | O_SYNC);
  if (file < 0) {
    ms->errorno = E_LOAD__ELF_FILE_IO;
    ms->additional_info = strerror(errno);
    return 1;
  }

  Elf64_Ehdr eh64; /* elf-header is fixed size */
  /* ELF header : at start of file */
  if (read_elf_header(file, &eh64)) {
    ms->errorno = E_LOAD__ELF_FILE_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(file, eh64, sh_tbl)) {
    ms->errorno = E_LOAD__ELF_FILE_MALLOC;
    return 1;
  }

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

  Elf64_Sym sym = {0};
  find_symbol_in_table(file, eh64, sh_tbl, symbol, &sym);
  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);

  // then position in file is
  unsigned long offset_in_file = text.sh_offset + sym.st_value;

  // read code into *dest
  lseek(file, (long)offset_in_file, SEEK_SET);
  if ((unsigned long)read(file, dest, size) != size) {
    ms->errorno = E_LOAD__ELF_FILE_IO;
    return 1;
  };

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %r12
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbx
movq %rcx, %rdi
movl $0x101000, %esi         # imm = 0x101000
xorl %eax, %eax
callq 0x21f0
testl %eax, %eax
js 0x3dbd
movl %eax, %ebp
leaq 0x60(%rsp), %rsi
movl %eax, %edi
callq 0x4902
testl %eax, %eax
je 0x3ddc
movl $0x1e, 0x88(%rbx)
jmp 0x3e42
movl $0x1c, 0x88(%rbx)
callq 0x2060
movl (%rax), %edi
callq 0x2260
movq %rax, 0x90(%rbx)
jmp 0x3e42
movzwl 0x9a(%rsp), %eax
movzwl 0x9c(%rsp), %edi
imulq %rax, %rdi
callq 0x21c0
testq %rax, %rax
je 0x3e38
movq %rax, %r14
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)
movl %ebp, %edi
movq %rax, %rsi
callq 0x45ec
testl %eax, %eax
je 0x3e5d
movl $0x1d, 0x88(%rbx)
movl $0x1, %r13d
movl %r13d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x48(%rsp)
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 0x2044(%rip), %rdx      # 0x5ee8
movl %ebp, %edi
movq %r14, %rsi
callq 0x4640
leaq 0x58(%rsp), %rax
movq (%rax), %rax
shlq $0x6, %rax
movq 0x18(%r14,%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x20(%r14,%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0xa0(%rsp), %rax
movq %r13, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
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)
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %rax, %r12
movq %rax, %rcx
callq 0x475a
movq %r14, %rdi
movq 0x50(%rsp), %r14
callq 0x2040
movq 0x8(%r12), %rsi
subq %rsi, %r14
cmpq %r15, %r14
ja 0x3f6f
addq 0x40(%rsp), %rsi
movl %ebp, %edi
xorl %edx, %edx
callq 0x2100
movl %ebp, %edi
movq 0x48(%rsp), %rsi
movq %r14, %rdx
callq 0x2150
cmpq %r14, %rax
je 0x3e48
movl $0x1c, 0x88(%rbx)
jmp 0x3e42
leaq 0x1f78(%rip), %rdi      # 0x5eee
leaq 0x1f83(%rip), %rsi      # 0x5f00
leaq 0x1f90(%rip), %rcx      # 0x5f14
movl $0x45, %edx
callq 0x2120