// File: /workspace/github_c_makefile_star2_O1/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, %ebp
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 0x2d9e(%rip), %rdi      # 0x407e
callq 0x1060
leaq 0x2db0(%rip), %rdi      # 0x409c
callq 0x1060
cmpl $0x2, %ebp
jb 0x14b9
movl $0x1, %r13d
movq $0x0, 0x30(%rsp)
movl $0x0, 0x24(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
movl %r13d, %eax
movq (%r14,%rax,8), %r15
cmpb $0x2d, (%r15)
jne 0x1436
movq %r15, %rdi
leaq 0x2ca6(%rip), %rsi      # 0x4004
callq 0x10b0
testl %eax, %eax
je 0x143b
movq %r15, %rdi
leaq 0x2c94(%rip), %rsi      # 0x4009
callq 0x10b0
testl %eax, %eax
je 0x144e
movq %r15, %rdi
leaq 0x2c82(%rip), %rsi      # 0x400e
callq 0x10b0
testl %eax, %eax
je 0x1453
movq %r15, %rdi
leaq 0x2c70(%rip), %rsi      # 0x4013
callq 0x10b0
testl %eax, %eax
je 0x1445
movq %r15, %rdi
leaq 0x2c5e(%rip), %rsi      # 0x4018
callq 0x10b0
testl %eax, %eax
je 0x145c
movq %r15, %rdi
leaq 0x2c4c(%rip), %rsi      # 0x401d
callq 0x10b0
testl %eax, %eax
je 0x1465
movq %r15, %rdi
leaq 0x2c3e(%rip), %rsi      # 0x4026
callq 0x10b0
testl %eax, %eax
je 0x146e
movq %r15, %rdi
leaq 0x2c35(%rip), %rsi      # 0x4030
callq 0x10b0
testl %eax, %eax
jne 0x16d7
leal 0x2(%r13), %r15d
cmpl %ebp, %r15d
jae 0x16f2
incl %r13d
movq (%r14,%r13,8), %rax
movq %rax, 0x30(%rsp)
movl %r15d, %eax
movq (%r14,%rax,8), %rdi
callq 0x1110
movl %eax, 0x24(%rsp)
movl %r15d, %r13d
jmp 0x1475
movq %r15, %rbx
jmp 0x1475
movb $0x1, %r12b
movb $0x1, %al
movq %rax, 0x10(%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
jmp 0x1475
movb $0x1, %r12b
jmp 0x1475
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x1475
movb $0x1, %al
movq %rax, 0x18(%rsp)
jmp 0x1475
movb $0x1, %al
movq %rax, 0x38(%rsp)
jmp 0x1475
movb $0x1, %al
movq %rax, 0x28(%rsp)
incl %r13d
cmpl %ebp, %r13d
jb 0x1343
testb %r12b, %r12b
sete %r13b
cmpb $0x0, 0x10(%rsp)
sete %r12b
cmpb $0x0, 0x8(%rsp)
sete %bpl
cmpb $0x0, 0x18(%rsp)
sete 0x8(%rsp)
cmpb $0x0, 0x38(%rsp)
sete 0x10(%rsp)
movzbl 0x24(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x14e8
xorl %ebx, %ebx
movb $0x1, %r13b
movb $0x1, %r12b
movb $0x1, %bpl
movb $0x1, 0x8(%rsp)
movb $0x1, 0x10(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x30(%rsp)
testq %rbx, %rbx
je 0x1659
leaq 0x2b58(%rip), %rsi      # 0x4050
movq %rbx, %rdi
callq 0x1100
testq %rax, %rax
je 0x16b4
movq %rax, %r15
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x1ed8
testl %eax, %eax
jne 0x16c1
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0x22f7
movl 0x64(%rbx), %edi
callq 0x10e0
leaq 0x40(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1f97
testb %r13b, %r13b
jne 0x1559
leaq 0x40(%rsp), %rdi
callq 0x23bc
leaq 0x40(%rsp), %rbx
leaq 0x70(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x249d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x21b1
testb %r12b, %r12b
jne 0x159b
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %r15, %rdx
movl $0x2, %ecx
callq 0x26d6
testb %bpl, %bpl
jne 0x15b7
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %r15, %rdx
movl $0x4, %ecx
callq 0x26d6
cmpb $0x0, 0x8(%rsp)
jne 0x15d8
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %r15, %rcx
callq 0x2ba6
cmpb $0x0, 0x10(%rsp)
jne 0x15ff
movzbl 0x28(%rsp), %r8d
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %r15, %rcx
callq 0x34ee
movq 0x30(%rsp), %r8
testq %r8, %r8
je 0x161f
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %r15, %rdx
movl 0x18(%rsp), %ecx
callq 0x3902
movq 0x48(%rsp), %rdi
callq 0x1030
movq 0x58(%rsp), %rdi
callq 0x1030
movq 0x60(%rsp), %rdi
callq 0x1030
movq %r15, %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 0x2a9a(%rip), %rdi      # 0x40fa
callq 0x1060
leaq 0x2ab5(%rip), %rdi      # 0x4121
callq 0x1060
leaq 0x2ad7(%rip), %rdi      # 0x414f
callq 0x1060
leaq 0x2afe(%rip), %rdi      # 0x4182
callq 0x1060
leaq 0x2b1f(%rip), %rdi      # 0x41af
callq 0x1060
leaq 0x2b40(%rip), %rdi      # 0x41dc
callq 0x1060
leaq 0x2b5e(%rip), %rdi      # 0x4206
callq 0x1060
xorl %edi, %edi
callq 0x1130
movq 0x8(%r14), %rsi
leaq 0x2994(%rip), %rdi      # 0x4053
jmp 0x16e1
leaq 0x2a0d(%rip), %rdi      # 0x40d5
callq 0x1060
movq %r15, %rdi
callq 0x1080
jmp 0x16e8
leaq 0x2958(%rip), %rdi      # 0x4036
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movl $0x1, %edi
callq 0x1130
leaq 0x2b4b(%rip), %rdi      # 0x4244
callq 0x1060
jmp 0x16e8