// File: exrd[P]neteruhsp/neteruhsp/neteruhsp.cc
// Function: neteruhsp::variable_set(neteruhsp::variable_t*, neteruhsp::value_t const&, int)
void variable_set( variable_t* var, const value_t& v, int idx )
{
	assert( var != nullptr );
	if ( var->type_ != v.type_ )
	{
		if ( idx > 0 )
		{
			raise_error( "型の異なる変数への代入@@ %s(%d)", var->name_, idx );
		}
		prepare_variable( var, v.type_, 64, 16 );
	}

	bool init_required = false;

	int granule_size = 0;
	if ( v.type_ == VALUE_STRING )
	{
		granule_size = static_cast<int>( strlen( v.svalue_ ) ) + 1;
	}
	if ( var->granule_size_ < granule_size )
	{
		init_required = true;
	}

	int len = var->length_;
	if ( idx < 0 )
	{
		raise_error( "負の添え字は無効です@@ %s(%d)", var->name_, idx );
	}
	if ( len <= idx )
	{
		raise_error( "存在しない添え字への代入@@ %s(%d)", var->name_, idx );
	}

	if ( init_required )
	{
		prepare_variable( var, v.type_, granule_size, len );
	}

	assert( var->type_ == v.type_ );
	auto data_ptr = variable_data_ptr( *var, idx );
	switch( var->type_ )
	{
		case VALUE_INT:
		{
			reinterpret_cast<int*>( data_ptr )[0] = v.ivalue_;
			break;
		}
		case VALUE_DOUBLE:
		{
			reinterpret_cast<double*>( data_ptr )[0] = v.dvalue_;
			break;
		}
		case VALUE_STRING:
		{
			strcpy( reinterpret_cast<char*>( data_ptr ), v.svalue_ );
			break;
		}
		default:
			assert( false );
			break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x953c
jmp 0x955b
leaq 0xb197(%rip), %rdi      # 0x146da
leaq 0x9507(%rip), %rsi      # 0x12a51
movl $0xf52, %edx            # imm = 0xF52
leaq 0xb193(%rip), %rcx      # 0x146e9
callq 0x30f0
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
je 0x95a1
cmpl $0x0, -0x14(%rbp)
jle 0x9588
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movl -0x14(%rbp), %edx
leaq 0xb1a9(%rip), %rdi      # 0x1472a
movb $0x0, %al
callq 0x4cd0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
movl $0x40, %edx
movl $0x10, %ecx
callq 0x9220
movb $0x0, -0x15(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x95c8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x30b0
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
cmpl -0x1c(%rbp), %eax
jge 0x95d8
movb $0x1, -0x15(%rbp)
movq -0x8(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x9600
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movl -0x14(%rbp), %edx
leaq 0xb15c(%rip), %rdi      # 0x14755
movb $0x0, %al
callq 0x4cd0
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jg 0x9620
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movl -0x14(%rbp), %edx
leaq 0xb164(%rip), %rdi      # 0x1477d
movb $0x0, %al
callq 0x4cd0
testb $0x1, -0x15(%rbp)
je 0x963b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
callq 0x9220
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x964c
jmp 0x966b
leaq 0xb158(%rip), %rdi      # 0x147ab
leaq 0x93f7(%rip), %rsi      # 0x12a51
movl $0xf77, %edx            # imm = 0xF77
leaq 0xb083(%rip), %rcx      # 0x146e9
callq 0x30f0
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x9700
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
subl $0x1, %eax
je 0x96a0
jmp 0x968c
movl -0x2c(%rbp), %eax
subl $0x2, %eax
je 0x96af
jmp 0x9696
movl -0x2c(%rbp), %eax
subl $0x3, %eax
je 0x96c2
jmp 0x96d5
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
jmp 0x96f4
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x96f4
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x3140
jmp 0x96f4
leaq 0xa293(%rip), %rdi      # 0x1396f
leaq 0x936e(%rip), %rsi      # 0x12a51
movl $0xf8b, %edx            # imm = 0xF8B
leaq 0xaffa(%rip), %rcx      # 0x146e9
callq 0x30f0
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)