// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_compiler.c
// Function: importFile
static Script* importFile(Compiler* compiler, const char* path) {
  ASSERT(compiler->scope_depth == DEPTH_GLOBAL, OOPS);

  PKVM* vm = compiler->vm;

  // Resolve the path.
  PkStringPtr resolved = { path, NULL, NULL };
  if (vm->config.resolve_path_fn != NULL) {
    resolved = vm->config.resolve_path_fn(vm, compiler->script->path->data,
                                          path);
  }

  if (resolved.string == NULL) {
    parseError(compiler, "Cannot resolve path '%s' from '%s'", path,
               compiler->script->path->data);
  }

  // Create new string for the resolved path. And free the resolved path.
  int index = (int)scriptAddName(compiler->script, compiler->vm,
                           resolved.string, (uint32_t)strlen(resolved.string));
  String* path_name = compiler->script->names.data[index];
  if (resolved.on_done != NULL) resolved.on_done(vm, resolved);

  // Check if the script already exists.
  Var entry = mapGet(vm->scripts, VAR_OBJ(path_name));
  if (!IS_UNDEF(entry)) {
    ASSERT(AS_OBJ(entry)->type == OBJ_SCRIPT, OOPS);

    // Push the script on the stack.
    emitOpcode(compiler, OP_IMPORT);
    emitShort(compiler, index);
    return (Script*)AS_OBJ(entry);
  }

  // The script not exists, make sure we have the script loading api function.
  if (vm->config.load_script_fn == NULL) {
    parseError(compiler, "Cannot import. The hosting application haven't "
               "registered the script loading API");
    return NULL;
  }

  // Load the script at the path.
  PkStringPtr source = vm->config.load_script_fn(vm, path_name->data);
  if (source.string == NULL) {
    parseError(compiler, "Error loading script at \"%s\"", path_name->data);
    return NULL;
  }

  // Make a new script and to compile it.
  Script* scr = newScript(vm, path_name, false);
  vmPushTempRef(vm, &scr->_super); // scr.
  mapSet(vm, vm->scripts, VAR_OBJ(path_name), VAR_OBJ(scr));
  vmPopTempRef(vm); // scr.

  // Push the script on the stack.
  emitOpcode(compiler, OP_IMPORT);
  emitShort(compiler, index);

  // Option for the compilation, even if we're running on repl mode the
  // imported script cannot run on repl mode.
  PkCompileOptions options = pkNewCompilerOptions();
  if (compiler->options) options = *compiler->options;
  options.repl_mode = false;

  // Compile the source to the script and clean the source.
  PkResult result = compile(vm, scr, source.string, &options);
  if (source.on_done != NULL) source.on_done(vm, source);

  if (result != PK_RESULT_SUCCESS) {
    parseError(compiler, "Compilation of imported script '%s' failed",
               path_name->data);
  }

  return scr;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq 0xd8(%rsp), %rax
cmpl $-0x1, 0xa0(%rax)
je 0x1a4c2
movq 0x18b69(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0xfcb7(%rip), %rsi      # 0x2a150
leaq 0xfdc4(%rip), %rdx      # 0x2a264
leaq 0x113fe(%rip), %rcx     # 0x2b8a5
leaq 0x10b29(%rip), %r8      # 0x2afd7
movl $0x87a, %r9d            # imm = 0x87A
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x1a4c4
movq 0xd8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xc8(%rsp)
leaq 0xa8(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2180
movq 0xd0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xc8(%rsp), %rax
cmpq $0x0, 0x108(%rax)
je 0x1a591
movq 0xc8(%rsp), %rax
movq 0x108(%rax), %rax
movq 0xc8(%rsp), %rsi
movq 0xd8(%rsp), %rcx
movq 0x18b0(%rcx), %rcx
movq 0x18(%rcx), %rdx
addq $0x1c, %rdx
movq 0xd0(%rsp), %rcx
leaq 0x88(%rsp), %rdi
callq *%rax
movq 0x88(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, 0xa8(%rsp)
jne 0x1a5d1
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rdx
movq 0xd8(%rsp), %rax
movq 0x18b0(%rax), %rax
movq 0x18(%rax), %rcx
addq $0x1c, %rcx
leaq 0x112e6(%rip), %rsi     # 0x2b8b0
movb $0x0, %al
callq 0x15f90
movq 0xd8(%rsp), %rax
movq 0x18b0(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0xd8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x2130
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl %eax, %ecx
callq 0x4c60
movl %eax, 0x84(%rsp)
movq 0xd8(%rsp), %rax
movq 0x18b0(%rax), %rax
movq 0x60(%rax), %rax
movslq 0x84(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0xb0(%rsp)
je 0x1a697
movq 0xb0(%rsp), %rax
movq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rsp)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rsp)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rsp)
callq *%rax
movq 0xc8(%rsp), %rax
movq 0x128(%rax), %rdi
movq 0x78(%rsp), %rsi
movabsq $-0x4000000000000, %rax # imm = 0xFFFC000000000000
orq %rax, %rsi
callq 0x5f40
movq %rax, 0x70(%rsp)
movabsq $0x7ffd000000000000, %rax # imm = 0x7FFD000000000000
cmpq %rax, 0x70(%rsp)
je 0x1a76b
jmp 0x1a6d9
movabsq $0xffffffffffff, %rax   # imm = 0xFFFFFFFFFFFF
andq 0x70(%rsp), %rax
cmpl $0x4, (%rax)
je 0x1a727
movq 0x18904(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0xfa52(%rip), %rsi      # 0x2a150
leaq 0xfb5f(%rip), %rdx      # 0x2a264
leaq 0x11199(%rip), %rcx     # 0x2b8a5
leaq 0x108c4(%rip), %r8      # 0x2afd7
movl $0x893, %r9d            # imm = 0x893
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x1a729
movq 0xd8(%rsp), %rdi
movl $0x26, %esi
callq 0x16650
movq 0xd8(%rsp), %rdi
movl 0x84(%rsp), %esi
callq 0x16690
movabsq $0xffffffffffff, %rax   # imm = 0xFFFFFFFFFFFF
andq 0x70(%rsp), %rax
movq %rax, 0xe0(%rsp)
jmp 0x1a96a
movq 0xc8(%rsp), %rax
cmpq $0x0, 0x110(%rax)
jne 0x1a7a4
movq 0xd8(%rsp), %rdi
leaq 0x11147(%rip), %rsi     # 0x2b8d3
movb $0x0, %al
callq 0x15f90
movq $0x0, 0xe0(%rsp)
jmp 0x1a96a
movq 0xc8(%rsp), %rax
movq 0x110(%rax), %rax
movq 0xc8(%rsp), %rsi
movq 0x78(%rsp), %rdx
addq $0x1c, %rdx
leaq 0x50(%rsp), %rdi
callq *%rax
cmpq $0x0, 0x50(%rsp)
jne 0x1a803
movq 0xd8(%rsp), %rdi
movq 0x78(%rsp), %rdx
addq $0x1c, %rdx
leaq 0x11139(%rip), %rsi     # 0x2b924
movb $0x0, %al
callq 0x15f90
movq $0x0, 0xe0(%rsp)
jmp 0x1a96a
movq 0xc8(%rsp), %rdi
movq 0x78(%rsp), %rsi
xorl %edx, %edx
callq 0x46d0
movq %rax, 0x48(%rsp)
movq 0xc8(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x9420
movq 0xc8(%rsp), %rdi
movq 0xc8(%rsp), %rax
movq 0x128(%rax), %rsi
movq 0x78(%rsp), %rdx
movabsq $-0x4000000000000, %rax # imm = 0xFFFC000000000000
orq %rax, %rdx
movq 0x48(%rsp), %rcx
movabsq $-0x4000000000000, %rax # imm = 0xFFFC000000000000
orq %rax, %rcx
callq 0x61a0
movq 0xc8(%rsp), %rdi
callq 0x95c0
movq 0xd8(%rsp), %rdi
movl $0x26, %esi
callq 0x16650
movq 0xd8(%rsp), %rdi
movl 0x84(%rsp), %esi
callq 0x16690
callq 0x8d70
movw %ax, 0x46(%rsp)
movq 0xd8(%rsp), %rax
cmpq $0x0, 0x98(%rax)
je 0x1a8d4
movq 0xd8(%rsp), %rax
movq 0x98(%rax), %rax
movw (%rax), %ax
movw %ax, 0x46(%rsp)
movb $0x0, 0x47(%rsp)
movq 0xc8(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq 0x46(%rsp), %rcx
callq 0x15420
movl %eax, 0x40(%rsp)
cmpq $0x0, 0x58(%rsp)
je 0x1a937
movq 0x58(%rsp), %rax
movq 0xc8(%rsp), %rdi
leaq 0x50(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rsp)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rsp)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rsp)
callq *%rax
cmpl $0x0, 0x40(%rsp)
je 0x1a95d
movq 0xd8(%rsp), %rdi
movq 0x78(%rsp), %rdx
addq $0x1c, %rdx
leaq 0x10feb(%rip), %rsi     # 0x2b941
movb $0x0, %al
callq 0x15f90
movq 0x48(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
addq $0xe8, %rsp
retq
nopw (%rax,%rax)