// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rdi, %r14
movq %rdx, %rdi
callq 0x10c0
movq %rax, 0x20(%rsp)
movl 0x58(%r15), %esi
imull 0x6c(%r15), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24ca
movl %eax, %ebp
movq %rbx, %rdi
callq 0x10c0
movq %rax, %r12
imull 0x58(%r15), %ebp
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24ca
movl %eax, (%r14)
testl %eax, %eax
je 0x2a15
movq %r12, 0x10(%rsp)
movl 0x58(%r15), %ebp
addl $-0x4, %ebp
xorl %r12d, %r12d
xorl %r13d, %r13d
testl %ebp, %ebp
jne 0x28f9
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24ca
movl %eax, %ebp
movq %rbx, %rdi
callq 0x10c0
movq %rax, 0x10(%rsp)
imull 0x58(%r15), %ebp
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x10f0
movl 0x58(%r15), %ebp
movq %rbx, %rdi
callq 0x24ca
movq 0x8(%r14), %rcx
cmpl $-0x1, %eax
movl $0x0, %edx
cmovel %edx, %eax
movl %eax, (%rcx,%r12,4)
cmpl %eax, %r13d
cmovbel %eax, %r13d
incq %r12
addl $-0x4, %ebp
movl (%r14), %eax
cmpq %rax, %r12
jb 0x28b9
addl $0xfff, %r13d           # imm = 0xFFF
andl $0xfffff000, %r13d      # imm = 0xFFFFF000
leaq (,%rax,4), %rcx
addq 0x8(%r14), %rcx
movq %rcx, 0x10(%r14)
testq %rax, %rax
je 0x2a20
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
movl %r13d, 0x1c(%rsp)
movq %r15, 0x28(%rsp)
testl %ebp, %ebp
jne 0x29a5
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x10f0
movq %rbx, %rdi
callq 0x24ca
movl %eax, %ebp
movq %rbx, %rdi
callq 0x10c0
movq %rax, 0x10(%rsp)
imull 0x58(%r15), %ebp
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x10f0
movl 0x58(%r15), %ebp
movq 0x8(%r14), %rax
movl (%rax,%r12,4), %r13d
movl $0xfff, %eax            # imm = 0xFFF
addl %eax, %r13d
cmpl $0x1000, %r13d          # imm = 0x1000
jb 0x29fc
movq %r12, 0x30(%rsp)
shrl $0xc, %r13d
movslq 0xc(%rsp), %r15
negl %r13d
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x24ca
movq 0x10(%r14), %rcx
movl %eax, (%rcx,%r15,4)
incq %r15
decl %r12d
cmpl %r12d, %r13d
jne 0x29d2
subl %r12d, 0xc(%rsp)
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r12
addl $-0x4, %ebp
incq %r12
movl (%r14), %eax
cmpq %rax, %r12
movl 0x1c(%rsp), %r13d
jb 0x2965
jmp 0x2a20
movq 0x8(%r14), %rax
movq %rax, 0x10(%r14)
xorl %r13d, %r13d
movl %r13d, %edi
callq 0x10e0
movq %rax, 0x18(%r14)
movq %rbx, %rdi
movq 0x20(%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