// File: /workspace/github_c_makefile_star2_O0/sadistech[P]nesromtool2/./src/subcommands/prg.c
// Function: subcommand_prg_extract_parse
prg_opts* subcommand_prg_extract_parse(int argc, char **argv) {
  prg_opts *opts = (prg_opts*)calloc(1, sizeof(prg_opts));

  static struct option longopts[] = {
    { "help", no_argument, NULL, 'h' },
    { "format", required_argument, NULL, 'f' },
    { NULL, 0, NULL, 0 }
  };

  if (strcmp(argv[0], "--help") == 0) {
    print_usage_prg_extract();
    exit(EXIT_SUCCESS);
  }

  strcpy(opts->romfile_path, argv[0]); // don't shift the args

  int ch;

  while ((ch = getopt_long(argc, argv, "f:", longopts, NULL)) != -1) {
    switch (ch) {
      case 'h':
        print_usage_prg_extract();
        exit(EXIT_SUCCESS);
        break;
      case 'f':
        opts->format = parse_prg_output_format(optarg);

        if (opts->format == prg_null_format) {
          // the format was invalid
          fprintf(stderr, "Invalid format: %s\n", optarg);
          free(opts);
          exit(EXIT_FAILURE);
        }

        break;
      case 0:
        break;
      default:
        fprintf(stderr, "Invalid argument: -%c\n", ch);
        exit(EXIT_FAILURE);
    }
  }

  argc -= optind;
  argv += optind;

  // read the bank index
  char *bank_index_str = *(argv++);
  opts->bank_index = atoi(bank_index_str);

  // read the outfile path
  strcpy(opts->outfile_path, *(argv++));

  // try to guess the format if the user didn't supply one
  if (! opts->format) {
    opts->format = filename_to_prg_output_format(opts->outfile_path);
  }

  if (! opts->format) {
    fprintf(stderr, "Could not glean the format from the output file name.");
    free(opts);
    exit(EXIT_FAILURE);
  }

  return opts;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, %edi
movl $0xc08, %esi            # imm = 0xC08
callq 0x21a0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x34e2(%rip), %rsi      # 0x84c2
callq 0x21b0
cmpl $0x0, %eax
jne 0x4ff8
movb $0x0, %al
callq 0x4280
xorl %edi, %edi
callq 0x2310
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x2090
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x4600(%rip), %rdx      # 0x9616
leaq 0x7613(%rip), %rcx      # 0xc630
xorl %eax, %eax
movl %eax, %r8d
callq 0x2110
movl %eax, -0x1c(%rbp)
cmpl $-0x1, %eax
je 0x50f0
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
testl %eax, %eax
je 0x50c4
jmp 0x5043
movl -0x2c(%rbp), %eax
subl $0x66, %eax
je 0x5065
jmp 0x504d
movl -0x2c(%rbp), %eax
subl $0x68, %eax
jne 0x50c6
jmp 0x5057
movb $0x0, %al
callq 0x4280
xorl %edi, %edi
callq 0x2310
movq 0x6f64(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x4f20
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x400(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x50c2
movq 0x6f64(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x6f32(%rip), %rax      # 0xbfd0
movq (%rax), %rdx
leaq 0x4571(%rip), %rsi      # 0x9619
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
jmp 0x50eb
jmp 0x50eb
movq 0x6f2b(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0x4553(%rip), %rsi      # 0x962d
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2310
jmp 0x5008
movq 0x6ed1(%rip), %rax      # 0xbfc8
movl (%rax), %ecx
movl -0x4(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4(%rbp)
movq 0x6ec0(%rip), %rax      # 0xbfc8
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x10(%rbp)
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x22e0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x404(%rax)
movq -0x18(%rbp), %rdi
addq $0x808, %rdi            # imm = 0x808
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x10(%rbp)
movq (%rax), %rsi
callq 0x2090
movq -0x18(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x5192
movq -0x18(%rbp), %rdi
addq $0x808, %rdi            # imm = 0x808
callq 0x4f60
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x400(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x51ca
movq 0x6e52(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4494(%rip), %rsi      # 0x9644
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x18(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)