// File: /workspace/github_c_makefile_star2_O3/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 0x1180a
movq %rax, %r12
movq 0x50(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r15
testq %r13, %r13
jne 0x10ef4
movq %r12, %rdi
callq 0x1e384
movq %rax, %rdi
callq 0x5738
movq (%rbp), %rdi
leaq 0x30(%rsp), %r14
movq %rax, %rsi
movq %r14, %rdx
callq 0x1e0f5
movq %rax, %r13
movq (%r14), %rsi
movq %rbx, %rdi
callq 0xf641
movq %rbp, 0x8(%rsp)
movq %r13, %rdi
callq 0x1e384
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xee74
movq %rax, %r14
movl $0x8, %ebp
movl $0x8, %edx
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rcx
callq 0xef42
movq 0x20(%r15), %rdi
movq %rsp, %rsi
callq 0x201b3
testq %rax, %rax
je 0x11120
movq %rax, %r12
movq %r14, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq (%rsp), %rsi
leaq 0x92e(%rip), %rcx       # 0x11885
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 0x503aa(%rip), %rdx     # 0x61328
callq 0x2ceff
cmpq %r14, (%rsp)
je 0x10fe6
movl $0x8, %ebp
xorl %r15d, %r15d
leaq 0x10(%rsp), %r13
leaq 0x18(%rsp), %r14
movq (%r12,%r15,8), %rdi
callq 0x1e31c
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2ceff
movq (%r12,%r15,8), %rdi
callq 0x12664
addq %rax, %rbp
movq (%r12,%r15,8), %rdi
callq 0x1e31c
movq %rax, %rdi
callq 0x12677
movq %rbp, %rdi
movq %rax, %rsi
callq 0x2e217
movq %rax, %rbp
incq %r15
cmpq (%rsp), %r15
jb 0x10f9b
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %r15
movq (%r15), %rdi
callq 0x22d46
movq (%r15), %rdi
leaq 0x30(%rsp), %r14
movq %rax, %rsi
movq %r14, %rdx
callq 0x1e0f5
movq %rax, %r13
movq (%r14), %rsi
movq %rbx, %rdi
callq 0xf641
movq %r13, %rdi
callq 0x1e384
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xee74
movq %rax, %r13
movq (%r15), %rdi
movq %rbp, %rsi
callq 0x1d57a
movq 0x502df(%rip), %rcx     # 0x61328
movq %rcx, 0x20(%rax)
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0xef42
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
xorl %edx, %edx
movq %r13, %rcx
callq 0xef42
cmpq $0x0, (%rsp)
je 0x11111
movl $0x8, %ebp
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
callq 0x1e31c
movq %rax, %rdi
callq 0x12677
movq %rbp, %rdi
movq %rax, %rsi
callq 0x2e217
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 0x1d5e2
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 0xf923
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rax, %rcx
callq 0xef42
movq (%r12,%r15,8), %rdi
callq 0x12664
movq %rax, %rbp
addq %r14, %rbp
incq %r15
cmpq (%rsp), %r15
jb 0x11082
movq %r12, %rdi
callq 0x4040
movq 0x28(%rsp), %r13
jmp 0x1114a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1d57a
movq 0x501f2(%rip), %rcx     # 0x61328
movq %rcx, 0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0xef42
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq