// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x290, %rsp            # imm = 0x290
movq %rsi, %r14
leaq 0x58(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x54(%rsp), %r9
andl $0x0, (%r9)
leaq 0x64(%rsp), %r12
andl $0x0, (%r12)
leaq 0x50(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x4c(%rsp), %rax
andl $0x0, (%rax)
leaq 0x140(%rsp), %rsi
movl $0x6, (%rsi)
movb $0x63, 0x4(%rsi)
leaq 0x6da4(%rip), %r8       # 0x1b4d2
movq %r8, 0x8(%rsi)
movq %rdx, 0x10(%rsi)
leaq 0x6d99(%rip), %rdx      # 0x1b4d6
movq %rdx, 0x18(%rsi)
xorps %xmm0, %xmm0
andl $0x0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
pushq $0x2
popq %r8
movl %r8d, 0x38(%rsi)
movb $0x64, 0x3c(%rsi)
leaq 0x6d9b(%rip), %rdx      # 0x1b4fa
movq %rdx, 0x40(%rsi)
movq %r9, 0x48(%rsi)
leaq 0x6d92(%rip), %rdx      # 0x1b500
movq %rdx, 0x50(%rsi)
andl $0x0, 0x68(%rsi)
movl %edi, %ebx
movups %xmm0, 0x58(%rsi)
movl %r8d, 0x70(%rsi)
movb $0x68, 0x74(%rsi)
leaq 0x53e4(%rip), %rdx      # 0x19b6f
movq %rdx, 0x78(%rsi)
movq %r12, 0x80(%rsi)
leaq 0x6d86(%rip), %rdx      # 0x1b523
movq %rdx, 0x88(%rsi)
andl $0x0, 0xa0(%rsi)
movaps 0xe5de(%rip), %xmm1     # 0x22d90
movaps %xmm0, 0x90(%rsi)
movl %r8d, 0xa8(%rsi)
movb $0x71, 0xac(%rsi)
leaq 0x6d78(%rip), %rdx      # 0x1b546
movq %rdx, 0xb0(%rsi)
movq %rcx, 0xb8(%rsi)
leaq 0x6d69(%rip), %rcx      # 0x1b54c
movq %rcx, 0xc0(%rsi)
andl $0x0, 0xd8(%rsi)
leaq 0xe0(%rsp), %rdx
movups %xmm0, 0xc8(%rsi)
movl %r8d, 0xe0(%rsi)
movb $0x76, 0xe4(%rsi)
leaq 0x6d74(%rip), %rcx      # 0x1b589
movq %rcx, 0xe8(%rsi)
movq %rax, 0xf0(%rsi)
leaq 0x6d67(%rip), %rax      # 0x1b591
movq %rax, 0xf8(%rsi)
andl $0x0, 0x110(%rsi)
movaps %xmm1, (%rdx)
movaps %xmm0, 0x100(%rsi)
movups %xmm0, 0x138(%rsi)
movups %xmm0, 0x128(%rsi)
movups %xmm0, 0x118(%rsi)
andq $0x0, 0x148(%rsi)
leaq 0xf0(%rsp), %r15
xorl %ebp, %ebp
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x16ab9
movq %r15, %rdi
movl %ebx, %esi
movq %r14, %rdx
callq 0x16ae6
cmpl %ebp, (%r12)
je 0x14898
leaq 0xf0(%rsp), %rdi
callq 0x16d19
jmp 0x14a50
cmpl $0x0, 0x4c(%rsp)
je 0x148c5
movq 0xe72a(%rip), %rax      # 0x22fd0
movq (%rax), %rdi
leaq 0x6d07(%rip), %rsi      # 0x1b5b7
leaq 0x6d0d(%rip), %rdx      # 0x1b5c4
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2250
jmp 0x14a50
movl %eax, %r15d
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0x58a8
leaq -0x485(%rip), %rax      # 0x1445a
movq %rax, 0x8(%rbx)
leaq -0x3c4(%rip), %rax      # 0x14526
movq %rax, 0x10(%rbx)
leaq -0x3bd(%rip), %rax      # 0x14538
movq %rax, 0x18(%rbx)
leaq 0x2b4(%rip), %rax       # 0x14bb4
movq %rax, 0x20(%rbx)
leaq 0x301(%rip), %rax       # 0x14c0c
movq %rax, 0x28(%rbx)
leaq 0x1e9(%rip), %rax       # 0x14aff
movq %rax, 0x30(%rbx)
leaq 0x258(%rip), %rax       # 0x14b79
movq %rax, 0x38(%rbx)
leaq -0x2f0(%rip), %rax      # 0x1463c
movq %rax, 0x48(%rbx)
leaq -0x3d9(%rip), %rax      # 0x1455e
movq %rax, 0x40(%rbx)
movq %rbx, %rdi
callq 0x58e3
movq %rax, %rbx
leaq 0x4b(%rsp), %rsi
movb $0x0, (%rsi)
movq %rax, %rdi
callq 0x5a93
movq %rbx, %rdi
callq 0x154ff
callq 0x58e0
cmpl $0x0, 0x54(%rsp)
movw %ax, 0x48(%rsp)
setne 0x48(%rsp)
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x149d0
xorps %xmm0, %xmm0
movups %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
movq %rax, 0x88(%rsp)
movups 0xe411(%rip), %xmm0     # 0x22db0
movups %xmm0, 0x30(%rsp)
movups 0xe3f5(%rip), %xmm0     # 0x22da0
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 0x14a41
testl %r15d, %r15d
je 0x14a62
movq (%r14), %rcx
leaq 0x50db(%rip), %rdx      # 0x19abe
leaq 0x88(%rsp), %r14
movq %r14, %rdi
callq 0x1455e
movq (%r14), %r14
leaq 0x68(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x1463c
cmpq $0x0, (%r15)
je 0x14a88
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 0x5b60
movl %eax, %ebp
movq %rbx, %rdi
callq 0x59b1
movl %ebp, %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x50(%rsp)
jne 0x14a75
leaq 0x6b85(%rip), %rdi      # 0x1b5f5
callq 0x20b0
leaq 0x48(%rsp), %rsi
movb $0x1, 0x1(%rsi)
movq %rbx, %rdi
callq 0x155e6
jmp 0x14a46
movq 0xe569(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
leaq 0x6b3b(%rip), %rsi      # 0x1b5d4
xorl %ebp, %ebp
movq %r14, %rdx
xorl %eax, %eax
callq 0x2250
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x14ad0
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 0x14a48
movups 0x68(%rsp), %xmm0
movups 0x78(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq *%rax
jmp 0x14a48
nop