// File: /workspace/github_c_makefile_star2_O2/oridb[P]mc/parse/infer.c
// Function: tymatchrank
int
tymatchrank(Type *pat, Type *to)
{
	int match, q;
	size_t i;
	Ucon *puc, *tuc;

	pat = tysearch(pat);
	to = tysearch(to);
	if (pat->type == Typaram) {
		if (!pat->trneed)
			return 0;
		for (i = 0; bsiter(pat->trneed, &i); i++)
			if (!tryconstrain(to, traittab[i], 0))
				return -1;
		return 0;
	} else if (pat->type == Tyvar) {
		return 1;
	} else if (pat->type != to->type) {
		return -1;
	}

	match = 0;
	switch (pat->type) {
	case Tystruct:
		if (pat->nmemb != to->nmemb)
			return -1;
		for (i = 0; i < pat->nmemb; i++) {
			if (!streq(declname(pat->sdecls[i]),declname( to->sdecls[i])))
				return -1;
			q = tymatchrank(decltype(pat->sdecls[i]), decltype(to->sdecls[i]));
			if (q < 0)
				return -1;
			match += q;
		}
		break;
	case Tyunion:
		if (pat->nmemb != to->nmemb)
			return -1;
		for (i = 0; i < pat->nmemb; i++) {
			if (!nameeq(pat->udecls[i], to->udecls[i]))
				return -1;
			puc = pat->udecls[i];
			tuc = to->udecls[i];
			if (puc->etype && tuc->etype) {
				q = tymatchrank(puc->etype, tuc->etype);
				if (q < 0)
					return -1;
				match += q;
			} else if (puc->etype != tuc->etype) {
				return -1;
			}
		}
		break;
	case Tyname:
		if (!nameeq(pat->name, to->name))
			return -1;
		if (pat->narg != to->narg)
			return -1;
		for (i = 0; i < pat->narg; i++) {
			q = tymatchrank(pat->arg[i], to->arg[i]);
			if (q < 0)
				return -1;
			match += q;
		}
		break;
	case Tyarray:
		/* unsized arrays are ok */
		if (pat->asize && to->asize) {
			if (!!litvaleq(pat->asize->expr.args[0], to->asize->expr.args[0]))
				return -1;
		} else if (pat->asize != to->asize) {
			return -1;
		}
		else return tymatchrank(pat->sub[0], to->sub[0]);
		break;
	default:
		if (pat->nsub != to->nsub)
			break;
		if (pat->type == to->type)
			match = 1;
		for (i = 0; i < pat->nsub; i++) {
			q = tymatchrank(pat->sub[i], to->sub[i]);
			if (q < 0)
				return -1;
			match += q;
		}
		break;
	}
	return match;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
callq 0x1589c
movq %rax, %r14
movq %rbx, %rdi
callq 0x1589c
movq %rax, %r15
movl (%r14), %eax
cmpl $0x19, %eax
je 0x1594d
pushq $-0x1
popq %rbx
cmpl $0x1a, %eax
jne 0x15955
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x15b61
xorl %eax, %eax
movq %rsp, %r12
leaq 0x498ed(%rip), %r13     # 0x5f1f8
movq %rax, (%rsp)
movq %r12, %rsi
callq 0x29e1a
testl %eax, %eax
je 0x15b61
movq (%r13), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x15b9e
testl %eax, %eax
je 0x15b63
movq (%rsp), %rax
incq %rax
movq 0x80(%r14), %rdi
jmp 0x1590b
pushq $0x1
popq %rbx
jmp 0x15b63
cmpl (%r15), %eax
jne 0x15b63
addl $-0x15, %eax
cmpl $0x7, %eax
ja 0x15b28
leaq 0x1bfff(%rip), %rcx     # 0x31970
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x88(%r14), %rax
movq 0x88(%r15), %rcx
testq %rax, %rax
je 0x15b78
testq %rcx, %rcx
je 0x15b63
movq 0x50(%rax), %rax
movq (%rax), %rdi
movq 0x50(%rcx), %rax
movq (%rax), %rsi
callq 0x1bfef
xorl %ebx, %ebx
negl %eax
sbbl %ebx, %ebx
jmp 0x15b63
movq 0x78(%r14), %rax
cmpq 0x78(%r15), %rax
jne 0x15b63
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq %rax, %r12
jae 0x15b74
movq 0x88(%r14), %rax
movq (%rax,%r12,8), %rdi
movq 0x88(%r15), %rax
movq (%rax,%r12,8), %rsi
callq 0x1c100
testl %eax, %eax
je 0x15b63
movq 0x88(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x88(%r15), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x28(%rax), %rdi
movq 0x28(%rcx), %rsi
testq %rdi, %rdi
je 0x15a34
testq %rsi, %rsi
je 0x15b63
callq 0x158be
testl %eax, %eax
js 0x15b63
addl %eax, %ebp
jmp 0x15a3d
cmpq %rsi, %rdi
jne 0x15b63
incq %r12
movq 0x78(%r14), %rax
jmp 0x159cb
movq 0x78(%r14), %rax
cmpq 0x78(%r15), %rax
jne 0x15b63
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpq %rax, %r13
jae 0x15b74
movq 0x88(%r14), %rax
movq (%rax,%r13,8), %rdi
callq 0x1befe
movq %rax, %r12
movq 0x88(%r15), %rax
movq (%rax,%r13,8), %rdi
callq 0x1befe
movq %r12, %rdi
movq %rax, %rsi
callq 0x2a626
testl %eax, %eax
je 0x15b63
movq 0x88(%r14), %rax
movq (%rax,%r13,8), %rdi
callq 0x1bf2d
movq %rax, %r12
movq 0x88(%r15), %rax
movq (%rax,%r13,8), %rdi
callq 0x1bf2d
movq %r12, %rdi
movq %rax, %rsi
callq 0x158be
testl %eax, %eax
js 0x15b63
addl %eax, %ebp
incq %r13
movq 0x78(%r14), %rax
jmp 0x15a59
movq 0x88(%r14), %rdi
movq 0x88(%r15), %rsi
callq 0x1c100
testl %eax, %eax
je 0x15b63
movq 0x48(%r14), %rax
cmpq 0x48(%r15), %rax
jne 0x15b63
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq %rax, %r12
jae 0x15b74
movq 0x40(%r14), %rax
movq (%rax,%r12,8), %rdi
movq 0x40(%r15), %rax
movq (%rax,%r12,8), %rsi
callq 0x158be
testl %eax, %eax
js 0x15b63
addl %eax, %ebp
incq %r12
movq 0x48(%r14), %rax
jmp 0x15aff
movq 0x70(%r14), %rax
cmpq 0x70(%r15), %rax
jne 0x15b61
pushq $0x1
popq %rbp
xorl %r12d, %r12d
cmpq %rax, %r12
jae 0x15b74
movq 0x68(%r14), %rax
movq (%rax,%r12,8), %rdi
movq 0x68(%r15), %rax
movq (%rax,%r12,8), %rsi
callq 0x158be
testl %eax, %eax
js 0x15b63
addl %eax, %ebp
incq %r12
movq 0x70(%r14), %rax
jmp 0x15b38
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %ebx
jmp 0x15b63
cmpq %rcx, %rax
jne 0x15b63
movq 0x68(%r14), %rax
movq (%rax), %rdi
movq 0x68(%r15), %rax
movq (%rax), %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x158be