// File: /workspace/github_c_makefile_star2_O3/shuffle2[P]nx2elf/elf_eh.cpp
// Function: ElfEHInfo::MeasureFrame(eh_frame_hdr const*, unsigned long*, unsigned long*)
bool ElfEHInfo::MeasureFrame(const eh_frame_hdr* hdr,
                             uintptr_t* eh_frame_ptr,
                             size_t* eh_frame_len) {
  if (hdr->version != 1) {
    return false;
  }
#define READ_RAW(type, ptr) \
  *(type*)ptr;              \
  ptr += sizeof(type);
  auto dw_decode = [&hdr](u8 enc, const u8*& buf, uintptr_t base = 0) {
    uintptr_t val = 0;
    if (base == 0) {
      base = (uintptr_t)buf;
    }
    if (enc == DW_EH_PE_omit) {
      // XXX hack for "nonexistant" value
      return (uintptr_t)0;
    }
    switch (enc & 0x70) {
    case DW_EH_PE_absptr:
      break;
    case DW_EH_PE_pcrel:
      val += base;
      break;
    case DW_EH_PE_datarel:
      val += (uintptr_t)hdr;
      break;
    default:
      fprintf(stderr, "unexpected enc base %02x\n", enc);
      break;
    }
    switch (enc & 0x0f) {
    case DW_EH_PE_udata2:
      val += READ_RAW(u16, buf);
      break;
    case DW_EH_PE_udata4:
      val += READ_RAW(u32, buf);
      break;
    case DW_EH_PE_sdata4:
      val += READ_RAW(s32, buf);
      break;
    default:
      fprintf(stderr, "unexpected enc type %02x\n", enc);
      break;
    }
    if (enc & DW_EH_PE_indirect) {
      val = *(uintptr_t*)val;
    }
    return val;
  };
  auto dw_fde_len = [](u32* fde_len, const u8* buf) {
    u32 len = READ_RAW(u32, buf);
    if (len == 0xffffffff) {
      return false;
    }
    *fde_len = len;
    return true;
  };
#undef READ_RAW

  const u8* ptr = (const u8*)&hdr[1];
  *eh_frame_ptr = dw_decode(hdr->eh_frame_ptr_enc, ptr);
  // XXX hack: default to len(eh_frame) == 8 if fde_count == 0
  *eh_frame_len = 8;
  size_t fde_count = dw_decode(hdr->fde_count_enc, ptr);
  uintptr_t max_ptr = 0;
  for (size_t i = 0; i < fde_count; i++) {
    auto func = dw_decode(hdr->table_enc, ptr);
    auto desc = dw_decode(hdr->table_enc, ptr);
    u32 fde_len;
    if (!dw_fde_len(&fde_len, (const u8*)desc)) {
      fprintf(stderr, "reading fde %zi failed\n", i);
      continue;
    }
    max_ptr = std::max(max_ptr, desc + fde_len);
  }
  if (max_ptr) {
    *eh_frame_len = max_ptr - *eh_frame_ptr;
  }
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movb (%rsi), %r13b
cmpb $0x1, %r13b
jne 0xbb11
movq %rcx, %r15
leaq 0x4(%rsi), %r14
movzbl 0x1(%rsi), %r12d
cmpl $0xff, %r12d
movq %rdx, -0x40(%rbp)
movq %rsi, -0x38(%rbp)
je 0xb812
movl %r12d, %ebx
andl $0x70, %ebx
je 0xb819
cmpl $0x30, %ebx
je 0xb816
cmpl $0x10, %ebx
jne 0xbb27
movq %r14, %rbx
jmp 0xb819
xorl %ebx, %ebx
jmp 0xb857
movq %rsi, %rbx
movl %r12d, %eax
andl $0xf, %eax
cmpl $0xb, %eax
je 0xb83f
cmpl $0x3, %eax
je 0xb845
cmpl $0x2, %eax
jne 0xbb51
movzwl 0x4(%rsi), %eax
addq %rax, %rbx
leaq 0x6(%rsi), %r14
jmp 0xb84f
movslq 0x4(%rsi), %rax
jmp 0xb848
movl 0x4(%rsi), %eax
addq %rax, %rbx
leaq 0x8(%rsi), %r14
testb %r12b, %r12b
jns 0xb857
movq (%rbx), %rbx
movq %rbx, (%rdx)
movq $0x8, (%r15)
movzbl 0x2(%rsi), %r12d
cmpl $0xff, %r12d
je 0xbb11
movl %r12d, %ecx
andl $0x70, %ecx
je 0xb891
cmpl $0x30, %ecx
je 0xb88e
cmpl $0x10, %ecx
jne 0xbb79
movq %r14, %rcx
jmp 0xb891
movq %rsi, %rcx
movl %r12d, %eax
andl $0xf, %eax
cmpl $0xb, %eax
je 0xb8b7
cmpl $0x3, %eax
je 0xb8bc
cmpl $0x2, %eax
jne 0xbba3
movzwl (%r14), %eax
addq %rax, %rcx
addq $0x2, %r14
jmp 0xb8c6
movslq (%r14), %rax
jmp 0xb8bf
movl (%r14), %eax
addq %rax, %rcx
addq $0x4, %r14
testb %r12b, %r12b
jns 0xb8ce
movq (%rcx), %rcx
testq %rcx, %rcx
je 0xbb11
movb %r13b, -0x29(%rbp)
movq %r15, -0x50(%rbp)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movabsq $0x1000000010001, %r8   # imm = 0x1000000010001
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %rcx, -0x48(%rbp)
movzbl 0x3(%rsi), %r12d
cmpl $0xff, %r12d
je 0xb96d
movl %r12d, %eax
andl $0x70, %eax
cmpl $0x30, %eax
ja 0xba0f
btq %rax, %r8
jae 0xba0f
movl %r12d, %eax
andl $0xf, %eax
cmpl $0xb, %eax
je 0xb93d
cmpl $0x3, %eax
je 0xb93d
cmpl $0x2, %eax
jne 0xba4a
addq $0x2, %r14
jmp 0xb941
addq $0x4, %r14
movzbl 0x3(%rsi), %r15d
cmpl $0xff, %r15d
je 0xb96d
movl %r15d, %r12d
andl $0x70, %r12d
je 0xb975
cmpl $0x30, %r12d
je 0xb972
cmpl $0x10, %r12d
jne 0xba85
movq %r14, %r12
jmp 0xb975
xorl %r12d, %r12d
jmp 0xb9b3
movq %rsi, %r12
movl %r15d, %eax
andl $0xf, %eax
cmpl $0xb, %eax
je 0xb99b
cmpl $0x3, %eax
je 0xb9a0
cmpl $0x2, %eax
jne 0xbac3
movzwl (%r14), %eax
addq %rax, %r12
addq $0x2, %r14
jmp 0xb9aa
movslq (%r14), %rax
jmp 0xb9a3
movl (%r14), %eax
addq %rax, %r12
addq $0x4, %r14
testb %r15b, %r15b
jns 0xb9b3
movq (%r12), %r12
movl (%r12), %eax
cmpq %rdi, %rax
je 0xb9d7
addq %rax, %r12
cmpq %r12, %rbx
cmovbeq %r12, %rbx
incq %r13
cmpq %r13, %rcx
jne 0xb8f7
jmp 0xbafe
movq 0xd5fa(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x961c(%rip), %rsi      # 0x15004
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
movabsq $0x1000000010001, %r8   # imm = 0x1000000010001
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb9c6
movq 0xd5c2(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x95fc(%rip), %rsi      # 0x1501c
movl %r12d, %edx
xorl %eax, %eax
callq 0x2230
movabsq $0x1000000010001, %r8   # imm = 0x1000000010001
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb91e
movq 0xd587(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x95db(%rip), %rsi      # 0x15036
movl %r12d, %edx
xorl %eax, %eax
callq 0x2230
movabsq $0x1000000010001, %r8   # imm = 0x1000000010001
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb941
movq 0xd54c(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x9586(%rip), %rsi      # 0x1501c
movl %r15d, %edx
xorl %eax, %eax
callq 0x2230
movabsq $0x1000000010001, %r8   # imm = 0x1000000010001
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
xorl %r12d, %r12d
jmp 0xb975
movq 0xd50e(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x9562(%rip), %rsi      # 0x15036
movl %r15d, %edx
xorl %eax, %eax
callq 0x2230
movabsq $0x1000000010001, %r8   # imm = 0x1000000010001
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb9aa
testq %rbx, %rbx
movq -0x50(%rbp), %rax
movb -0x29(%rbp), %r13b
je 0xbb11
subq (%rdx), %rbx
movq %rbx, (%rax)
cmpb $0x1, %r13b
sete %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd4aa(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x94e4(%rip), %rsi      # 0x1501c
xorl %ebx, %ebx
movl %r12d, %edx
xorl %eax, %eax
callq 0x2230
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb819
movq 0xd480(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x94d4(%rip), %rsi      # 0x15036
movl %r12d, %edx
xorl %eax, %eax
callq 0x2230
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb84f
movq 0xd458(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x9492(%rip), %rsi      # 0x1501c
movl %r12d, %edx
xorl %eax, %eax
callq 0x2230
xorl %ecx, %ecx
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb891
movq 0xd42e(%rip), %rax      # 0x18fd8
movq (%rax), %rdi
leaq 0x9482(%rip), %rsi      # 0x15036
movl %r12d, %edx
xorl %eax, %eax
movq %rcx, %rbx
callq 0x2230
movq %rbx, %rcx
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
jmp 0xb8c6
nopl (%rax)