// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rsi, (%rsp)
cmpl $0x1, %edi
jne 0x1293f
movq %rdx, %rbx
movq %rsi, %rdi
callq 0x11276
movl %eax, %ebp
movq %rdx, 0x60(%rsp)
movq (%rbx), %rdi
movq (%rsp), %rax
movq 0x538(%rax), %rsi
callq 0x109ba
movl %eax, %ebx
movq %rdx, 0x58(%rsp)
movq (%rsp), %rax
movq 0x4f8(%rax), %rdi
movq 0x548(%rax), %rsi
callq 0x109ba
movq %rdx, 0x20(%rsp)
cmpl $0x4, %ebp
jne 0x1295d
cmpl $0x4, %ebx
jne 0x1295d
cmpl $0x6, %eax
jne 0x1295d
movq 0x58(%rsp), %rsi
addq $0x20, %rsi
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0xae38
movq 0x60(%rsp), %rdx
addq $0x20, %rdx
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0xb00f
movq (%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x115d6
movq %rax, %rbx
movq 0x20(%rsp), %rdi
movl $0x4, %esi
movq %rax, %rdx
callq 0xf51a
movl %eax, %ebp
cmpl $0x2, %eax
jne 0x12869
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x68(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xaf2e
movl $0x4f8, %r8d            # imm = 0x4F8
movq %rsp, %rcx
addq (%rcx), %r8
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0xf(%rsp), %rdx
callq 0xd344
movq 0x10(%rsp), %rdi
movq (%rsp), %rax
movq 0x538(%rax), %rsi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x10c8e
movq 0x10(%rsp), %r15
movq (%rsp), %r13
movq 0x550(%r13), %r12
movl $0x68, %edi
callq 0x5380
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
callq 0xf2dc
movq %r15, %rdi
movq %r12, %rsi
movl $0x6, %edx
movq %r14, %rcx
callq 0x10c8e
movq (%rsp), %rdi
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x12872
movq 0xe759(%rip), %rcx      # 0x20fb8
cmpb $0x0, (%rcx)
je 0x1286e
incl 0x8(%rax)
jmp 0x12872
movq %rdx, %r14
jmp 0x128ec
lock
incl 0x8(%rax)
leaq 0x38(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x11342
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x12890
callq 0x6614
movq 0x10(%rsp), %rdi
movq (%rsp), %rax
movq 0x550(%rax), %rsi
callq 0x109ba
movl %eax, %ebp
movq %rdx, %r14
movq 0x20(%rsp), %rdi
movl $0x4, %esi
movq %rbx, %rdx
movl %eax, %ecx
movq %r14, %r8
callq 0xf3ea
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x128d0
callq 0x6614
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x128ec
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12907
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12928
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %eax
movq %r14, %rdx
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x5921(%rip), %rsi      # 0x18274
movq %rax, %rdi
callq 0x5150
jmp 0x12979
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x5903(%rip), %rsi      # 0x18274
movq %rax, %rdi
callq 0x5150
movq 0xe670(%rip), %rsi      # 0x20ff0
movq 0xe629(%rip), %rdx      # 0x20fb0
movq %r14, %rdi
callq 0x55d0
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x129d8
callq 0x6614
jmp 0x129d8
movq %rax, %rbx
movl $0x68, %esi
movq %r14, %rdi
callq 0x5390
jmp 0x129d8
jmp 0x129b7
movq %rax, %rbx
jmp 0x129e7
jmp 0x129be
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x12a3f
movq %rax, %rbx
jmp 0x12a1e
movq %rax, %rbx
jmp 0x12a03
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x129e7
callq 0x6614
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x12a03
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12a1e
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12a3f
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x5390
movq %rbx, %rdi
callq 0x55e0