// File: /workspace/github_c_makefile_star2_O1/oridb[P]mc/6/simp.c
// Function: simpcast
static Node *
simpcast(Simp *s, Node *val, Type *to)
{
	Node *r;
	Type *t;

	r = NULL;
	t = tybase(exprtype(val));
	if (tyeq(tybase(to), tybase(t))) {
		r = rval(s, val, NULL);
		r->expr.type = to;
		return r;
	}
	/* do the type conversion */
	switch (tybase(to)->type) {
	case Tybool:
	case Tyint8: case Tyint16: case Tyint32: case Tyint64:
	case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint64:
	case Tyint: case Tyuint: case Tychar: case Tybyte:
	case Typtr:
		switch (t->type) {
			/* ptr -> slice conversion is disallowed */
		case Tyslice:
			/* FIXME: we should only allow casting to pointers. */
			if (tysize(to) != Ptrsz)
				fatal(val, "bad cast from %s to %s", tystr(exprtype(val)), tystr(to));
			val = rval(s, val, NULL);
			r = slicebase(s, val, NULL);
			break;
		case Tyfunc:
			if (to->type != Typtr)
				fatal(val, "bad cast from %s to %s", tystr(exprtype(val)), tystr(to));
			r = getcode(s, val);
			break;
			/* signed conversions */
		case Tyint8: case Tyint16: case Tyint32: case Tyint64:
		case Tyint:
			r = intconvert(s, val, to, 1);
			break;
			/* unsigned conversions */
		case Tybool:
		case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint64:
		case Tyuint: case Tychar: case Tybyte:
		case Typtr:
			r = intconvert(s, val, to, 0);
			break;
		case Tyflt32: case Tyflt64:
			if (tybase(to)->type == Typtr)
				fatal(val, "bad cast from %s to %s",
						tystr(exprtype(val)), tystr(to));
			r = mkexpr(val->loc, Oflt2int, rval(s, val, NULL), NULL);
			r->expr.type = to;
			break;
		default:
			fatal(val, "bad cast from %s to %s",
					tystr(exprtype(val)), tystr(to));
		}
		break;
	case Tyflt32: case Tyflt64:
		switch (t->type) {
		case Tyint8: case Tyint16: case Tyint32: case Tyint64:
		case Tyuint8: case Tyuint16: case Tyuint32: case Tyuint64:
		case Tyint: case Tyuint: case Tychar: case Tybyte:
			r = mkexpr(val->loc, Oint2flt, rval(s, val, NULL), NULL);
			r->expr.type = to;
			break;
		case Tyflt32: case Tyflt64:
			r = mkexpr(val->loc, Oflt2flt, rval(s, val, NULL), NULL);
			r->expr.type = to;
			break;
		default:
			fatal(val, "bad cast from %s to %s",
					tystr(exprtype(val)), tystr(to));
			break;
		}
		break;
		/* no other destination types are handled as things stand */
	default:
		fatal(val, "bad cast from %s to %s",
				tystr(exprtype(val)), tystr(to));
	}
	return r;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, %rdi
callq 0x1e5d4
movq %rax, %rdi
callq 0x22c44
movq %rax, %r12
movq %r14, %rdi
callq 0x22c44
movq %rax, %r13
movq %r12, %rdi
callq 0x22c44
movq %r13, %rdi
movq %rax, %rsi
callq 0x244b4
testl %eax, %eax
je 0x10e92
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xfb13
movq %r14, 0x20(%rax)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %r14, %rdi
callq 0x22c44
movl (%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0xd, %ecx
jae 0x10eed
movl (%r12), %eax
addl $-0x2, %eax
cmpl $0x12, %eax
ja 0x10fcb
leaq 0x213b1(%rip), %rcx     # 0x3226c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
jmp 0x10edf
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0x1, %ecx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x118ae
leal -0xf(%rax), %ecx
cmpl $0x2, %ecx
jae 0x10f53
movl (%r12), %eax
leal -0x3(%rax), %ecx
cmpl $0xc, %ecx
jae 0x10fa9
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xfb13
movq (%rbx), %rdi
movl $0x4d, %esi
jmp 0x10f42
movq %r14, %rdi
callq 0x22c44
cmpl $0x12, (%rax)
je 0x10fcb
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xfb13
movq (%rbx), %rdi
movl $0x4c, %esi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1d832
jmp 0x10e84
cmpl $0x12, %eax
je 0x10ea4
jmp 0x10fcb
movq %r14, %rdi
callq 0x125dd
cmpq $0x8, %rax
jne 0x10fcb
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xfb13
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x115d0
cmpl $0x12, (%r14)
jne 0x10fcb
movq %r15, %rdi
movq %rbx, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xefb4
addl $-0xf, %eax
cmpl $0x2, %eax
jae 0x10fcb
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xfb13
movq (%rbx), %rdi
movl $0x4e, %esi
jmp 0x10f42
movq %rbx, %rdi
callq 0x1e5d4
movq %rax, %rdi
callq 0x23e44
movq %rax, %r15
movq %r14, %rdi
callq 0x23e44
leaq 0x215de(%rip), %rsi     # 0x325cb
movq %rbx, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x13aa3