// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, %edi
movl $0xc0c, %esi            # imm = 0xC0C
callq 0x21a0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x2842(%rip), %rsi      # 0x84c2
callq 0x21b0
cmpl $0x0, %eax
jne 0x5c98
movb $0x0, %al
callq 0x4570
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 0x39cc(%rip), %rdx      # 0x9682
leaq 0x6af3(%rip), %rcx      # 0xc7b0
xorl %eax, %eax
movl %eax, %r8d
callq 0x2110
movl %eax, -0x1c(%rbp)
cmpl $-0x1, %eax
je 0x5db3
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
subl $0x66, %eax
je 0x5d05
jmp 0x5ce0
movl -0x2c(%rbp), %eax
subl $0x68, %eax
je 0x5cf7
jmp 0x5cea
movl -0x2c(%rbp), %eax
subl $0x6f, %eax
je 0x5d64
jmp 0x5d80
movb $0x0, %al
callq 0x4570
xorl %edi, %edi
callq 0x2310
movq 0x62c4(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x58d0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x400(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x5d62
movq 0x62c4(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x6292(%rip), %rax      # 0xbfd0
movq (%rax), %rdx
leaq 0x38d1(%rip), %rsi      # 0x9619
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
jmp 0x5dae
movq -0x18(%rbp), %rdi
addq $0x40c, %rdi            # imm = 0x40C
movq 0x625a(%rip), %rax      # 0xbfd0
movq (%rax), %rsi
callq 0x2090
jmp 0x5dae
movq 0x6271(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0x3899(%rip), %rsi      # 0x962d
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
jmp 0x5ca8
movq 0x620e(%rip), %rax      # 0xbfc8
movl (%rax), %ecx
movl -0x4(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4(%rbp)
movq 0x61fd(%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, 0x408(%rax)
movq -0x18(%rbp), %rdi
addq $0x40c, %rdi            # imm = 0x40C
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 0x5e55
movq -0x18(%rbp), %rdi
addq $0x80c, %rdi            # imm = 0x80C
callq 0x5930
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x400(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x400(%rax)
jne 0x5e8d
movq 0x618f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x37d1(%rip), %rsi      # 0x9644
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x18(%rbp), %rdi
addq $0x80c, %rdi            # imm = 0x80C
callq 0x20f0
cmpq $0x0, %rax
jne 0x5eb7
movq -0x18(%rbp), %rdi
addq $0x80c, %rdi            # imm = 0x80C
movq -0x18(%rbp), %rsi
callq 0x2090
movq -0x18(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)