// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movl %edi, %ebp
movaps 0x1038b(%rip), %xmm0    # 0x29d90
leaq 0xe0(%rsp), %rdx
movaps %xmm0, (%rdx)
xorl %r15d, %r15d
leaq 0x50(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x4c(%rsp), %r8
movl %r15d, (%r8)
leaq 0x58(%rsp), %r12
movl %r15d, (%r12)
leaq 0x48(%rsp), %rcx
movl %r15d, (%rcx)
leaq 0x44(%rsp), %rax
movl %r15d, (%rax)
leaq 0x140(%rsp), %rsi
movl $0x6, (%rsi)
movb $0x63, 0x4(%rsi)
leaq 0x6a5d(%rip), %r9       # 0x204b2
movq %r9, 0x8(%rsi)
movq %rdi, 0x10(%rsi)
leaq 0x6a52(%rip), %rdi      # 0x204b6
movq %rdi, 0x18(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsi)
movl %r15d, 0x30(%rsi)
movl $0x2, %edi
movl %edi, 0x38(%rsi)
movb $0x64, 0x3c(%rsi)
leaq 0x6a54(%rip), %r9       # 0x204da
movq %r9, 0x40(%rsi)
movq %r8, 0x48(%rsi)
leaq 0x6a4b(%rip), %r8       # 0x204e0
movq %r8, 0x50(%rsi)
movups %xmm0, 0x58(%rsi)
movl %r15d, 0x68(%rsi)
movl %edi, 0x70(%rsi)
movb $0x68, 0x74(%rsi)
leaq 0x50a0(%rip), %r8       # 0x1eb4f
movq %r8, 0x78(%rsi)
movq %r12, 0x80(%rsi)
leaq 0x6a42(%rip), %r8       # 0x20503
movq %r8, 0x88(%rsi)
movl %r15d, 0xa0(%rsi)
movaps %xmm0, 0x90(%rsi)
movl %edi, 0xa8(%rsi)
movb $0x71, 0xac(%rsi)
leaq 0x6a3c(%rip), %r8       # 0x20526
movq %r8, 0xb0(%rsi)
movq %rcx, 0xb8(%rsi)
leaq 0x6a2d(%rip), %rcx      # 0x2052c
movq %rcx, 0xc0(%rsi)
movups %xmm0, 0xc8(%rsi)
movl %r15d, 0xd8(%rsi)
movl %edi, 0xe0(%rsi)
movb $0x76, 0xe4(%rsi)
leaq 0x6a41(%rip), %rcx      # 0x20569
movq %rcx, 0xe8(%rsi)
movq %rax, 0xf0(%rsi)
leaq 0x6a34(%rip), %rax      # 0x20571
movq %rax, 0xf8(%rsi)
movaps %xmm0, 0x100(%rsi)
movl %r15d, 0x110(%rsi)
movq %r15, 0x148(%rsi)
movups %xmm0, 0x138(%rsi)
movups %xmm0, 0x128(%rsi)
movups %xmm0, 0x118(%rsi)
leaq 0xf0(%rsp), %r14
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x1c3aa
movq %r14, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x1c3da
cmpl %r15d, (%r12)
je 0x19ba5
leaq 0xf0(%rsp), %rdi
callq 0x1c67c
jmp 0x19d59
cmpl $0x0, 0x44(%rsp)
je 0x19bd3
movq 0x1041d(%rip), %rax     # 0x29fd0
movq (%rax), %rdi
leaq 0x69da(%rip), %rsi      # 0x20597
leaq 0x69e0(%rip), %rdx      # 0x205a4
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x2250
jmp 0x19d59
movl %eax, %ebp
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0xb4ec
leaq -0x486(%rip), %rax      # 0x19766
movq %rax, 0x8(%r14)
leaq -0x3c5(%rip), %rax      # 0x19832
movq %rax, 0x10(%r14)
leaq -0x3be(%rip), %rax      # 0x19844
movq %rax, 0x18(%r14)
leaq 0x205(%rip), %rax       # 0x19e12
movq %rax, 0x20(%r14)
leaq 0x22a(%rip), %rax       # 0x19e42
movq %rax, 0x28(%r14)
leaq 0x194(%rip), %rax       # 0x19db7
movq %rax, 0x30(%r14)
leaq 0x1d7(%rip), %rax       # 0x19e05
movq %rax, 0x38(%r14)
leaq -0x2f1(%rip), %rax      # 0x19948
movq %rax, 0x48(%r14)
leaq -0x3da(%rip), %rax      # 0x1986a
movq %rax, 0x40(%r14)
movq %r14, %rdi
callq 0xb527
movq %rax, %r14
leaq 0x43(%rsp), %rsi
movb $0x0, (%rsi)
movq %rax, %rdi
callq 0xb6ba
movq %r14, %rdi
callq 0x1a897
callq 0xb524
cmpl $0x0, 0x4c(%rsp)
movw %ax, 0x40(%rsp)
setne 0x40(%rsp)
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x19cdc
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %rax, 0x80(%rsp)
movups 0x10105(%rip), %xmm0    # 0x29db0
movups %xmm0, 0x30(%rsp)
movups 0x100e9(%rip), %xmm0    # 0x29da0
movups %xmm0, 0x20(%rsp)
movaps 0x80(%rsp), %xmm0
movaps 0x90(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
xorl %esi, %esi
jmp 0x19d49
testl %ebp, %ebp
je 0x19d6c
movq (%rbx), %rcx
leaq 0x4db0(%rip), %rdx      # 0x1ea9e
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x1986a
movq (%rbx), %rdx
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0x19948
cmpq $0x0, (%rbx)
je 0x19d92
movups 0x80(%rsp), %xmm0
movups 0x90(%rsp), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups 0x60(%rsp), %xmm0
movups 0x70(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0xb786
movl %eax, %r15d
movq %r14, %rdi
callq 0xb603
movl %r15d, %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x48(%rsp)
jne 0x19d7f
leaq 0x685b(%rip), %rdi      # 0x205d5
callq 0x20b0
leaq 0x40(%rsp), %rsi
movb $0x1, 0x1(%rsi)
movq %r14, %rdi
callq 0x1a8fa
jmp 0x19d4e
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x5c(%rsp), %rbx
movq %r14, %rdx
movq %rbx, %rcx
callq 0x7ca2
movl (%rbx), %r15d
jmp 0x19d51