// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %r8, %r12
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rcx, %rdi
movl $0x101000, %esi         # imm = 0x101000
xorl %eax, %eax
callq 0x21f0
testl %eax, %eax
js 0x3ab9
movl %eax, %ebp
leaq 0x50(%rsp), %rsi
movl %eax, %edi
callq 0x45b6
testl %eax, %eax
je 0x3ad8
movl $0x1e, 0x88(%rbx)
jmp 0x3b3b
movl $0x1c, 0x88(%rbx)
callq 0x2060
movl (%rax), %edi
callq 0x2260
movq %rax, 0x90(%rbx)
jmp 0x3b3b
movzwl 0x8a(%rsp), %eax
movzwl 0x8c(%rsp), %edi
imulq %rax, %rdi
callq 0x21c0
testq %rax, %rax
je 0x3b31
movq %rax, %r13
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movups 0x70(%rsp), %xmm2
movups 0x80(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl %ebp, %edi
movq %rax, %rsi
callq 0x42b0
testl %eax, %eax
je 0x3b54
movl $0x1d, 0x88(%rbx)
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x48(%rsp), %rax
andq $0x0, (%rax)
movq %rax, %rcx
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movups 0x70(%rsp), %xmm2
movups 0x80(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x234f(%rip), %rdx      # 0x5ee0
movl %ebp, %edi
movq %r13, %rsi
callq 0x4301
leaq 0x48(%rsp), %rax
movq (%rax), %rax
shlq $0x6, %rax
movq 0x18(%r13,%rax), %rcx
movq %rcx, 0x40(%rsp)
movq 0x20(%r13,%rax), %rax
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rcx
movaps %xmm0, (%rcx)
andq $0x0, 0x10(%rcx)
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movups 0x70(%rsp), %xmm2
movups 0x80(%rsp), %xmm3
movups %xmm3, 0x30(%rsp)
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
movq %rax, %r12
callq 0x441a
movq %r13, %rdi
callq 0x2040
leaq 0x90(%rsp), %rax
movq 0x8(%rax), %rsi
subq %rsi, %r12
cmpq %r15, %r12
ja 0x3c55
addq 0x40(%rsp), %rsi
xorl %r15d, %r15d
movl %ebp, %edi
xorl %edx, %edx
callq 0x2100
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2150
cmpq %r12, %rax
je 0x3b3f
movl $0x1c, 0x88(%rbx)
jmp 0x3b3b
leaq 0x228a(%rip), %rdi      # 0x5ee6
leaq 0x2295(%rip), %rsi      # 0x5ef8
leaq 0x22a2(%rip), %rcx      # 0x5f0c
pushq $0x45
popq %rdx
callq 0x2120