// File: /workspace/github_c_makefile_star2_O0/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
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 0x32b2(%rip), %rsi      # 0x84c2
callq 0x21b0
cmpl $0x0, %eax
jne 0x5228
movb $0x0, %al
callq 0x4340
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 0x443c(%rip), %rdx      # 0x9682
leaq 0x7443(%rip), %rcx      # 0xc690
xorl %eax, %eax
movl %eax, %r8d
callq 0x2110
movl %eax, -0x1c(%rbp)
cmpl $-0x1, %eax
je 0x5343
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
subl $0x66, %eax
je 0x5295
jmp 0x5270
movl -0x2c(%rbp), %eax
subl $0x68, %eax
je 0x5287
jmp 0x527a
movl -0x2c(%rbp), %eax
subl $0x6f, %eax
je 0x52f4
jmp 0x5310
movb $0x0, %al
callq 0x4340
xorl %edi, %edi
callq 0x2310
movq 0x6d34(%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 0x52f2
movq 0x6d34(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x6d02(%rip), %rax      # 0xbfd0
movq (%rax), %rdx
leaq 0x4341(%rip), %rsi      # 0x9619
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
jmp 0x533e
movq -0x18(%rbp), %rdi
addq $0x808, %rdi            # imm = 0x808
movq 0x6cca(%rip), %rax      # 0xbfd0
movq (%rax), %rsi
callq 0x2090
jmp 0x533e
movq 0x6ce1(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0x4309(%rip), %rsi      # 0x962d
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
jmp 0x5238
movq 0x6c7e(%rip), %rax      # 0xbfc8
movl (%rax), %ecx
movl -0x4(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4(%rbp)
movq 0x6c6d(%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 $0x408, %rdi            # imm = 0x408
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 0x53e5
movq -0x18(%rbp), %rdi
addq $0x408, %rdi            # imm = 0x408
callq 0x4f60
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x400(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x541d
movq 0x6bff(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x4241(%rip), %rsi      # 0x9644
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x18(%rbp), %rdi
addq $0x808, %rdi            # imm = 0x808
callq 0x20f0
cmpq $0x0, %rax
jne 0x5447
movq -0x18(%rbp), %rdi
addq $0x808, %rdi            # imm = 0x808
movq -0x18(%rbp), %rsi
callq 0x2090
movq -0x18(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)