// File: /workspace/github_c_makefile_star2_O2/lucvoo[P]sparse-dev/expand.c
// Function: cast_value
void cast_value(struct expression *expr, struct symbol *newtype, struct expression *old)
{
	struct symbol *oldtype = old->ctype;
	int old_size = oldtype->bit_size;
	int new_size = newtype->bit_size;
	long long value, mask, signmask;
	long long oldmask, oldsignmask, dropped;

	if (is_float_type(newtype) || is_float_type(oldtype))
		goto Float;

	// For pointers and integers, we can just move the value around
	expr->type = EXPR_VALUE;
	expr->taint = old->taint;
	if (old_size == new_size) {
		expr->value = old->value;
		expr->ctype = newtype;
		return;
	}

	// expand it to the full "long long" value
	value = get_longlong(old);
	expr->ctype = newtype;

Int:
	// _Bool requires a zero test rather than truncation.
	if (is_bool_type(newtype)) {
		expr->value = !!value;
		if (!conservative && value != 0 && value != 1)
			warning(old->pos, "odd constant _Bool cast (%llx becomes 1)", value);
		return;
	}

	// Truncate it to the new size
	signmask = 1ULL << (new_size-1);
	mask = signmask | (signmask-1);
	expr->value = value & mask;

	// Stop here unless checking for truncation
	if (!Wcast_truncate || conservative)
		return;
	
	// Check if we dropped any bits..
	oldsignmask = 1ULL << (old_size-1);
	oldmask = oldsignmask | (oldsignmask-1);
	dropped = oldmask & ~mask;

	// OK if the bits were (and still are) purely sign bits
	if (value & dropped) {
		if (!(value & oldsignmask) || !(value & signmask) || (value & dropped) != dropped)
			warning(old->pos, "cast truncates bits from constant value (%llx becomes %llx)",
				value & oldmask,
				value & mask);
	}
	return;

Float:
	if (!is_float_type(newtype)) {
		value = (long long)old->fvalue;
		expr->type = EXPR_VALUE;
		expr->taint = 0;
		expr->ctype = newtype;
		goto Int;
	}

	if (!is_float_type(oldtype))
		expr->fvalue = (long double)get_longlong(old);
	else
		expr->fvalue = old->fvalue;

	if (newtype->rank <= 0) {
		if (newtype->rank == 0)
			expr->fvalue = (double)expr->fvalue;
		else
			expr->fvalue = (float)expr->fvalue;
	}
	expr->type = EXPR_FVALUE;
	expr->ctype = newtype;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, 0x8(%rsp)
movq %rdx, %rbx
movq 0x10(%rdx), %r13
movl 0x50(%r13), %r12d
movl 0x50(%rsi), %r14d
movq %rsi, %rdi
callq 0xe71b
movl %eax, %ebp
movq %r13, %rdi
callq 0xe71b
testl %ebp, %ebp
je 0xe530
testl %eax, %eax
je 0xe57b
fldt 0x20(%rbx)
jmp 0xe58c
movq %r14, %rcx
testl %eax, %eax
je 0xe5f4
fldt 0x20(%rbx)
fnstcw 0x4(%rsp)
movzwl 0x4(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x6(%rsp)
fldcw 0x6(%rsp)
fistpll 0x18(%rsp)
fldcw 0x4(%rsp)
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %r14
movb $0x1, (%r14)
andl $0x0, 0x28(%r14)
movq %r12, %r13
movq %rcx, %r12
jmp 0xe628
movq %rbx, %rdi
callq 0xe735
movq %rax, 0x30(%rsp)
fildll 0x30(%rsp)
movq 0x8(%rsp), %rcx
fld %st(0)
fstpt 0x20(%rcx)
movl 0x54(%r15), %eax
shll $0x6, %eax
cmpl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
jg 0xe5dc
cmpl $0x20000000, %eax       # imm = 0x20000000
fstl 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
fldl 0x20(%rsp)
fxch %st(1)
fstps 0x14(%rsp)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
flds 0x10(%rsp)
fcmovb %st(1), %st
fstp %st(1)
fstpt 0x20(%rcx)
fldz
fstp %st(0)
movb $0x1c, (%rcx)
movq %r15, 0x10(%rcx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r14
movb $0x1, (%r14)
movl 0x28(%rbx), %eax
movl %eax, 0x28(%r14)
movq %r12, %r13
movq %rcx, %r12
cmpl %r12d, %r13d
jne 0xe61d
movq 0x20(%rbx), %rax
movq %rax, 0x20(%r14)
movq %r15, 0x10(%r14)
jmp 0xe5e5
movq %rbx, %rdi
callq 0xe735
movq %rax, %rdx
movq %r15, 0x10(%r14)
cmpb $0x3, (%r15)
jne 0xe636
movq 0x60(%r15), %r15
cmpq 0x4b9a3(%rip), %r15     # 0x59fe0
je 0xe6da
leal -0x1(%r12), %ecx
pushq $0x1
popq %rsi
shlq %cl, %rsi
negb %r12b
pushq $-0x1
popq %rdi
movq %rdi, %r8
movl %r12d, %ecx
shrq %cl, %r8
movq %rdx, %rax
andq %r8, %rax
movq %rax, 0x20(%r14)
leaq 0x4bf22(%rip), %rcx     # 0x5a590
cmpl $0x0, (%rcx)
je 0xe5e5
testb $0x1, 0x50e02(%rip)     # 0x5f480
jne 0xe5e5
movl %r13d, %ecx
negb %cl
shrq %cl, %rdi
notq %r8
andq %rdi, %r8
testq %r8, %rdx
je 0xe5e5
decl %r13d
btsq %r13, %rsi
orq %r8, %rsi
movq %rdx, %rcx
notq %rcx
testq %rcx, %rsi
je 0xe5e5
andq %rdi, %rdx
movq 0x8(%rbx), %rdi
leaq 0x2aafb(%rip), %rsi     # 0x391bd
movq %rax, %rcx
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1108a
xorl %eax, %eax
testq %rdx, %rdx
setne %al
movq %rax, 0x20(%r14)
cmpq $0x2, %rdx
setb %al
orb 0x50d8d(%rip), %al      # 0x5f480
testb $0x1, %al
jne 0xe5e5
movq 0x8(%rbx), %rdi
leaq 0x2aa8e(%rip), %rsi     # 0x39194
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1108a