// File: /workspace/github_c_makefile_star2_O2/voithos[P]awl/src/builtins.c
// Function: builtin_import
awlval* builtin_import(awlenv* e, awlval* a) {
    AWLASSERT_ARGCOUNT(a, 1, "import");
    EVAL_ARGS(e, a);
    AWLASSERT_TYPE(a, 0, AWLVAL_STR, "import");

    // Check the import path
    char* importPath = safe_malloc(strlen(a->cell[0]->str) + 5); // extra space for extension
    strcpy(importPath, a->cell[0]->str);
    strcat(importPath, ".awl");

    // Attempt twice: once with the .awl extension, and once with the raw path
    for (int attempt = 0; attempt < 2; attempt++) {
        struct stat s;
        errno = 0;
        int statErr = stat(importPath, &s);

        bool hasError = statErr || !S_ISREG(s.st_mode);

        // Keep going if we've successfully found a file
        if (!hasError) {
            break;
        } else {
            free(importPath);

            // Try the raw path if we have once more attempt
            if (attempt == 0) {
                importPath = safe_malloc(strlen(a->cell[0]->str) + 1);
                strcpy(importPath, a->cell[0]->str);
            } else {
                // Return error otherwise
                awlval* errval;
                if (statErr && errno == ENOENT) {
                    errval = awlval_err("path '%s' does not exist", a->cell[0]->str);
                } else if (!S_ISREG(s.st_mode)) {
                    errval = awlval_err("path '%s' is not a regular file", a->cell[0]->str);
                } else {
                    errval = awlval_err("unknown import error");
                }
                awlval_del(a);
                return errval;
            }
        }
    }

    awlval* v;
    char* err;
    if (awlval_parse_file(importPath, &v, &err)) {
        free(importPath);

        while (v->count) {
            awlval* x = awlval_eval(e, awlval_pop(v, 0));
            if (x->type == AWLVAL_ERR) {
                awlval_println(x);
            }
            awlval_del(x);
        }

        awlval_del(v);
        awlval_del(a);

        return awlval_qexpr();
    } else {
        free(importPath);

        awlval* errval = awlval_err("could not import %s", err);
        free(err);
        awlval_del(a);

        return errval;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbx
movl 0x4(%rsi), %ecx
cmpl $0x1, %ecx
jne 0x4a9e
movq %rdi, %r14
movq %rbx, %rsi
callq 0x55f9
movq %rax, %rbx
cmpl $0x2, (%rax)
je 0x4bf1
movq 0x8(%rbx), %rax
movq (%rax), %rax
movl (%rax), %edi
cmpl $0x5, %edi
jne 0x4ac9
movq %r14, 0x10(%rsp)
movq 0x18(%rax), %rdi
callq 0x2110
leaq 0x5(%rax), %rdi
callq 0x4c06
movq %rax, %r15
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0x2080
movq %r15, %rdi
callq 0x2110
movl $0x6c77612e, (%r15,%rax) # imm = 0x6C77612E
movb $0x0, 0x4(%r15,%rax)
movb $0x1, %r13b
callq 0x2060
movq %rax, %r12
andl $0x0, (%r12)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2250
movl %eax, %ebp
movl 0x30(%rsp), %r14d
andl $0xf000, %r14d          # imm = 0xF000
movl %r14d, %eax
xorl $0x8000, %eax           # imm = 0x8000
orl %ebp, %eax
je 0x4af8
movq %r15, %rdi
callq 0x23f0
testb $0x1, %r13b
je 0x4b59
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
callq 0x2110
leaq 0x1(%rax), %rdi
callq 0x4c06
movq %rax, %r15
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0x2080
xorl %r13d, %r13d
jmp 0x4a28
leaq 0xb655(%rip), %rdi      # 0x100fa
leaq 0xbaa0(%rip), %rsi      # 0x1054c
pushq $0x1
popq %rdx
xorl %eax, %eax
callq 0xdb45
movq %rax, %r14
movq %rbx, %rdi
callq 0xded1
movq %r14, %rax
jmp 0x4bf4
callq 0xd97c
movq %rax, %r14
pushq $0x5
popq %rdi
callq 0xd97c
leaq 0xb661(%rip), %rdi      # 0x10141
leaq 0xba65(%rip), %rsi      # 0x1054c
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xdb45
jmp 0x4ab6
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
callq 0xd2c4
movl %eax, %ebp
movq %r15, %rdi
callq 0x23f0
testb %bpl, %bpl
je 0x4b91
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rdi
cmpl $0x0, 0x4(%rdi)
je 0x4bb3
xorl %esi, %esi
callq 0xe2bf
movq %r14, %rdi
movq %rax, %rsi
callq 0x52a8
movq %rax, %r15
cmpl $0x2, (%rax)
jne 0x4b4f
movq %r15, %rdi
callq 0xd469
movq %r15, %rdi
callq 0xded1
jmp 0x4b1e
testl %ebp, %ebp
je 0x4b78
cmpl $0x2, (%r12)
jne 0x4b78
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xb9e2(%rip), %rdi      # 0x10558
jmp 0x4bdc
cmpl $0x8000, %r14d          # imm = 0x8000
jne 0x4bca
leaq 0xba09(%rip), %rdi      # 0x10591
xorl %eax, %eax
callq 0xdb45
jmp 0x4be3
movq 0x8(%rsp), %rsi
leaq 0xba09(%rip), %rdi      # 0x105a6
xorl %eax, %eax
callq 0xdb45
movq %rax, %r14
movq 0x8(%rsp), %rdi
callq 0x23f0
jmp 0x4be6
callq 0xded1
movq %rbx, %rdi
callq 0xded1
callq 0xde89
movq %rax, %rbx
jmp 0x4bf1
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xb995(%rip), %rdi      # 0x10571
xorl %eax, %eax
callq 0xdb45
movq %rax, %r14
movq %rbx, %rdi
callq 0xded1
movq %r14, %rbx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq