// File: /workspace/github_c_makefile_star2_O3/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 0x1e384
movq %rax, %rdi
callq 0x227ac
movq %rax, %r12
movq %r14, %rdi
callq 0x227ac
movq %rax, %r13
movq %r12, %rdi
callq 0x227ac
movq %r13, %rdi
movq %rax, %rsi
callq 0x2403c
testl %eax, %eax
je 0x10cb6
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xf923
movq %r14, 0x20(%rax)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %r14, %rdi
callq 0x227ac
movl (%rax), %eax
leal -0x2(%rax), %ecx
cmpl $0xd, %ecx
jae 0x10d11
movl (%r12), %eax
addl $-0x2, %eax
cmpl $0x12, %eax
ja 0x10def
leaq 0x2159d(%rip), %rcx     # 0x3227c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
jmp 0x10d03
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0x1, %ecx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x116ef
leal -0xf(%rax), %ecx
cmpl $0x2, %ecx
jae 0x10d77
movl (%r12), %eax
leal -0x3(%rax), %ecx
cmpl $0xc, %ecx
jae 0x10dcd
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xf923
movq (%rbx), %rdi
movl $0x4d, %esi
jmp 0x10d66
movq %r14, %rdi
callq 0x227ac
cmpl $0x12, (%rax)
je 0x10def
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xf923
movq (%rbx), %rdi
movl $0x4c, %esi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1d5e2
jmp 0x10ca8
cmpl $0x12, %eax
je 0x10cc8
jmp 0x10def
movq %r14, %rdi
callq 0x12421
cmpq $0x8, %rax
jne 0x10def
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xf923
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x11411
cmpl $0x12, (%r14)
jne 0x10def
movq %r15, %rdi
movq %rbx, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xed94
addl $-0xf, %eax
cmpl $0x2, %eax
jae 0x10def
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xf923
movq (%rbx), %rdi
movl $0x4e, %esi
jmp 0x10d66
movq %rbx, %rdi
callq 0x1e384
movq %rax, %rdi
callq 0x2398e
movq %rax, %r15
movq %r14, %rdi
callq 0x2398e
leaq 0x217ca(%rip), %rsi     # 0x325db
movq %rbx, %rdi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x138e7