// File: /workspace/github_c_makefile_star2_O0/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
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 -0x18(%rbp), %rax
movl $0x10, 0x404(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x2ae4(%rip), %rsi      # 0x84c2
callq 0x21b0
cmpl $0x0, %eax
jne 0x59f6
movb $0x0, %al
callq 0x44b0
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 0x3d85(%rip), %rdx      # 0x9799
leaq 0x6d15(%rip), %rcx      # 0xc730
xorl %eax, %eax
movl %eax, %r8d
callq 0x2110
movl %eax, -0x1c(%rbp)
cmpl $-0x1, %eax
je 0x5b18
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
testl %eax, %eax
je 0x5aec
jmp 0x5a41
movl -0x2c(%rbp), %eax
subl $0x66, %eax
je 0x5a70
jmp 0x5a4b
movl -0x2c(%rbp), %eax
subl $0x68, %eax
je 0x5a62
jmp 0x5a55
movl -0x2c(%rbp), %eax
subl $0x77, %eax
je 0x5acf
jmp 0x5aee
movb $0x0, %al
callq 0x44b0
xorl %edi, %edi
callq 0x2310
movq 0x6559(%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 0x5acd
movq 0x6559(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x6527(%rip), %rax      # 0xbfd0
movq (%rax), %rdx
leaq 0x3b66(%rip), %rsi      # 0x9619
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
jmp 0x5b13
movq 0x64fa(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x22e0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x404(%rax)
jmp 0x5b13
jmp 0x5b13
movq 0x6503(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0x3b2b(%rip), %rsi      # 0x962d
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2310
jmp 0x5a06
movq 0x64a9(%rip), %rax      # 0xbfc8
movl (%rax), %ecx
movl -0x4(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4(%rbp)
movq 0x6498(%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 $0x80c, %rdi            # imm = 0x80C
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 0x5bba
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 0x5bf2
movq 0x642a(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3a6c(%rip), %rsi      # 0x9644
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x18(%rbp), %rax
cmpl $0x1, 0x404(%rax)
jl 0x5c0f
movq -0x18(%rbp), %rax
cmpl $0x200, 0x404(%rax)     # imm = 0x200
jle 0x5c3f
movq 0x63e2(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x3b7f(%rip), %rsi      # 0x979f
movl $0x200, %edx            # imm = 0x200
movb $0x0, %al
callq 0x21d0
movq -0x18(%rbp), %rdi
callq 0x2040
movl $0x1, %edi
callq 0x2310
movq -0x18(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)