// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
movq %rdx, %rdi
callq 0x10c0
movq %rax, 0x28(%rsp)
movl 0x58(%r12), %esi
imull 0x6c(%r12), %esi
movq $0x0, 0x18(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24de
movl %eax, %r15d
movq %rbx, %rdi
callq 0x10c0
movq %rax, 0x10(%rsp)
imull 0x58(%r12), %r15d
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24de
movl %eax, (%r14)
movq %r12, 0x20(%rsp)
movl 0x58(%r12), %r15d
addl $-0x4, %r15d
testl %eax, %eax
je 0x296a
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x20(%rsp), %rbp
testl %r15d, %r15d
jne 0x2926
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24de
movl %eax, %r15d
movq %rbx, %rdi
callq 0x10c0
movq %rax, 0x10(%rsp)
imull 0x58(%rbp), %r15d
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x10f0
movl 0x58(%rbp), %r15d
movq %rbx, %rdi
callq 0x24de
movq 0x8(%r14), %rcx
cmpl $-0x1, %eax
movl $0x0, %edx
cmovel %edx, %eax
movl %eax, (%rcx,%r13,4)
cmpl %eax, %r12d
cmovbel %eax, %r12d
incq %r13
addl $-0x4, %r15d
movl (%r14), %eax
cmpq %rax, %r13
jb 0x28e4
addl $0xfff, %r12d           # imm = 0xFFF
andl $0xfffff000, %r12d      # imm = 0xFFFFF000
movq %r12, 0x18(%rsp)
movl %eax, %eax
shlq $0x2, %rax
addq 0x8(%r14), %rax
movq %rax, 0x10(%r14)
cmpl $0x0, (%r14)
je 0x2a37
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
testl %r15d, %r15d
jne 0x29d6
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24de
movl %eax, %r15d
movq %rbx, %rdi
callq 0x10c0
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %r12
imull 0x58(%r12), %r15d
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x10f0
movl 0x58(%r12), %r15d
movq 0x8(%r14), %rax
movl (%rax,%r13,4), %ebp
movl $0xfff, %eax            # imm = 0xFFF
addl %eax, %ebp
cmpl $0x1000, %ebp           # imm = 0x1000
jb 0x2a24
movq %r13, 0x30(%rsp)
shrl $0xc, %ebp
movslq 0xc(%rsp), %r12
negl %ebp
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x24de
movq 0x10(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
decl %r13d
cmpl %r13d, %ebp
jne 0x29ff
subl %r13d, 0xc(%rsp)
movq 0x30(%rsp), %r13
addl $-0x4, %r15d
incq %r13
movl (%r14), %eax
cmpq %rax, %r13
jb 0x298d
movq 0x18(%rsp), %rdi
callq 0x10e0
movq %rax, 0x18(%r14)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq