// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1a960
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1955d
movq -0x10(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rdi
callq 0x30340
movq %rax, %rdi
callq 0x7a40
movq %rax, %rsi
movq -0x98(%rbp), %rax
movq (%rax), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2ffd0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x172b0
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x30340
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq %rax, %rdx
callq 0x16af0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rcx
movl $0x8, %edx
callq 0x16b90
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x60(%rbp), %rsi
callq 0x333b0
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0x198c9
movq -0x90(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl $0x8, %edx
leaq 0x1417(%rip), %rcx      # 0x1aa00
callq 0x60b0
movq $0x0, -0x88(%rbp)
movq $0x0, -0x68(%rbp)
movq 0x63840(%rip), %rdx     # 0x7ce48
leaq -0x88(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x48290
movq $0x8, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x196bc
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30260
movq %rax, %rdx
leaq -0x88(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x48290
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1c480
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30260
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x16770
movq %rax, -0x78(%rbp)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x19628
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
movq -0x88(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq (%rax), %rdi
callq 0x37490
movq %rax, %rsi
movq -0xc8(%rbp), %rax
movq (%rax), %rdi
leaq -0x48(%rbp), %rdx
callq 0x2ffd0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x172b0
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rdi
callq 0x30340
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq %rax, %rdx
callq 0x16af0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rsi
movq (%rax), %rdi
callq 0x2f3c0
movq %rax, -0x58(%rbp)
movq 0x636ef(%rip), %rcx     # 0x7ce48
movq -0x58(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x58(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
callq 0x16b90
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
callq 0x16b90
movq $0x8, -0x70(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x198bb
movq -0x70(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30260
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0x16770
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq -0x90(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x20(%rcx), %rdx
movq (%rax), %rdi
movl $0x33, %esi
xorl %eax, %eax
movl %eax, %ecx
movb $0x0, %al
callq 0x2f400
movq %rax, -0x40(%rbp)
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x28(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x177e0
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
movq %rax, %rcx
callq 0x16b90
movq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1c480
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x1979b
movq -0x90(%rbp), %rdi
callq 0x6040
jmp 0x19901
movq -0x10(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2f3c0
movq %rax, -0x38(%rbp)
movq 0x63564(%rip), %rcx     # 0x7ce48
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
callq 0x16b90
movq -0x18(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
nop