// File: 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:
subq $0x218, %rsp            # imm = 0x218
movq %rdx, 0xa8(%rsp)
movl %edi, 0x204(%rsp)
movq %rsi, 0x1f8(%rsp)
movq %rdx, 0x1f0(%rsp)
cmpl $0x1, 0x204(%rsp)
je 0x2ad57
movl $0x10, %edi
callq 0x6200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xa0(%rsp)
leaq 0xe685(%rip), %rsi      # 0x39394
callq 0x6160
jmp 0x2ad16
movq 0xa0(%rsp), %rdi
movq 0x212cb(%rip), %rsi     # 0x4bff0
movq 0x21274(%rip), %rdx     # 0x4bfa0
callq 0x6780
movq 0xa0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
callq 0x6320
jmp 0x2b2c7
movq 0x1f8(%rsp), %rdi
callq 0x26b50
movq 0xa8(%rsp), %rdi
movl %eax, 0x1d0(%rsp)
movq %rdx, 0x1d8(%rsp)
callq 0x7bd0
movq %rax, %rdi
movq 0x1f8(%rsp), %rax
movq 0x538(%rax), %rsi
callq 0x25e70
movl %eax, 0x1c0(%rsp)
movq %rdx, 0x1c8(%rsp)
movq 0x1f8(%rsp), %rdi
addq $0x4f8, %rdi            # imm = 0x4F8
callq 0x7bd0
movq %rax, %rdi
movq 0x1f8(%rsp), %rax
movq 0x548(%rax), %rsi
callq 0x25e70
movl %eax, 0x1b0(%rsp)
movq %rdx, 0x1b8(%rsp)
cmpl $0x4, 0x1d0(%rsp)
jne 0x2adfe
cmpl $0x4, 0x1c0(%rsp)
jne 0x2adfe
cmpl $0x6, 0x1b0(%rsp)
je 0x2ae65
movl $0x10, %edi
callq 0x6200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x98(%rsp)
leaq 0xe577(%rip), %rsi      # 0x39394
callq 0x6160
jmp 0x2ae24
movq 0x98(%rsp), %rdi
movq 0x211bd(%rip), %rsi     # 0x4bff0
movq 0x21166(%rip), %rdx     # 0x4bfa0
callq 0x6780
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
callq 0x6320
jmp 0x2b2c7
movq 0x1c8(%rsp), %rsi
addq $0x20, %rsi
leaq 0x190(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x17930
movq 0x90(%rsp), %rsi
movq 0x1d8(%rsp), %rdx
addq $0x20, %rdx
leaq 0x170(%rsp), %rdi
callq 0x17c30
jmp 0x2aea9
movq 0x1f8(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x27030
movq %rax, 0x88(%rsp)
jmp 0x2aec8
movq 0x88(%rsp), %rdi
callq 0x1d180
movq %rdx, 0x78(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x2aee3
movq 0x78(%rsp), %rax
movl 0x84(%rsp), %ecx
movl %ecx, 0x160(%rsp)
movq %rax, 0x168(%rsp)
movq 0x1b8(%rsp), %rdi
movups 0x160(%rsp), %xmm0
movaps %xmm0, 0x150(%rsp)
movl 0x150(%rsp), %esi
movq 0x158(%rsp), %rdx
callq 0x216c0
movq %rdx, 0x68(%rsp)
movl %eax, 0x74(%rsp)
jmp 0x2af35
movq 0x68(%rsp), %rax
movl 0x74(%rsp), %ecx
movl %ecx, 0x208(%rsp)
movq %rax, 0x210(%rsp)
cmpl $0x2, 0x208(%rsp)
je 0x2af99
movl $0x1, 0x14c(%rsp)
jmp 0x2b27c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x2b2ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x2b2ad
leaq 0x128(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x6650
movq 0x60(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0x17af0
jmp 0x2afbf
movq 0x1f8(%rsp), %rdx
addq $0x4f8, %rdx            # imm = 0x4F8
leaq 0x118(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
callq 0x1d240
jmp 0x2afe5
leaq 0x118(%rsp), %rdi
callq 0x7bd0
movq %rax, %rdi
movq 0x1f8(%rsp), %rax
movq 0x538(%rax), %rsi
movups 0x160(%rsp), %xmm0
movaps %xmm0, 0x100(%rsp)
movl 0x100(%rsp), %edx
movq 0x108(%rsp), %rcx
callq 0x26220
jmp 0x2b02a
leaq 0x118(%rsp), %rdi
callq 0x7bd0
movq %rax, 0x48(%rsp)
movq 0x1f8(%rsp), %rax
movq 0x550(%rax), %rax
movq %rax, 0x50(%rsp)
movl $0x68, %edi
callq 0x6460
movq %rax, 0x58(%rsp)
jmp 0x2b061
movq 0x58(%rsp), %rdi
movq 0x1f8(%rsp), %rsi
callq 0x212d0
jmp 0x2b075
movq 0x58(%rsp), %rdi
callq 0x7be0
movq %rdx, 0x38(%rsp)
movl %eax, 0x44(%rsp)
jmp 0x2b08a
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdi
movq 0x38(%rsp), %rax
movl 0x44(%rsp), %ecx
movl %ecx, 0xf0(%rsp)
movq %rax, 0xf8(%rsp)
movl 0xf0(%rsp), %edx
movq 0xf8(%rsp), %rcx
callq 0x26220
jmp 0x2b0c2
movq 0x1f8(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
leaq 0x118(%rsp), %rsi
callq 0x7d80
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
leaq 0x128(%rsp), %rsi
callq 0x26c90
movq %rdx, 0x28(%rsp)
movl %eax, 0x34(%rsp)
jmp 0x2b10b
movq 0x28(%rsp), %rax
movl 0x34(%rsp), %ecx
movl %ecx, 0xd0(%rsp)
movq %rax, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x7db0
leaq 0x118(%rsp), %rdi
callq 0x7bd0
movq %rax, %rdi
movq 0x1f8(%rsp), %rax
movq 0x550(%rax), %rsi
callq 0x25e70
movq %rdx, 0x8(%rsp)
movl %eax, 0x14(%rsp)
jmp 0x2b15f
movq 0x8(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, 0x208(%rsp)
movq %rax, 0x210(%rsp)
movq 0x1b8(%rsp), %rdi
movups 0x160(%rsp), %xmm0
movaps %xmm0, 0xc0(%rsp)
movups 0x208(%rsp), %xmm0
movaps %xmm0, 0xb0(%rsp)
movl 0xc0(%rsp), %esi
movq 0xc8(%rsp), %rdx
movl 0xb0(%rsp), %ecx
movq 0xb8(%rsp), %r8
callq 0x21550
jmp 0x2b1c4
movl $0x1, 0x14c(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x7db0
leaq 0x128(%rsp), %rdi
callq 0x6290
jmp 0x2b27c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x2b26d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x2b260
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
movl $0x68, %esi
callq 0x6470
jmp 0x2b260
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x7db0
leaq 0x118(%rsp), %rdi
callq 0x7db0
leaq 0x128(%rsp), %rdi
callq 0x6290
jmp 0x2b2ad
leaq 0x170(%rsp), %rdi
callq 0x6290
leaq 0x190(%rsp), %rdi
callq 0x6290
movl 0x208(%rsp), %eax
movq 0x210(%rsp), %rdx
addq $0x218, %rsp            # imm = 0x218
retq
leaq 0x170(%rsp), %rdi
callq 0x6290
leaq 0x190(%rsp), %rdi
callq 0x6290
movq 0x1e8(%rsp), %rdi
callq 0x67a0
nopw %cs:(%rax,%rax)