// File: /workspace/github_c_makefile_star2_O2/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, 0x38(%rsp)
testq %rsi, %rsi
je 0x284fe
movq %rdi, %r13
movq (%rdi), %rbx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x18(%rbx), %rax
cmpq %rax, %r14
jae 0x27f9e
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x1b5e6
addl $-0x35, %eax
cmpl $-0x2, %eax
adcq $0x0, %r15
incq %r14
jmp 0x27f75
testq %r15, %r15
je 0x27fd9
testq %rax, %rax
je 0x2851d
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpq %rax, %r14
jae 0x27fee
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rdi
callq 0x1b5e6
addl $-0x33, %eax
cmpl $0x2, %eax
jae 0x27fe6
incq %r14
movq 0x18(%rbx), %rax
jmp 0x27fb2
movq 0x30(%rbx), %r14
incq 0x68(%r14)
jmp 0x28494
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %r12
leaq 0x30(%rsp), %rax
andq $0x0, (%rax)
leaq 0x48(%rsp), %r15
andq $0x0, (%r15)
xorl %ebx, %ebx
movq %r12, 0x8(%rsp)
movq %r13, 0x28(%rsp)
cmpq 0x38(%rsp), %rbx
je 0x280c9
movq (%r13,%rbx,8), %r12
xorl %r13d, %r13d
cmpq 0x18(%r12), %r13
jae 0x280b7
movq 0x10(%r12), %rax
movq (%rax,%r13,8), %rbp
movq 0x8(%rbp), %rdi
callq 0x1b5e6
leal -0x33(%rax), %ecx
cmpl $0x2, %ecx
jae 0x28049
incq %r13
jmp 0x2801f
cmpl $0x35, %eax
jne 0x284df
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq (%rbp), %rsi
callq 0x2934b
testl %eax, %eax
je 0x28044
xorl %r14d, %r14d
movq 0x48(%rsp), %rax
cmpq %rax, %r14
jae 0x28094
movq 0x30(%rsp), %rax
movq (%rax,%r14,8), %rdi
movq 0x8(%rbp), %rsi
callq 0x29379
testl %eax, %eax
jne 0x2808f
incq %r14
jmp 0x2806a
movq 0x48(%rsp), %rax
testq %rax, %rax
setne %cl
cmpq %r14, %rax
setne %al
testb %al, %cl
jne 0x28044
movq 0x8(%rbp), %rdx
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x29b2d
jmp 0x28044
incq %rbx
movq 0x8(%rsp), %r12
movq 0x28(%rsp), %r13
jmp 0x2800c
leaq 0x58(%rsp), %rax
andq $0x0, (%rax)
leaq 0x60(%rsp), %rax
andq $0x0, (%rax)
leaq 0x50(%rsp), %rax
andq $0x0, (%rax)
leaq 0x40(%rsp), %rax
andq $0x0, (%rax)
xorl %ebx, %ebx
cmpq 0x48(%rsp), %rbx
jae 0x28274
andq $0x0, 0x70(%rsp)
andq $0x0, 0x68(%rsp)
xorl %eax, %eax
movq %rbx, 0x78(%rsp)
cmpq 0x38(%rsp), %rax
je 0x2822e
movq (%r13,%rax,8), %r13
movq 0x18(%r13), %r15
testq %r15, %r15
je 0x284c0
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %rax
movq %rax, 0x80(%rsp)
movq (%r12), %rbp
andq $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq %r15, %r12
jae 0x28191
movq 0x10(%r13), %rax
movq (%rax,%r12,8), %r14
movq (%r14), %rsi
movq %rbp, %rdi
callq 0x2934b
testl %eax, %eax
je 0x28176
movq %r14, %rbx
jmp 0x2818c
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x29b2d
movq 0x18(%r13), %r15
incq %r12
jmp 0x28155
pushq $0x48
popq %rdi
callq 0x29a78
movq %rax, %rbp
movl (%r13), %eax
movl %eax, (%rbp)
movq 0x8(%r13), %rax
movq %rax, 0x8(%rbp)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rbp)
movq 0x18(%rsp), %rax
movq %rax, 0x18(%rbp)
movq 0x20(%r13), %rax
movq %rax, 0x20(%rbp)
movq 0x28(%r13), %rax
movq %rax, 0x28(%rbp)
movq 0x30(%r13), %rax
movq %rax, 0x30(%rbp)
testq %rbx, %rbx
je 0x28200
movq 0x8(%rbx), %rdi
callq 0x1b5e6
addl $-0x33, %eax
cmpl $0x2, %eax
jb 0x28200
movq 0x8(%rbx), %rsi
movq 0x80(%rsp), %rdi
callq 0x29379
testl %eax, %eax
je 0x28212
leaq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movq %rbp, %rdx
callq 0x29b2d
movq 0x8(%rsp), %r12
movq 0x28(%rsp), %r13
movq 0x10(%rsp), %rax
incq %rax
movq 0x78(%rsp), %rbx
jmp 0x2810d
movq 0x68(%rsp), %rsi
testq %rsi, %rsi
je 0x2826c
movq 0x70(%rsp), %rdi
callq 0x27f4a
leaq 0x58(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rax, %rdx
callq 0x29b2d
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %rdx
leaq 0x50(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x29b2d
incq %rbx
jmp 0x280ef
leaq 0x20(%rsp), %rax
andq $0x0, (%rax)
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
xorl %r12d, %r12d
pushq $-0x1
popq %r14
cmpq 0x38(%rsp), %r12
je 0x28320
movq (%r13,%r12,8), %r15
movq 0x18(%r15), %rbx
xorl %ebp, %ebp
movq %r14, %r13
cmpq %rbp, %rbx
je 0x282cf
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x10(%r15), %rax
movq (%rax,%rbp,8), %rax
movq (%rax), %rsi
callq 0x2934b
testl %eax, %eax
je 0x282ca
movq %rbp, %r13
incq %rbp
jmp 0x282a6
cmpq $-0x1, %r13
je 0x28301
movq 0x10(%r15), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rdi
callq 0x1b5e6
cmpl $0x33, %eax
je 0x28301
movq 0x10(%r15), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rdi
callq 0x1b5e6
cmpl $0x34, %eax
jne 0x28313
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x29b2d
incq %r12
movq 0x28(%rsp), %r13
jmp 0x2828d
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x2833b
movq 0x20(%rsp), %rdi
callq 0x27f4a
movq %rax, 0x10(%rsp)
jmp 0x28344
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %r15
xorl %r14d, %r14d
cmpq 0x36f1d(%rip), %r14     # 0x5f270
jae 0x28403
movq 0x36f18(%rip), %rax     # 0x5f278
movq (%rax,%r14,8), %r15
cmpb $0x0, 0x28(%r15)
je 0x28375
incq %r14
movq 0x8(%rsp), %r15
jmp 0x2834c
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x50(%rsp), %r12
movq 0x40(%rsp), %r13
movq 0x58(%rsp), %rbp
movq 0x18(%r15), %rdi
callq 0x29450
testl %eax, %eax
je 0x2836b
cmpq %r13, 0x38(%r15)
jne 0x2836b
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x283d4
movq 0x40(%r15), %rax
movq (%rax,%rbx,8), %rax
movl (%rax), %eax
movq (%rbp,%rbx,8), %rcx
cmpl (%rcx), %eax
jne 0x2836b
movq 0x30(%r15), %rax
movq (%rax,%rbx,8), %rdi
movq (%r12,%rbx,8), %rsi
callq 0x29379
incq %rbx
testl %eax, %eax
jne 0x283a2
jmp 0x2836b
movq 0x50(%r15), %rax
movq 0x10(%rsp), %rcx
orq %rax, %rcx
je 0x283f1
movl (%rax), %eax
movq 0x10(%rsp), %rcx
cmpl (%rcx), %eax
jne 0x2836b
movq 0x36e80(%rip), %rax     # 0x5f278
movq (%rax,%r14,8), %r14
movq 0x8(%rsp), %r15
jmp 0x2841d
movq 0x8(%r15), %rbx
movq (%rbx), %rdi
callq 0x1bc4f
movq (%rbx), %rdi
movq %rax, %rsi
callq 0x2923b
movq %rax, %r14
incq 0x68(%r14)
movq 0x10(%r15), %rdi
movq %rdi, 0x18(%r14)
movq 0x40(%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 0x1b5e6
addl $-0x13, %eax
cmpl $0x32, %eax
ja 0x2853c
movabsq $0x6000100a00001, %rcx  # imm = 0x6000100A00001
btq %rax, %rcx
jae 0x284a9
movq 0x10(%r15), %rdi
callq 0x1bf90
movq %rax, %rdi
callq 0x1fcd8
movq %rax, %rdi
callq 0x293dc
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 0x2853c
movq 0x10(%r15), %rax
movq 0x50(%rax), %rax
movq (%rax), %rdi
jmp 0x2847b
leaq 0xcb79(%rip), %rdi      # 0x35040
leaq 0xc997(%rip), %rsi      # 0x34e65
leaq 0xcb85(%rip), %rcx      # 0x3505a
movl $0x247, %edx            # imm = 0x247
callq 0x4180
leaq 0x5acb(%rip), %rdi      # 0x2dfb1
leaq 0xc978(%rip), %rsi      # 0x34e65
leaq 0xcb28(%rip), %rcx      # 0x3501c
movl $0x2e4, %edx            # imm = 0x2E4
callq 0x4180
leaq 0xcb09(%rip), %rdi      # 0x3500e
leaq 0xc959(%rip), %rsi      # 0x34e65
leaq 0xcb09(%rip), %rcx      # 0x3501c
movl $0x2a1, %edx            # imm = 0x2A1
callq 0x4180
leaq 0xcb1c(%rip), %rdi      # 0x35040
leaq 0xc93a(%rip), %rsi      # 0x34e65
leaq 0xcaea(%rip), %rcx      # 0x3501c
movl $0x2b5, %edx            # imm = 0x2B5
callq 0x4180
movq 0x8(%r15), %rbx
movq %rbx, %rdi
callq 0x1b5e6
movl %eax, %eax
leaq 0x34e4f(%rip), %rcx     # 0x5d3a0
movq (%rcx,%rax,8), %r14
movq 0x8(%r15), %rdi
callq 0x1bf90
movq %rax, %rdi
callq 0x21009
leaq 0xc993(%rip), %rsi      # 0x34f00
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x120fd