// File: /workspace/github_c_makefile_star2_O2/mikeakohn[P]print_pe/build/../src/resource.c
// Function: parse_resource_dir
int parse_resource_dir(
  FILE *in,
  struct section_header_t *section_header,
  int offset,
  int level,
  int res_type,
  int dump)
{
  struct resource_dir_t resource_dir;
  struct resource_dir_entry_t resource_dir_entry;
  struct resource_data_t resource_data;
  int count,t;
  char filename[256];

  read_resource_dir(in, section_header->PointerToRawData + offset, &resource_dir);
  print_resource_dir(&resource_dir, level);

  offset = offset + 16;

  count = resource_dir.NumberOfNamedEntries + resource_dir.NumberOfIdEntries;

  for (t = 0; t < count; t++)
  {
    read_resource_dir_entry(
      in,
      section_header->PointerToRawData,
      &resource_dir_entry,
      offset);

    print_resource_dir_entry(&resource_dir_entry, level, t);

    if ((resource_dir_entry.OffsetToData & 0x80000000) == 0)
    {
      // High bit 0: Address of a resource data entry (a leaf).
      read_resource_data(
        in,
        section_header->PointerToRawData,
        &resource_data,
        resource_dir_entry.OffsetToData);

      print_resource_data(&resource_data, level);

      sprintf(filename, "res%d.bin", resource_data.OffsetToData);

      if (dump == 1)
      {
        rip_binary(
          in,
          filename,
          section_header->PointerToRawData + (resource_data.OffsetToData - section_header->VirtualAddress),
          resource_data.Size);
      }

      if (res_type == RT_VERSION)
      {
        parse_version_info(
          in,
          section_header->PointerToRawData + (resource_data.OffsetToData - section_header->VirtualAddress),
          resource_data.Size);
      }

      if (level == 0)
      {
        print_resource_type(resource_dir_entry.Name, level);
      }
    }
      else
    {
      // High bit 1: The lower 31 bits are the address of another
      // resource directory table (the next level down).
      if (level == 0)
      {
        print_resource_type(resource_dir_entry.Name, level);

        parse_resource_dir(
          in,
          section_header,
          resource_dir_entry.OffsetToData & 0x7fffffff,
          level + 1,
          resource_dir_entry.Name,
          dump);
      }
        else
      {
        parse_resource_dir(
          in,
          section_header,
          resource_dir_entry.OffsetToData & 0x7fffffff,
          level + 1,
          res_type,
          dump);
      }
    }

    offset = offset + 8;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movl %r9d, 0x8(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, %r14d
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r13
movl 0x18(%rsi), %esi
addl %edx, %esi
leaq 0x28(%rsp), %rbx
movq %rbx, %rdx
callq 0x66bb
movq %rbx, %rdi
movl %r14d, %esi
callq 0x6744
addl $0x10, %r15d
movzwl 0xc(%rbx), %eax
movzwl 0xe(%rbx), %ecx
addl %eax, %ecx
movl %ecx, 0x14(%rsp)
leal 0x1(%r14), %eax
movl %eax, 0x10(%rsp)
xorl %ebx, %ebx
cmpl %ebx, 0x14(%rsp)
je 0x66a7
movl 0x18(%r12), %esi
movq %r13, %rdi
leaq 0x38(%rsp), %rbp
movq %rbp, %rdx
movl %r15d, %ecx
callq 0x6831
movq %rbp, %rdi
movl %r14d, %esi
movl %ebx, %edx
callq 0x68b5
movl 0x3c(%rsp), %ecx
testl %ecx, %ecx
js 0x6650
movl 0x18(%r12), %esi
movq %r13, %rdi
leaq 0x18(%rsp), %rbp
movq %rbp, %rdx
callq 0x697b
movq %rbp, %rdi
movl %r14d, %esi
callq 0x69ed
movl 0x18(%rsp), %ebp
leaq 0x140(%rsp), %rdi
leaq 0x385e(%rip), %rsi      # 0x9e50
movl %ebp, %edx
xorl %eax, %eax
callq 0x3140
cmpl $0x1, 0x8(%rsp)
jne 0x6622
movl 0x18(%r12), %edx
addl %ebp, %edx
subl 0x10(%r12), %edx
movl 0x1c(%rsp), %ecx
movq %r13, %rdi
leaq 0x140(%rsp), %rsi
callq 0x5d40
cmpl $0x10, 0xc(%rsp)
jne 0x663e
addl 0x18(%r12), %ebp
subl 0x10(%r12), %ebp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x5f30
testl %r14d, %r14d
jne 0x669c
movl 0x38(%rsp), %edi
xorl %esi, %esi
callq 0x6aa4
jmp 0x669c
testl %r14d, %r14d
je 0x666e
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r13, %rdi
movq %r12, %rsi
movl %ecx, %edx
movl 0x10(%rsp), %ecx
movl 0xc(%rsp), %r8d
jmp 0x6692
movl 0x38(%rsp), %edi
xorl %esi, %esi
callq 0x6aa4
movl 0x3c(%rsp), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %edx
movl 0x38(%rsp), %r8d
movq %r13, %rdi
movq %r12, %rsi
pushq $0x1
popq %rcx
movl 0x8(%rsp), %r9d
callq 0x6524
addl $0x8, %r15d
incl %ebx
jmp 0x6584
xorl %eax, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq