// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movq %rsi, 0x8(%rsp)
cmpl $0x1, %edi
jne 0x127c2
movq %rdx, %r15
movq %rsi, %r14
movq %rsi, %rdi
callq 0x11122
movl %eax, %r12d
movq %rdx, %rbx
movq (%r15), %rdi
movq 0x538(%r14), %rsi
callq 0x108ce
movl %eax, %ebp
movq %rdx, %r15
movq 0x4f8(%r14), %rdi
movq 0x548(%r14), %rsi
callq 0x108ce
movq %rdx, 0x50(%rsp)
cmpl $0x4, %r12d
jne 0x127e0
cmpl $0x4, %ebp
jne 0x127e0
cmpl $0x6, %eax
jne 0x127e0
movq %rdx, %r14
addq $0x20, %r15
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0xacd8
addq $0x20, %rbx
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xaeaf
movq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x11482
movq %rax, %rbx
movq %r14, %rdi
movl $0x4, %esi
movq %rax, %rdx
callq 0xf4b8
movl %eax, %ebp
cmpl $0x2, %eax
jne 0x126eb
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xadce
movl $0x4f8, %r8d            # imm = 0x4F8
leaq 0x8(%rsp), %rcx
addq (%rcx), %r8
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0x7(%rsp), %rdx
callq 0xd21a
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rax
movq 0x538(%rax), %rsi
movl $0x4, %edx
movq %rbx, %rcx
callq 0x10b4e
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r13
movq 0x550(%r13), %r12
movl $0x68, %edi
callq 0x5380
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
callq 0xf274
movq %r15, %rdi
movq %r12, %rsi
movl $0x6, %edx
movq %r14, %rcx
callq 0x10b4e
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x126f4
movq 0xe8d7(%rip), %rcx      # 0x20fb8
cmpb $0x0, (%rcx)
je 0x126f0
incl 0x8(%rax)
jmp 0x126f4
movq %rdx, %r14
jmp 0x1276f
lock
incl 0x8(%rax)
leaq 0x30(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x111ee
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x12712
callq 0x65d6
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rax
movq 0x550(%rax), %rsi
callq 0x108ce
movl %eax, %ebp
movq %rdx, %r14
movq 0x50(%rsp), %rdi
movl $0x4, %esi
movq %rbx, %rdx
movl %eax, %ecx
movq %r14, %r8
callq 0xf382
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x12753
callq 0x65d6
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x1276f
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1278a
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x127ab
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x5390
movl %ebp, %eax
movq %r14, %rdx
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x5a9e(%rip), %rsi      # 0x18274
movq %rax, %rdi
callq 0x5150
jmp 0x127fc
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x5a80(%rip), %rsi      # 0x18274
movq %rax, %rdi
callq 0x5150
movq 0xe7ed(%rip), %rsi      # 0x20ff0
movq 0xe7a6(%rip), %rdx      # 0x20fb0
movq %r14, %rdi
callq 0x55d0
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x1285b
callq 0x65d6
jmp 0x1285b
movq %rax, %rbx
movl $0x68, %esi
movq %r14, %rdi
callq 0x5390
jmp 0x1285b
jmp 0x1283a
movq %rax, %rbx
jmp 0x1286a
jmp 0x12841
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x128c2
movq %rax, %rbx
jmp 0x128a1
movq %rax, %rbx
jmp 0x12886
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1286a
callq 0x65d6
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x12886
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x128a1
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x5390
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x128c2
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x5390
movq %rbx, %rdi
callq 0x55e0