// File: /workspace/github_c_makefile_star2_O2/sadistech[P]nesromtool2/./src/subcommands/chr.c
// Function: subcommand_chr_replace_parse
chr_opts* subcommand_chr_replace_parse(int argc, char **argv) {
  chr_opts *opts = (chr_opts*)calloc(1, sizeof(chr_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_chr_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_chr_replace();
        exit(EXIT_SUCCESS);
        break;

      case 'f':
        opts->format = parse_chr_output_format(optarg);

        if (opts->format == null_format) {
          // the format was invalid
          fprintf(stderr, "Invalid format: %s\n", optarg);
          free(opts);
          exit(EXIT_FAILURE);
        }
        break;
      case 'o':
        strcpy(opts->sourcefile_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_chr_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);
  }

  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, %r14
movl %edi, %ebp
pushq $0x1
popq %rdi
movl $0xc0c, %esi            # imm = 0xC0C
callq 0x21c0
movq %rax, %r15
movq (%r14), %rbx
leaq 0x1bb2(%rip), %rsi      # 0x64c2
movq %rbx, %rdi
callq 0x21d0
testl %eax, %eax
je 0x4a3e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20a0
movq %r15, 0x8(%rsp)
leaq 0x40c(%r15), %rax
movq %rax, 0x10(%rsp)
leaq 0x2d3f(%rip), %r12      # 0x7682
leaq 0x6e76(%rip), %r13      # 0xb7c0
movq 0x667f(%rip), %r15      # 0xafd0
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x2130
cmpl $0x66, %eax
je 0x497d
cmpl $0x6f, %eax
jne 0x49b7
movq (%r15), %rsi
movq 0x10(%rsp), %rdi
callq 0x20a0
jmp 0x4951
movq (%r15), %rbx
movq %rbx, %rdi
callq 0x46a8
movq 0x8(%rsp), %rcx
movl %eax, 0x400(%rcx)
testl %eax, %eax
jne 0x4951
movq 0x665a(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x2c71(%rip), %rsi      # 0x7619
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21f0
jmp 0x4a66
cmpl $-0x1, %eax
jne 0x4a39
movq 0x6605(%rip), %rax      # 0xafc8
movslq (%rax), %rbx
movq (%r14,%rbx,8), %rdi
callq 0x2300
movq 0x8(%rsp), %r15
movl %eax, 0x408(%r15)
movq 0x8(%r14,%rbx,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x20a0
cmpl $0x0, 0x400(%r15)
jne 0x4a0b
leaq 0x80c(%r15), %rdi
callq 0x46dd
movl %eax, 0x400(%r15)
testl %eax, %eax
je 0x4a78
cmpb $0x0, 0x80c(%r15)
jne 0x4a27
movq %r15, %rdi
addq $0x80c, %rdi            # imm = 0x80C
movq %r15, %rsi
callq 0x20a0
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x4a4c
xorl %eax, %eax
callq 0x3899
xorl %edi, %edi
callq 0x2330
movq 0x65a5(%rip), %rcx      # 0xaff8
movq (%rcx), %rdi
leaq 0x2bd0(%rip), %rsi      # 0x762d
movl %eax, %edx
xorl %eax, %eax
callq 0x21f0
movq 0x8(%rsp), %rdi
callq 0x2040
pushq $0x1
popq %rdi
callq 0x2330
movq 0x6579(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x2bbb(%rip), %rdi      # 0x7644
pushq $0x35
popq %rsi
pushq $0x1
popq %rdx
callq 0x2340
movq %r15, %rdi
jmp 0x4a6b