// File: /workspace/github_c_makefile_star2_O1/mahrr[P]raven/src/vm.c
// Function: native_import
static bool native_import(VM *vm, Value *arguments, size_t count, Value *result) {
    MAYBE_UNUSED(count);

    Value argument = arguments[0];
    if (Is_String(argument) == false) {
        runtime_error(vm, "`import` expected string, got %s", type_repr(argument));
        return false;
    }

    // read the file
    const char *path = As_String(argument)->chars;
    char *source = NULL;
    {
        FILE *file = fopen(path, "rb");
        if (file == NULL) {
            runtime_error(vm, "`import` error reading '%s' (%s)", path, strerror(errno));
            return false;
        }

        fseek(file, 0L, SEEK_END);
        size_t size = ftell(file);
        rewind(file);

        source = (char *)malloc(size + 1);
        if (source == NULL) {
            runtime_error(vm, "`import` error reading '%s' (out of memory)", path);
            fclose(file);
            return false;
        }

        size_t bytes_read = fread(source, sizeof (char), size, file);
        if (bytes_read < size || ferror(file)) {
            runtime_error(vm, "`import` error reading '%s' (%s)", path, strerror(errno));
            fclose(file);
            free(source);
            return false;
        }
        fclose(file);
        source[size] = '\0';
    }

    // execute the source
    Value exported = Nil_Value;
    {
        VM sandbox = {0};
        sandbox.reset_on_exit = false;
        sandbox.allocator = vm->allocator;
        sandbox.path = path;
        sandbox.x = Nil_Value;

        reset_stack(&sandbox);
        table_init(&sandbox.globals);
        register_natives(&sandbox);

        // since the current context's object are not reachable to the sandbox they need
        // to be hided, so they are not freed in the `interpret` call by the GC, this is
        // achieved by marking the beginning of the current objects list to be the end of
        // the sandbox object list
        sandbox.allocator.objects_end = sandbox.allocator.objects;

        RavFunction *function = compile(&sandbox, source, path);
        if (function == NULL) {
            free(source);
            return false;
        }

        RavClosure *closure = object_closure(&sandbox.allocator, function);
        push(&sandbox, Obj_Value(closure));
        push_frame(&sandbox, closure, 0);

        // TODO: errors should dump the current context stack
        InterpretResult result = run_vm(&sandbox);
        if (result != INTERPRET_OK) {
            free(source);
            return false;
        }

        // reset allocator state to the current context
        sandbox.allocator.objects_end = NULL;
        vm->allocator = sandbox.allocator;

        // obtian the exported value from the X register
        exported = sandbox.x;

        // clear the sandbox resources
        table_free(&sandbox.globals);
    }

    *result = exported;

    free(source);
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40c98, %rsp          # imm = 0x40C98
movq %rdi, %r14
movq (%rsi), %rdi
movq %rdi, %rax
shrq $0x32, %rax
cmpl $0x3fff, %eax           # imm = 0x3FFF
jb 0x45dc
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq %rdi, %rax
cmpl $0x0, (%rax)
jne 0x45dc
movq %rcx, %rbx
movq 0x18(%rax), %r12
leaq 0x6bdd(%rip), %rsi      # 0xb00d
movq %r12, %rdi
callq 0x21c0
testq %rax, %rax
movq %rbx, 0x8(%rsp)
je 0x468b
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x21a0
movq %r13, %rdi
callq 0x2150
movq %rax, %rbp
movq %r13, %rdi
callq 0x20c0
leaq 0x1(%rbp), %rdi
callq 0x2180
movq %rax, %r15
testq %rax, %rax
je 0x46b8
movl $0x1, %esi
movq %r15, %rdi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x2080
cmpq %rbp, %rax
jb 0x4651
movq %r13, %rdi
callq 0x2070
testl %eax, %eax
jne 0x4651
movq %r13, %rdi
callq 0x20a0
movb $0x0, (%r15,%rbp)
movb $0x1, %bl
testb %bl, %bl
je 0x45d8
leaq 0x58(%rsp), %rdi
movl $0x40c40, %edx          # imm = 0x40C40
xorl %esi, %esi
callq 0x2100
leaq 0x10(%rsp), %rbp
movl $0x9, %ecx
movq %rbp, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r12, 0x48(%rbp)
movabsq $0x7ffc000000000001, %rbx # imm = 0x7FFC000000000001
movq %rbx, 0x50(%rbp)
leaq 0x68(%rsp), %r13
movq %r13, 0x40000(%r13)
movl $0x0, 0x40c10(%r13)
leaq 0x40c80(%rsp), %rdi
callq 0x4e28
movq %rbp, %rdi
callq 0x2966
movq -0x48(%r13), %rax
movq %rax, -0x40(%r13)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5d70
testq %rax, %rax
je 0x45cc
movabsq $-0x4000000000001, %r13 # imm = 0xFFFBFFFFFFFFFFFF
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x55e4
incq %r13
orq %rax, %r13
movq 0x40058(%r12), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x40058(%r12)
movq %r13, (%rcx)
movslq 0x40c68(%r12), %rdx
cmpq $0x80, %rdx
je 0x46db
leal 0x1(%rdx), %esi
movl %esi, 0x40c78(%rsp)
leaq (%rdx,%rdx,2), %rdx
movq %rax, 0x40078(%rsp,%rdx,8)
movq 0x10(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, 0x40080(%rsp,%rdx,8)
movq %rcx, 0x40088(%rsp,%rdx,8)
leaq 0x10(%rsp), %rdi
callq 0x2cbd
testl %eax, %eax
je 0x45f9
movq %r15, %rdi
callq 0x2030
xorl %ebp, %ebp
jmp 0x4628
xorl %ebp, %ebp
jmp 0x463d
callq 0x4bd6
leaq 0x6b07(%rip), %rsi      # 0xb0ef
xorl %ebp, %ebp
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22dc
jmp 0x463d
leaq 0x10(%rsp), %rax
movq $0x0, 0x18(%rax)
movl $0x9, %ecx
movq %r14, %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x50(%rax), %rbx
leaq 0x40c80(%rsp), %rdi
callq 0x4e3e
movb $0x1, %bpl
testb %bpl, %bpl
je 0x463d
movq 0x8(%rsp), %rax
movq %rbx, (%rax)
movq %r15, %rdi
callq 0x2030
movl %ebp, %eax
addq $0x40c98, %rsp          # imm = 0x40C98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2050
movl (%rax), %edi
callq 0x2210
leaq 0x6aac(%rip), %rsi      # 0xb110
xorl %ebx, %ebx
movq %r14, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x22dc
movq %r13, %rdi
callq 0x20a0
movq %r15, %rdi
callq 0x2030
jmp 0x44bb
callq 0x2050
movl (%rax), %edi
callq 0x2210
leaq 0x6a72(%rip), %rsi      # 0xb110
xorl %r15d, %r15d
movq %r14, %rdi
movq %r12, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x22dc
xorl %ebx, %ebx
jmp 0x44bb
leaq 0x6a72(%rip), %rsi      # 0xb131
xorl %ebx, %ebx
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x22dc
movq %r13, %rdi
callq 0x20a0
jmp 0x44bb
leaq 0x6cd7(%rip), %rsi      # 0xb3b9
leaq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0x22dc
jmp 0x45be