// File: /workspace/github_c_makefile_star2_O2/mikeakohn[P]print_pe/build/../src/print_pdb.c
// Function: main
int main(int argc, char *argv[])
{
  FILE *in;
  const char *filename = NULL;
  struct pdb_header_t pdb_header;
  struct pdb_dir_t pdb_dir;
  struct pdb_dbi_t pdb_dbi;
  uint32_t n;
  uint8_t show_dir = 0;
  uint8_t show_tpi = 0;
  uint8_t show_ipi = 0;
  uint8_t show_dbi = 0;
  uint8_t show_symbols = 0;
  uint8_t demangle_symbols = 0;
  uint8_t dump_index = 0;
  char *dump_filename = NULL;

  memset(&pdb_header, 0, sizeof(pdb_header));
  memset(&pdb_dir, 0, sizeof(pdb_dir));
  memset(&pdb_dbi, 0, sizeof(pdb_dbi));

  printf("\nprint_pdb (January 17, 2022)\n");
  printf("Copyright 2022 - Michael Kohn  http://www.mikekohn.net/\n\n");

  for (n = 1; n < argc; n++)
  {
    if (argv[n][0] == '-')
    {
      if (strcmp(argv[n], "-all") == 0)
      {
        show_dir = 1;
        show_tpi = 1;
        show_ipi = 1;
      }
        else
      if (strcmp(argv[n], "-dir") == 0)
      {
        show_dir = 1;
      }
        else
      if (strcmp(argv[n], "-tpi") == 0)
      {
        show_tpi = 1;
      }
        else
      if (strcmp(argv[n], "-ipi") == 0)
      {
        show_ipi = 1;
      }
        else
      if (strcmp(argv[n], "-dbi") == 0)
      {
        show_dbi = 1;
      }
        else
      if (strcmp(argv[n], "-symbols") == 0)
      {
        show_symbols = 1;
      }
        else
      if (strcmp(argv[n], "-demangle") == 0)
      {
        demangle_symbols = 1;
      }
        else
      if (strcmp(argv[n], "-dump") == 0)
      {
        if (n + 2 >= argc)
        {
          printf("Error: -dump requires a filename and index\n");
          exit(1);
        }

        dump_filename = argv[++n];
        dump_index = atoi(argv[++n]);
      }
        else
      {
        printf("Error: Unknown option %s\n", argv[n]);
        exit(1);
      }
    }
      else
    {
      filename = argv[n];
    }
  }

  if (filename == NULL)
  {
    printf("Usage: print_pe [options] <input file>\n");
    printf("  -all                     (Show all streams)\n");
    printf("  -dir                     (Show directory stream)\n");
    printf("  -tpi                     (Show TPI stream)\n");
    printf("  -ipi                     (Show IPI stream)\n");
    printf("  -symbols                 (Show symbols)\n");
    printf("  -dump <filename> <index> (Dump stream at index to filename)\n");
    exit(0);
  }

  in = fopen(filename, "rb");

  if (in == NULL)
  {
    printf("Could not open input file %s for reading.\n", argv[1]);
    exit(1);
  }

  if (read_pdb_header(&pdb_header, in) != 0)
  {
    printf("Error: Not recognized as a PDB file.\n");
    fclose(in);
    exit(1);
  }

  print_pdb_header(&pdb_header);

  pdb_dir.stream_sizes = (uint32_t *)malloc(pdb_header.number_of_dir_bytes);

  read_pdb_dir(&pdb_dir, &pdb_header, in);

  if (show_dir != 0)
  {
    print_pdb_dir(&pdb_dir);
  }

  print_pdb_stream_info(&pdb_dir, &pdb_header, in);
  read_pdb_names(&pdb_dir, &pdb_header, in);

  if (show_tpi != 0)
  {
    print_pdb_tpi_stream(&pdb_dir, &pdb_header, in, 2);
  }

  if (show_ipi != 0)
  {
    print_pdb_tpi_stream(&pdb_dir, &pdb_header, in, 4);
  }

  if (show_dbi != 0)
  {
    print_pdb_dbi_stream(&pdb_dir, &pdb_header, &pdb_dbi, in);
  }

  if (show_symbols != 0)
  {
    print_pdb_symbols(&pdb_dir, &pdb_header, &pdb_dbi, in, demangle_symbols);
  }

  if (dump_filename != NULL)
  {
    dump_pdb_stream(&pdb_dir, &pdb_header, in, dump_index, dump_filename);
  }

  free(pdb_dir.stream_sizes);
  free(pdb_dir.heap);
  free(pdb_dir.names);
  fclose(in);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %r14
movl %edi, %r15d
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0x110(%rsp)
leaq 0x2d9d(%rip), %rdi      # 0x407e
callq 0x1060
leaq 0x2daf(%rip), %rdi      # 0x409c
callq 0x1060
pushq $0x1
popq %rbp
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x4(%rsp)
movq $0x0, 0x30(%rsp)
cmpl %r15d, %ebp
jae 0x1482
movl %ebp, %r13d
movq (%r14,%r13,8), %r12
cmpb $0x2d, (%r12)
jne 0x1434
movq %r12, %rdi
leaq 0x2ca1(%rip), %rsi      # 0x4004
callq 0x10b0
testl %eax, %eax
je 0x1439
movq %r12, %rdi
leaq 0x2c8f(%rip), %rsi      # 0x4009
callq 0x10b0
testl %eax, %eax
je 0x1450
movq %r12, %rdi
leaq 0x2c7d(%rip), %rsi      # 0x400e
callq 0x10b0
testl %eax, %eax
je 0x1459
movq %r12, %rdi
leaq 0x2c6b(%rip), %rsi      # 0x4013
callq 0x10b0
testl %eax, %eax
je 0x1447
movq %r12, %rdi
leaq 0x2c59(%rip), %rsi      # 0x4018
callq 0x10b0
testl %eax, %eax
je 0x1462
movq %r12, %rdi
leaq 0x2c47(%rip), %rsi      # 0x401d
callq 0x10b0
testl %eax, %eax
je 0x146b
movq %r12, %rdi
leaq 0x2c39(%rip), %rsi      # 0x4026
callq 0x10b0
testl %eax, %eax
je 0x1474
movq %r12, %rdi
leaq 0x2c30(%rip), %rsi      # 0x4030
callq 0x10b0
testl %eax, %eax
jne 0x1678
addl $0x2, %ebp
cmpl %r15d, %ebp
jae 0x168b
movq 0x8(%r14,%r13,8), %rax
movq %rax, 0x30(%rsp)
movl %ebp, %eax
movq (%r14,%rax,8), %rdi
callq 0x1110
movl %eax, 0x4(%rsp)
jmp 0x147b
movq %r12, %rbx
jmp 0x147b
movb $0x1, %al
movq %rax, 0x18(%rsp)
movb $0x1, %al
movq %rax, 0x10(%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
jmp 0x147b
movb $0x1, %al
movq %rax, 0x18(%rsp)
jmp 0x147b
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x147b
movb $0x1, %al
movq %rax, 0x38(%rsp)
jmp 0x147b
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x147b
movb $0x1, %al
movq %rax, 0x20(%rsp)
incl %ebp
jmp 0x133e
testq %rbx, %rbx
je 0x15fa
leaq 0x2bbe(%rip), %rsi      # 0x4050
movq %rbx, %rdi
callq 0x1100
testq %rax, %rax
je 0x1655
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x1dc8
testl %eax, %eax
jne 0x1662
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x21a3
movl 0x64(%r14), %edi
callq 0x10e0
leaq 0x40(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1e7c
cmpb $0x0, 0x18(%rsp)
je 0x14f6
leaq 0x40(%rsp), %rdi
callq 0x2266
leaq 0x40(%rsp), %r14
leaq 0x70(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x231b
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2065
cmpb $0x0, 0x10(%rsp)
je 0x1538
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
pushq $0x2
popq %rcx
movq %rbx, %rdx
callq 0x24ca
cmpb $0x0, 0x8(%rsp)
movq 0x30(%rsp), %r14
je 0x1559
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
pushq $0x4
popq %rcx
movq %rbx, %rdx
callq 0x24ca
cmpb $0x0, 0x38(%rsp)
je 0x157a
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %rbx, %rcx
callq 0x2993
cmpb $0x0, 0x28(%rsp)
je 0x15a1
movzbl 0x20(%rsp), %r8d
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %rbx, %rcx
callq 0x3188
testq %r14, %r14
je 0x15c0
movzbl 0x4(%rsp), %ecx
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %r8
callq 0x358a
movq 0x48(%rsp), %rdi
callq 0x1030
movq 0x58(%rsp), %rdi
callq 0x1030
movq 0x60(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1080
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2af9(%rip), %rdi      # 0x40fa
callq 0x1060
leaq 0x2b14(%rip), %rdi      # 0x4121
callq 0x1060
leaq 0x2b36(%rip), %rdi      # 0x414f
callq 0x1060
leaq 0x2b5d(%rip), %rdi      # 0x4182
callq 0x1060
leaq 0x2b7e(%rip), %rdi      # 0x41af
callq 0x1060
leaq 0x2b9f(%rip), %rdi      # 0x41dc
callq 0x1060
leaq 0x2bbd(%rip), %rdi      # 0x4206
callq 0x1060
xorl %edi, %edi
callq 0x1130
movq 0x8(%r14), %rsi
leaq 0x29f3(%rip), %rdi      # 0x4053
jmp 0x1682
leaq 0x2a6c(%rip), %rdi      # 0x40d5
callq 0x1060
movq %rbx, %rdi
callq 0x1080
jmp 0x1697
leaq 0x29b7(%rip), %rdi      # 0x4036
movq %r12, %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x1697
leaq 0x2bb2(%rip), %rdi      # 0x4244
callq 0x1060
pushq $0x1
popq %rdi
callq 0x1130
nop