// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movl %edi, %ebp
pushq $0x1
popq %rdi
movl $0xc08, %esi            # imm = 0xC08
callq 0x21c0
movq %rax, %rbx
movq (%r15), %r14
leaq 0x2484(%rip), %rsi      # 0x64c2
movq %r14, %rdi
callq 0x21d0
testl %eax, %eax
je 0x4133
movq %rbx, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20a0
leaq 0x35b2(%rip), %r12      # 0x7616
leaq 0x75d5(%rip), %r13      # 0xb640
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x2130
testl %eax, %eax
je 0x406b
cmpl $0x66, %eax
jne 0x40cb
movq 0x6f42(%rip), %rax      # 0xafd0
movq (%rax), %r14
movq %r14, %rdi
callq 0x3fdc
movl %eax, 0x400(%rbx)
testl %eax, %eax
jne 0x406b
movq 0x6f4e(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x3565(%rip), %rsi      # 0x7619
movq %r14, %rdx
xorl %eax, %eax
callq 0x21f0
movq %rbx, %rdi
callq 0x2040
jmp 0x415b
cmpl $-0x1, %eax
jne 0x412e
movq 0x6ef1(%rip), %rax      # 0xafc8
movslq (%rax), %r14
movq (%r15,%r14,8), %rdi
callq 0x2300
movl %eax, 0x404(%rbx)
movq (%rsp), %r12
addq $0x808, %r12            # imm = 0x808
movq 0x8(%r15,%r14,8), %rsi
movq %r12, %rdi
callq 0x20a0
cmpl $0x0, 0x400(%rbx)
jne 0x411c
movq %r12, %rdi
callq 0x3ff4
movl %eax, 0x400(%rbx)
testl %eax, %eax
je 0x4163
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x4141
xorl %eax, %eax
callq 0x3627
xorl %edi, %edi
callq 0x2330
movq 0x6eb0(%rip), %rcx      # 0xaff8
movq (%rcx), %rdi
leaq 0x34db(%rip), %rsi      # 0x762d
movl %eax, %edx
xorl %eax, %eax
callq 0x21f0
pushq $0x1
popq %rdi
callq 0x2330
movq 0x6e8e(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x34d0(%rip), %rdi      # 0x7644
pushq $0x35
popq %rsi
pushq $0x1
popq %rdx
callq 0x2340
jmp 0x40be