// File: /workspace/github_c_makefile_star2_O1/sadistech[P]nesromtool2/./src/subcommands/prg.c
// Function: subcommand_prg_replace_parse
prg_opts* subcommand_prg_replace_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' },
    { "outfile", required_argument, NULL, 'o' },
    { NULL, 0, NULL, 0 }
  };

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

  strcpy(opts->romfile_path, argv[0]);

  int ch;

  while ((ch = getopt_long(argc, argv, "f:o:", longopts, NULL)) != -1) {
    switch (ch) {
      case 'h':
        print_usage_prg_replace();
        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 'o':
        strcpy(opts->outfile_path, optarg);
        break;
      default:
        fprintf(stderr, "Invalid argument: -%c\n", ch);
        free(opts);
        exit(EXIT_FAILURE);
    }
  }

  argc -= optind;
  argv += optind;

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

  strcpy(opts->sourcefile_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->sourcefile_path);
  }

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

  if (strlen(opts->outfile_path) == 0) {
    // clobber the existing ROM
    strcpy(opts->outfile_path, opts->romfile_path);
  }

  return opts;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movl %edi, %ebp
movl $0x1, %edi
movl $0xc08, %esi            # imm = 0xC08
callq 0x21c0
movq %rax, %rbx
movq (%r12), %r14
leaq 0x26d7(%rip), %rsi      # 0x74c2
movq %r14, %rdi
callq 0x21d0
testl %eax, %eax
je 0x4f36
movq %rbx, %rdi
movq %rbx, (%rsp)
movq %r14, %rsi
callq 0x20a0
movq %rbx, 0x10(%rsp)
leaq 0x808(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x3860(%rip), %r13      # 0x8682
leaq 0x7877(%rip), %r15      # 0xc6a0
movq 0x71a0(%rip), %rbx      # 0xbfd0
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x2130
cmpl $0x67, %eax
jg 0x4e77
cmpl $0x66, %eax
jne 0x4e8f
movq (%rbx), %r14
movq %r14, %rdi
leaq 0x37b8(%rip), %rsi      # 0x8612
callq 0x21d0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movq (%rsp), %rax
movl %ecx, 0x400(%rax)
je 0x4e30
jmp 0x4f4f
cmpl $0x6f, %eax
jne 0x4f31
movq (%rbx), %rsi
movq 0x8(%rsp), %rdi
callq 0x20a0
jmp 0x4e30
cmpl $-0x1, %eax
jne 0x4f44
movq 0x7129(%rip), %rax      # 0xbfc8
movslq (%rax), %rbx
movq (%r12,%rbx,8), %rdi
callq 0x2300
movq (%rsp), %r14
movl %eax, 0x404(%r14)
movq 0x10(%rsp), %r15
addq $0x408, %r15            # imm = 0x408
movq 0x8(%r12,%rbx,8), %rsi
movq %r15, %rdi
callq 0x20a0
cmpl $0x0, 0x400(%r14)
jne 0x4efe
movq %r15, %rdi
callq 0x3ad8
leaq 0x24b3(%rip), %rsi      # 0x739b
movq %rax, %rdi
callq 0x21d0
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movl %ecx, 0x400(%r14)
cmpl $0x0, 0x400(%r14)
je 0x4f5b
cmpb $0x0, 0x808(%r14)
jne 0x4f1f
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x20a0
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x4f44
xorl %eax, %eax
callq 0x40cb
xorl %edi, %edi
callq 0x2330
movl %eax, %edi
movq (%rsp), %rsi
callq 0x24fa
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x249b
movq %r14, %rdi
callq 0x24ca