// File: /workspace/github_c_makefile_star2_O1/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
xorl %eax, %eax
movq %rax, 0x3711d(%rip)     # 0x62270
movq %rax, 0x3711e(%rip)     # 0x62278
movq 0x20(%rdi), %rbx
movq %rdi, 0x20(%rsp)
movq 0x28(%rdi), %r13
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
testq %rbx, %rbx
je 0x2b1ac
movq %rdx, %r14
movq %rsi, %r15
xorl %r12d, %r12d
leaq 0x10(%rsp), %rbp
movq (%r13,%r12,8), %rax
movq 0x18(%rax), %rdx
movq (%r14,%r12,8), %rcx
movl %r12d, %edi
movq %r15, %rsi
leaq 0x18(%rsp), %r8
movq %rbp, %r9
callq 0x2b5c7
incq %r12
cmpq %r12, %rbx
jne 0x2b184
movq %r13, 0x8(%rsp)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x2b32b
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r13, %rbp
movq 0x18(%rsp), %rax
movq (%rax,%r12,8), %r13
testq %rbp, %rbp
je 0x2b25d
movslq (%r13), %rax
cmpl %eax, (%rbp)
jne 0x2b25d
movq 0x28(%rbp), %rdx
movq 0x28(%r13), %rcx
cmpq %rcx, %rdx
jne 0x2b585
cmpq $0x0, 0x28(%r13)
je 0x2b25d
xorl %ebx, %ebx
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 0x2b555
movq 0x8(%rcx), %r14
movq 0x8(%rax), %rdi
callq 0x1e5d4
movq %rax, %r15
movq %r14, %rdi
callq 0x1e5d4
movq %r15, %rdi
movq %rax, %rsi
callq 0x244b4
testl %eax, %eax
je 0x2b555
incq %rbx
cmpq 0x28(%r13), %rbx
jb 0x2b1fd
cmpl $0x0, 0x38(%r13)
je 0x2b27d
movq 0x20(%r13), %rax
movq 0x30(%r13), %rcx
movq %rax, 0x58(%rcx)
movq 0x28(%r13), %rax
movq %rax, 0x60(%rcx)
jmp 0x2b31a
movslq (%r13), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq 0x20(%rax), %rax
cmpq $0x0, 0x20(%rax)
je 0x2b31a
movq 0x20(%r13), %rcx
movq 0x28(%r13), %rdx
xorl %esi, %esi
testq %rdx, %rdx
je 0x2b311
movq 0x28(%rax), %rdi
movq (%rdi,%rsi,8), %rdi
cmpl $0xb, 0x8(%rdi)
jne 0x2b311
xorl %edi, %edi
movq (%rcx,%rdi,8), %r8
cmpl $0x1d, 0x18(%r8)
jne 0x2b517
movq 0x50(%r8), %r8
movq (%r8), %r9
cmpl $0x33, 0x18(%r9)
jne 0x2b4f8
cmpl $0x0, 0x30(%r9)
jne 0x2b4d9
movq 0x28(%rax), %r10
movq (%r10,%rsi,8), %r10
movq 0x18(%r10), %r11
cmpq 0x38(%r9), %r11
jne 0x2b309
cmpq $0x0, 0x30(%r10)
jne 0x2b536
movq 0x8(%r8), %r8
movq %r8, 0x30(%r10)
incq %rdi
cmpq %rdi, %rdx
jne 0x2b2b8
incq %rsi
cmpq 0x20(%rax), %rsi
jb 0x2b2a3
incq %r12
movq 0x10(%rsp), %rsi
cmpq %rsi, %r12
jb 0x2b1c5
movq 0x18(%rsp), %rdi
callq 0x2b624
movq %rax, %r14
movq 0x10(%rsp), %rax
testq %rax, %rax
movq 0x20(%rsp), %rbp
je 0x2b369
movq 0x18(%rsp), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movq 0x30(%rsi), %rdi
cmpq $0x0, 0x68(%rdi)
je 0x2b570
incq %rdx
cmpq %rdx, %rax
jne 0x2b34e
leaq 0x35e70(%rip), %rax     # 0x611e0
cmpb $0x0, 0x4d(%rax)
jne 0x2b38b
leaq 0xdaa1(%rip), %rdi      # 0x38e1e
callq 0x4030
testq %rax, %rax
je 0x2b422
leaq 0xda8c(%rip), %rdi      # 0x38e1e
callq 0x4030
movq %rax, %rdi
callq 0x4340
movq %rax, %r15
movq %rax, %rdi
movl $0x40, %esi
callq 0x4130
testq %rax, %rax
je 0x2b40e
leaq 0xda65(%rip), %r12      # 0x38e20
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 0x1d56c
movq %rax, %rdi
movq %r13, %rsi
callq 0x4230
testl %eax, %eax
jne 0x2b422
incq %r15
movq (%rbp), %rdi
callq 0x1d580
movslq %eax, %rbx
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4260
cmpq %rbx, %rax
jne 0x2b422
movq 0x18bbb(%rip), %rax     # 0x43fd0
movq (%rax), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2afa3
movq %r14, %rdi
callq 0x2bd65
testl %eax, %eax
je 0x2b59c
leaq 0xda16(%rip), %rdi      # 0x38e4f
callq 0x4030
testq %rax, %rax
je 0x2b4c7
leaq 0xda0d(%rip), %rdi      # 0x38e5b
leaq 0xa1ad(%rip), %rsi      # 0x35602
callq 0x42e0
testq %rax, %rax
je 0x2b5a8
movq %rax, %r15
movq (%rbp), %rdi
callq 0x1d56c
movq %rax, %r12
movq (%rbp), %rdi
callq 0x1d580
movl %eax, %ebx
movq %r14, %rdi
callq 0x2bdbb
movq %rax, %r13
movq 0x36de1(%rip), %rbp     # 0x62270
movq %r14, %rdi
callq 0x2be32
movq %rax, (%rsp)
leaq 0xda08(%rip), %rsi      # 0x38eaa
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 0x2be9b
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdb14(%rip), %rdi      # 0x38ff4
leaq 0xd98a(%rip), %rsi      # 0x38e71
leaq 0xdab6(%rip), %rcx      # 0x38fa4
movl $0xa9, %edx
callq 0x4180
leaq 0xdacf(%rip), %rdi      # 0x38fce
leaq 0xd96b(%rip), %rsi      # 0x38e71
leaq 0xda97(%rip), %rcx      # 0x38fa4
movl $0xa8, %edx
callq 0x4180
leaq 0xda6e(%rip), %rdi      # 0x38f8c
leaq 0xd94c(%rip), %rsi      # 0x38e71
leaq 0xda78(%rip), %rcx      # 0x38fa4
movl $0xa7, %edx
callq 0x4180
leaq 0xdadf(%rip), %rdi      # 0x3901c
leaq 0xd92d(%rip), %rsi      # 0x38e71
leaq 0xda59(%rip), %rcx      # 0x38fa4
movl $0xac, %edx
callq 0x4180
movslq (%r13), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xd85a(%rip), %rsi      # 0x38dc3
xorl %eax, %eax
callq 0x13aa3
movslq (%rsi), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xd87b(%rip), %rsi      # 0x38dfe
jmp 0x2b569
movq 0x8(%rsp), %rsi
movq (%rsi,%rax,8), %rdi
leaq 0xd7e5(%rip), %rsi      # 0x38d7a
xorl %eax, %eax
callq 0x13aa3
leaq 0xd87f(%rip), %rsi      # 0x38e22
movq %rbp, %rdi
jmp 0x2b569
leaq 0xd8b6(%rip), %rdi      # 0x38e65
leaq 0xd8bb(%rip), %rsi      # 0x38e71
leaq 0xd8bc(%rip), %rcx      # 0x38e79
movl $0x3ed, %edx            # imm = 0x3ED
callq 0x4180