// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
cmpl $0x2, %edi
jne 0x13cda
movq %rsi, %rbx
movq %rsi, %rdi
callq 0x11122
movl %eax, %ebp
movq %rdx, %r15
movq %rbx, %rdi
callq 0x11122
cmpl $0x4, %ebp
jne 0x13cf8
cmpl $0x5, %eax
jne 0x13cf8
movq %rdx, %r14
leaq 0x58(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0x7c3e
leaq 0x28(%rsp), %rdi
movq %rbx, -0x8(%rdi)
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
callq 0x7c3e
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
movl $0x28, %edi
callq 0x5380
movq %rax, %r15
movq 0x20(%rsp), %rax
movq %rax, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
callq 0x7c3e
movq %rsp, %rdx
movq %r15, (%rdx)
leaq 0x1f8(%rip), %rax       # 0x13e56
movq %rax, 0x18(%rdx)
leaq 0x549(%rip), %rax       # 0x141b2
movq %rax, 0x10(%rdx)
addq $0x318, %rbx            # imm = 0x318
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14c96
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x13c96
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x13cad
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5390
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x13cc4
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x5390
movl $0x2, %eax
xorl %edx, %edx
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x4798(%rip), %rsi      # 0x18486
movq %rax, %rdi
callq 0x5150
jmp 0x13d14
movl $0x10, %edi
callq 0x51b0
movq %rax, %r14
leaq 0x477a(%rip), %rsi      # 0x18486
movq %rax, %rdi
callq 0x5150
movq 0xd2d5(%rip), %rsi      # 0x20ff0
movq 0xd28e(%rip), %rdx      # 0x20fb0
movq %r14, %rdi
callq 0x55d0
jmp 0x13d2c
movq %rax, %rbx
movq %r14, %rdi
callq 0x5280
jmp 0x13dc3
jmp 0x13d85
movq %rax, %rbx
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x13d90
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x13d90
jmp 0x13d85
movq %rax, %rbx
movl $0x28, %esi
movq %r15, %rdi
callq 0x5390
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x13d90
movq %rsp, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x13d90
movq %rax, %rdi
callq 0x647b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x13dac
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x5390
jmp 0x13dac
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x13dc3
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x5390
movq %rbx, %rdi
callq 0x55e0