// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %esi
addl -0x14(%rbp), %esi
leaq -0x30(%rbp), %rdx
callq 0x7dc0
movl -0x18(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x7e80
movl -0x14(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x14(%rbp)
movzwl -0x24(%rbp), %eax
movzwl -0x22(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x14c(%rbp)
movl $0x0, -0x150(%rbp)
movl -0x150(%rbp), %eax
cmpl -0x14c(%rbp), %eax
jge 0x7db0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %esi
movl -0x14(%rbp), %ecx
leaq -0x138(%rbp), %rdx
callq 0x7fa0
movl -0x18(%rbp), %esi
movl -0x150(%rbp), %edx
leaq -0x138(%rbp), %rdi
callq 0x8060
movl -0x134(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
jne 0x7d2a
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %esi
movl -0x134(%rbp), %ecx
leaq -0x148(%rbp), %rdx
callq 0x8170
movl -0x18(%rbp), %esi
leaq -0x148(%rbp), %rdi
callq 0x8210
leaq -0x250(%rbp), %rdi
movl -0x148(%rbp), %edx
leaq 0x4144(%rip), %rsi      # 0xbdf0
movb $0x0, %al
callq 0x3130
cmpl $0x1, -0x20(%rbp)
jne 0x7ce5
movq -0x8(%rbp), %rdi
leaq -0x250(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %edx
movl -0x148(%rbp), %eax
movq -0x10(%rbp), %rcx
subl 0x10(%rcx), %eax
addl %eax, %edx
movl -0x144(%rbp), %ecx
callq 0x7250
cmpl $0x10, -0x1c(%rbp)
jne 0x7d14
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl -0x148(%rbp), %ecx
movq -0x10(%rbp), %rdx
subl 0x10(%rdx), %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
movl -0x144(%rbp), %edx
callq 0x74b0
cmpl $0x0, -0x18(%rbp)
jne 0x7d28
movl -0x138(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x82e0
jmp 0x7d93
cmpl $0x0, -0x18(%rbp)
jne 0x7d6a
movl -0x138(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0x82e0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x134(%rbp), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl -0x138(%rbp), %r8d
movl -0x20(%rbp), %r9d
callq 0x7bb0
jmp 0x7d91
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x134(%rbp), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl -0x18(%rbp), %ecx
addl $0x1, %ecx
movl -0x1c(%rbp), %r8d
movl -0x20(%rbp), %r9d
callq 0x7bb0
jmp 0x7d93
movl -0x14(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x14(%rbp)
movl -0x150(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x150(%rbp)
jmp 0x7c17
xorl %eax, %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
nopl (%rax,%rax)