// File: /workspace/github_c_makefile_star2_O1/oridb[P]mc/parse/specialize.c
// Function: tyspecialize
Type *
tyspecialize(Type *orig, Tysubst *tsmap, Htab *delayed)
{
	Type *t, *ret, *tmp, *var;
	Traitspec *ts;
	size_t i, narg;
	Type **arg;

	t = tysearch(orig);
	tmp = substget(tsmap, t);
	if (tmp)
		return tysearch(tmp);
	arg = NULL;
	narg = 0;
	switch (t->type) {
	case Typaram:
		ret = mktyvar(t->loc);
		ret->trneed = bsdup(t->trneed);
		substput(tsmap, t, ret);
		for (i = 0; i < t->nspec; i++) {
			ts = zalloc(sizeof(Traitspec));
			ts->trait = t->spec[i]->trait;
			ts->ntrait = t->spec[i]->ntrait;
			ts->param = tyspecialize(t->spec[i]->param, tsmap, delayed);
			if (t->spec[i]->aux)
				ts->aux = tyspecialize(t->spec[i]->aux, tsmap, delayed);
			lappend(&ret->spec, &ret->nspec, ts);
		}
		break;
	case Tygeneric:
		var = mktyvar(t->loc);
		substput(tsmap, t, var);
		if (orig->type == Tyunres) {
			substpush(tsmap);
			for (i = 0; i < orig->narg; i++)
				substput(tsmap, t->gparam[i], orig->arg[i]);
		}
		for (i = 0; i < t->ngparam; i++)
			lappend(&arg, &narg, tyspecialize(t->gparam[i], tsmap, delayed));
		ret = mktyname(t->loc, t->name, tyspecialize(t->sub[0], tsmap, delayed));
		if (orig->type == Tyunres)
			substpop(tsmap);
		ret->arg = arg;
		ret->narg = narg;
		tytab[var->tid] = ret;
		break;
	case Tyname:
		if (!hasparams(t))
			return t;
		var = mktyvar(t->loc);
		substput(tsmap, t, var);
		for (i = 0; i < t->narg; i++)
			lappend(&arg, &narg, tyspecialize(t->arg[i], tsmap, delayed));
		ret = mktyname(t->loc, t->name, tyspecialize(t->sub[0], tsmap, delayed));
		ret->arg = arg;
		ret->narg = narg;
		tytab[var->tid] = ret;
		break;
	case Tystruct:
		ret = tydup(t);
		substput(tsmap, t, ret);
		pushstab(NULL);
		for (i = 0; i < t->nmemb; i++)
			ret->sdecls[i] = specializenode(t->sdecls[i], tsmap);
		popstab();
		break;
	case Tyunion:
		ret = tydup(t);
		substput(tsmap, t, ret);
		for (i = 0; i < t->nmemb; i++) {
			tmp = NULL;
			if (ret->udecls[i]->etype)
				tmp = tyspecialize(t->udecls[i]->etype, tsmap, delayed);
			ret->udecls[i] = mkucon(t->loc, t->udecls[i]->name, ret, tmp);
			ret->udecls[i]->utype = ret;
			ret->udecls[i]->id = i;
			ret->udecls[i]->synth = 1;
		}
		break;
	case Tyvar:
		ret = t;
		if (delayed && hthas(delayed, t)) {
			tmp = htget(delayed, t);
			htput(delayed, ret, tyspecialize(tmp, tsmap, delayed));
		}
		break;
	default:
		if (t->nsub > 0) {
			ret = tydup(t);
			substput(tsmap, t, ret);
			for (i = 0; i < t->nsub; i++)
				ret->sub[i] = tyspecialize(t->sub[i], tsmap, delayed);
		} else {
			ret = t;
		}
		break;
	}
	if (t->seqaux)
		ret->seqaux = tyspecialize(t->seqaux, tsmap, delayed);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r13
callq 0x1727c
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x1e949
testq %rax, %rax
je 0x1ea53
movq %rax, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1727c
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl (%r15), %eax
addl $-0x17, %eax
cmpl $0x6, %eax
ja 0x1ec77
leaq 0x186b7(%rip), %rcx     # 0x3712c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %rbp, (%rsp)
jmpq *%rax
movq %r15, %rdi
callq 0x22dee
movq %rax, %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2dbc2
xorl %edi, %edi
callq 0x20070
cmpq $0x0, 0x78(%r15)
je 0x1eadc
xorl %ebx, %ebx
movq 0x88(%r15), %rax
movq (%rax,%rbx,8), %rdi
movq %r14, %rsi
callq 0x1ef85
movq 0x88(%r12), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq 0x78(%r15), %rbx
jb 0x1eab4
callq 0x200b6
jmp 0x1ef55
movq %r15, %r12
testq %rbp, %rbp
je 0x1ef55
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2decd
movq %r15, %r12
testl %eax, %eax
je 0x1ef55
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2dd9a
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1ea0e
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2dbc2
movq %r15, %r12
jmp 0x1ef55
movq 0x8(%r15), %rdi
callq 0x22fd7
movq %rax, %rdx
leaq 0x8(%r14), %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movq %r15, %rsi
movq %rdx, 0x20(%rsp)
callq 0x2dbc2
cmpl $0x1b, (%r13)
jne 0x1ebbb
leaq 0x5320(%rip), %rdi      # 0x23e8e
leaq 0x593f(%rip), %rsi      # 0x244b4
callq 0x2db1c
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2d3ff
cmpq $0x0, 0x48(%r13)
je 0x1ebbb
xorl %ebx, %ebx
movq 0x30(%r15), %rax
movq (%rax,%rbx,8), %rsi
movq 0x40(%r13), %rax
movq (%rax,%rbx,8), %rdx
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x8(%rax,%rcx,8), %rdi
callq 0x2dbc2
incq %rbx
cmpq 0x48(%r13), %rbx
jb 0x1eb91
movq %r12, 0x18(%rsp)
cmpq $0x0, 0x38(%r15)
movq (%rsp), %rbx
je 0x1ebfe
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq 0x30(%r15), %rax
movq (%rax,%rbp,8), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1ea0e
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2d3ff
incq %rbp
cmpq 0x38(%r15), %rbp
jb 0x1ebd2
movq 0x68(%r15), %rax
movq 0x88(%r15), %r12
movq (%rax), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1ea0e
movq 0x8(%r15), %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x230fd
movq %rax, %r12
cmpl $0x1b, (%r13)
movq %rbx, %rbp
jne 0x1ec48
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x2d436
movq %rax, %rdi
callq 0x2db8b
movq 0x8(%rsp), %rax
movq %rax, 0x40(%r12)
movq 0x10(%rsp), %rax
movq %rax, 0x48(%r12)
leaq 0x4356d(%rip), %rax     # 0x621d0
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movl 0x4(%rcx), %ecx
movq %r12, (%rax,%rcx,8)
jmp 0x1ef55
cmpq $0x0, 0x70(%r15)
movq %r15, %r12
je 0x1ef55
movq %r15, %rdi
callq 0x22dee
movq %rax, %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2dbc2
cmpq $0x0, 0x70(%r15)
je 0x1ef55
xorl %ebx, %ebx
movq 0x68(%r15), %rax
movq (%rax,%rbx,8), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1ea0e
movq 0x68(%r12), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
cmpq 0x70(%r15), %rbx
jb 0x1ecb4
jmp 0x1ef55
movq %r15, %rdi
callq 0x22dee
movq %rax, %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2dbc2
cmpq $0x0, 0x78(%r15)
je 0x1ef55
xorl %ebx, %ebx
movq 0x88(%r12), %rax
movq (%rax,%rbx,8), %rax
cmpq $0x0, 0x28(%rax)
je 0x1ed3f
movq 0x88(%r15), %rax
movq (%rax,%rbx,8), %rax
movq 0x28(%rax), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1ea0e
movq %rax, %rcx
jmp 0x1ed41
xorl %ecx, %ecx
movq 0x8(%r15), %rdi
movq 0x88(%r15), %rax
movq (%rax,%rbx,8), %rax
movq 0x18(%rax), %rsi
movq %r12, %rdx
callq 0x1e3de
movq 0x88(%r12), %rcx
movq %rax, (%rcx,%rbx,8)
movq 0x88(%r12), %rax
movq (%rax,%rbx,8), %rax
movq %r12, 0x20(%rax)
movq 0x88(%r12), %rax
movq (%rax,%rbx,8), %rax
movq %rbx, 0x8(%rax)
movq 0x88(%r12), %rax
movq (%rax,%rbx,8), %rax
movl $0x1, 0x10(%rax)
incq %rbx
cmpq 0x78(%r15), %rbx
jb 0x1ed0d
jmp 0x1ef55
movq 0x8(%r15), %rdi
callq 0x22fd7
movq %rax, %r12
movq 0x80(%r15), %rdi
callq 0x2d680
movq %rax, 0x80(%r12)
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2dbc2
cmpq $0x0, 0x20(%r15)
je 0x1ef55
leaq 0x18(%r12), %rax
movq %rax, 0x20(%rsp)
movq %r12, %rax
addq $0x20, %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movq (%rsp), %rbp
movl $0x20, %edi
callq 0x2d348
movq %rax, %rbx
movq 0x18(%r15), %rax
movq (%rax,%r13,8), %rcx
movq (%rcx), %rcx
movq %rcx, (%rbx)
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rbx)
movq 0x10(%rax), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1ea0e
movq %rax, 0x10(%rbx)
movq 0x18(%r15), %rax
movq (%rax,%r13,8), %rax
movq 0x18(%rax), %rdi
testq %rdi, %rdi
je 0x1ee66
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1ea0e
movq %rax, 0x18(%rbx)
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
callq 0x2d3ff
incq %r13
cmpq 0x20(%r15), %r13
jb 0x1ee0c
movq (%rsp), %rbp
jmp 0x1ef55
movq %r15, %rdi
callq 0x23690
testl %eax, %eax
je 0x1ef80
movq 0x8(%r15), %rdi
callq 0x22fd7
movq %rax, %rdx
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movq %r15, %rsi
movq %rdx, (%rsp)
callq 0x2dbc2
cmpq $0x0, 0x48(%r15)
movq %rbp, %r13
je 0x1eefe
xorl %ebp, %ebp
leaq 0x8(%rsp), %rbx
leaq 0x10(%rsp), %r12
movq 0x40(%r15), %rax
movq (%rax,%rbp,8), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1ea0e
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2d3ff
incq %rbp
cmpq 0x48(%r15), %rbp
jb 0x1eed4
movq 0x68(%r15), %rax
movq 0x88(%r15), %rbx
movq (%rax), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1ea0e
movq 0x8(%r15), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x230fd
movq %rax, %r12
movq 0x8(%rsp), %rax
movq %rax, 0x40(%r12)
movq 0x10(%rsp), %rax
movq %rax, 0x48(%r12)
leaq 0x4328c(%rip), %rax     # 0x621d0
movq (%rax), %rax
movq (%rsp), %rcx
movl 0x4(%rcx), %ecx
movq %r12, (%rax,%rcx,8)
movq %r13, %rbp
movq 0x28(%r15), %rdi
testq %rdi, %rdi
je 0x1ef6e
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1ea0e
movq %rax, 0x28(%r12)
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %r12
jmp 0x1ef6e