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

  char *appname = basename(argv[0]);

  if (strcmp(appname, "chr2png") == 0) {
    opts->app = app_chr2png;
    opts->width = 16; // default width;
  } else if (strcmp(appname, "png2chr") == 0) {
    opts->app = app_png2chr;
  } else {
    fprintf(stderr, "Cannot run program. Unknown argv[0] (%s)\n", appname);
    exit(EXIT_FAILURE);
  }

  // now let's parse arguments

  static struct option longopts[] = {
    { "help", no_argument, NULL, 'h' },
    { "outdir", required_argument, NULL, 'o' },
    { "width", required_argument, NULL, 'w' },
    { "size", required_argument, NULL, 's' },
    { NULL, 0, NULL, 0 }
  };

  int ch;

  while ((ch = getopt_long(argc, argv, "ho:w:s:", longopts, NULL)) != -1) {
    switch (ch) {
      case 'h':
        if (opts->app == app_chr2png) {
          print_usage_chr2png();
        } else {
          print_usage_png2chr();
        }
        exit(EXIT_SUCCESS);
        break;

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

      case 'w':
        if (opts->app == app_png2chr) {
          fprintf(stderr, "Setting width has no effect when converting to CHR.\n");
        }

        opts->width = atoi(optarg);
        break;

      case 's':
        // if the value is 'chr' then that's 512
        // if the value is 'table' then it's 256
        // otherwise it should be a number
        if (strncmp(optarg, "chr", 4) == 0) {
          opts->size = 512;
        } else if (strncmp(optarg, "table", 6) == 0) {
          opts->size = 256;
        } else {
          opts->size = atoi(optarg);

          if (opts->size < 0 || opts->size > 512) {
            fprintf(stderr, "Please choose a value between 0 and 512\n");
            exit(EXIT_FAILURE);
          }
        }

        break;

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

  argc -= optind;
  argv += optind;

  if (opts->size == 0) {
    opts->size = 512;
  }

  switch (opts->app) {
    case app_chr2png:
      chr2png(opts, argc, argv);
      break;
    case app_png2chr:
      png2chr(opts, argc, argv);
      break;
    default:
      printf("Invalid argument.\n");
      exit(EXIT_FAILURE);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movl %edi, %ebp
movl $0x1, %edi
movl $0x100c, %esi           # imm = 0x100C
callq 0x21c0
movq %rax, %rbx
movq %rax, (%rsp)
movq (%r14), %rdi
callq 0x2210
movq %rax, %r15
leaq 0x2434(%rip), %rsi      # 0x8280
movq %rax, %rdi
callq 0x21d0
testl %eax, %eax
je 0x5e77
leaq 0x256b(%rip), %rsi      # 0x83ca
movq %r15, %rdi
callq 0x21d0
testl %eax, %eax
jne 0x6014
movl $0x2, (%rbx)
jmp 0x5e87
movl $0x1, (%rbx)
movl $0x10, 0x1004(%rbx)
addq $0x4, (%rsp)
leaq 0x2a0b(%rip), %r12      # 0x889e
leaq 0x6ae6(%rip), %r13      # 0xc980
movq 0x612f(%rip), %r15      # 0xbfd0
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x2130
cmpl $0x6e, %eax
jg 0x5ec2
testl %eax, %eax
je 0x5ea1
jmp 0x5f92
cmpl $0x6f, %eax
je 0x5eee
cmpl $0x73, %eax
je 0x5efc
cmpl $0x77, %eax
jne 0x5ffa
cmpl $0x2, (%rbx)
je 0x5f88
movq (%r15), %rdi
callq 0x2300
movl %eax, 0x1004(%rbx)
jmp 0x5ea1
movq (%r15), %rsi
movq (%rsp), %rdi
callq 0x20a0
jmp 0x5ea1
movq %r12, %r13
movq %r15, %r12
movq (%r15), %r15
movl $0x4, %edx
movq %r15, %rdi
leaq 0x182e(%rip), %rsi      # 0x7742
callq 0x2090
testl %eax, %eax
je 0x5f60
movl $0x6, %edx
movq %r15, %rdi
leaq 0x29af(%rip), %rsi      # 0x88db
callq 0x2090
testl %eax, %eax
je 0x5f6c
movq %r15, %rdi
callq 0x2300
movl %eax, 0x1008(%rbx)
cmpl $0x201, %eax            # imm = 0x201
movq %r12, %r15
movq %r13, %r12
leaq 0x6a2b(%rip), %r13      # 0xc980
jb 0x5ea1
jmp 0x600f
movl $0x200, 0x1008(%rbx)    # imm = 0x200
jmp 0x5f76
movl $0x100, 0x1008(%rbx)    # imm = 0x100
movq %r12, %r15
movq %r13, %r12
leaq 0x69fd(%rip), %r13      # 0xc980
jmp 0x5ea1
callq 0x28bb
jmp 0x5ede
cmpl $-0x1, %eax
jne 0x5fe7
movq 0x602a(%rip), %rax      # 0xbfc8
movslq (%rax), %rax
cmpl $0x0, 0x1008(%rbx)
jne 0x5fb4
movl $0x200, 0x1008(%rbx)    # imm = 0x200
leaq (%r14,%rax,8), %rdx
movl (%rbx), %eax
cmpl $0x2, %eax
je 0x5fce
cmpl $0x1, %eax
jne 0x601c
movq %rbx, %rdi
callq 0x6032
jmp 0x5fd6
movq %rbx, %rdi
callq 0x6203
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x5ffa
cmpl $0x1, (%rbx)
jne 0x6001
xorl %eax, %eax
callq 0x474f
jmp 0x6008
movl %eax, %edi
callq 0x28d7
xorl %eax, %eax
callq 0x4825
xorl %edi, %edi
callq 0x2330
callq 0x2896
movq %r15, %rdi
callq 0x2872
leaq 0x2a31(%rip), %rdi      # 0x8a54
callq 0x20b0
movl $0x1, %edi
callq 0x2330