// File: /workspace/github_c_makefile_star2_O0/mikeakohn[P]print_pe/build/../src/pdb.c
// Function: read_pdb_dir
int read_pdb_dir(
  struct pdb_dir_t *pdb_dir,
  struct pdb_header_t *pdb_header,
  FILE *in)
{
  long marker = ftell(in);
  long block_marker;
  uint32_t block_bytes;
  uint32_t block_address;
  uint32_t n;

  fseek(in, pdb_header->block_map_address * pdb_header->block_size, SEEK_SET);

  block_address = read_uint32(in);
  block_marker = ftell(in);
  fseek(in, block_address * pdb_header->block_size, SEEK_SET);

  pdb_dir->stream_count = read_uint32(in);

  block_bytes = pdb_header->block_size - 4;

  uint32_t heap_length = 0;

  for (n = 0; n < pdb_dir->stream_count; n++)
  {
    if (block_bytes == 0)
    {
      fseek(in, block_marker, SEEK_SET);
      block_address = read_uint32(in);
      block_marker = ftell(in);
      fseek(in, block_address * pdb_header->block_size, SEEK_SET);
      block_bytes = pdb_header->block_size;
    }

    pdb_dir->stream_sizes[n] = read_uint32(in);

    // FIXME: For some reason a PDB used for testing this is reading in
    // a stream size of -1.
    if (pdb_dir->stream_sizes[n] == 0xffffffff)
    {
      pdb_dir->stream_sizes[n] = 0;
    }

    if (heap_length < pdb_dir->stream_sizes[n])
    {
      heap_length = pdb_dir->stream_sizes[n];
    }

    block_bytes -= 4;
  }

  pdb_dir->stream_blocks = pdb_dir->stream_sizes + pdb_dir->stream_count;

  int ptr = 0, i;

  for (n = 0; n < pdb_dir->stream_count; n++)
  {
    if (block_bytes == 0)
    {
      fseek(in, block_marker, SEEK_SET);
      block_address = read_uint32(in);
      block_marker = ftell(in);
      fseek(in, block_address * pdb_header->block_size, SEEK_SET);
      block_bytes = pdb_header->block_size;
    }

    int count = calc_block_count(pdb_dir->stream_sizes[n]);

    for (i = 0; i < count; i++)
    {
      pdb_dir->stream_blocks[ptr++] = read_uint32(in);
    }

    block_bytes -= 4;
  }

  heap_length = (heap_length + 4095) & 0xfffff000;
  pdb_dir->heap = (uint8_t *)malloc(heap_length);

  fseek(in, marker, SEEK_SET);

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10a0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x6c(%rax), %eax
movq -0x10(%rbp), %rcx
imull 0x58(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movq -0x18(%rbp), %rdi
callq 0x2df0
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10a0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
imull 0x58(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movq -0x18(%rbp), %rdi
callq 0x2df0
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
subl $0x4, %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x34a6
cmpl $0x0, -0x2c(%rbp)
jne 0x342f
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x10d0
movq -0x18(%rbp), %rdi
callq 0x2df0
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10a0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
imull 0x58(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2df0
movl %eax, %edx
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x346b
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x34(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
jae 0x348f
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x33ce
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x8(%rbp), %rax
movl (%rax), %eax
shlq $0x2, %rax
addq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x359d
cmpl $0x0, -0x2c(%rbp)
jne 0x3532
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x10d0
movq -0x18(%rbp), %rdi
callq 0x2df0
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x10a0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
imull 0x58(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10d0
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %edi
callq 0x35e0
movl %eax, -0x44(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x3586
movq -0x18(%rbp), %rdi
callq 0x2df0
movl %eax, %edx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x354f
movl -0x2c(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x34d1
movl -0x38(%rbp), %eax
addl $0xfff, %eax            # imm = 0xFFF
andl $0xfffff000, %eax       # imm = 0xFFFFF000
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, %edi
callq 0x10c0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x10d0
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)