// File: /workspace/github_c_makefile_star2_O3/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
cmpl $0x2, %r15d
jb 0x160b
movl $0x1, %r13d
movq $0x0, 0x30(%rsp)
movl $0x0, 0x4(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
xorl %ebp, %ebp
movl %r13d, %r12d
movq (%r14,%r12,8), %rbx
cmpb $0x2d, (%rbx)
jne 0x143c
movq %rbx, %rdi
leaq 0x2c9f(%rip), %rsi      # 0x4004
callq 0x10b0
testl %eax, %eax
je 0x1441
movq %rbx, %rdi
leaq 0x2c8d(%rip), %rsi      # 0x4009
callq 0x10b0
testl %eax, %eax
je 0x1458
movq %rbx, %rdi
leaq 0x2c7b(%rip), %rsi      # 0x400e
callq 0x10b0
testl %eax, %eax
je 0x1461
movq %rbx, %rdi
leaq 0x2c69(%rip), %rsi      # 0x4013
callq 0x10b0
testl %eax, %eax
je 0x144f
movq %rbx, %rdi
leaq 0x2c57(%rip), %rsi      # 0x4018
callq 0x10b0
testl %eax, %eax
je 0x146a
movq %rbx, %rdi
leaq 0x2c45(%rip), %rsi      # 0x401d
callq 0x10b0
testl %eax, %eax
je 0x1473
movq %rbx, %rdi
leaq 0x2c37(%rip), %rsi      # 0x4026
callq 0x10b0
testl %eax, %eax
je 0x147c
movq %rbx, %rdi
leaq 0x2c2a(%rip), %rsi      # 0x4030
callq 0x10b0
testl %eax, %eax
jne 0x1689
addl $0x2, %r13d
cmpl %r15d, %r13d
jae 0x169c
movq 0x8(%r14,%r12,8), %rax
movq %rax, 0x30(%rsp)
movl %r13d, %eax
movq (%r14,%rax,8), %rdi
callq 0x1110
movl %eax, 0x4(%rsp)
jmp 0x1483
movq %rbx, %rbp
jmp 0x1483
movb $0x1, %al
movq %rax, 0x18(%rsp)
movb $0x1, %al
movq %rax, 0x10(%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
jmp 0x1483
movb $0x1, %al
movq %rax, 0x18(%rsp)
jmp 0x1483
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x1483
movb $0x1, %al
movq %rax, 0x38(%rsp)
jmp 0x1483
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x1483
movb $0x1, %al
movq %rax, 0x20(%rsp)
incl %r13d
cmpl %r15d, %r13d
jb 0x134b
testq %rbp, %rbp
je 0x160b
leaq 0x2bb1(%rip), %rsi      # 0x4050
movq %rbp, %rdi
callq 0x1100
testq %rax, %rax
je 0x1666
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x1e60
testl %eax, %eax
jne 0x1673
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x2276
movl 0x64(%r14), %edi
callq 0x10e0
leaq 0x40(%rsp), %rdi
movq %rax, 0x8(%rdi)
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1f1e
cmpb $0x0, 0x18(%rsp)
je 0x1503
leaq 0x40(%rsp), %rdi
callq 0x233b
leaq 0x40(%rsp), %r14
leaq 0x70(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x241c
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2134
cmpb $0x0, 0x10(%rsp)
je 0x1547
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rbx, %rdx
movl $0x2, %ecx
callq 0x25c7
cmpb $0x0, 0x8(%rsp)
movq 0x30(%rsp), %r14
je 0x156a
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rbx, %rdx
movl $0x4, %ecx
callq 0x25c7
cmpb $0x0, 0x38(%rsp)
je 0x158b
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %rbx, %rcx
callq 0x2a9b
cmpb $0x0, 0x28(%rsp)
je 0x15b2
movzbl 0x20(%rsp), %r8d
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %rbx, %rcx
callq 0x3304
testq %r14, %r14
je 0x15d1
movzbl 0x4(%rsp), %ecx
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %r8
callq 0x3718
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 0x2ae8(%rip), %rdi      # 0x40fa
callq 0x1060
leaq 0x2b03(%rip), %rdi      # 0x4121
callq 0x1060
leaq 0x2b25(%rip), %rdi      # 0x414f
callq 0x1060
leaq 0x2b4c(%rip), %rdi      # 0x4182
callq 0x1060
leaq 0x2b6d(%rip), %rdi      # 0x41af
callq 0x1060
leaq 0x2b8e(%rip), %rdi      # 0x41dc
callq 0x1060
leaq 0x2bac(%rip), %rdi      # 0x4206
callq 0x1060
xorl %edi, %edi
callq 0x1130
movq 0x8(%r14), %rsi
leaq 0x29e2(%rip), %rdi      # 0x4053
jmp 0x1693
leaq 0x2a5b(%rip), %rdi      # 0x40d5
callq 0x1060
movq %rbx, %rdi
callq 0x1080
jmp 0x16a8
leaq 0x29a6(%rip), %rdi      # 0x4036
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x16a8
leaq 0x2ba1(%rip), %rdi      # 0x4244
callq 0x1060
movl $0x1, %edi
callq 0x1130
nop