// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, %edi
movl $0x100c, %esi           # imm = 0x100C
callq 0x21a0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x21f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x26cc(%rip), %rsi      # 0x9280
callq 0x21b0
cmpl $0x0, %eax
jne 0x6bd8
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x18(%rbp), %rax
movl $0x10, 0x1004(%rax)
jmp 0x6c21
movq -0x20(%rbp), %rdi
leaq 0x27e7(%rip), %rsi      # 0x93ca
callq 0x21b0
cmpl $0x0, %eax
jne 0x6bf9
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
jmp 0x6c1f
movq 0x53f8(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x2c5c(%rip), %rsi      # 0x986a
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2310
jmp 0x6c21
jmp 0x6c23
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x2c6f(%rip), %rdx      # 0x98a0
leaq 0x5d38(%rip), %rcx      # 0xc970
xorl %eax, %eax
movl %eax, %r8d
callq 0x2110
movl %eax, -0x24(%rbp)
cmpl $-0x1, %eax
je 0x6df9
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
testl %eax, %eax
je 0x6dcd
jmp 0x6c5e
movl -0x28(%rbp), %eax
subl $0x68, %eax
je 0x6c8d
jmp 0x6c68
movl -0x28(%rbp), %eax
subl $0x6f, %eax
je 0x6cad
jmp 0x6c72
movl -0x28(%rbp), %eax
subl $0x73, %eax
je 0x6d0a
jmp 0x6c80
movl -0x28(%rbp), %eax
subl $0x77, %eax
je 0x6cc9
jmp 0x6dcf
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x6c9f
movb $0x0, %al
callq 0x4930
jmp 0x6ca6
movb $0x0, %al
callq 0x49f0
xorl %edi, %edi
callq 0x2310
movq -0x18(%rbp), %rdi
addq $0x4, %rdi
movq 0x5314(%rip), %rax      # 0xbfd0
movq (%rax), %rsi
callq 0x2090
jmp 0x6df4
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x6cea
movq 0x531f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x2bc5(%rip), %rsi      # 0x98a8
movb $0x0, %al
callq 0x21d0
movq 0x52df(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x22e0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1004(%rax)
jmp 0x6df4
movq 0x52bf(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
leaq 0x1a27(%rip), %rsi      # 0x8742
movl $0x4, %edx
callq 0x2080
cmpl $0x0, %eax
jne 0x6d3d
movq -0x18(%rbp), %rax
movl $0x200, 0x1008(%rax)    # imm = 0x200
jmp 0x6dcb
movq 0x528c(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
leaq 0x2b8f(%rip), %rsi      # 0x98dd
movl $0x6, %edx
callq 0x2080
cmpl $0x0, %eax
jne 0x6d6d
movq -0x18(%rbp), %rax
movl $0x100, 0x1008(%rax)    # imm = 0x100
jmp 0x6dc9
movq 0x525c(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x22e0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x1008(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x1008(%rax)
jl 0x6da5
movq -0x18(%rbp), %rax
cmpl $0x200, 0x1008(%rax)    # imm = 0x200
jle 0x6dc7
movq 0x524c(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x2b2d(%rip), %rsi      # 0x98e3
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2310
jmp 0x6dc9
jmp 0x6dcb
jmp 0x6df4
jmp 0x6df4
movq 0x5222(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movl -0x24(%rbp), %edx
leaq 0x284a(%rip), %rsi      # 0x962d
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2310
jmp 0x6c23
movq 0x51c8(%rip), %rax      # 0xbfc8
movl (%rax), %ecx
movl -0x8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x8(%rbp)
movq 0x51b7(%rip), %rax      # 0xbfc8
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x1008(%rax)
jne 0x6e40
movq -0x18(%rbp), %rax
movl $0x200, 0x1008(%rax)    # imm = 0x200
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
subl $0x1, %eax
je 0x6e5a
jmp 0x6e50
movl -0x2c(%rbp), %eax
subl $0x2, %eax
je 0x6e6c
jmp 0x6e7e
movq -0x18(%rbp), %rdi
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
callq 0x6ea0
jmp 0x6e96
movq -0x18(%rbp), %rdi
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
callq 0x7170
jmp 0x6e96
leaq 0x2a87(%rip), %rdi      # 0x990c
movb $0x0, %al
callq 0x2120
movl $0x1, %edi
callq 0x2310
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop