// File: /workspace/github_cpp_makefile_star2_O2/iamlouk[P]asbi/src/macros.cc
// Function: macro_import(int, asbi::Context*, std::shared_ptr<asbi::Env>)
static Value macro_import(int n, Context* ctx, std::shared_ptr<Env> env) {
	if (n != 1)
		throw std::runtime_error("import macro usage error or environment corruption");

	auto arg = ctx->pop();
	auto __file = env->lookup(ctx->names.__file);
	auto __imports = ctx->global_env->lookup(ctx->names.__imports);
	// std::cerr << "import(arg: " << arg.to_string(true) << ", __file: " << __file.to_string(true) << ", __imports: " << __imports.to_string(true) << ")\n";
	if (arg.type != type_t::String || __file.type != type_t::String || __imports.type != type_t::Map)
		throw std::runtime_error("import macro usage error or environment corruption");

	auto dir = utils::dirname(__file._string->data);
	auto path = utils::join(dir, arg._string->data);
	auto filepathvalue = Value::string(ctx->new_string(path));

	if (auto data = __imports._map->get(filepathvalue); data.type != type_t::Nil)
		return data;

	// std::cout << "loading file: " << filepath << '\n';

	std::string content;
	utils::readfile(path, content);

	auto new_env = std::make_shared<Env>(ctx, ctx->global_env);
	new_env->decl(ctx->names.__file, filepathvalue);
	new_env->decl(ctx->names.exports, Value::map(new MapContainer(ctx)));

	ctx->run(content, new_env);

	auto data = new_env->lookup(ctx->names.exports);
	__imports._map->set(filepathvalue, data);
	return data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, (%rsp)
cmpl $0x1, %edi
jne 0x10709
movq %rdx, %rbx
movq %rsi, %r15
movq %rsi, %rdi
callq 0xf1b4
movl %eax, %r13d
movq %rdx, %r14
movq (%rbx), %rdi
movq 0x538(%r15), %rsi
callq 0xeb40
movl %eax, %ebp
movq %rdx, %r12
movq 0x4f8(%r15), %rdi
movq 0x548(%r15), %rsi
callq 0xeb40
cmpl $0x4, %r13d
jne 0x10725
cmpl $0x4, %ebp
jne 0x10725
cmpl $0x6, %eax
jne 0x10725
movq %rdx, %rbx
addq $0x20, %r12
leaq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xa126
addq $0x20, %r14
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xa2ad
leaq 0x48(%rsp), %rsi
movq %r15, %rdi
callq 0xf44a
movq %rax, %r14
pushq $0x4
popq %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xda08
movl %eax, %ebp
cmpl $0x2, %eax
jne 0x106db
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xa1fb
addq $0x4f8, %r15            # imm = 0x4F8
leaq 0x8(%rsp), %rdi
movq %rsp, %rsi
movq %r15, %rdx
callq 0xc019
movq 0x8(%rsp), %rdi
movq (%rsp), %rax
movq 0x538(%rax), %rsi
pushq $0x4
popq %rdx
movq %r14, %rcx
callq 0xed22
movq 0x8(%rsp), %r12
movq (%rsp), %rbp
movq 0x550(%rbp), %r13
pushq $0x68
popq %rdi
callq 0x5390
movq %rax, %r15
movq %rax, %rdi
movq %rbp, %rsi
callq 0xd80c
pushq $0x6
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0xed22
movq (%rsp), %r15
leaq 0x18(%rsp), %r12
leaq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x6356
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0xf262
leaq 0x20(%rsp), %rdi
callq 0x638a
movq 0x8(%rsp), %rdi
movq (%rsp), %rax
movq 0x550(%rax), %rsi
callq 0xeb40
movl %eax, %ebp
movq %rdx, %r12
pushq $0x4
popq %rsi
movq %rbx, %rdi
movq %r14, %rdx
movl %eax, %ecx
movq %r12, %r8
callq 0xd90c
leaq 0x10(%rsp), %rdi
callq 0x638a
leaq 0x28(%rsp), %rdi
callq 0x5170
jmp 0x106de
movq %rdx, %r12
leaq 0x48(%rsp), %rdi
callq 0x5170
leaq 0x68(%rsp), %rdi
callq 0x5170
movl %ebp, %eax
movq %r12, %rdx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x51b0
movq %rax, %r14
leaq 0x4b69(%rip), %rsi      # 0x15284
movq %rax, %rdi
callq 0x5120
jmp 0x1073f
pushq $0x10
popq %rdi
callq 0x51b0
movq %rax, %r14
leaq 0x4b4d(%rip), %rsi      # 0x15284
movq %rax, %rdi
callq 0x5120
movq 0xf8aa(%rip), %rsi      # 0x1fff0
movq 0xf85b(%rip), %rdx      # 0x1ffa8
movq %r14, %rdi
callq 0x55f0
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x638a
jmp 0x10797
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r15, %rdi
callq 0x53a0
jmp 0x10797
jmp 0x10776
movq %rax, %rbx
jmp 0x107a1
jmp 0x1077d
movq %rax, %rbx
movq %r14, %rdi
callq 0x5290
jmp 0x107bf
movq %rax, %rbx
jmp 0x107b5
movq %rax, %rbx
jmp 0x107ab
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x638a
leaq 0x28(%rsp), %rdi
callq 0x5170
leaq 0x48(%rsp), %rdi
callq 0x5170
leaq 0x68(%rsp), %rdi
callq 0x5170
movq %rbx, %rdi
callq 0x5600