// File: /workspace/github_c_makefile_star2_O1/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
testq %rsi, %rsi
je 0x2bce1
movq %rdi, %r12
movq %rsi, %r13
movq (%rdi), %rbx
cmpq $0x0, 0x18(%rbx)
je 0x2b689
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x10(%rbx), %rax
movq (%rax,%r15,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d672
addl $-0x35, %eax
cmpl $-0x2, %eax
adcq $0x0, %r14
incq %r15
movq 0x18(%rbx), %rcx
cmpq %rcx, %r15
jb 0x2b654
testq %r14, %r14
sete %al
testq %rcx, %rcx
sete %cl
jmp 0x2b68d
movb $0x1, %al
movb $0x1, %cl
testb %al, %al
je 0x2b69e
movq 0x30(%rbx), %r14
incq 0x68(%r14)
jmp 0x2bc72
testb %cl, %cl
jne 0x2bd00
cmpq $0x0, 0x18(%rbx)
movq %r12, 0x20(%rsp)
je 0x2b6e2
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d672
addl $-0x33, %eax
cmpl $0x2, %eax
jae 0x2b6ed
incq %r14
cmpq 0x18(%rbx), %r14
jb 0x2b6be
jmp 0x2b6fa
movq $0x0, 0x8(%rsp)
jmp 0x2b6fa
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
leaq 0x28(%rsp), %rax
movq %rbx, (%rax)
leaq 0x18(%rsp), %r12
movq %rbx, (%r12)
cmpq $0x1, %r13
adcq %rbx, %r13
movq %r13, 0x40(%rsp)
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %r14
cmpq $0x0, 0x18(%r14)
je 0x2b7f2
xorl %r13d, %r13d
movq 0x10(%r14), %rax
movq (%rax,%r13,8), %rbp
movq 0x8(%rbp), %rdi
callq 0x1d672
leal -0x33(%rax), %ecx
cmpl $0x2, %ecx
jae 0x2b757
incq %r13
cmpq 0x18(%r14), %r13
jb 0x2b730
jmp 0x2b7f2
cmpl $0x35, %eax
jne 0x2bcc2
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movq (%rbp), %rdx
movq (%rcx), %rax
cmpq (%rdx), %rax
jne 0x2b749
testq %rax, %rax
je 0x2b793
movq 0x8(%rcx), %rcx
movq 0x8(%rdx), %rdx
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
cmpl (%rdx,%rsi,4), %edi
jne 0x2b749
incq %rsi
cmpq %rsi, %rax
jne 0x2b783
cmpq $0x0, 0x18(%rsp)
je 0x2b7c0
xorl %r15d, %r15d
movq 0x28(%rsp), %rax
movq (%rax,%r15,8), %rdi
movq 0x8(%rbp), %rsi
callq 0x2cbf0
testl %eax, %eax
jne 0x2b7c3
incq %r15
cmpq 0x18(%rsp), %r15
jb 0x2b79e
jmp 0x2b7c3
xorl %r15d, %r15d
movq 0x18(%rsp), %rax
testq %rax, %rax
setne %cl
cmpq %rax, %r15
setne %al
testb %al, %cl
jne 0x2b749
movq 0x8(%rbp), %rdx
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x2d3ff
jmp 0x2b749
incq %rbx
cmpq 0x40(%rsp), %rbx
jne 0x2b719
xorl %eax, %eax
movq %rax, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movq %rax, 0x50(%rsp)
movq %rax, 0x48(%rsp)
cmpq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rbx
je 0x2b9d1
leaq 0x38(%rsp), %r13
leaq 0x30(%rsp), %rbp
xorl %r14d, %r14d
xorl %eax, %eax
movq %rax, 0x70(%rsp)
movq %rax, 0x68(%rsp)
xorl %eax, %eax
movq %r14, 0x78(%rsp)
movq (%rbx,%rax,8), %r15
cmpq $0x0, 0x18(%r15)
je 0x2bca3
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r14,8), %rax
movq %rax, 0x80(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %r12
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movq %rax, 0x30(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x10(%r15), %rax
movq (%rax,%rbx,8), %rdx
movq (%rdx), %rsi
movq (%r12), %rax
cmpq (%rsi), %rax
jne 0x2b8bf
testq %rax, %rax
je 0x2b8ba
movq 0x8(%r12), %rcx
movq 0x8(%rsi), %rsi
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
cmpl (%rsi,%rdi,4), %r8d
jne 0x2b8bf
incq %rdi
cmpq %rdi, %rax
jne 0x2b8a8
movq %rdx, %r14
jmp 0x2b8ca
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2d3ff
incq %rbx
cmpq 0x18(%r15), %rbx
jb 0x2b884
movl $0x48, %edi
callq 0x2d348
movq %rax, %r12
movl (%r15), %eax
movl %eax, (%r12)
movq 0x8(%r15), %rax
movq %rax, 0x8(%r12)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%r12)
movq 0x30(%rsp), %rax
movq %rax, 0x18(%r12)
movq 0x20(%r15), %rax
movq %rax, 0x20(%r12)
movq 0x28(%r15), %rax
movq %rax, 0x28(%r12)
movq 0x30(%r15), %rax
movq %rax, 0x30(%r12)
testq %r14, %r14
je 0x2b951
movq 0x8(%r14), %rdi
callq 0x1d672
addl $-0x33, %eax
cmpl $0x2, %eax
jb 0x2b951
movq 0x8(%r14), %rsi
movq 0x80(%rsp), %rdi
callq 0x2cbf0
testl %eax, %eax
movl $0x0, %eax
cmoveq %rax, %r12
testq %r12, %r12
je 0x2b968
leaq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movq %r12, %rdx
callq 0x2d3ff
movq 0x10(%rsp), %rax
incq %rax
cmpq 0x40(%rsp), %rax
movq 0x20(%rsp), %rbx
movq 0x78(%rsp), %r14
jne 0x2b846
movq 0x68(%rsp), %rsi
testq %rsi, %rsi
je 0x2b9c3
movq 0x70(%rsp), %rdi
callq 0x2b624
leaq 0x58(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rax, %rdx
callq 0x2d3ff
movq 0x28(%rsp), %rax
movq (%rax,%r14,8), %rdx
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x2d3ff
incq %r14
cmpq 0x18(%rsp), %r14
jb 0x2b833
xorl %ebx, %ebx
leaq 0x38(%rsp), %r15
movq %rbx, (%r15)
leaq 0x30(%rsp), %r12
movq %rbx, (%r12)
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %r13
movq 0x18(%r13), %rax
testq %rax, %rax
je 0x2ba61
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%r13), %rdx
movq (%rcx), %rsi
movq $-0x1, %r8
xorl %edi, %edi
testq %rsi, %rsi
sete %r9b
movq (%rdx,%rdi,8), %r10
movq (%r10), %r10
cmpq (%r10), %rsi
setne %r11b
movq %rdi, %r14
cmovneq %r8, %r14
orb %r9b, %r11b
jne 0x2ba54
movq 0x8(%rcx), %r9
movq 0x8(%r10), %r10
xorl %r11d, %r11d
movl (%r9,%r11,4), %ebp
cmpl (%r10,%r11,4), %ebp
jne 0x2ba51
incq %r11
cmpq %r11, %rsi
jne 0x2ba3a
movq %rdi, %r14
jmp 0x2ba54
movq %r8, %r14
incq %rdi
movq %r14, %r8
cmpq %rax, %rdi
jne 0x2ba0e
jmp 0x2ba68
movq $-0x1, %r14
cmpq $-0x1, %r14
je 0x2ba9a
movq 0x10(%r13), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d672
cmpl $0x33, %eax
je 0x2ba9a
movq 0x10(%r13), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x1d672
cmpl $0x34, %eax
jne 0x2baa8
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2d3ff
incq %rbx
cmpq 0x40(%rsp), %rbx
jne 0x2b9e4
movq 0x30(%rsp), %rsi
testq %rsi, %rsi
je 0x2bad1
movq 0x38(%rsp), %rdi
callq 0x2b624
movq %rax, 0x10(%rsp)
jmp 0x2bada
movq $0x0, 0x10(%rsp)
cmpq $0x0, 0x3678e(%rip)     # 0x62270
je 0x2bb9e
xorl %r14d, %r14d
movq 0x36786(%rip), %rax     # 0x62278
movq (%rax,%r14,8), %r15
cmpb $0x0, 0x28(%r15)
je 0x2bb0e
incq %r14
cmpq 0x36769(%rip), %r14     # 0x62270
jb 0x2baeb
jmp 0x2bb9e
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x50(%rsp), %r13
movq 0x48(%rsp), %rbp
movq 0x58(%rsp), %r12
movq 0x18(%r15), %rdi
callq 0x2ccd5
testl %eax, %eax
je 0x2bafd
cmpq %rbp, 0x38(%r15)
jne 0x2bafd
testq %rbp, %rbp
je 0x2bb70
xorl %ebx, %ebx
movq 0x40(%r15), %rax
movq (%rax,%rbx,8), %rax
movl (%rax), %eax
movq (%r12,%rbx,8), %rcx
cmpl (%rcx), %eax
jne 0x2bafd
movq 0x30(%r15), %rax
movq (%rax,%rbx,8), %rdi
movq (%r13,%rbx,8), %rsi
callq 0x2cbf0
testl %eax, %eax
je 0x2bafd
incq %rbx
cmpq %rbx, %rbp
jne 0x2bb40
movq 0x50(%r15), %rax
movq 0x10(%rsp), %rcx
orq %rax, %rcx
je 0x2bb8d
movl (%rax), %eax
movq 0x10(%rsp), %rcx
cmpl (%rcx), %eax
jne 0x2bafd
movq 0x366e4(%rip), %rax     # 0x62278
movq (%rax,%r14,8), %r14
incq 0x68(%r14)
jmp 0x2bba1
xorl %r14d, %r14d
testq %r14, %r14
jne 0x2bbf5
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rbx
movq (%rbx), %rdi
callq 0x1e053
movq %rax, %r15
movq (%rbx), %rbx
movl $0x70, %edi
callq 0x2d348
movq %rax, %r14
movq %r15, 0x10(%rax)
movq %rbx, 0x4(%rax)
leaq 0x36697(%rip), %rsi     # 0x62270
movl 0x36691(%rip), %eax     # 0x62270
movl %eax, (%r14)
leaq 0x3668f(%rip), %rdi     # 0x62278
movq %r14, %rdx
callq 0x2d3ff
incq 0x68(%r14)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %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 0x1d672
addl $-0x13, %eax
cmpl $0x32, %eax
ja 0x2bd1f
movabsq $0x6000100a00001, %rcx  # imm = 0x6000100A00001
btq %rax, %rcx
jae 0x2bc87
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1e5d4
movq %rax, %rdi
callq 0x22c44
movq %rax, %rdi
callq 0x2cc57
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 0x2bd1f
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x50(%rax), %rax
movq (%rax), %rdi
jmp 0x2bc59
leaq 0xd3c9(%rip), %rdi      # 0x39073
leaq 0xd1c0(%rip), %rsi      # 0x38e71
leaq 0xd3d5(%rip), %rcx      # 0x3908d
movl $0x247, %edx            # imm = 0x247
callq 0x4180
leaq 0x62d8(%rip), %rdi      # 0x31fa1
leaq 0xd1a1(%rip), %rsi      # 0x38e71
leaq 0xd378(%rip), %rcx      # 0x3904f
movl $0x2e4, %edx            # imm = 0x2E4
callq 0x4180
leaq 0xd359(%rip), %rdi      # 0x39041
leaq 0xd182(%rip), %rsi      # 0x38e71
leaq 0xd359(%rip), %rcx      # 0x3904f
movl $0x2a1, %edx            # imm = 0x2A1
callq 0x4180
leaq 0xd36c(%rip), %rdi      # 0x39073
leaq 0xd163(%rip), %rsi      # 0x38e71
leaq 0xd33a(%rip), %rcx      # 0x3904f
movl $0x2b5, %edx            # imm = 0x2B5
callq 0x4180
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rbx
movq %rbx, %rdi
callq 0x1d672
movl %eax, %eax
leaq 0x34667(%rip), %rcx     # 0x603a0
movq (%rcx,%rax,8), %r15
movq 0x8(%r14), %rdi
callq 0x1e5d4
movq %rax, %rdi
callq 0x23e44
leaq 0xd1b7(%rip), %rsi      # 0x38f0c
movq %rbx, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x13aa3