// File: /workspace/github_c_makefile_star2_O1/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 0x23c8(%rip), %rsi      # 0x8280
movq %rax, %rdi
callq 0x21d0
testl %eax, %eax
je 0x5ee3
leaq 0x24ff(%rip), %rsi      # 0x83ca
movq %r15, %rdi
callq 0x21d0
testl %eax, %eax
jne 0x6080
movl $0x2, (%rbx)
jmp 0x5ef3
movl $0x1, (%rbx)
movl $0x10, 0x1004(%rbx)
addq $0x4, (%rsp)
leaq 0x299f(%rip), %r12      # 0x889e
leaq 0x6a7a(%rip), %r13      # 0xc980
movq 0x60c3(%rip), %r15      # 0xbfd0
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x2130
cmpl $0x6e, %eax
jg 0x5f2e
testl %eax, %eax
je 0x5f0d
jmp 0x5ffe
cmpl $0x6f, %eax
je 0x5f5a
cmpl $0x73, %eax
je 0x5f68
cmpl $0x77, %eax
jne 0x6066
cmpl $0x2, (%rbx)
je 0x5ff4
movq (%r15), %rdi
callq 0x2300
movl %eax, 0x1004(%rbx)
jmp 0x5f0d
movq (%r15), %rsi
movq (%rsp), %rdi
callq 0x20a0
jmp 0x5f0d
movq %r12, %r13
movq %r15, %r12
movq (%r15), %r15
movl $0x4, %edx
movq %r15, %rdi
leaq 0x17c2(%rip), %rsi      # 0x7742
callq 0x2090
testl %eax, %eax
je 0x5fcc
movl $0x6, %edx
movq %r15, %rdi
leaq 0x2943(%rip), %rsi      # 0x88db
callq 0x2090
testl %eax, %eax
je 0x5fd8
movq %r15, %rdi
callq 0x2300
movl %eax, 0x1008(%rbx)
cmpl $0x201, %eax            # imm = 0x201
movq %r12, %r15
movq %r13, %r12
leaq 0x69bf(%rip), %r13      # 0xc980
jb 0x5f0d
jmp 0x607b
movl $0x200, 0x1008(%rbx)    # imm = 0x200
jmp 0x5fe2
movl $0x100, 0x1008(%rbx)    # imm = 0x100
movq %r12, %r15
movq %r13, %r12
leaq 0x6991(%rip), %r13      # 0xc980
jmp 0x5f0d
callq 0x28bb
jmp 0x5f4a
cmpl $-0x1, %eax
jne 0x6053
movq 0x5fbe(%rip), %rax      # 0xbfc8
movslq (%rax), %rax
cmpl $0x0, 0x1008(%rbx)
jne 0x6020
movl $0x200, 0x1008(%rbx)    # imm = 0x200
leaq (%r14,%rax,8), %rdx
movl (%rbx), %eax
cmpl $0x2, %eax
je 0x603a
cmpl $0x1, %eax
jne 0x6088
movq %rbx, %rdi
callq 0x609e
jmp 0x6042
movq %rbx, %rdi
callq 0x626f
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x6066
cmpl $0x1, (%rbx)
jne 0x606d
xorl %eax, %eax
callq 0x4787
jmp 0x6074
movl %eax, %edi
callq 0x28d7
xorl %eax, %eax
callq 0x485d
xorl %edi, %edi
callq 0x2330
callq 0x2896
movq %r15, %rdi
callq 0x2872
leaq 0x29c5(%rip), %rdi      # 0x8a54
callq 0x20b0
movl $0x1, %edi
callq 0x2330