// File: /workspace/github_cpp_makefile_star2_O1/iamlouk[P]asbi/src/procenv.cc
// Function: macro_system(int, asbi::Context*, std::shared_ptr<asbi::Env>)
static Value macro_system(int n, Context* ctx, std::shared_ptr<Env>) {
	if (n != 2)
		throw std::runtime_error("env:$ usage error");

	auto arg = ctx->pop();
	auto callback = ctx->pop();
	if (arg.type != type_t::String || callback.type != type_t::Lambda)
		throw std::runtime_error("env:$ usage error");

	std::string cmd = arg._string->data;
	auto task = [ctx, cmd](evts::callback_t cb) -> void {
		pid_t pid = fork();
		if (pid == -1) {
			std::vector<Value> args;
			args.push_back(Value::symbol(ctx->names.err));
			args.push_back(Value::number(errno));
			cb(&args, true);
			return;
		}

		if (pid == 0) {
			const char* shell = getenv("SHELL");
			shell = shell != NULL ? shell : "/bin/sh";
			execl(shell, shell, "-c", cmd.c_str(), NULL);
			return;
		}

		int wstatus;
		if (waitpid(pid, &wstatus, 0) == -1) {
			// TODO...
			throw std::runtime_error("waitpid failed");
		}

		std::vector<Value> args;
		if (WIFEXITED(wstatus)) {
			args.push_back(Value::symbol(ctx->names.ok));
			args.push_back(Value::number(WEXITSTATUS(wstatus)));
		} else {
			args.push_back(Value::symbol(ctx->names.err));
			args.push_back(Value::number(WTERMSIG(wstatus)));
		}
		cb(&args, true);
	};

	ctx->evtloop.addTask(callback._lambda, task);

	return Value::nil();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
cmpl $0x2, %edi
jne 0x13e97
movq %rsi, %rbx
movq %rsi, %rdi
callq 0x11276
movl %eax, %ebp
movq %rdx, 0x28(%rsp)
movq %rbx, %rdi
callq 0x11276
movq %rdx, 0x20(%rsp)
cmpl $0x4, %ebp
jne 0x13eb5
cmpl $0x5, %eax
jne 0x13eb5
movq 0x28(%rsp), %rax
leaq 0x68(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x7d0e
leaq 0x38(%rsp), %rdi
movq %rbx, -0x8(%rdi)
leaq 0x48(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
callq 0x7d0e
movq 0x20(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movl $0x28, %edi
callq 0x5380
movq %rax, %r15
movq 0x30(%rsp), %rax
movq %rax, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
addq %rsi, %rdx
callq 0x7d0e
movq %rsp, %rdx
movq %r15, (%rdx)
leaq 0x1f7(%rip), %rax       # 0x14012
movq %rax, 0x18(%rdx)
leaq 0x598(%rip), %rax       # 0x143be
movq %rax, 0x10(%rdx)
addq $0x318, %rbx            # imm = 0x318
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14ec8
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x13e53
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x38(%rsp), %rdi
cmpq %r13, %rdi
je 0x13e6a
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x13e81
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x5390
movl $0x2, %eax
xorl %edx, %edx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x45db(%rip), %rsi      # 0x18486
movq %rax, %rdi
callq 0x5150
jmp 0x13ed1
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x45bd(%rip), %rsi      # 0x18486
movq %rax, %rdi
callq 0x5150
movq 0xd118(%rip), %rsi      # 0x20ff0
movq 0xd0d1(%rip), %rdx      # 0x20fb0
movq %r14, %rdi
callq 0x55d0
jmp 0x13ee9
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x13f80
jmp 0x13f5e
movq %rax, %rbx
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x13f45
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x13f45
jmp 0x13f5e
movq %rax, %rbx
movl $0x28, %esi
movq %r15, %rdi
callq 0x5390
jmp 0x13f2e
movq %rax, %rbx
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x13f45
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x38(%rsp), %rdi
cmpq %r13, %rdi
je 0x13f69
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x13f69
movq %rax, %rdi
callq 0x64ab
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x13f80
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x5390
movq %rbx, %rdi
callq 0x55e0