// File: /workspace/github_c_makefile_star2_O3/atomlang[P]atomlang/src/pk_compiler.c
// Function: compile
PkResult compile(PKVM* vm, Script* script, const char* source,
                 const PkCompileOptions* options) {

  // Skip utf8 BOM if there is any.
  if (strncmp(source, "\xEF\xBB\xBF", 3) == 0) source += 3;

  Compiler _compiler;
  Compiler* compiler = &_compiler; //< Compiler pointer for quick access.
  compilerInit(compiler, vm, source, script, options);

  // If compiling for an imported script the vm->compiler would be the compiler
  // of the script that imported this script. Add the all the compilers into a
  // link list.
  compiler->next_compiler = vm->compiler;
  vm->compiler = compiler;

  // If the script doesn't has a body by default, it's probably was created by
  // the native api function (pkNewModule() that'll return a module without a
  // main function) so just create and add the function here.
  if (script->body == NULL) scriptAddMain(vm, script);

  // If we're compiling for a script that was already compiled (when running
  // REPL or evaluating an expression) we don't need the old main anymore.
  // just use the globals and functions of the script and use a new body func.
  pkByteBufferClear(&script->body->fn->opcodes, vm);

  // Remember the count of the globals, functions and types, If the compilation
  // failed discard all the globals and functions added by the compilation.
  uint32_t globals_count = script->globals.count;
  uint32_t functions_count = script->functions.count;
  uint32_t types_count = script->classes.count;

  Func curr_fn;
  curr_fn.depth = DEPTH_SCRIPT;
  curr_fn.ptr = script->body;
  curr_fn.outer_func = NULL;
  compiler->func = &curr_fn;

  // Lex initial tokens. current <-- next.
  lexToken(compiler);
  lexToken(compiler);
  skipNewLines(compiler);

  if (match(compiler, TK_MODULE)) {

    // If the script running a REPL or compiled multiple times by hosting
    // application module attribute might already set. In that case make it
    // Compile error.
    if (script->module != NULL) {
      parseError(compiler, "Module name already defined.");

    } else {
      consume(compiler, TK_NAME, "Expected a name for the module.");
      const char* name = compiler->previous.start;
      uint32_t len = compiler->previous.length;
      script->module = newStringLength(vm, name, len);
      consumeEndStatement(compiler);
    }
  }

  while (!match(compiler, TK_EOF)) {
    compileTopLevelStatement(compiler);
    skipNewLines(compiler);
  }

  emitFunctionEnd(compiler);

  // Resolve forward names (function names that are used before defined).
  for (int i = 0; i < compiler->forwards_count; i++) {
    ForwardName* forward = &compiler->forwards[i];
    const char* name = forward->name;
    int length = forward->length;
    int index = scriptGetFunc(script, name, (uint32_t)length);
    if (index != -1) {
      patchForward(compiler, forward->func, forward->instruction, index);
    } else {
      // need_more_lines is only true for unexpected EOF errors. For syntax
      // errors it'll be false by now but. Here it's a semantic errors, so
      // we're overriding it to false.
      compiler->need_more_lines = false;
      resolveError(compiler, forward->line, "Name '%.*s' is not defined.",
                   length, name);
    }
  }

  vm->compiler = compiler->next_compiler;

  // If compilation failed, discard all the invalid functions and globals.
  if (compiler->has_errors) {
    script->globals.count = script->global_names.count = globals_count;
    script->functions.count = functions_count;
    script->classes.count = types_count;
  }

#if DEBUG_DUMP_COMPILED_CODE
  pkByteBuffer buff;
  pkByteBufferInit(&buff);
  dumpFunctionCode(compiler->vm, script->body, &buff);
  printf("%s", buff.data);
  pkByteBufferClear(&buff, vm);
#endif

  // Return the compilation result.
  if (compiler->has_errors) {
    if (compiler->options && compiler->options->repl_mode &&
        compiler->need_more_lines) {
      return PK_RESULT_UNEXPECTED_EOF;
    }
    return PK_RESULT_COMPILE_ERROR;
  }
  return PK_RESULT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3948, %rsp           # imm = 0x3948
movq %rsi, %r14
movq %rdi, %r15
xorl %eax, %eax
cmpb $-0x11, (%rdx)
jne 0xfed8
cmpb $-0x45, 0x1(%rdx)
jne 0xfed8
xorl %esi, %esi
cmpb $-0x41, 0x2(%rdx)
sete %sil
leaq (%rsi,%rsi,2), %rsi
jmp 0xfeda
xorl %esi, %esi
addq %rsi, %rdx
leaq 0x78(%rsp), %rsi
movq %r15, (%rsi)
movq %rdx, 0x10(%rsi)
movq %r14, 0x18b0(%rsi)
movq %rdx, 0x18(%rsi)
movw $0x0, 0x90(%rsi)
movq %rcx, 0x98(%rsi)
movq %rdx, 0x20(%rsi)
movl $0x1, 0x28(%rsi)
movl %eax, 0x70(%rsi)
movq %rax, 0x78(%rsi)
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, 0x80(%rsi)
movabsq $0x7ffd000000000000, %rcx # imm = 0x7FFD000000000000
movq %rcx, 0x88(%rsi)
movl $0xffffffff, 0xa0(%rsi) # imm = 0xFFFFFFFF
movq %rax, 0x18a8(%rsi)
movl %eax, 0x38c8(%rsi)
movb %al, 0x38cc(%rsi)
movb %al, 0x38ce(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x18b8(%rsi)
movq 0x120(%r15), %rax
movq %rax, 0x8(%rsi)
movq %rsi, 0x120(%r15)
movq 0x80(%r14), %rax
testq %rax, %rax
jne 0xff95
movq %r15, %rdi
movq %r14, %rsi
callq 0x90fa
movq 0x80(%r14), %rax
movq 0x38(%rax), %rdi
movq %r15, %rsi
callq 0x84ca
movl 0x28(%r14), %eax
movl %eax, 0x24(%rsp)
movl 0x48(%r14), %eax
movl %eax, 0x20(%rsp)
movl 0x58(%r14), %eax
movl %eax, 0x1c(%rsp)
leaq 0x58(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0x80(%r14), %rcx
movq %rcx, 0x8(%rax)
movq $0x0, 0x18(%rax)
leaq 0x78(%rsp), %rbx
movq %rax, 0x18c0(%rbx)
movq %rbx, %rdi
callq 0x10771
movq %rbx, %rdi
callq 0x10771
movq %rbx, %rdi
callq 0x11bcc
movl 0x50(%rbx), %eax
cmpl $0x2d, %eax
jne 0x1006f
leaq 0x78(%rsp), %rdi
callq 0x10771
cmpq $0x0, 0x10(%r14)
je 0x1001d
leaq 0xcde5(%rip), %rsi      # 0x1ce00
jmp 0x1005c
leaq 0xcdf9(%rip), %rdx      # 0x1ce1d
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movl $0x47, %esi
callq 0x10df5
movq 0x38(%rbx), %rsi
movl 0x40(%rbx), %edx
movq %r15, %rdi
callq 0x7f0c
movq %rax, 0x10(%r14)
movq %rbx, %rdi
callq 0x11cf3
testb %al, %al
jne 0x10068
leaq 0xd4f9(%rip), %rsi      # 0x1d555
leaq 0x78(%rsp), %rdi
xorl %eax, %eax
callq 0x10d4f
movl 0xc8(%rsp), %eax
movq %r15, 0x30(%rsp)
movq %r14, 0x28(%rsp)
cmpl $0x1, %eax
je 0x10664
leaq 0x78(%rsp), %r15
leaq 0xcbe2(%rip), %r14      # 0x1cc70
movl $0xff, %ebp
movb $0x0, 0x3946(%rsp)
addl $-0x2d, %eax
cmpl $0x6, %eax
ja 0x10247
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
leaq 0xd4bc(%rip), %rsi      # 0x1d57e
jmp 0x105e8
movq %r15, %rdi
callq 0x10771
cmpl $-0x1, 0x118(%rsp)
jne 0x10767
movq %r15, %rdi
callq 0x12627
movq %rax, %rbx
movq %r15, %rdi
movl $0x30, %esi
leaq 0xd59b(%rip), %rdx      # 0x1d692
callq 0x10df5
cmpl $0x17, 0xc8(%rsp)
jne 0x1013a
movq %r15, %rdi
callq 0x10771
testq %rbx, %rbx
je 0x1011e
movq %r15, %rdi
movq %rbx, %rsi
callq 0x12a6f
movq %r15, %rdi
movl $0x25, %esi
callq 0x10fbe
movq %r15, %rdi
xorl %esi, %esi
callq 0x11014
jmp 0x105d2
movq %r15, %rdi
movl $0x47, %esi
leaq 0xd564(%rip), %rdx      # 0x1d6ad
callq 0x10df5
movq 0x78(%rsp), %rsi
movq 0xb0(%rsp), %rdx
movl 0xb8(%rsp), %ecx
movq 0x1928(%rsp), %rdi
callq 0x9306
movl %eax, %ebx
movq %r15, %rdi
movl $0x31, %esi
callq 0x10fbe
movq %r15, %rdi
xorl %esi, %esi
callq 0x11014
movl %ebx, %esi
shrl $0x8, %esi
andl %ebp, %esi
movq %r15, %rdi
callq 0x10fbe
andl %ebp, %ebx
movq %r15, %rdi
movl %ebx, %esi
callq 0x10fbe
cmpl $0x31, 0xc8(%rsp)
jne 0x101c9
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
movl $0x47, %esi
leaq 0xd504(%rip), %rdx      # 0x1d6c8
callq 0x10df5
movq 0xb0(%rsp), %rdx
movl 0xb8(%rsp), %ecx
movl 0xbc(%rsp), %esi
movq %r15, %rdi
callq 0x12b50
cmpl $-0x1, %eax
je 0x101fb
movq %r15, %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x11f77
movq %r15, %rdi
movl $0x25, %esi
callq 0x10fbe
movq %r15, %rdi
xorl %esi, %esi
callq 0x11014
cmpl $0x5, 0xc8(%rsp)
jne 0x1011e
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
callq 0x11bcc
jmp 0x1013a
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
xorl %esi, %esi
jmp 0x10601
movq %r15, %rdi
callq 0x115b4
jmp 0x1064c
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
movl $0x47, %esi
leaq 0xd34a(%rip), %rdx      # 0x1d5b5
callq 0x10df5
movq 0xb0(%rsp), %rbx
movl 0xb8(%rsp), %ebp
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x1132e
testl %eax, %eax
je 0x102a6
movq %r15, %rdi
leaq 0xcc7b(%rip), %rsi      # 0x1cf15
movl %ebp, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x10d4f
movq 0x78(%rsp), %rdi
movq 0x1928(%rsp), %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x93cd
movq %rax, 0x10(%rsp)
movq 0x20(%rax), %rax
movl $0x0, 0x20(%rax)
movq 0x1928(%rsp), %rax
movl 0x48(%rax), %ebp
decl %ebp
cmpl $0x100, %ebp            # imm = 0x100
jne 0x10300
movq %r15, %rdi
leaq 0xcc52(%rip), %rsi      # 0x1cf3e
movl $0x100, %edx            # imm = 0x100
xorl %eax, %eax
callq 0x10d4f
movq 0x1928(%rsp), %rax
movl 0x58(%rax), %ebx
decl %ebx
cmpl $0xff, %ebx
jne 0x1032b
movq %r15, %rdi
leaq 0xd2b4(%rip), %rsi      # 0x1d5cb
movl $0xff, %edx
xorl %eax, %eax
callq 0x10d4f
movq 0x1928(%rsp), %rax
movq 0x1938(%rsp), %rcx
movq 0x8(%rcx), %rdx
cmpq 0x80(%rax), %rdx
jne 0x1076c
movq 0x10(%rsp), %r14
movq 0x20(%r14), %rax
movq %rcx, 0x50(%rsp)
movq %rax, 0x40(%rsp)
movl 0x118(%rsp), %eax
movl %eax, 0x38(%rsp)
movl %ebp, 0x48(%rsp)
leaq 0x38(%rsp), %rcx
movq %rcx, 0x1938(%rsp)
incl %eax
movl %eax, 0x118(%rsp)
movq %r15, %rdi
movl $0x8, %esi
callq 0x10fbe
movq %r15, %rdi
xorl %esi, %esi
callq 0x11014
movq %r15, %rdi
movl %ebx, %esi
callq 0x10fbe
movq %r15, %rdi
callq 0x11bcc
movq %r14, %rbp
addq $0x28, %rbp
movl 0xc8(%rsp), %eax
cmpl $0x1, %eax
je 0x10608
cmpl $0x35, %eax
je 0x10608
movq %r15, %rdi
movl $0x47, %esi
leaq 0xd1e1(%rip), %rdx      # 0x1d5b5
callq 0x10df5
movq 0x78(%rsp), %rsi
movq 0xb0(%rsp), %rdx
movl 0xb8(%rsp), %ecx
movq 0x1928(%rsp), %rdi
callq 0x9306
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rax
movl 0x30(%rax), %r13d
testl %r13d, %r13d
je 0x10487
movq 0x1928(%rsp), %rax
movq 0x60(%rax), %rax
movl 0xc(%rsp), %ecx
movq (%rax,%rcx,8), %r14
leaq 0x1c(%r14), %r12
xorl %ebx, %ebx
movq 0x1928(%rsp), %rax
movq 0x60(%rax), %rax
movq (%rbp), %rcx
movl (%rcx,%rbx,4), %ecx
movq (%rax,%rcx,8), %rsi
movl 0x10(%r14), %eax
cmpl 0x10(%rsi), %eax
jne 0x1047c
movl 0x14(%r14), %edx
cmpl 0x14(%rsi), %edx
jne 0x1047c
addq $0x1c, %rsi
movq %r12, %rdi
callq 0x22d0
testl %eax, %eax
jne 0x1047c
movq %r15, %rdi
leaq 0xd198(%rip), %rsi      # 0x1d601
movq %r12, %rdx
xorl %eax, %eax
callq 0x10d4f
movq 0x10(%rsp), %rax
movl 0x30(%rax), %r13d
incq %rbx
movl %r13d, %eax
cmpq %rax, %rbx
jb 0x10426
movq 0x78(%rsp), %rsi
movq %rbp, %rdi
movl 0xc(%rsp), %edx
callq 0x8459
movq %r15, %rdi
movl $0x1a, %esi
leaq 0xd185(%rip), %rdx      # 0x1d62c
callq 0x10df5
movq %r15, %rdi
movl $0x1, %esi
callq 0x11eb4
movq %r15, %rdi
callq 0x11cf3
testb %al, %al
jne 0x104d6
movq %r15, %rdi
leaq 0xd086(%rip), %rsi      # 0x1d555
xorl %eax, %eax
callq 0x10d4f
movq %r15, %rdi
movl $0xb, %esi
callq 0x10fbe
movq %r15, %rdi
xorl %esi, %esi
callq 0x11014
movq %r15, %rdi
callq 0x11bcc
jmp 0x103ac
movq %r15, %rdi
callq 0x10771
cmpl $-0x1, 0x118(%rsp)
je 0x10521
jmp 0x10762
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
callq 0x11bcc
movq %r15, %rdi
callq 0x12627
movq %rax, %rbx
cmpl $0x31, 0xc8(%rsp)
jne 0x10563
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
movl $0x47, %esi
leaq 0xd17b(%rip), %rdx      # 0x1d6c8
callq 0x10df5
movq 0xb0(%rsp), %rdx
movl 0xb8(%rsp), %ecx
jmp 0x10578
testq %rbx, %rbx
je 0x105ad
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
je 0x105a2
movl 0x14(%rdx), %ecx
addq $0x1c, %rdx
movl 0xbc(%rsp), %esi
movq %r15, %rdi
callq 0x12b50
cmpl $-0x1, %eax
je 0x1059d
movq %r15, %rdi
movl %eax, %esi
movl $0x1, %edx
callq 0x11f77
jmp 0x105ad
testq %rbx, %rbx
je 0x105ad
movq %r15, %rdi
movq %rbx, %rsi
callq 0x12a6f
movq %r15, %rdi
movl $0x25, %esi
callq 0x10fbe
movq %r15, %rdi
xorl %esi, %esi
callq 0x11014
cmpl $0x5, 0xc8(%rsp)
je 0x10511
movq %r15, %rdi
callq 0x11cf3
testb %al, %al
jne 0x1064c
movq %r15, %rdi
leaq 0xcf6d(%rip), %rsi      # 0x1d555
xorl %eax, %eax
callq 0x10d4f
jmp 0x1064c
movq %r15, %rdi
callq 0x10771
movq %r15, %rdi
movl $0x1, %esi
callq 0x11049
jmp 0x1064c
movq %r15, %rdi
movl $0x35, %esi
leaq 0xd03e(%rip), %rdx      # 0x1d655
callq 0x10df5
movq %r15, %rdi
callq 0x11588
movq %r15, %rdi
callq 0x10e35
movq 0x1938(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x1938(%rsp)
leaq 0xc629(%rip), %r14      # 0x1cc70
movl $0xff, %ebp
movq %r15, %rdi
callq 0x11bcc
movl 0xc8(%rsp), %eax
cmpl $0x1, %eax
jne 0x10093
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0x10771
movq %rbx, %rdi
callq 0x10e35
cmpl $0x0, 0x38c8(%rbx)
jle 0x106ec
leaq 0x195c(%rsp), %r15
leaq 0x78(%rsp), %rbx
xorl %r12d, %r12d
movq -0xc(%r15), %r14
movl -0x4(%r15), %ebp
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xa40c
cmpl $-0x1, %eax
je 0x106be
movq -0x14(%r15), %rcx
movslq -0x1c(%r15), %rdx
movq (%rcx), %rcx
movb %al, (%rcx,%rdx)
jmp 0x106d8
movb $0x0, 0x109(%rsp)
movl (%r15), %esi
movq %rbx, %rdi
movl %ebp, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x10e5b
incq %r12
movslq 0x3940(%rsp), %rax
addq $0x20, %r15
cmpq %rax, %r12
jl 0x10692
movq 0x80(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rax, 0x120(%rcx)
xorl %eax, %eax
cmpb $0x0, 0x108(%rsp)
je 0x10750
movq 0x28(%rsp), %rax
movl 0x24(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl %ecx, 0x28(%rax)
movl 0x20(%rsp), %ecx
movl %ecx, 0x48(%rax)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x58(%rax)
movq 0x110(%rsp), %rax
testq %rax, %rax
je 0x1074b
cmpb $0x1, 0x1(%rax)
jne 0x1074b
movl $0x1, %eax
cmpb $0x0, 0x109(%rsp)
jne 0x10750
movl $0x2, %eax
addq $0x3948, %rsp           # imm = 0x3948
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4acd
callq 0x4b03
callq 0x4b39