// File: /workspace/github_c_makefile_star2_O2/sadistech[P]nesromtool2/./src/subcommands/title.c
// Function: subcommand_title_set_parse
title_opts* subcommand_title_set_parse(int argc, char **argv) {
  title_opts *opts = (title_opts*)calloc(1, sizeof(title_opts));

  static struct option longopts[] = {
    { "help", no_argument, NULL, 'h' },
    { "outfile", required_argument, NULL, 'o' },
    { NULL, 0, NULL, 0 }
  };

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

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

  int ch;

  while ((ch = getopt_long(argc, argv, "o:h", longopts, NULL)) != -1) {
    switch (ch) {
      case 'h':
        print_usage_title_set();
        exit(EXIT_SUCCESS);
        break;

      case 'o':
        strcpy(opts->outfile_path, optarg);

        break;
      case 0:
        break;
      default:
        fprintf(stderr, "Invalid argument: -%c\n", ch);
        exit(EXIT_FAILURE);
    }
  }

  argc -= optind;
  argv += optind;

  // read in the title from the CLI. make sure that it doesn't go over the max length.

  char *title = argv[0];

  if (strnlen(title, NRT_TITLE_MAX_LENGTH + 1) > NRT_TITLE_MAX_LENGTH) {
    fprintf(stderr, "Title cannot be more than %d characters.\n", NRT_TITLE_MAX_LENGTH);
    free(opts);
    exit(EXIT_FAILURE);
  }

  strncpy(opts->title, title, NRT_TITLE_MAX_LENGTH);

  // if the user didn't specify an outfile, then we want the outfile to be
  // the same as the romfile.
  if (strlen(opts->outfile_path) == 0) {
    strcpy(opts->outfile_path, opts->romfile_path);
  }

  return opts;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movl %edi, %ebp
pushq $0x1
popq %rdi
movl $0x8ff, %esi            # imm = 0x8FF
callq 0x21c0
movq %rax, %rbx
movq (%r14), %r15
leaq 0x15cc(%rip), %rsi      # 0x64c2
movq %r15, %rdi
callq 0x21d0
testl %eax, %eax
je 0x4fc8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x20a0
movq %rbx, (%rsp)
leaq 0x4ff(%rbx), %r15
leaq 0x290c(%rip), %r12      # 0x782f
leaq 0x6936(%rip), %r13      # 0xb860
movq 0x609f(%rip), %rbx      # 0xafd0
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x2130
testl %eax, %eax
je 0x4f31
cmpl $0x6f, %eax
jne 0x4f5a
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x20a0
jmp 0x4f31
cmpl $-0x1, %eax
jne 0x4fc3
movq 0x6062(%rip), %rax      # 0xafc8
movslq (%rax), %rax
movq (%r14,%rax,8), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
callq 0x2190
cmpq $0x100, %rax            # imm = 0x100
movq (%rsp), %rbx
jae 0x4ff8
movq %rbx, %rdi
addq $0x400, %rdi            # imm = 0x400
movl $0xff, %edx
movq %r14, %rsi
callq 0x2070
cmpb $0x0, 0x4ff(%rbx)
jne 0x4fb1
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20a0
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x4fd6
xorl %eax, %eax
callq 0x3a89
xorl %edi, %edi
callq 0x2330
movq 0x601b(%rip), %rcx      # 0xaff8
movq (%rcx), %rdi
leaq 0x2646(%rip), %rsi      # 0x762d
movl %eax, %edx
xorl %eax, %eax
callq 0x21f0
pushq $0x1
popq %rdi
callq 0x2330
movq 0x5ff9(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x282a(%rip), %rsi      # 0x7833
movl $0xff, %edx
xorl %eax, %eax
callq 0x21f0
movq %rbx, %rdi
callq 0x2040
jmp 0x4ff0