// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movq %rsi, %r14
movl %edi, %ebp
pushq $0x1
popq %rdi
movl $0x100c, %esi           # imm = 0x100C
callq 0x21c0
movq %rax, %rbx
movq %rax, 0x10(%rsp)
movq (%r14), %rdi
callq 0x2210
movq %rax, %r12
leaq 0x1e76(%rip), %rsi      # 0x7280
movq %rax, %rdi
callq 0x21d0
testl %eax, %eax
je 0x5435
leaq 0x1fad(%rip), %rsi      # 0x73ca
movq %r12, %rdi
callq 0x21d0
testl %eax, %eax
jne 0x5636
movl $0x2, (%rbx)
jmp 0x5445
movl $0x1, (%rbx)
movl $0x10, 0x1004(%rbx)
addq $0x4, 0x10(%rsp)
leaq 0x244c(%rip), %r12      # 0x789e
leaq 0x6527(%rip), %r13      # 0xb980
movq 0x5b70(%rip), %r15      # 0xafd0
movq %rbx, 0x8(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x2130
testl %eax, %eax
je 0x5465
cmpl $0x6f, %eax
je 0x5513
cmpl $0x73, %eax
je 0x54ac
cmpl $0x77, %eax
jne 0x5577
cmpl $0x2, (%rbx)
je 0x5556
movq (%r15), %rdi
callq 0x2300
movl %eax, 0x1004(%rbx)
jmp 0x5465
movq %r13, %rbx
movq %r12, %r13
movq %r15, %r12
movq (%r15), %r15
movq %r15, %rdi
leaq 0x1280(%rip), %rsi      # 0x6742
pushq $0x4
popq %rdx
callq 0x2090
testl %eax, %eax
je 0x5525
movq %r15, %rdi
leaq 0x2403(%rip), %rsi      # 0x78db
pushq $0x6
popq %rdx
callq 0x2090
testl %eax, %eax
je 0x5536
movq %r15, %rdi
callq 0x2300
movq 0x8(%rsp), %rcx
movl %eax, 0x1008(%rcx)
cmpl $0x201, %eax            # imm = 0x201
movq %r12, %r15
movq %r13, %r12
movq %rbx, %r13
movq %rcx, %rbx
jb 0x5465
jmp 0x5612
movq (%r15), %rsi
movq 0x10(%rsp), %rdi
callq 0x20a0
jmp 0x5465
movq 0x8(%rsp), %rax
movl $0x200, 0x1008(%rax)    # imm = 0x200
jmp 0x5545
movq 0x8(%rsp), %rax
movl $0x100, 0x1008(%rax)    # imm = 0x100
movq %r12, %r15
movq %r13, %r12
movq %rbx, %r13
movq %rax, %rbx
jmp 0x5465
movq 0x5a9b(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x233f(%rip), %rdi      # 0x78a6
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x2340
jmp 0x549c
cmpl $-0x1, %eax
jne 0x55d0
movq 0x5a45(%rip), %rax      # 0xafc8
movslq (%rax), %rax
cmpl $0x0, 0x1008(%rbx)
jne 0x5599
movl $0x200, 0x1008(%rbx)    # imm = 0x200
leaq (%r14,%rax,8), %rdx
movl (%rbx), %eax
cmpl $0x2, %eax
je 0x55b7
cmpl $0x1, %eax
jne 0x5653
movq %rbx, %rdi
callq 0x5661
jmp 0x55bf
movq %rbx, %rdi
callq 0x5860
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x55e8
movq 0x8(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x5604
xorl %eax, %eax
callq 0x3bc5
jmp 0x560b
movq 0x5a09(%rip), %rcx      # 0xaff8
movq (%rcx), %rdi
leaq 0x2034(%rip), %rsi      # 0x762d
movl %eax, %edx
xorl %eax, %eax
callq 0x21f0
jmp 0x562e
xorl %eax, %eax
callq 0x3c63
xorl %edi, %edi
callq 0x2330
movq 0x59df(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x22be(%rip), %rdi      # 0x78e1
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x2340
pushq $0x1
popq %rdi
callq 0x2330
movq 0x59bb(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x2221(%rip), %rsi      # 0x7868
movq %r12, %rdx
xorl %eax, %eax
callq 0x21f0
jmp 0x562e
leaq 0x23fa(%rip), %rdi      # 0x7a54
callq 0x20b0
jmp 0x562e