// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq %rdx, %rdi
callq 0x10c0
movq %rax, 0x18(%rsp)
movl 0x58(%r15), %esi
imull 0x6c(%r15), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x2422
movl %eax, %r13d
movq %rbx, %rdi
callq 0x10c0
movq %rax, (%rsp)
imull 0x58(%r15), %r13d
movq %rbx, %rdi
movq %r13, %rsi
xorl %r13d, %r13d
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x2422
movl %eax, (%r14)
movq %r15, 0x8(%rsp)
movl 0x58(%r15), %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
addl $-0x4, %r15d
movl %eax, %ecx
cmpq %rcx, %r12
jae 0x283b
testl %r15d, %r15d
jne 0x2817
movq %rbx, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x2422
movl %eax, %r15d
movq %rbx, %rdi
callq 0x10c0
movq %rax, (%rsp)
movq 0x8(%rsp), %r13
imull 0x58(%r13), %r15d
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x10f0
movl 0x58(%r13), %r15d
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x2422
cmpl $-0x1, %eax
cmovel %r13d, %eax
cmpl %eax, %ebp
cmovbel %eax, %ebp
movq 0x8(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movl (%r14), %eax
jmp 0x27c4
movq %rbp, 0x10(%rsp)
shlq $0x2, %rcx
addq 0x8(%r14), %rcx
movq %rcx, 0x10(%r14)
xorl %r12d, %r12d
xorl %ebp, %ebp
movl %eax, %eax
cmpq %rax, %r12
jae 0x28f7
testl %r15d, %r15d
jne 0x28a9
movq %rbx, %rdi
movq (%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x2422
movl %eax, %r15d
movq %rbx, %rdi
callq 0x10c0
movq %rax, (%rsp)
movq %r12, %r13
movq 0x8(%rsp), %r12
imull 0x58(%r12), %r15d
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x10f0
movl 0x58(%r12), %r15d
movq %r13, %r12
movq 0x8(%r14), %rax
movslq %ebp, %r13
movq %r12, 0x20(%rsp)
movl (%rax,%r12,4), %ebp
movl $0xfff, %eax            # imm = 0xFFF
addl %eax, %ebp
shrl $0xc, %ebp
addl %r13d, %ebp
movslq %ebp, %r12
cmpq %r13, %r12
je 0x28e3
movq %rbx, %rdi
callq 0x2422
movq 0x10(%r14), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
jmp 0x28c9
addl $-0x4, %r15d
movq 0x20(%rsp), %r12
incq %r12
movl (%r14), %eax
jmp 0x2851
movq 0x10(%rsp), %rdi
addl $0xfff, %edi            # imm = 0xFFF
andl $0xfffff000, %edi       # imm = 0xFFFFF000
callq 0x10e0
movq %rax, 0x18(%r14)
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq