// File: /workspace/github_c_makefile_star2_O3/atomlang[P]atomlang/cli/main.c
// Function: main
int main(int argc, const char** argv) {

  // Parse command line arguments.

  const char* usage[] = {
    "atomlang ... [-c cmd | file] ...",
    NULL,
  };

  const char* cmd = NULL;
  int debug = false, help = false, quiet = false, version = false;
  struct argparse_option cli_opts[] = {
      OPT_STRING('c', "cmd", (void*)&cmd,
        "Evaluate and run the passed string.", NULL, 0, 0),

      OPT_BOOLEAN('d', "debug", (void*)&debug,
        "Compile and run the debug version.", NULL, 0, 0),

      OPT_BOOLEAN('h', "help",  (void*)&help,
        "Prints this help message and exit.", NULL, 0, 0),

      OPT_BOOLEAN('q', "quiet", (void*)&quiet,
        "Don't print version and copyright statement on REPL startup.",
        NULL, 0, 0),

      OPT_BOOLEAN('v', "version", &version,
        "Prints the atomlang version and exit.", NULL, 0, 0),
      OPT_END(),
  };

  // Parse the options.
  struct argparse argparse;
  argparse_init(&argparse, cli_opts, usage, 0);
  argc = argparse_parse(&argparse, argc, argv);

  if (help) { // atomlang --help.
    argparse_usage(&argparse);
    return 0;
  }

  if (version) { // atomlang --version
    fprintf(stdout, "atomlang %s\n", PK_VERSION_STRING);
    return 0;
  }

  int exitcode = 0;

  // Create and initialize atomlang VM.
  PKVM* vm = intializeatomlangVM();
  VmUserData user_data;
  user_data.repl_mode = false;
  pkSetUserData(vm, &user_data);

  registerModules(vm);

  PkCompileOptions options = pkNewCompilerOptions();
  options.debug = debug;

  if (cmd != NULL) { // atomlang -c "print('foo')"

    PkStringPtr source = { cmd, NULL, NULL, 0, 0 };
    PkStringPtr path = { "$(Source)", NULL, NULL, 0, 0 };
    PkResult result = pkInterpretSource(vm, source, path, NULL);
    exitcode = (int)result;

  } else if (argc == 0) { // Run on REPL mode.

   // Print the copyright and license notice, if --quiet not set.
    if (!quiet) {
      printf("%s", CLI_NOTICE);
    }

    options.repl_mode = true;
    exitcode = repl(vm, &options);

  } else { // atomlang file.pk ...

    PkStringPtr resolved = resolvePath(vm, ".", argv[0]);
    PkStringPtr source = loadScript(vm, resolved.string);

    if (source.string != NULL) {
      PkResult result = pkInterpretSource(vm, source, resolved, &options);
      exitcode = (int)result;
    } else {
      fprintf(stderr, "Error: cannot open file at \"%s\"\n", resolved.string);
      if (resolved.on_done != NULL) resolved.on_done(vm, resolved);
      if (source.on_done != NULL) source.on_done(vm, source);
    }
  }

  // Cleanup the VM and exit.
  pkFreeVM(vm);
  return exitcode;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, %r15
movl %edi, %ebx
movaps 0x1003c(%rip), %xmm0    # 0x28d90
leaq 0xe0(%rsp), %rdx
movaps %xmm0, (%rdx)
xorl %r14d, %r14d
leaq 0x58(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x54(%rsp), %r8
movl %r14d, (%r8)
leaq 0x64(%rsp), %r13
movl %r14d, (%r13)
leaq 0x50(%rsp), %rcx
movl %r14d, (%rcx)
leaq 0x4c(%rsp), %rax
movl %r14d, (%rax)
leaq 0x140(%rsp), %rsi
movl $0x6, (%rsi)
movb $0x63, 0x4(%rsi)
leaq 0x674e(%rip), %r9       # 0x1f4f2
movq %r9, 0x8(%rsi)
movq %rdi, 0x10(%rsi)
leaq 0x6743(%rip), %rdi      # 0x1f4f6
movq %rdi, 0x18(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsi)
movl %r14d, 0x30(%rsi)
movl $0x2, %edi
movl %edi, 0x38(%rsi)
movb $0x64, 0x3c(%rsi)
leaq 0x6745(%rip), %r9       # 0x1f51a
movq %r9, 0x40(%rsi)
movq %r8, 0x48(%rsi)
leaq 0x673c(%rip), %r8       # 0x1f520
movq %r8, 0x50(%rsi)
movups %xmm0, 0x58(%rsi)
movl %r14d, 0x68(%rsi)
movl %edi, 0x70(%rsi)
movb $0x68, 0x74(%rsi)
leaq 0x4d91(%rip), %r8       # 0x1db8f
movq %r8, 0x78(%rsi)
movq %r13, 0x80(%rsi)
leaq 0x6733(%rip), %r8       # 0x1f543
movq %r8, 0x88(%rsi)
movl %r14d, 0xa0(%rsi)
movaps %xmm0, 0x90(%rsi)
movl %edi, 0xa8(%rsi)
movb $0x71, 0xac(%rsi)
leaq 0x672d(%rip), %r8       # 0x1f566
movq %r8, 0xb0(%rsi)
movq %rcx, 0xb8(%rsi)
leaq 0x671e(%rip), %rcx      # 0x1f56c
movq %rcx, 0xc0(%rsi)
movups %xmm0, 0xc8(%rsi)
movl %r14d, 0xd8(%rsi)
movl %edi, 0xe0(%rsi)
movb $0x76, 0xe4(%rsi)
leaq 0x6732(%rip), %rcx      # 0x1f5a9
movq %rcx, 0xe8(%rsi)
movq %rax, 0xf0(%rsi)
leaq 0x6725(%rip), %rax      # 0x1f5b1
movq %rax, 0xf8(%rsi)
movaps %xmm0, 0x100(%rsi)
movl %r14d, 0x110(%rsi)
movq %r14, 0x148(%rsi)
movups %xmm0, 0x138(%rsi)
movups %xmm0, 0x128(%rsi)
movups %xmm0, 0x118(%rsi)
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
xorl %ecx, %ecx
callq 0x1b527
movq %r12, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x1b554
cmpl %r14d, (%r13)
je 0x18ef4
leaq 0xf0(%rsp), %rdi
callq 0x1b7d6
jmp 0x190b3
cmpl $0x0, 0x4c(%rsp)
je 0x18f22
movq 0x100ce(%rip), %rax     # 0x28fd0
movq (%rax), %rdi
leaq 0x66cb(%rip), %rsi      # 0x1f5d7
leaq 0x66d1(%rip), %rdx      # 0x1f5e4
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2250
jmp 0x190b3
movl %eax, %ebp
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0xb340
leaq -0x485(%rip), %rax      # 0x18ab6
movq %rax, 0x8(%rbx)
leaq -0x3c4(%rip), %rax      # 0x18b82
movq %rax, 0x10(%rbx)
leaq -0x3bd(%rip), %rax      # 0x18b94
movq %rax, 0x18(%rbx)
leaq 0x264(%rip), %rax       # 0x191c0
movq %rax, 0x20(%rbx)
leaq 0x283(%rip), %rax       # 0x191ea
movq %rax, 0x28(%rbx)
leaq 0x1f5(%rip), %rax       # 0x19167
movq %rax, 0x30(%rbx)
leaq 0x236(%rip), %rax       # 0x191b3
movq %rax, 0x38(%rbx)
leaq -0x2f0(%rip), %rax      # 0x18c98
movq %rax, 0x48(%rbx)
leaq -0x3d9(%rip), %rax      # 0x18bba
movq %rax, 0x40(%rbx)
movq %rbx, %rdi
callq 0xb37b
movq %rax, %rbx
leaq 0x4b(%rsp), %rsi
movb $0x0, (%rsi)
movq %rax, %rdi
callq 0xb507
movq %rbx, %rdi
callq 0x19c0d
callq 0xb378
cmpl $0x0, 0x54(%rsp)
movw %ax, 0x48(%rsp)
setne 0x48(%rsp)
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x1902f
xorps %xmm0, %xmm0
movups %xmm0, 0x90(%rsp)
movq $0x0, 0xa0(%rsp)
movq %rax, 0x88(%rsp)
movups 0xfdb2(%rip), %xmm0     # 0x28db0
movups %xmm0, 0x30(%rsp)
movups 0xfd96(%rip), %xmm0     # 0x28da0
movups %xmm0, 0x20(%rsp)
movups 0x88(%rsp), %xmm0
movups 0x98(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x190a3
testl %ebp, %ebp
je 0x190c8
movq (%r15), %rcx
leaq 0x4a9d(%rip), %rdx      # 0x1dade
leaq 0x88(%rsp), %r14
movq %r14, %rdi
callq 0x18bba
movq (%r14), %r15
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x18c98
cmpq $0x0, (%r14)
je 0x190ee
movups 0x88(%rsp), %xmm0
movups 0x98(%rsp), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups 0x68(%rsp), %xmm0
movups 0x78(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0xb5cb
movl %eax, %r14d
movq %rbx, %rdi
callq 0xb450
movl %r14d, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x50(%rsp)
jne 0x190db
leaq 0x653f(%rip), %rdi      # 0x1f615
callq 0x20b0
leaq 0x48(%rsp), %rsi
movb $0x1, 0x1(%rsi)
movq %rbx, %rdi
callq 0x19c6e
jmp 0x190a8
movq 0xff03(%rip), %rax      # 0x28ff8
movq (%rax), %rdi
leaq 0x64f5(%rip), %rsi      # 0x1f5f4
xorl %r14d, %r14d
movq %r15, %rdx
xorl %eax, %eax
callq 0x2250
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x19137
movups 0x88(%rsp), %xmm0
movups 0x98(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq *%rax
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x190ab
movups 0x68(%rsp), %xmm0
movups 0x78(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq *%rax
jmp 0x190ab
nop