// File: /workspace/github_c_makefile_star2_O0/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
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
movl $0x101000, %esi         # imm = 0x101000
movb $0x0, %al
callq 0x21e0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x4e1c
movq -0x10(%rbp), %rax
movl $0x1c, 0x88(%rax)
callq 0x2050
movl (%rax), %edi
callq 0x2240
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x5014
movl -0x34(%rbp), %edi
leaq -0x78(%rbp), %rsi
callq 0x5da0
cmpl $0x0, %eax
je 0x4e47
movq -0x10(%rbp), %rax
movl $0x1e, 0x88(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x5014
movzwl -0x3e(%rbp), %eax
movl %eax, %edi
movzwl -0x3c(%rbp), %eax
imulq %rax, %rdi
callq 0x21b0
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
je 0x4e98
movl -0x34(%rbp), %edi
movq -0x80(%rbp), %rsi
movups -0x78(%rbp), %xmm0
movups -0x68(%rbp), %xmm1
movups -0x58(%rbp), %xmm2
movups -0x48(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5910
cmpl $0x0, %eax
je 0x4eb2
movq -0x10(%rbp), %rax
movl $0x1d, 0x88(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x5014
movq $0x0, -0x88(%rbp)
movl -0x34(%rbp), %edi
movq -0x80(%rbp), %rsi
movups -0x78(%rbp), %xmm0
movups -0x68(%rbp), %xmm1
movups -0x58(%rbp), %xmm2
movups -0x48(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq 0x30d3(%rip), %rdx      # 0x7fc0
leaq -0x88(%rbp), %rcx
callq 0x5990
movq -0x80(%rbp), %rsi
movq -0x88(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
leaq -0xc8(%rbp), %rdi
movl $0x40, %edx
callq 0x21a0
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movl -0x34(%rbp), %edi
movq -0x80(%rbp), %rsi
movq -0x30(%rbp), %rdx
movups -0x78(%rbp), %xmm0
movups -0x68(%rbp), %xmm1
movups -0x58(%rbp), %xmm2
movups -0x48(%rbp), %xmm3
movq %rsp, %rax
movups %xmm3, 0x30(%rax)
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0xe0(%rbp), %rcx
callq 0x5b70
movq -0x80(%rbp), %rdi
callq 0x2040
movq -0xa8(%rbp), %rax
subq -0xd8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0x20(%rbp), %rax
ja 0x4f95
jmp 0x4fb4
leaq 0x302a(%rip), %rdi      # 0x7fc6
leaq 0x3035(%rip), %rsi      # 0x7fd8
movl $0x45, %edx
leaq 0x303d(%rip), %rcx      # 0x7fec
callq 0x2100
movq -0xb0(%rbp), %rax
addq -0xd8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl -0x34(%rbp), %edi
movq -0xf0(%rbp), %rsi
xorl %edx, %edx
callq 0x20e0
movl -0x34(%rbp), %edi
movq -0x18(%rbp), %rsi
movq -0xe8(%rbp), %rdx
callq 0x2140
cmpq -0xe8(%rbp), %rax
je 0x500d
movq -0x10(%rbp), %rax
movl $0x1c, 0x88(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x5014
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq