// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40d30, %rsp          # imm = 0x40D30
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x65e0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x5af0
movq -0x10(%rbp), %rax
movq %rax, -0x40d08(%rbp)
movq -0x30(%rbp), %rdi
callq 0x6790
movq -0x40d08(%rbp), %rdi
movq %rax, %rdx
leaq 0x860f(%rip), %rsi      # 0xe0ef
movb $0x0, %al
callq 0x6630
movb $0x0, -0x1(%rbp)
jmp 0x5e16
movabsq $0x3ffffffffffff, %rax  # imm = 0x3FFFFFFFFFFFF
andq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x84f4(%rip), %rsi      # 0xe00d
callq 0x21c0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x5b73
movq -0x10(%rbp), %rax
movq %rax, -0x40d18(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40d10(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2200
movq -0x40d18(%rbp), %rdi
movq -0x40d10(%rbp), %rdx
movq %rax, %rcx
leaq 0x85ad(%rip), %rsi      # 0xe110
movb $0x0, %al
callq 0x6630
movb $0x0, -0x1(%rbp)
jmp 0x5e16
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x21a0
movq -0x48(%rbp), %rdi
callq 0x2160
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x20c0
movq -0x50(%rbp), %rdi
addq $0x1, %rdi
callq 0x2190
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x5bdb
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x856f(%rip), %rsi      # 0xe131
movb $0x0, %al
callq 0x6630
movq -0x48(%rbp), %rdi
callq 0x2090
movb $0x0, -0x1(%rbp)
jmp 0x5e16
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movl $0x1, %esi
callq 0x2070
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x5c0d
movq -0x48(%rbp), %rdi
callq 0x2060
cmpl $0x0, %eax
je 0x5c69
movq -0x10(%rbp), %rax
movq %rax, -0x40d28(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40d20(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x2200
movq -0x40d28(%rbp), %rdi
movq -0x40d20(%rbp), %rdx
movq %rax, %rcx
leaq 0x84c9(%rip), %rsi      # 0xe110
movb $0x0, %al
callq 0x6630
movq -0x48(%rbp), %rdi
callq 0x2090
movq -0x40(%rbp), %rdi
callq 0x2030
movb $0x0, -0x1(%rbp)
jmp 0x5e16
movq -0x48(%rbp), %rdi
callq 0x2090
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movabsq $0x7ffc000000000001, %rax # imm = 0x7FFC000000000001
movq %rax, -0x60(%rbp)
leaq -0x40ce8(%rbp), %rdi
xorl %esi, %esi
movl $0x40c88, %edx          # imm = 0x40C88
callq 0x2100
movb $0x0, -0xc88(%rbp)
leaq -0x40ce8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x48, %edx
callq 0x2180
movq -0x38(%rbp), %rax
movq %rax, -0x40ca0(%rbp)
movabsq $0x7ffc000000000001, %rax # imm = 0x7FFC000000000001
movq %rax, -0x40c98(%rbp)
leaq -0x40ce8(%rbp), %rdi
callq 0x26e0
leaq -0x40ce8(%rbp), %rdi
addq $0x40c70, %rdi          # imm = 0x40C70
callq 0x7260
leaq -0x40ce8(%rbp), %rdi
callq 0x2710
movq -0x40cd8(%rbp), %rax
movq %rax, -0x40cd0(%rbp)
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq -0x40ce8(%rbp), %rdi
callq 0x89f0
movq %rax, -0x40cf0(%rbp)
cmpq $0x0, -0x40cf0(%rbp)
jne 0x5d47
movq -0x40(%rbp), %rdi
callq 0x2030
movb $0x0, -0x1(%rbp)
jmp 0x5e16
leaq -0x40ce8(%rbp), %rdi
movq -0x40cf0(%rbp), %rsi
callq 0x7ce0
movq %rax, -0x40cf8(%rbp)
movq -0x40cf8(%rbp), %rsi
movabsq $-0x4000000000000, %rax # imm = 0xFFFC000000000000
orq %rax, %rsi
leaq -0x40ce8(%rbp), %rdi
callq 0x2bb0
movq -0x40cf8(%rbp), %rsi
leaq -0x40ce8(%rbp), %rdi
xorl %edx, %edx
callq 0x2be0
leaq -0x40ce8(%rbp), %rdi
callq 0x2ca0
movl %eax, -0x40cfc(%rbp)
cmpl $0x0, -0x40cfc(%rbp)
je 0x5dc0
movq -0x40(%rbp), %rdi
callq 0x2030
movb $0x0, -0x1(%rbp)
jmp 0x5e16
movq $0x0, -0x40cd0(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x40ce8(%rbp), %rsi
movl $0x48, %edx
callq 0x2180
movq -0x40c98(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x40ce8(%rbp), %rdi
addq $0x40c70, %rdi          # imm = 0x40C70
callq 0x7290
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rdi
callq 0x2030
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x40d30, %rsp          # imm = 0x40D30
popq %rbp
retq
nopw %cs:(%rax,%rax)