// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movb $0x0, -0x105(%rbp)
movb $0x0, -0x106(%rbp)
movb $0x0, -0x107(%rbp)
movb $0x0, -0x108(%rbp)
movb $0x0, -0x109(%rbp)
movb $0x0, -0x10a(%rbp)
movb $0x0, -0x10b(%rbp)
movq $0x0, -0x118(%rbp)
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x70, %edx
callq 0x1090
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x1090
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x1090
leaq 0x3d17(%rip), %rdi      # 0x5004
movb $0x0, %al
callq 0x1070
leaq 0x3d28(%rip), %rdi      # 0x5023
movb $0x0, %al
callq 0x1070
movl $0x1, -0x104(%rbp)
movl -0x104(%rbp), %eax
cmpl -0x8(%rbp), %eax
jae 0x1559
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x1531
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3d13(%rip), %rsi      # 0x505d
callq 0x10a0
cmpl $0x0, %eax
jne 0x136e
movb $0x1, -0x105(%rbp)
movb $0x1, -0x106(%rbp)
movb $0x1, -0x107(%rbp)
jmp 0x152f
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3cdf(%rip), %rsi      # 0x5062
callq 0x10a0
cmpl $0x0, %eax
jne 0x1399
movb $0x1, -0x105(%rbp)
jmp 0x152d
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3cb9(%rip), %rsi      # 0x5067
callq 0x10a0
cmpl $0x0, %eax
jne 0x13c4
movb $0x1, -0x106(%rbp)
jmp 0x152b
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3c93(%rip), %rsi      # 0x506c
callq 0x10a0
cmpl $0x0, %eax
jne 0x13ef
movb $0x1, -0x107(%rbp)
jmp 0x1529
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3c6d(%rip), %rsi      # 0x5071
callq 0x10a0
cmpl $0x0, %eax
jne 0x141a
movb $0x1, -0x108(%rbp)
jmp 0x1527
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3c47(%rip), %rsi      # 0x5076
callq 0x10a0
cmpl $0x0, %eax
jne 0x1445
movb $0x1, -0x109(%rbp)
jmp 0x1525
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3c25(%rip), %rsi      # 0x507f
callq 0x10a0
cmpl $0x0, %eax
jne 0x1470
movb $0x1, -0x10a(%rbp)
jmp 0x1523
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
leaq 0x3c04(%rip), %rsi      # 0x5089
callq 0x10a0
cmpl $0x0, %eax
jne 0x14fb
movl -0x104(%rbp), %eax
addl $0x2, %eax
cmpl -0x8(%rbp), %eax
jb 0x14b5
leaq 0x3beb(%rip), %rdi      # 0x508f
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1120
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x104(%rbp)
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x118(%rbp)
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x104(%rbp)
movl %ecx, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x1100
movb %al, -0x10b(%rbp)
jmp 0x1521
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
leaq 0x3bab(%rip), %rdi      # 0x50bb
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1120
jmp 0x1523
jmp 0x1525
jmp 0x1527
jmp 0x1529
jmp 0x152b
jmp 0x152d
jmp 0x152f
jmp 0x1543
movq -0x10(%rbp), %rax
movl -0x104(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
jmp 0x1545
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0x130c
cmpq $0x0, -0x20(%rbp)
jne 0x15c9
leaq 0x3b6e(%rip), %rdi      # 0x50d5
movb $0x0, %al
callq 0x1070
leaq 0x3b88(%rip), %rdi      # 0x50fd
movb $0x0, %al
callq 0x1070
leaq 0x3ba9(%rip), %rdi      # 0x512c
movb $0x0, %al
callq 0x1070
leaq 0x3bcf(%rip), %rdi      # 0x5160
movb $0x0, %al
callq 0x1070
leaq 0x3bef(%rip), %rdi      # 0x518e
movb $0x0, %al
callq 0x1070
leaq 0x3c0f(%rip), %rdi      # 0x51bc
movb $0x0, %al
callq 0x1070
leaq 0x3c2c(%rip), %rdi      # 0x51e7
movb $0x0, %al
callq 0x1070
xorl %edi, %edi
callq 0x1120
movq -0x20(%rbp), %rdi
leaq 0x3c52(%rip), %rsi      # 0x5226
callq 0x10f0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1604
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x3c36(%rip), %rdi      # 0x5229
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1120
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x2510
cmpl $0x0, %eax
je 0x163a
leaq 0x3c34(%rip), %rdi      # 0x5254
movb $0x0, %al
callq 0x1070
movq -0x18(%rbp), %rdi
callq 0x1060
movl $0x1, %edi
callq 0x1120
leaq -0x90(%rbp), %rdi
callq 0x2b00
movl -0x2c(%rbp), %eax
movl %eax, %edi
callq 0x10d0
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2690
movzbl -0x105(%rbp), %eax
cmpl $0x0, %eax
je 0x1686
leaq -0xc0(%rbp), %rdi
callq 0x2c10
movq -0x18(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2d10
movq -0x18(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2960
movzbl -0x106(%rbp), %eax
cmpl $0x0, %eax
je 0x16dc
movq -0x18(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x2, %ecx
callq 0x2fe0
movzbl -0x107(%rbp), %eax
cmpl $0x0, %eax
je 0x1704
movq -0x18(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x4, %ecx
callq 0x2fe0
movzbl -0x108(%rbp), %eax
cmpl $0x0, %eax
je 0x172e
movq -0x18(%rbp), %rcx
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x3980
movzbl -0x109(%rbp), %eax
cmpl $0x0, %eax
je 0x1760
movq -0x18(%rbp), %rcx
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x100(%rbp), %rdx
movzbl -0x10a(%rbp), %r8d
callq 0x4680
cmpq $0x0, -0x118(%rbp)
je 0x178f
movq -0x18(%rbp), %rdx
movzbl -0x10b(%rbp), %ecx
movq -0x118(%rbp), %r8
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4e00
movq -0xb8(%rbp), %rdi
callq 0x1030
movq -0xa8(%rbp), %rdi
callq 0x1030
movq -0xa0(%rbp), %rdi
callq 0x1030
movq -0x18(%rbp), %rdi
callq 0x1060
xorl %eax, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw (%rax,%rax)