// File: /workspace/github_c_makefile_star2_O3/oridb[P]mc/mi/match.c
// Function: compile
static Dtree *
compile(Frontier **frontier, size_t nfrontier)
{
	size_t i, j, k;
	Dtree *dt, *out, **edge, *any;
	Frontier *fs, **row, **defaults ;
	Node **cs, **pat;
	Slot *slot, *s;
	size_t ncs, ncons, nrow, nedge, ndefaults, npat;

	assert(nfrontier > 0);

	fs = frontier[0];

	/* scan constructors horizontally */
	ncons = 0;
	for (i = 0; i < fs->nslot; i++) {
		switch (exprop(fs->slot[i]->pat)) {
		case Ovar:
		case Ogap:
			break;
		default:
			ncons++;
		}
	}
	if (ncons == 0) {
		fs->final->refcnt++;
		return fs->final;
	}

	assert(fs->nslot > 0);

	/* NOTE:
	 * at the moment we have not implemented any smarter heuristics described in the papers.
	 * we always select the first found constructor, i.e. the top-left one.
	 */

	slot = NULL;
	for (i = 0; i < fs->nslot; i++) {
		switch (exprop(fs->slot[i]->pat)) {
		case Ovar:
		case Ogap:
			continue;
		default:
			slot = fs->slot[i];
			goto pi_found;
		}
	}

pi_found:
	/* scan constructors vertically at pi to create the set 'CS' */
	cs = NULL;
	ncs = 0;
	for (i = 0; i < nfrontier; i++) {
		fs = frontier[i];
		for (j = 0; j < fs->nslot; j++) {
			s = fs->slot[j];
			switch (exprop(s->pat)) {
			case Ovar:
			case Ogap:
				break;
			case Olit:
				if (patheq(slot->path, s->path)) {
					/* NOTE:
					 * we could use a hash table, but given that the n is usually small,
					 * an exhaustive search would suffice.
					 */
					/* look for a duplicate entry to skip it; we want a unique set of constructors. */
					for (k = 0; k < ncs; k++) {
						if (pateq(cs[k], s->pat))
							break;
					}
					if (ncs == 0 || k == ncs)
						lappend(&cs, &ncs, s->pat);
				}
				break;
			default:
				assert(0);
			}
		}
	}
	/* project a new frontier for each selected constructor */
	edge = NULL;
	nedge = 0;
	pat = NULL;
	npat = 0;
	for (i = 0; i < ncs; i++) {
		row = NULL;
		nrow = 0;
		for (j = 0; j < nfrontier; j++) {
			fs = frontier[j];
			fs = project(cs[i], slot->path, slot->load, frontier[j]);
			if (fs != NULL)
				lappend(&row, &nrow, fs);
		}

		if (nrow > 0) {
			dt = compile(row, nrow);
			lappend(&edge, &nedge, dt);
			lappend(&pat, &npat, cs[i]);
		}
	}

	/* compile the defaults */
	defaults = NULL;
	ndefaults = 0;
	for (i = 0; i < nfrontier; i++) {
		fs = frontier[i];
		k = -1;
		for (j = 0; j < fs->nslot; j++) {
			/* locate the occurrence of pi in fs */
			if (patheq(slot->path, fs->slot[j]->path))
				k = j;
		}

		if (k == -1 || exprop(fs->slot[k]->pat) == Ovar || exprop(fs->slot[k]->pat) == Ogap)
			lappend(&defaults, &ndefaults, fs);
	}
	if (ndefaults)
		any = compile(defaults, ndefaults);
	else
		any = NULL;

	/* construct the result dtree */
	out = NULL;

	/* TODO
	 * use a hash table to avoid the quadratic complexity
	 * when we have a large N and the bottleneck becomes obvious.
	 */
	for (i = 0; i < ndtree; i++) {
		if (!dtree[i]->accept && dtreusable(dtree[i], slot->load, pat, npat, edge, nedge, any)) {
			out = dtree[i];
			out->refcnt++;
			break;
		}
	}
	if (out == NULL) {
		out = mkdtree(slot->pat->loc, genlbl(slot->pat->loc));
		out->refcnt++;
	}

	out->load = slot->load;
	out->npat = npat,
	out->pat = pat,
	out->nnext = nedge;
	out->next = edge;
	out->any = any;

	switch (exprop(slot->load)) {
	case Outag:
		out->nconstructors = nconstructors(tybase(exprtype(slot->load->expr.args[0])));
		break;
	case Oudata:
	case Omemb:
	case Otupget:
	case Oidx:
	case Oderef:
	case Ovar:
		out->nconstructors = nconstructors(tybase(exprtype(slot->load)));
		break;
	default:
		fatal(slot->pat, "unsupported pattern %s of type %s", opstr[exprop(slot->pat)], tystr(exprtype(slot->pat)));
	}
	return out;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, 0x40(%rsp)
testq %rsi, %rsi
je 0x2b7d8
movq %rdi, %r13
movq (%rdi), %rbx
cmpq $0x0, 0x18(%rbx)
je 0x2b204
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x10(%rbx), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d422
addl $-0x35, %eax
cmpl $-0x2, %eax
adcq $0x0, %r14
incq %r15
movq 0x18(%rbx), %rax
cmpq %rax, %r15
jb 0x2b1a5
testq %r14, %r14
je 0x2b204
testq %rax, %rax
je 0x2b7f7
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d422
addl $-0x33, %eax
cmpl $0x2, %eax
jae 0x2b211
incq %r14
cmpq 0x18(%rbx), %r14
jb 0x2b1e0
jmp 0x2b219
movq 0x30(%rbx), %r14
incq 0x68(%r14)
jmp 0x2b76d
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %r15
xorl %ebx, %ebx
leaq 0x20(%rsp), %rax
movq %rbx, (%rax)
leaq 0x18(%rsp), %r12
movq %rbx, (%r12)
movq %r15, 0x8(%rsp)
movq %r13, 0x38(%rsp)
movq (%r13,%rbx,8), %r14
cmpq $0x0, 0x18(%r14)
je 0x2b304
xorl %r13d, %r13d
movq 0x10(%r14), %rax
movq (%rax,%r13,8), %rbp
movq 0x8(%rbp), %rdi
callq 0x1d422
leal -0x33(%rax), %ecx
cmpl $0x2, %ecx
jae 0x2b270
incq %r13
cmpq 0x18(%r14), %r13
jb 0x2b249
jmp 0x2b304
cmpl $0x35, %eax
jne 0x2b7b9
movq (%r15), %rcx
movq (%rbp), %rdx
movq (%rcx), %rax
cmpq (%rdx), %rax
jne 0x2b262
testq %rax, %rax
je 0x2b2a7
movq 0x8(%rcx), %rcx
movq 0x8(%rdx), %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
cmpl (%rdx,%rsi,4), %edi
jne 0x2b262
incq %rsi
cmpq %rsi, %rax
jne 0x2b297
cmpq $0x0, 0x18(%rsp)
je 0x2b2ee
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rdi
movq 0x8(%rbp), %rsi
callq 0x2c6c9
movq 0x18(%rsp), %rcx
testl %eax, %eax
jne 0x2b2d5
incq %r15
cmpq %rcx, %r15
jb 0x2b2b2
testq %rcx, %rcx
setne %al
cmpq %rcx, %r15
setne %cl
testb %cl, %al
movq 0x8(%rsp), %r15
jne 0x2b262
movq 0x8(%rbp), %rdx
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2ceff
jmp 0x2b262
incq %rbx
cmpq 0x40(%rsp), %rbx
movq 0x38(%rsp), %r13
jne 0x2b236
xorl %eax, %eax
movq %rax, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
cmpq %rax, 0x18(%rsp)
je 0x2b4db
leaq 0x30(%rsp), %r13
leaq 0x28(%rsp), %rbp
xorl %ebx, %ebx
xorl %eax, %eax
movq %rax, 0x70(%rsp)
movq %rax, 0x68(%rsp)
xorl %ecx, %ecx
movq %rbx, 0x78(%rsp)
movq 0x38(%rsp), %rax
movq (%rax,%rcx,8), %r12
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x2b79a
movq %rcx, 0x10(%rsp)
movq 0x20(%rsp), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rcx, 0x80(%rsp)
movq (%r15), %r14
xorl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x10(%r12), %rcx
movq (%rcx,%r15,8), %rdx
movq (%rdx), %rdi
movq (%r14), %rcx
cmpq (%rdi), %rcx
jne 0x2b3d3
testq %rcx, %rcx
je 0x2b3ce
movq 0x8(%r14), %rsi
movq 0x8(%rdi), %rdi
xorl %r8d, %r8d
movl (%rsi,%r8,4), %r9d
cmpl (%rdi,%r8,4), %r9d
jne 0x2b3d3
incq %r8
cmpq %r8, %rcx
jne 0x2b3bc
movq %rdx, %rbx
jmp 0x2b3e3
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2ceff
movq 0x18(%r12), %rax
incq %r15
cmpq %rax, %r15
jb 0x2b398
movl $0x48, %edi
callq 0x2ce48
movq %rax, %r14
movl (%r12), %eax
movl %eax, (%r14)
movq 0x8(%r12), %rax
movq %rax, 0x8(%r14)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%r14)
movq 0x28(%rsp), %rax
movq %rax, 0x18(%r14)
movq 0x20(%r12), %rax
movq %rax, 0x20(%r14)
movq 0x28(%r12), %rax
movq %rax, 0x28(%r14)
movq 0x30(%r12), %rax
movq %rax, 0x30(%r14)
testq %rbx, %rbx
je 0x2b460
movq 0x8(%rbx), %rdi
callq 0x1d422
addl $-0x33, %eax
cmpl $0x2, %eax
jb 0x2b460
movq 0x8(%rbx), %rsi
movq 0x80(%rsp), %rdi
callq 0x2c6c9
testl %eax, %eax
je 0x2b472
leaq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movq %r14, %rdx
callq 0x2ceff
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rcx
incq %rcx
cmpq 0x40(%rsp), %rcx
movq 0x78(%rsp), %rbx
jne 0x2b357
movq 0x68(%rsp), %rsi
testq %rsi, %rsi
je 0x2b4cd
movq 0x70(%rsp), %rdi
callq 0x2b173
leaq 0x58(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rax, %rdx
callq 0x2ceff
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rdx
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x2ceff
incq %rbx
cmpq 0x18(%rsp), %rbx
jb 0x2b344
xorl %r14d, %r14d
leaq 0x30(%rsp), %r15
movq %r14, (%r15)
leaq 0x28(%rsp), %rbx
movq %r14, (%rbx)
movq 0x38(%rsp), %rax
movq (%rax,%r14,8), %r12
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x2b5a3
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%r12), %rdx
movq (%rcx), %rsi
movq $-0x1, %r13
xorl %edi, %edi
movq %r13, %r8
testq %rsi, %rsi
sete %r9b
movq (%rdx,%rdi,8), %r10
movq (%r10), %r10
cmpq (%r10), %rsi
setne %r11b
movq %rdi, %r13
cmovneq %r8, %r13
orb %r9b, %r11b
jne 0x2b567
movq 0x8(%rcx), %r9
movq 0x8(%r10), %r10
xorl %r11d, %r11d
movl (%r9,%r11,4), %ebp
cmpl (%r10,%r11,4), %ebp
jne 0x2b564
incq %r11
cmpq %r11, %rsi
jne 0x2b54d
movq %rdi, %r13
jmp 0x2b567
movq %r8, %r13
incq %rdi
cmpq %rax, %rdi
jne 0x2b51e
cmpq $-0x1, %r13
je 0x2b5a3
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d422
cmpl $0x33, %eax
je 0x2b5a3
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d422
cmpl $0x34, %eax
jne 0x2b5b1
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2ceff
incq %r14
cmpq 0x40(%rsp), %r14
jne 0x2b4ee
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x2b5da
movq 0x30(%rsp), %rdi
callq 0x2b173
movq %rax, 0x10(%rsp)
jmp 0x2b5e3
movq $0x0, 0x10(%rsp)
cmpq $0x0, 0x36c85(%rip)     # 0x62270
je 0x2b6a8
xorl %r14d, %r14d
movq 0x36c7d(%rip), %rax     # 0x62278
movq (%rax,%r14,8), %r15
cmpb $0x0, 0x28(%r15)
je 0x2b617
incq %r14
cmpq 0x36c60(%rip), %r14     # 0x62270
jb 0x2b5f4
jmp 0x2b6a8
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r13
movq 0x58(%rsp), %rbp
movq 0x18(%r15), %rdi
callq 0x2c7ae
testl %eax, %eax
je 0x2b606
cmpq %r13, 0x38(%r15)
jne 0x2b606
testq %r13, %r13
je 0x2b679
xorl %ebx, %ebx
movq 0x40(%r15), %rax
movq (%rax,%rbx,8), %rax
movl (%rax), %eax
movq (%rbp,%rbx,8), %rcx
cmpl (%rcx), %eax
jne 0x2b606
movq 0x30(%r15), %rax
movq (%rax,%rbx,8), %rdi
movq (%r12,%rbx,8), %rsi
callq 0x2c6c9
testl %eax, %eax
je 0x2b606
incq %rbx
cmpq %rbx, %r13
jne 0x2b649
movq 0x50(%r15), %rax
movq 0x10(%rsp), %rcx
orq %rax, %rcx
je 0x2b696
movl (%rax), %eax
movq 0x10(%rsp), %rcx
cmpl (%rcx), %eax
jne 0x2b606
movq 0x36bdb(%rip), %rax     # 0x62278
movq (%rax,%r14,8), %r14
movq 0x8(%rsp), %r12
jmp 0x2b6f4
movq 0x8(%rsp), %r12
movq 0x8(%r12), %rbx
movq (%rbx), %rdi
callq 0x1de03
movq %rax, %r15
movq (%rbx), %rbx
movl $0x70, %edi
callq 0x2ce48
movq %rax, %r14
movq %r15, 0x10(%rax)
movq %rbx, 0x4(%rax)
leaq 0x36b94(%rip), %rsi     # 0x62270
movl 0x36b8e(%rip), %eax     # 0x62270
movl %eax, (%r14)
leaq 0x36b8c(%rip), %rdi     # 0x62278
movq %r14, %rdx
callq 0x2ceff
incq 0x68(%r14)
movq 0x10(%r12), %rdi
movq %rdi, 0x18(%r14)
movq 0x48(%rsp), %rax
movq %rax, 0x38(%r14)
movq 0x50(%rsp), %rax
movq %rax, 0x30(%r14)
movq 0x60(%rsp), %rax
movq %rax, 0x48(%r14)
movq 0x58(%rsp), %rax
movq %rax, 0x40(%r14)
movq 0x10(%rsp), %rax
movq %rax, 0x50(%r14)
callq 0x1d422
addl $-0x13, %eax
cmpl $0x32, %eax
ja 0x2b816
movabsq $0x6000100a00001, %rcx  # imm = 0x6000100A00001
btq %rax, %rcx
jae 0x2b782
movq 0x10(%r12), %rdi
callq 0x1e384
movq %rax, %rdi
callq 0x227ac
movq %rax, %rdi
callq 0x2c730
movq %rax, 0x20(%r14)
movq %r14, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x30, %rax
jne 0x2b816
movq 0x10(%r12), %rax
movq 0x50(%rax), %rax
movq (%rax), %rdi
jmp 0x2b754
leaq 0xd87b(%rip), %rdi      # 0x3901c
leaq 0xd699(%rip), %rsi      # 0x38e41
leaq 0xd887(%rip), %rcx      # 0x39036
movl $0x247, %edx            # imm = 0x247
callq 0x4180
leaq 0x67f1(%rip), %rdi      # 0x31fb1
leaq 0xd67a(%rip), %rsi      # 0x38e41
leaq 0xd82a(%rip), %rcx      # 0x38ff8
movl $0x2e4, %edx            # imm = 0x2E4
callq 0x4180
leaq 0xd80b(%rip), %rdi      # 0x38fea
leaq 0xd65b(%rip), %rsi      # 0x38e41
leaq 0xd80b(%rip), %rcx      # 0x38ff8
movl $0x2a1, %edx            # imm = 0x2A1
callq 0x4180
leaq 0xd81e(%rip), %rdi      # 0x3901c
leaq 0xd63c(%rip), %rsi      # 0x38e41
leaq 0xd7ec(%rip), %rcx      # 0x38ff8
movl $0x2b5, %edx            # imm = 0x2B5
callq 0x4180
movq 0x8(%r12), %rbx
movq %rbx, %rdi
callq 0x1d422
movl %eax, %eax
leaq 0x34b74(%rip), %rcx     # 0x603a0
movq (%rcx,%rax,8), %r14
movq 0x8(%r12), %rdi
callq 0x1e384
movq %rax, %rdi
callq 0x2398e
leaq 0xd693(%rip), %rsi      # 0x38edc
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x138e7