// File: /workspace/github_c_makefile_star2_O3/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 %rsi, %r15
xorl %esi, %esi
movq %rsi, 0x3760a(%rip)     # 0x62270
movq %rsi, 0x3760b(%rip)     # 0x62278
movq 0x20(%rdi), %rbx
movq %rdi, 0x10(%rsp)
movq 0x28(%rdi), %rax
movq %rax, 0x8(%rsp)
movq %rsi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
testq %rbx, %rbx
je 0x2ae4b
movq %rdx, %r14
xorl %r12d, %r12d
leaq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r13
movq (%r13,%r12,8), %rax
movq 0x18(%rax), %rdx
movq (%r14,%r12,8), %rcx
movl %r12d, %edi
movq %r15, %rsi
leaq 0x20(%rsp), %r8
movq %rbp, %r9
callq 0x2b0ef
incq %r12
cmpq %r12, %rbx
jne 0x2aca2
cmpq $0x0, 0x18(%rsp)
je 0x2ae49
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r13, %rbp
movq 0x20(%rsp), %rax
movq (%rax,%r12,8), %r13
testq %rbp, %rbp
je 0x2ad73
movslq (%rbp), %rax
cmpl (%r13), %eax
jne 0x2ad73
movq 0x28(%rbp), %rdx
movq 0x28(%r13), %rcx
cmpq %rcx, %rdx
jne 0x2b0ab
testq %rdx, %rdx
je 0x2ad73
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 0x50(%rcx), %rcx
movq (%rax), %rdx
movq (%rcx), %rsi
movq 0x38(%rdx), %rdx
cmpq 0x38(%rsi), %rdx
jne 0x2b07b
movq 0x8(%rcx), %r14
movq 0x8(%rax), %rdi
callq 0x1e384
movq %rax, %r15
movq %r14, %rdi
callq 0x1e384
movq %r15, %rdi
movq %rax, %rsi
callq 0x2403c
testl %eax, %eax
je 0x2b07b
incq %rbx
cmpq 0x28(%r13), %rbx
jb 0x2ad13
cmpl $0x0, 0x38(%r13)
je 0x2ad93
movq 0x20(%r13), %rax
movq 0x30(%r13), %rcx
movq %rax, 0x58(%rcx)
movq 0x28(%r13), %rax
movq %rax, 0x60(%rcx)
jmp 0x2ae36
movslq (%r13), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq 0x20(%rax), %rax
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x2ae36
movq 0x20(%r13), %rcx
movq 0x28(%r13), %rdx
xorl %esi, %esi
testq %rdx, %rdx
je 0x2ae2e
movq 0x28(%rax), %r8
movq (%r8,%rsi,8), %r8
cmpl $0xb, 0x8(%r8)
jne 0x2ae2e
xorl %edi, %edi
movq (%rcx,%rdi,8), %r8
cmpl $0x1d, 0x18(%r8)
jne 0x2b03d
movq 0x50(%r8), %r8
movq (%r8), %r9
cmpl $0x33, 0x18(%r9)
jne 0x2b01e
cmpl $0x0, 0x30(%r9)
jne 0x2afff
movq 0x28(%rax), %r10
movq (%r10,%rsi,8), %r10
movq 0x18(%r10), %r11
cmpq 0x38(%r9), %r11
jne 0x2ae22
cmpq $0x0, 0x30(%r10)
jne 0x2b05c
movq 0x8(%r8), %r8
movq %r8, 0x30(%r10)
incq %rdi
cmpq %rdi, %rdx
jne 0x2add1
movq 0x20(%rax), %rdi
incq %rsi
cmpq %rdi, %rsi
jb 0x2adbb
incq %r12
movq 0x18(%rsp), %rsi
cmpq %rsi, %r12
jb 0x2acdc
jmp 0x2ae4b
xorl %esi, %esi
movq 0x20(%rsp), %rdi
callq 0x2b173
movq %rax, %r14
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x2ae84
movq 0x20(%rsp), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movq 0x30(%rsi), %rdi
cmpq $0x0, 0x68(%rdi)
je 0x2b096
incq %rdx
cmpq %rdx, %rax
jne 0x2ae69
leaq 0x36355(%rip), %rax     # 0x611e0
cmpb $0x0, 0x4d(%rax)
jne 0x2aea6
leaq 0xdf56(%rip), %rdi      # 0x38dee
callq 0x4030
testq %rax, %rax
je 0x2af45
leaq 0xdf41(%rip), %rdi      # 0x38dee
callq 0x4030
movq %rax, %rdi
callq 0x4340
movq %rax, %r15
movq %rax, %rdi
movl $0x40, %esi
callq 0x4130
testq %rax, %rax
je 0x2af31
leaq 0xdf1a(%rip), %r12      # 0x38df0
movq %r15, %rdi
movq %r12, %rsi
callq 0x42f0
movq %rax, %r13
xorl %edi, %edi
movq %r12, %rsi
callq 0x42f0
movq %rax, %r15
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x1d31c
movq %rax, %rdi
movq %r13, %rsi
callq 0x4230
testl %eax, %eax
jne 0x2af45
incq %r15
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x1d330
movslq %eax, %rbx
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4260
cmpq %rbx, %rax
jne 0x2af45
movq 0x19098(%rip), %rax     # 0x43fd0
movq (%rax), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2aab3
movq %r14, %rdi
callq 0x2b859
testl %eax, %eax
je 0x2b0c2
leaq 0xdec3(%rip), %rdi      # 0x38e1f
callq 0x4030
testq %rax, %rax
je 0x2afed
leaq 0xdeba(%rip), %rdi      # 0x38e2b
leaq 0xa69a(%rip), %rsi      # 0x35612
callq 0x42e0
testq %rax, %rax
je 0x2b0d0
movq %rax, %r15
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
callq 0x1d31c
movq %rax, %r12
movq (%rbx), %rdi
callq 0x1d330
movl %eax, %ebx
movq %r14, %rdi
callq 0x2b8af
movq %rax, %r13
movq 0x372bb(%rip), %rbp     # 0x62270
movq %r14, %rdi
callq 0x2b926
movq %rax, (%rsp)
leaq 0xdeb2(%rip), %rsi      # 0x38e7a
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 0x2b98f
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xdf97(%rip), %rdi      # 0x38f9d
leaq 0xde34(%rip), %rsi      # 0x38e41
leaq 0xdf39(%rip), %rcx      # 0x38f4d
movl $0xa9, %edx
callq 0x4180
leaq 0xdf52(%rip), %rdi      # 0x38f77
leaq 0xde15(%rip), %rsi      # 0x38e41
leaq 0xdf1a(%rip), %rcx      # 0x38f4d
movl $0xa8, %edx
callq 0x4180
leaq 0xdef1(%rip), %rdi      # 0x38f35
leaq 0xddf6(%rip), %rsi      # 0x38e41
leaq 0xdefb(%rip), %rcx      # 0x38f4d
movl $0xa7, %edx
callq 0x4180
leaq 0xdf62(%rip), %rdi      # 0x38fc5
leaq 0xddd7(%rip), %rsi      # 0x38e41
leaq 0xdedc(%rip), %rcx      # 0x38f4d
movl $0xac, %edx
callq 0x4180
movslq (%r13), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xdd04(%rip), %rsi      # 0x38d93
xorl %eax, %eax
callq 0x138e7
movslq (%rsi), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
leaq 0xdd25(%rip), %rsi      # 0x38dce
jmp 0x2b08f
movq 0x8(%rsp), %rsi
movq (%rsi,%rax,8), %rdi
leaq 0xdc8f(%rip), %rsi      # 0x38d4a
xorl %eax, %eax
callq 0x138e7
leaq 0xdd29(%rip), %rsi      # 0x38df2
movq 0x10(%rsp), %rdi
jmp 0x2b08f
leaq 0xdd5e(%rip), %rdi      # 0x38e35
leaq 0xdd63(%rip), %rsi      # 0x38e41
leaq 0xdd64(%rip), %rcx      # 0x38e49
movl $0x3ed, %edx            # imm = 0x3ED
callq 0x4180