// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, 0x4(%rax)
je 0x6698
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
leaq 0x13a77(%rip), %rdi     # 0x1a0e2
leaq 0x13ec2(%rip), %rsi     # 0x1a534
movl $0x1, %edx
movb $0x0, %al
callq 0x16600
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x69e6
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7c60
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x66bf
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x69e6
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
cmpl $0x5, (%rax)
je 0x672d
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movl (%rax), %edi
callq 0x161a0
movq %rax, -0x100(%rbp)
movl $0x5, %edi
callq 0x161a0
movq -0x100(%rbp), %rcx
movq %rax, %r8
leaq 0x13a26(%rip), %rdi     # 0x1a129
leaq 0x13e2a(%rip), %rsi     # 0x1a534
xorl %edx, %edx
movb $0x0, %al
callq 0x16600
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x69e6
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
callq 0x20f0
movq %rax, %rdi
addq $0x5, %rdi
callq 0x6a00
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
callq 0x2070
movq -0x30(%rbp), %rdi
leaq 0x13dc7(%rip), %rsi     # 0x1a53b
callq 0x2320
movl $0x0, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
jge 0x68f6
callq 0x2050
movl $0x0, (%rax)
movq -0x30(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x2210
movl %eax, -0xcc(%rbp)
movb $0x1, %al
cmpl $0x0, -0xcc(%rbp)
movb %al, -0x101(%rbp)
jne 0x67d7
movl -0xb0(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
sete %al
xorb $-0x1, %al
movb %al, -0x101(%rbp)
movb -0x101(%rbp), %al
andb $0x1, %al
movb %al, -0xcd(%rbp)
testb $0x1, -0xcd(%rbp)
jne 0x67f3
jmp 0x68f6
movq -0x30(%rbp), %rdi
callq 0x23a0
cmpl $0x0, -0x34(%rbp)
jne 0x6843
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
callq 0x20f0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x6a00
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
callq 0x2070
jmp 0x68e4
cmpl $0x0, -0xcc(%rbp)
je 0x687c
callq 0x2050
cmpl $0x2, (%rax)
jne 0x687c
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0x13cd4(%rip), %rdi     # 0x1a540
movb $0x0, %al
callq 0x16600
movq %rax, -0xd8(%rbp)
jmp 0x68cb
movl -0xb0(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
je 0x68b4
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0x13cb5(%rip), %rdi     # 0x1a559
movb $0x0, %al
callq 0x16600
movq %rax, -0xd8(%rbp)
jmp 0x68c9
leaq 0x13cbe(%rip), %rdi     # 0x1a579
movb $0x0, %al
callq 0x16600
movq %rax, -0xd8(%rbp)
jmp 0x68cb
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0xd8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x69e6
jmp 0x68e6
jmp 0x68e8
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x6780
movq -0x30(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x15320
testb $0x1, %al
jne 0x6916
jmp 0x69a1
movq -0x30(%rbp), %rdi
callq 0x23a0
movq -0xe0(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x6981
movq -0x10(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xe0(%rbp), %rdi
xorl %esi, %esi
callq 0x174b0
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x7770
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x6973
movq -0xf0(%rbp), %rdi
callq 0x159d0
movq -0xf0(%rbp), %rdi
callq 0x16cf0
jmp 0x691f
movq -0xe0(%rbp), %rdi
callq 0x16cf0
movq -0x18(%rbp), %rdi
callq 0x16cf0
callq 0x16c00
movq %rax, -0x8(%rbp)
jmp 0x69e6
movq -0x30(%rbp), %rdi
callq 0x23a0
movq -0xe8(%rbp), %rsi
leaq 0x13bd6(%rip), %rdi     # 0x1a58e
movb $0x0, %al
callq 0x16600
movq %rax, -0xf8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x23a0
movq -0x18(%rbp), %rdi
callq 0x16cf0
movq -0xf8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)