// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movq %rsi, %rbx
movl 0x4(%rsi), %ecx
cmpl $0x1, %ecx
jne 0x5380
movq %rdi, %r14
movq %rbx, %rsi
callq 0x5f58
movq %rax, %rbx
cmpl $0x2, (%rax)
je 0x54ab
movq 0x8(%rbx), %rax
movq (%rax), %rax
movl (%rax), %edi
cmpl $0x5, %edi
jne 0x53ad
movq 0x18(%rax), %r12
movq %r12, %rdi
callq 0x2110
leaq 0x5(%rax), %rdi
callq 0x2290
testq %rax, %rax
je 0x54c0
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
callq 0x2080
movq %r15, %rdi
callq 0x2110
movl $0x6c77612e, (%r15,%rax) # imm = 0x6C77612E
movb $0x0, 0x4(%r15,%rax)
callq 0x2060
movq %rax, %r12
movl $0x0, (%rax)
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x2250
testl %eax, %eax
jne 0x52de
movl $0xf000, %eax           # imm = 0xF000
andl 0x20(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x53de
movq %r15, %rdi
callq 0x23f0
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %r13
movq %r13, %rdi
callq 0x2110
leaq 0x1(%rax), %rdi
callq 0x2290
testq %rax, %rax
je 0x54c5
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
callq 0x2080
movl $0x0, (%r12)
leaq 0x8(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x2250
movl %eax, %ebp
movl 0x18(%r13), %r13d
andl $0xf000, %r13d          # imm = 0xF000
movl %r13d, %eax
xorl $0x8000, %eax           # imm = 0x8000
orl %ebp, %eax
je 0x53de
movq %r15, %rdi
callq 0x23f0
testl %ebp, %ebp
je 0x546b
cmpl $0x2, (%r12)
jne 0x546b
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xd1dd(%rip), %rdi      # 0x12558
jmp 0x5496
leaq 0xcd73(%rip), %rdi      # 0x120fa
leaq 0xd1be(%rip), %rsi      # 0x1254c
movl $0x1, %edx
xorl %eax, %eax
callq 0xfe6b
movq %rax, %r14
movq %rbx, %rdi
callq 0x1029f
movq %r14, %rax
jmp 0x54ae
callq 0xfc74
movq %rax, %r14
movl $0x5, %edi
callq 0xfc74
leaq 0xcd7b(%rip), %rdi      # 0x12141
leaq 0xd17f(%rip), %rsi      # 0x1254c
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xfe6b
jmp 0x539a
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
movq %r15, %rdi
callq 0xf409
movl %eax, %ebp
movq %r15, %rdi
callq 0x23f0
testb %bpl, %bpl
je 0x5434
movq 0x8(%rsp), %rdi
cmpl $0x0, 0x4(%rdi)
je 0x5454
xorl %esi, %esi
callq 0x1075b
movq %r14, %rdi
movq %rax, %rsi
callq 0x5be8
movq %rax, %r15
cmpl $0x2, (%rax)
jne 0x542a
movq %r15, %rdi
callq 0xf717
movq %r15, %rdi
callq 0x1029f
jmp 0x53fd
movq (%rsp), %rsi
leaq 0xd167(%rip), %rdi      # 0x125a6
xorl %eax, %eax
callq 0xfe6b
movq %rax, %r14
movq (%rsp), %rdi
callq 0x23f0
jmp 0x54a0
callq 0x1029f
movq %rbx, %rdi
callq 0x1029f
callq 0x10233
movq %rax, %rbx
jmp 0x54ab
cmpl $0x8000, %r13d          # imm = 0x8000
jne 0x5484
leaq 0xd116(%rip), %rdi      # 0x12591
xorl %eax, %eax
callq 0xfe6b
jmp 0x549d
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xd0db(%rip), %rdi      # 0x12571
xorl %eax, %eax
callq 0xfe6b
movq %rax, %r14
movq %rbx, %rdi
callq 0x1029f
movq %r14, %rbx
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2425
callq 0x2400