// File: /workspace/github_c_makefile_star2_O2/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 $0x3938, %rsp           # imm = 0x3938
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
cmpb $-0x11, (%rdx)
jne 0xb7a0
cmpb $-0x45, 0x1(%rdx)
jne 0xb7a0
xorl %eax, %eax
cmpb $-0x41, 0x2(%rdx)
sete %al
leaq (%rax,%rax,2), %rax
jmp 0xb7a2
xorl %eax, %eax
addq %rax, %rdx
leaq 0x68(%rsp), %rax
movq 0x8(%rsp), %r14
movq %r14, (%rax)
movq %rdx, 0x10(%rax)
movq %rbx, 0x18b0(%rax)
movq %rdx, 0x18(%rax)
andw $0x0, 0x90(%rax)
movq %rcx, 0x98(%rax)
movq %rdx, 0x20(%rax)
movl $0x1, 0x28(%rax)
andl $0x0, 0x70(%rax)
andq $0x0, 0x78(%rax)
movabsq $0x100000000, %rcx      # imm = 0x100000000
movq %rcx, 0x80(%rax)
movabsq $0x7ffd000000000000, %rcx # imm = 0x7FFD000000000000
movq %rcx, 0x88(%rax)
orl $-0x1, 0xa0(%rax)
andq $0x0, 0x18a8(%rax)
andl $0x0, 0x38c8(%rax)
xorl %ecx, %ecx
movb %cl, 0x38cc(%rax)
movb %cl, 0x38ce(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18b8(%rax)
movq 0x120(%r14), %rcx
movq %rcx, 0x8(%rax)
movq %rax, 0x120(%r14)
movq 0x80(%rbx), %rax
testq %rax, %rax
jne 0xb864
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3695
movq 0x80(%rbx), %rax
movq 0x38(%rax), %rdi
movq %r14, %rsi
callq 0x2ba1
movl 0x28(%rbx), %eax
movl %eax, 0x20(%rsp)
movl 0x48(%rbx), %eax
movl %eax, 0x1c(%rsp)
movl 0x58(%rbx), %eax
movl %eax, 0x18(%rsp)
leaq 0x48(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq %rbx, 0x10(%rsp)
movq 0x80(%rbx), %rcx
movq %rcx, 0x8(%rax)
andq $0x0, 0x18(%rax)
leaq 0x68(%rsp), %rbx
movq %rax, 0x18c0(%rbx)
movq %rbx, %rdi
callq 0xbfe9
movq %rbx, %rdi
callq 0xbfe9
movq %rbx, %rdi
callq 0xd399
pushq $0x2d
popq %rsi
movq %rbx, %rdi
callq 0xc438
testb %al, %al
je 0xb92d
movq 0x10(%rsp), %r14
cmpq $0x0, 0x10(%r14)
je 0xb8f9
leaq 0xd4f5(%rip), %rsi      # 0x18de0
leaq 0x68(%rsp), %rdi
xorl %eax, %eax
callq 0xc455
jmp 0xb92d
leaq 0xd4fd(%rip), %rdx      # 0x18dfd
leaq 0x68(%rsp), %rbx
pushq $0x47
popq %rsi
movq %rbx, %rdi
callq 0xc4fb
movq 0x38(%rbx), %rsi
movl 0x40(%rbx), %edx
movq 0x8(%rsp), %rdi
callq 0x261f
movq %rax, 0x10(%r14)
movq %rbx, %rdi
callq 0xc53b
leaq 0x68(%rsp), %r15
pushq $0x1
popq %r13
pushq $0x2e
popq %rbp
pushq $0x25
popq %r14
movq %r15, %rdi
movl %r13d, %esi
callq 0xc438
testb %al, %al
jne 0xbe75
movb $0x0, 0x3936(%rsp)
movq %r15, %rdi
movl %ebp, %esi
callq 0xc438
movq %r15, %rdi
testb %al, %al
je 0xbbbe
pushq $0x47
popq %rsi
leaq 0xdc1e(%rip), %rdx      # 0x19595
callq 0xc4fb
movq 0xa0(%rsp), %rbx
movl 0xa8(%rsp), %ebp
movq %r15, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0xcb5c
testl %eax, %eax
je 0xb9b2
movq %r15, %rdi
leaq 0xd54f(%rip), %rsi      # 0x18ef5
movl %ebp, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc455
movq 0x68(%rsp), %rdi
movq 0x1918(%rsp), %rsi
movq %rbx, %rdx
movl %ebp, %ecx
callq 0x3980
movq %rax, %rbx
movq 0x20(%rax), %rax
andl $0x0, 0x20(%rax)
movq 0x1918(%rsp), %rax
movl 0x48(%rax), %r14d
decl %r14d
cmpl $0x100, %r14d           # imm = 0x100
jne 0xba0a
movq %r15, %rdi
leaq 0xd528(%rip), %rsi      # 0x18f1e
movl $0x100, %edx            # imm = 0x100
xorl %eax, %eax
callq 0xc455
movq 0x1918(%rsp), %rax
movl 0x58(%rax), %ebp
decl %ebp
cmpl $0xff, %ebp
jne 0xba35
movq %r15, %rdi
leaq 0xdb8a(%rip), %rsi      # 0x195ab
movl $0xff, %edx
xorl %eax, %eax
callq 0xc455
movq 0x1918(%rsp), %rax
movq 0x1928(%rsp), %rcx
movq 0x8(%rcx), %rdx
cmpq 0x80(%rax), %rdx
jne 0xbf58
movq 0x20(%rbx), %rax
movq %rcx, 0x40(%rsp)
movq %rax, 0x30(%rsp)
movl 0x108(%rsp), %eax
movl %eax, 0x28(%rsp)
movl %r14d, 0x38(%rsp)
leaq 0x28(%rsp), %rcx
movq %rcx, 0x1928(%rsp)
incl %eax
movl %eax, 0x108(%rsp)
movq %r15, %rdi
pushq $0x8
popq %rsi
callq 0xc734
movq %r15, %rdi
movl %ebp, %esi
callq 0xc773
movq %r15, %rdi
callq 0xd399
movq %rbx, %rbp
addq $0x28, %rbp
movl 0xb8(%rsp), %eax
cmpl $0x1, %eax
je 0xbbd1
cmpl $0x35, %eax
je 0xbbd1
movq %r15, %r13
movq %r15, %rdi
pushq $0x47
popq %rsi
leaq 0xdac6(%rip), %rdx      # 0x19595
callq 0xc4fb
movq 0x68(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
movq 0x1918(%rsp), %rdi
callq 0x38d9
movq 0x1918(%rsp), %rcx
movq 0x60(%rcx), %rdx
movl %eax, 0x24(%rsp)
movl %eax, %ecx
movq (%rdx,%rcx,8), %r12
leaq 0x1c(%r12), %r14
xorl %r15d, %r15d
movl 0x30(%rbx), %eax
cmpq %rax, %r15
jae 0xbb70
movq 0x1918(%rsp), %rax
movq 0x60(%rax), %rax
movq (%rbp), %rcx
movl (%rcx,%r15,4), %ecx
movq (%rax,%rcx,8), %rsi
movl 0x10(%r12), %eax
cmpl 0x10(%rsi), %eax
jne 0xbb6b
movl 0x14(%r12), %edx
cmpl 0x14(%rsi), %edx
jne 0xbb6b
addq $0x1c, %rsi
movq %r14, %rdi
callq 0x22d0
testl %eax, %eax
jne 0xbb6b
movq %r13, %rdi
leaq 0xda80(%rip), %rsi      # 0x195e1
movq %r14, %rdx
xorl %eax, %eax
callq 0xc455
incq %r15
jmp 0xbb13
movq 0x68(%rsp), %rsi
movq %rbp, %rdi
movl 0x24(%rsp), %edx
callq 0x2b54
movq %r13, %r15
movq %r13, %rdi
pushq $0x1a
popq %rsi
leaq 0xda7b(%rip), %rdx      # 0x1960c
callq 0xc4fb
movq %r13, %rdi
callq 0xd50e
movq %r13, %rdi
callq 0xc53b
movq %r13, %rdi
pushq $0xb
popq %rsi
callq 0xc734
movq %r13, %rdi
callq 0xd399
jmp 0xbaa6
pushq $0x33
popq %rsi
callq 0xc438
movq %r15, %rdi
testb %al, %al
je 0xbc14
xorl %esi, %esi
jmp 0xbc26
movq %r15, %rdi
pushq $0x35
popq %rsi
leaq 0xda57(%rip), %rdx      # 0x19635
callq 0xc4fb
movq %r15, %rdi
callq 0xcdbf
movq %r15, %rdi
callq 0xc55c
movq 0x1928(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x1928(%rsp)
pushq $0x1
popq %r13
pushq $0x2e
popq %rbp
pushq $0x25
popq %r14
jmp 0xbc2b
pushq $0x32
popq %rsi
callq 0xc438
movq %r15, %rdi
testb %al, %al
je 0xbc38
movl %r13d, %esi
callq 0xc82e
movq %r15, %rdi
callq 0xd399
jmp 0xb93d
pushq $0x2f
popq %rsi
callq 0xc438
testb %al, %al
je 0xbd6c
cmpl $-0x1, 0x108(%rsp)
jne 0xbf8d
movq %r15, %rdi
callq 0xdf0d
movq %rax, %rbx
movq %r15, %rdi
pushq $0x30
popq %rsi
leaq 0xda04(%rip), %rdx      # 0x19672
callq 0xc4fb
movq %r15, %rdi
pushq $0x17
popq %rsi
callq 0xc438
testb %al, %al
je 0xbca2
testq %rbx, %rbx
je 0xbc92
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe3dc
movq %r15, %rdi
movl %r14d, %esi
callq 0xc734
jmp 0xbe39
movq %r15, %rdi
pushq $0x47
popq %rsi
leaq 0xd9de(%rip), %rdx      # 0x1968d
callq 0xc4fb
movq 0x68(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
movq 0x1918(%rsp), %rdi
callq 0x38d9
movl %eax, %ebx
movq %r15, %rdi
pushq $0x31
popq %r12
movl %r12d, %esi
callq 0xc734
movq %r15, %rdi
movl %ebx, %esi
callq 0xc748
movq %r15, %rdi
movl %r12d, %esi
callq 0xc438
testb %al, %al
je 0xbd11
movq %r15, %rdi
pushq $0x47
popq %rsi
leaq 0xd99c(%rip), %rdx      # 0x196a8
callq 0xc4fb
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
movl 0xac(%rsp), %esi
movq %r15, %rdi
callq 0xe533
cmpl $-0x1, %eax
je 0xbd41
movq %r15, %rdi
movl %eax, %esi
movl %r13d, %edx
callq 0xd71a
movq %r15, %rdi
movl %r14d, %esi
callq 0xc734
movq %r15, %rdi
pushq $0x5
popq %rsi
callq 0xc438
testb %al, %al
je 0xbc92
movq %r15, %rdi
callq 0xd399
jmp 0xbca2
movq %r15, %rdi
pushq $0x30
popq %rsi
callq 0xc438
testb %al, %al
je 0xbe46
cmpl $-0x1, 0x108(%rsp)
je 0xbd96
jmp 0xbfbb
movq %r15, %rdi
callq 0xd399
movq %r15, %rdi
callq 0xdf0d
movq %rax, %rbx
movq %r15, %rdi
pushq $0x31
popq %rsi
callq 0xc438
testb %al, %al
je 0xbdd3
movq %r15, %rdi
pushq $0x47
popq %rsi
leaq 0xd8eb(%rip), %rdx      # 0x196a8
callq 0xc4fb
movq 0xa0(%rsp), %rdx
movl 0xa8(%rsp), %ecx
jmp 0xbde8
testq %rbx, %rbx
je 0xbe1b
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
je 0xbe10
movl 0x14(%rdx), %ecx
addq $0x1c, %rdx
movl 0xac(%rsp), %esi
movq %r15, %rdi
callq 0xe533
cmpl $-0x1, %eax
je 0xbe0b
movq %r15, %rdi
movl %eax, %esi
movl %r13d, %edx
callq 0xd71a
jmp 0xbe1b
testq %rbx, %rbx
je 0xbe1b
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe3dc
movq %r15, %rdi
movl %r14d, %esi
callq 0xc734
movq %r15, %rdi
pushq $0x5
popq %rsi
callq 0xc438
testb %al, %al
jne 0xbd8e
movq %r15, %rdi
callq 0xc53b
jmp 0xbc2b
movq %r15, %rdi
pushq $0x2d
popq %rsi
callq 0xc438
movq %r15, %rdi
testb %al, %al
je 0xbe6b
leaq 0xd6ff(%rip), %rsi      # 0x1955e
xorl %eax, %eax
callq 0xc455
jmp 0xbc2b
callq 0xce1b
jmp 0xbc2b
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
callq 0xc55c
leaq 0x194c(%rsp), %r15
xorl %r12d, %r12d
movslq 0x3930(%rsp), %rax
cmpq %rax, %r12
jge 0xbee9
movq -0xc(%r15), %r14
movl -0x4(%r15), %ebp
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x48ce
cmpl $-0x1, %eax
je 0xbec6
movq -0x14(%r15), %rcx
movslq -0x1c(%r15), %rdx
movq (%rcx), %rcx
movb %al, (%rcx,%rdx)
jmp 0xbee0
movb $0x0, 0xf9(%rsp)
movl (%r15), %esi
movq %rbx, %rdi
movl %ebp, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0xc574
incq %r12
addq $0x20, %r15
jmp 0xbe8d
movq 0x70(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x120(%rcx)
xorl %eax, %eax
cmpb $0x0, 0xf8(%rsp)
je 0xbf46
movq 0x10(%rsp), %rax
movl 0x20(%rsp), %ecx
movl %ecx, 0x38(%rax)
movl %ecx, 0x28(%rax)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x48(%rax)
movl 0x18(%rsp), %ecx
movl %ecx, 0x58(%rax)
movq 0x100(%rsp), %rax
testq %rax, %rax
je 0xbf43
cmpb $0x1, 0x1(%rax)
jne 0xbf43
cmpb $0x0, 0xf9(%rsp)
movl %r13d, %eax
jne 0xbf46
pushq $0x2
popq %rax
addq $0x3938, %rsp           # imm = 0x3938
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17099(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xc187(%rip), %rsi      # 0x180f0
leaq 0xc294(%rip), %rdx      # 0x18204
leaq 0xd65e(%rip), %rcx      # 0x195d5
leaq 0xcee5(%rip), %r8       # 0x18e63
movl $0x7b1, %r9d            # imm = 0x7B1
xorl %eax, %eax
callq 0x2250
ud2
movq 0x17064(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xc152(%rip), %rsi      # 0x180f0
leaq 0xc25f(%rip), %rdx      # 0x18204
leaq 0xd6b4(%rip), %rcx      # 0x19660
leaq 0xceb0(%rip), %r8       # 0x18e63
movl $0x94d, %r9d            # imm = 0x94D
jmp 0xbf84
movq 0x17036(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xc124(%rip), %rsi      # 0x180f0
leaq 0xc231(%rip), %rdx      # 0x18204
leaq 0xd83a(%rip), %rcx      # 0x19814
leaq 0xce82(%rip), %r8       # 0x18e63
movl $0x989, %r9d            # imm = 0x989
jmp 0xbf84