// File: /workspace/github_c_makefile_star2_O3/sadistech[P]nesromtool2/./src/subcommands/chr.c
// Function: subcommand_chr_extract_parse
chr_opts* subcommand_chr_extract_parse(int argc, char **argv) {
  chr_opts *opts = (chr_opts*)calloc(1, sizeof(chr_opts));

  // default settings
  opts->width = 16;

  static struct option longopts[] = {
    { "help", no_argument, NULL, 'h' },
    { "format", required_argument, NULL, 'f' },
    { "width", required_argument, NULL, 'w' },
    { NULL, 0, NULL, 0 }
  };

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

  strcpy(opts->romfile_path, argv[0]); // don't shift the args

  int ch;

  while ((ch = getopt_long(argc, argv, "f:w:h", longopts, NULL)) != -1) {
    switch (ch) {
      case 'h':
        print_usage_chr_extract();
        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 'w':
        opts->width = atoi(optarg);
        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_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 (opts->width < 1 || opts->width > NRT_CHR_TILE_COUNT) {
    fprintf(stderr, "Width must be between 1 and %d\n", NRT_CHR_TILE_COUNT);
    free(opts);
    exit(EXIT_FAILURE);
  }

  /* printf("Going to open %s and extract bank %d to '%s'\n", opts->romfile_path, opts->bank_index, opts->outfile_path); */
  /* printf("Format: '%d'\n", opts->format); */

  return opts;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movl %edi, %ebp
movl $0x1, %edi
movl $0xc0c, %esi            # imm = 0xC0C
callq 0x21c0
movq %rax, %rbx
movl $0x10, 0x404(%rax)
movq (%r15), %r12
leaq 0x21e6(%rip), %rsi      # 0x74c2
movq %r12, %rdi
callq 0x21d0
testl %eax, %eax
je 0x53f0
movq %rbx, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x20a0
leaq 0x3497(%rip), %r12      # 0x8799
leaq 0x7437(%rip), %r13      # 0xc740
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x2130
cmpl $0x65, %eax
jle 0x5346
cmpl $0x66, %eax
je 0x534c
cmpl $0x77, %eax
jne 0x53eb
movq 0x6c9a(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x2300
movl %eax, 0x404(%rbx)
jmp 0x5309
testl %eax, %eax
je 0x5309
jmp 0x5373
movq 0x6c7d(%rip), %rax      # 0xbfd0
movq (%rax), %r14
movq %r14, %rdi
callq 0x5230
movl %eax, 0x400(%rbx)
testl %eax, %eax
jne 0x5309
movq %r14, %rdi
movq %rbx, %rsi
callq 0x25c9
cmpl $-0x1, %eax
jne 0x53fe
movq 0x6c45(%rip), %rax      # 0xbfc8
movslq (%rax), %r14
movq (%r15,%r14,8), %rdi
callq 0x2300
movl %eax, 0x408(%rbx)
movq (%rsp), %r12
addq $0x80c, %r12            # imm = 0x80C
movq 0x8(%r15,%r14,8), %rsi
movq %r12, %rdi
callq 0x20a0
cmpl $0x0, 0x400(%rbx)
jne 0x53c8
movq %r12, %rdi
callq 0x5267
movl %eax, 0x400(%rbx)
testl %eax, %eax
je 0x540d
movl $0xfffffdff, %eax       # imm = 0xFFFFFDFF
movl 0x404(%rbx), %ecx
addl %eax, %ecx
cmpl %eax, %ecx
jbe 0x5405
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x53fe
xorl %eax, %eax
callq 0x4234
xorl %edi, %edi
callq 0x2330
movl %eax, %edi
callq 0x2659
movq %rbx, %rdi
callq 0x25f8
movq %rbx, %rdi
callq 0x2629