// File: /workspace/github_c_makefile_star2_O2/oridb[P]mc/mi/match.c
// Function: gendtree
Dtree *
gendtree(Node *m, Node *val, Node **lbl, size_t nlbl)
{
	Dtree *root;
	Node **pat;
	size_t npat;
	size_t i, j;
	Frontier **frontier, *cur, *last;
	size_t nfrontier;
	FILE *csv;
	char *dbgloc, *dbgfn, *dbgln;


	ndtree = 0;
	dtree = NULL;

	pat = m->matchstmt.matches;
	npat = m->matchstmt.nmatches;

	frontier = NULL;
	nfrontier = 0;
	for (i = 0; i < npat; i++) {
		genfrontier(i, val, pat[i]->match.pat, lbl[i], &frontier, &nfrontier);
	}

	/* to determine if two different sets of captures come from a or-pattern, which is NOT allowed. */
	last = NULL;
	for (i = 0; i < nfrontier; i++) {
		cur = frontier[i];
		if (last && last->i == cur->i) {
			if (last->ncap != cur->ncap)
				fatal(pat[cur->i], "number of wildcard variables in the or-patterns are not equal (%d != %d)", last->ncap, cur->ncap);
			for (j = 0; j < cur->ncap; j++) {
				if (!capcompatible(last->cap[j], cur->cap[j]))
					fatal(pat[cur->i], "wildcard variables have different types in the or-patterns");
			}
		}
		/*
		 * If the match arm does not have or-pattern, we can
		 * insert the assignements of the captures at the
		 * beginning of the associated block.  Otherwise,
		 * the captures can bind different locations with
		 * the same identifier in thehe or-patterns, and
		 * thus the assignments must be carried out before
		 * jumping into the block.  For this reason, in the
		 * case of having or-pattern, we store the
		 * information of captures in the dtree MATCH node
		 * and delegate the insertion of the captures
		 * assignments to the ir generation of dtree
		 */
		if (cur->hasorpat) {
			cur->final->cap = cur->cap;
			cur->final->ncap = cur->ncap;
		} else {
			addcapture(pat[cur->i]->match.block, cur->cap, cur->ncap);
		}
		last = cur;
	}
	root = compile(frontier, nfrontier);

	for (i = 0; i < nfrontier; i++)
		if (frontier[i]->final->refcnt == 0)
			fatal(pat[frontier[i]->i], "pattern matched by earlier case");

	if (debugopt['M'] || getenv("M")) {
		dbgloc = strdup(getenv("M"));
		if (strchr(dbgloc, '@')) {
			dbgfn = strtok(dbgloc, "@");
			dbgln = strtok(NULL, "@")+1; /* offset by 1 to skip the charactor 'L' */
			if (!strcmp(fname(m->loc), dbgfn) && lnum(m->loc) == strtol(dbgln, 0, 0))
				dtreedump(stdout, root);
		}
		else
			dtreedump(stdout, root);

	}
	if (!verifymatch(root)) {
		fatal(m, "nonexhaustive pattern set in match statement");
	}
	if (getenv("MATCH_STATS")) {
		csv = fopen("match.csv", "a");
		assert(csv != NULL);
		fprintf(csv, "%s@L%d, %ld, %zd, %zd\n", fname(m->loc), lnum(m->loc), refsum(root), ndtree, dtheight(root));
		fclose(csv);

		/* clear 'emitted' so it can be reused by genmatchcode. */
		clearemit(root);
	}

	return root;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
andq $0x0, 0x377c4(%rip)     # 0x5f270
movq %rsi, %r15
andq $0x0, 0x377c1(%rip)     # 0x5f278
movq 0x20(%rdi), %rbx
movq %rdi, 0x20(%rsp)
movq 0x28(%rdi), %rax
movq %rax, 0x8(%rsp)
leaq 0x18(%rsp), %r12
andq $0x0, (%r12)
leaq 0x10(%rsp), %r13
andq $0x0, (%r13)
xorl %ebp, %ebp
cmpq %rbp, %rbx
je 0x27b0a
movq 0x8(%rsp), %rax
movq (%rax,%rbp,8), %rax
movq 0x18(%rax), %rdx
movq (%r14,%rbp,8), %rcx
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %r8
movq %r13, %r9
callq 0x27eed
incq %rbp
jmp 0x27adf
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
cmpq %rsi, %r12
jae 0x27c60
movq (%rdi,%r12,8), %r13
testq %rbp, %rbp
je 0x27ba9
movslq (%rbp), %rax
cmpl (%r13), %eax
jne 0x27ba9
movq 0x28(%rbp), %rdx
movq 0x28(%r13), %rcx
cmpq %rcx, %rdx
jne 0x27eab
xorl %ebx, %ebx
cmpq 0x28(%r13), %rbx
jae 0x27ba9
movq 0x20(%rbp), %rax
movq (%rax,%rbx,8), %rax
movq 0x20(%r13), %rcx
movq (%rcx,%rbx,8), %rcx
movq 0x50(%rax), %rax
movq (%rax), %rdx
movq 0x50(%rcx), %rcx
movq (%rcx), %rsi
movq 0x38(%rdx), %rdx
cmpq 0x38(%rsi), %rdx
jne 0x27e90
movq 0x8(%rcx), %r14
movq 0x8(%rax), %rdi
callq 0x1bf90
movq %rax, %r15
movq %r14, %rdi
callq 0x1bf90
movq %r15, %rdi
movq %rax, %rsi
callq 0x215f5
incq %rbx
testl %eax, %eax
jne 0x27b48
jmp 0x27e90
cmpl $0x0, 0x38(%r13)
je 0x27bcf
movq 0x20(%r13), %rax
movq 0x30(%r13), %rcx
movq %rax, 0x58(%rcx)
movq 0x28(%r13), %rax
movq %rax, 0x60(%rcx)
incq %r12
movq %r13, %rbp
jmp 0x27b0f
movslq (%r13), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq 0x20(%rax), %rax
movq 0x20(%r13), %rcx
movq 0x28(%r13), %rdx
xorl %esi, %esi
cmpq 0x20(%rax), %rsi
jae 0x27bc4
movq 0x28(%rax), %rdi
movq (%rdi,%rsi,8), %rdi
cmpl $0xb, 0x8(%rdi)
jne 0x27c5b
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x27c5b
movq (%rcx,%rdi,8), %r8
cmpl $0x1d, 0x18(%r8)
jne 0x27e52
movq 0x50(%r8), %r8
movq (%r8), %r9
cmpl $0x33, 0x18(%r9)
jne 0x27e33
cmpl $0x0, 0x30(%r9)
jne 0x27e14
movq 0x28(%rax), %r10
movq (%r10,%rsi,8), %r10
movq 0x18(%r10), %r11
cmpq 0x38(%r9), %r11
jne 0x27c56
cmpq $0x0, 0x30(%r10)
jne 0x27e71
movq 0x8(%r8), %r8
movq %r8, 0x30(%r10)
incq %rdi
jmp 0x27c00
incq %rsi
jmp 0x27bea
callq 0x27f4a
movq %rax, %r14
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rbp
cmpq %rcx, %rax
je 0x27ca8
movq (%rdx,%rcx,8), %rsi
movq 0x30(%rsi), %rdi
incq %rcx
cmpq $0x0, 0x68(%rdi)
jne 0x27c79
movslq (%rsi), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xd14f(%rip), %rsi      # 0x34df2
jmp 0x27ea4
leaq 0x36531(%rip), %rax     # 0x5e1e0
cmpb $0x0, 0x4d(%rax)
jne 0x27cca
leaq 0xd156(%rip), %rdi      # 0x34e12
callq 0x4030
testq %rax, %rax
je 0x27d5d
leaq 0xd141(%rip), %rdi      # 0x34e12
callq 0x4030
movq %rax, %rdi
callq 0x4340
movq %rax, %r15
pushq $0x40
popq %rsi
movq %rax, %rdi
callq 0x4130
testq %rax, %rax
je 0x27d4b
leaq 0xd11c(%rip), %r12      # 0x34e14
movq %r15, %rdi
movq %r12, %rsi
callq 0x42f0
movq %rax, %r13
xorl %edi, %edi
movq %r12, %rsi
callq 0x42f0
movq %rax, %r15
movq (%rbp), %rdi
callq 0x1b4f4
movq %rax, %rdi
movq %r13, %rsi
callq 0x4230
testl %eax, %eax
jne 0x27d5d
incq %r15
movq (%rbp), %rdi
callq 0x1b508
movslq %eax, %rbx
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4260
cmpq %rbx, %rax
jne 0x27d5d
movq 0x1927e(%rip), %rax     # 0x40fd0
movq (%rax), %rdi
movq %r14, %rsi
callq 0x279d0
movq %r14, %rdi
callq 0x2857d
testl %eax, %eax
je 0x27ec2
leaq 0xd0cf(%rip), %rdi      # 0x34e43
callq 0x4030
testq %rax, %rax
je 0x27e02
leaq 0xd0c6(%rip), %rdi      # 0x34e4f
leaq 0x9852(%rip), %rsi      # 0x315e2
callq 0x42e0
testq %rax, %rax
je 0x27ece
movq %rax, %r15
movq (%rbp), %rdi
callq 0x1b4f4
movq %rax, %r12
movq (%rbp), %rdi
callq 0x1b508
movl %eax, %ebx
movq %r14, %rdi
callq 0x285d4
movq %rax, %r13
movq 0x374a6(%rip), %rbp     # 0x5f270
movq %r14, %rdi
callq 0x28641
movq %rax, (%rsp)
leaq 0xd0c1(%rip), %rsi      # 0x34e9e
movq %r15, %rdi
movq %r12, %rdx
movl %ebx, %ecx
movq %r13, %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0x4240
movq %r15, %rdi
callq 0x4110
movq %r14, %rdi
callq 0x286a4
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd1a6(%rip), %rdi      # 0x34fc1
leaq 0xd043(%rip), %rsi      # 0x34e65
leaq 0xd148(%rip), %rcx      # 0x34f71
movl $0xa9, %edx
callq 0x4180
leaq 0xd161(%rip), %rdi      # 0x34f9b
leaq 0xd024(%rip), %rsi      # 0x34e65
leaq 0xd129(%rip), %rcx      # 0x34f71
movl $0xa8, %edx
callq 0x4180
leaq 0xd100(%rip), %rdi      # 0x34f59
leaq 0xd005(%rip), %rsi      # 0x34e65
leaq 0xd10a(%rip), %rcx      # 0x34f71
movl $0xa7, %edx
callq 0x4180
leaq 0xd171(%rip), %rdi      # 0x34fe9
leaq 0xcfe6(%rip), %rsi      # 0x34e65
leaq 0xd0eb(%rip), %rcx      # 0x34f71
movl $0xac, %edx
callq 0x4180
movslq (%r13), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xcf13(%rip), %rsi      # 0x34db7
xorl %eax, %eax
callq 0x120fd
movq 0x8(%rsp), %rsi
movq (%rsi,%rax,8), %rdi
leaq 0xceb3(%rip), %rsi      # 0x34d6e
xorl %eax, %eax
callq 0x120fd
leaq 0xcf4d(%rip), %rsi      # 0x34e16
movq %rbp, %rdi
jmp 0x27ea4
leaq 0xcf84(%rip), %rdi      # 0x34e59
leaq 0xcf89(%rip), %rsi      # 0x34e65
leaq 0xcf8a(%rip), %rcx      # 0x34e6d
movl $0x3ed, %edx            # imm = 0x3ED
callq 0x4180