// File: /workspace/github_c_makefile_star2_O1/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 0x54cc
movq %rdi, %r14
movq %rbx, %rsi
callq 0x6214
movq %rax, %rbx
cmpl $0x2, (%rax)
jne 0x5506
movq %rbx, %rbp
movq %rbp, %rax
jmp 0x54f4
leaq 0xcc27(%rip), %rdi      # 0x120fa
leaq 0xd072(%rip), %rsi      # 0x1254c
movl $0x1, %edx
xorl %eax, %eax
callq 0x1032f
movq %rax, %r14
movq %rbx, %rdi
callq 0x10767
movq %r14, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rax
movq (%rax), %rax
movl (%rax), %edi
cmpl $0x5, %edi
jne 0x56fa
movq %r14, 0x10(%rsp)
movq 0x18(%rax), %r12
movq %r12, %rdi
callq 0x2110
leaq 0x5(%rax), %rdi
callq 0x2290
testq %rax, %rax
je 0x5760
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)
movl $0x1, %r14d
callq 0x2060
movq %rax, %r12
movl $0x0, (%r12)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2250
movl %eax, %r13d
testl %eax, %eax
jne 0x55a7
movl 0x30(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x55a7
xorl %ecx, %ecx
movl $0x2, %eax
jmp 0x565b
movq %r15, %rdi
callq 0x23f0
cmpl $0x1, %r14d
jne 0x55ee
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 0x575b
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
callq 0x2080
xorl %eax, %eax
movb $0x1, %cl
jmp 0x565b
testl %r13d, %r13d
je 0x560e
cmpl $0x2, (%r12)
jne 0x560e
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xcf4c(%rip), %rdi      # 0x12558
jmp 0x5642
movl 0x30(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x5630
leaq 0xcf6a(%rip), %rdi      # 0x12591
xorl %eax, %eax
callq 0x1032f
jmp 0x5649
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xcf2f(%rip), %rdi      # 0x12571
xorl %eax, %eax
callq 0x1032f
movq %rax, %rbp
movq %rbx, %rdi
callq 0x10767
movl $0x1, %eax
xorl %ecx, %ecx
testb %cl, %cl
je 0x566d
decl %r14d
je 0x556d
movl $0x2, %eax
cmpl $0x2, %eax
jne 0x54c7
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
callq 0xf8d2
movl %eax, %ebp
movq %r15, %rdi
callq 0x23f0
testb %bpl, %bpl
je 0x572e
movq 0x18(%rsp), %rdi
cmpl $0x0, 0x4(%rdi)
je 0x56e0
movq 0x10(%rsp), %r14
xorl %esi, %esi
callq 0x10c23
movq %r14, %rdi
movq %rax, %rsi
callq 0x5e90
movq %rax, %r15
cmpl $0x2, (%rax)
jne 0x56cd
movq %r15, %rdi
callq 0xfbdf
movq %r15, %rdi
callq 0x10767
movq 0x18(%rsp), %rdi
cmpl $0x0, 0x4(%rdi)
jne 0x56ab
callq 0x10767
movq %rbx, %rdi
callq 0x10767
callq 0x106fb
movq %rax, %rbp
jmp 0x54c7
callq 0x10138
movq %rax, %r14
movl $0x5, %edi
callq 0x10138
leaq 0xca2e(%rip), %rdi      # 0x12141
leaq 0xce32(%rip), %rsi      # 0x1254c
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x1032f
jmp 0x54e6
movq 0x8(%rsp), %rsi
leaq 0xce6c(%rip), %rdi      # 0x125a6
xorl %eax, %eax
callq 0x1032f
movq %rax, %rbp
movq 0x8(%rsp), %rdi
callq 0x23f0
movq %rbx, %rdi
callq 0x10767
jmp 0x54c7
callq 0x2400
callq 0x2425