// File: /workspace/github_c_makefile_star2_O1/oridb[P]mc/6/simp.c
// Function: capture
static Node *
capture(Simp *s, Node *n, Node *dst)
{
	Node *fn, *t, *f, *e, *val, *dcl, *fp, *envsz;
	size_t nenv, nenvt, off, sz, i;
	Type **envt;
	Node **env;

	f = simpcode(s, n);
	fn = n->expr.args[0];
	fn = fn->lit.fnval;
	if (!dst) {
		dst = gentemp(n->loc, closuretype(exprtype(f)), &dcl);
		forcelocal(s, dcl);
	}
	fp = addr(s, dst, exprtype(dst));
	assignat(s, fp, Ptrsz, f);

	env = getclosure(fn->func.scope, &nenv);
	if (env) {
		/*
		 * we need these in a deterministic order so that we can
		 * put them in the right place both when we use them and
		 * when we capture them. 
		 */
		qsort(env, nenv, sizeof(Node*), envcmp);

		/* make the tuple that will hold the environment */
		envt = NULL;
		nenvt = 0;
		/* reserve space for size */
		lappend(&envt, &nenvt, tyintptr);
		sz = Ptrsz;
		for (i = 0; i < nenv; i++) {
			lappend(&envt, &nenvt, decltype(env[i]));
			sz += size(env[i]);
			sz = alignto(sz, decltype(env[i]));
		}

		t = gentemp(n->loc, mktytuple(n->loc, envt, nenvt), &dcl);
		forcelocal(s, dcl);
		e = addr(s, t, exprtype(t));
		envsz = mkintlit(n->loc, sz);
		envsz->expr.type = tyintptr;
		assignat(s, e, 0, envsz);
		assignat(s, fp, 0, e);

		off = Ptrsz;    /* we start with the size of the env */
		for (i = 0; i < nenv; i++) {
			off = alignto(off, decltype(env[i]));
			val = mkexpr(n->loc, Ovar, env[i]->decl.name, NULL);
			val->expr.type = env[i]->decl.type;
			val->expr.did = env[i]->decl.did;
			assignat(s, e, off, rval(s, val, NULL));
			off += size(env[i]);
		}
		free(env);
	} else {
		/*
		 * We need to zero out the environment, so that
		 * duplicating the function doesn't think we have
		 * a bogus environment.
		 */
		e = mkintlit(n->loc, 0);
		e->expr.type = tyintptr;
		assignat(s, fp, 0, e);
	}
	return dst;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %rbx
callq 0x119c9
movq %rax, %r12
movq 0x50(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r15
testq %r13, %r13
jne 0x110d0
movq %r12, %rdi
callq 0x1e5d4
movq %rax, %rdi
callq 0x57ee
movq (%rbp), %rdi
leaq 0x30(%rsp), %r14
movq %rax, %rsi
movq %r14, %rdx
callq 0x1e345
movq %rax, %r13
movq (%r14), %rsi
movq %rbx, %rdi
callq 0xf834
movq %rbp, 0x8(%rsp)
movq %r13, %rdi
callq 0x1e5d4
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xf094
movq %rax, %r14
movl $0x8, %ebp
movl $0x8, %edx
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rcx
callq 0xf157
movq 0x20(%r15), %rdi
movq %rsp, %rsi
callq 0x203f2
testq %rax, %rax
je 0x112fc
movq %rax, %r12
movq %r14, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq (%rsp), %rsi
leaq 0x911(%rip), %rcx       # 0x11a44
movl $0x8, %edx
movq %rax, %rdi
callq 0x40d0
xorl %r14d, %r14d
leaq 0x10(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x18(%rsp), %rsi
movq %r14, (%rsi)
movq 0x501ce(%rip), %rdx     # 0x61328
callq 0x2d3ff
cmpq %r14, (%rsp)
je 0x111c2
movl $0x8, %ebp
xorl %r15d, %r15d
leaq 0x10(%rsp), %r13
leaq 0x18(%rsp), %r14
movq (%r12,%r15,8), %rdi
callq 0x1e56c
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2d3ff
movq (%r12,%r15,8), %rdi
callq 0x1281a
addq %rax, %rbp
movq (%r12,%r15,8), %rdi
callq 0x1e56c
movq %rax, %rdi
callq 0x1282d
movq %rbp, %rdi
movq %rax, %rsi
callq 0x2e697
movq %rax, %rbp
incq %r15
cmpq (%rsp), %r15
jb 0x11177
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %r15
movq (%r15), %rdi
callq 0x231f4
movq (%r15), %rdi
leaq 0x30(%rsp), %r14
movq %rax, %rsi
movq %r14, %rdx
callq 0x1e345
movq %rax, %r13
movq (%r14), %rsi
movq %rbx, %rdi
callq 0xf834
movq %r13, %rdi
callq 0x1e5d4
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xf094
movq %rax, %r13
movq (%r15), %rdi
movq %rbp, %rsi
callq 0x1d7ca
movq 0x50103(%rip), %rcx     # 0x61328
movq %rcx, 0x20(%rax)
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0xf157
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
xorl %edx, %edx
movq %r13, %rcx
callq 0xf157
cmpq $0x0, (%rsp)
je 0x112ed
movl $0x8, %ebp
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
callq 0x1e56c
movq %rax, %rdi
callq 0x1282d
movq %rbp, %rdi
movq %rax, %rsi
callq 0x2e697
movq %rax, %r14
movq (%r12,%r15,8), %rax
movq 0x20(%rax), %rdx
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movl $0x33, %esi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1d832
movq (%r12,%r15,8), %rcx
movq 0x28(%rcx), %rcx
movq %rcx, 0x20(%rax)
movq (%r12,%r15,8), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, 0x38(%rax)
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0xfb13
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rax, %rcx
callq 0xf157
movq (%r12,%r15,8), %rdi
callq 0x1281a
movq %rax, %rbp
addq %r14, %rbp
incq %r15
cmpq (%rsp), %r15
jb 0x1125e
movq %r12, %rdi
callq 0x4040
movq 0x28(%rsp), %r13
jmp 0x11326
movq 0x8(%rsp), %rax
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1d7ca
movq 0x50016(%rip), %rcx     # 0x61328
movq %rcx, 0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0xf157
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq