// File: /workspace/github_c_makefile_star2_O2/DeforaOS[P]libSystem/src/variable.c
// Function: variable_new_deserialize_type
Variable * variable_new_deserialize_type(VariableType type, size_t * size,
		char const * data)
{
	Variable * v;
	size_t s;
	uint32_t u32;
	uint64_t u64;
	float f;
	double d;
	Buffer * b;
	int res;
	unsigned char const * udata = (unsigned char const *)data;

#ifdef DEBUG
	fprintf(stderr, "DEBUG: %s(%u, %lu, %p)\n", __func__, type, *size,
			(void *)data);
#endif
	/* estimate the size required */
	s = (type < sizeof(_variable_sizes) / sizeof(*_variable_sizes))
		? _variable_sizes[type] : 0;
	switch(type)
	{
		case VT_NULL:
		case VT_BOOL:
		case VT_INT8:
		case VT_UINT8:
		case VT_INT16:
		case VT_UINT16:
		case VT_INT32:
		case VT_UINT32:
		case VT_INT64:
		case VT_UINT64:
		case VT_POINTER:
			break;
		case VT_BUFFER:
			if(*size < s)
				break;
			memcpy(&u32, data, s);
			u32 = _bswap32(u32);
			s += u32;
			break;
		case VT_FLOAT:
		case VT_DOUBLE:
		case VT_STRING:
			for(s = 1; s <= *size && data[s - 1] != '\0'; s++);
			if(s < *size)
				break;
			/* fallthrough */
		default:
			error_set_code(1, "Unable to deserialize type %u",
					type);
			return NULL;
	}
	if(*size < s)
	{
		*size = s;
		error_set_code(1, "More data needed to deserialize type %u",
				type);
		return NULL;
	}
	*size = s;
	/* convert the data if necessary */
	switch(type)
	{
		case VT_NULL:
			v = variable_new(type);
			break;
		case VT_BOOL:
			if((data[0] & 0xfe) != 0x0)
			{
				error_set_code(1, "Invalid boolean value");
				return NULL;
			}
			v = variable_new(type, data[0]);
			break;
		case VT_INT8:
		case VT_UINT8:
			v = variable_new(type, data[0]);
			break;
		case VT_INT16:
		case VT_UINT16:
			v = variable_new(type, (uint32_t)udata[0] << 8
					| udata[1]);
			break;
		case VT_INT32:
		case VT_UINT32:
			v = variable_new(type, (uint32_t)udata[0] << 24
					| (uint32_t)udata[1] << 16
					| (uint32_t)udata[2] << 8
					| (uint32_t)udata[3]);
			break;
		case VT_FLOAT:
			res = sscanf(data, "%e", &f);
			if(res != 1)
			{
				error_set_code(1, "Invalid float value");
				return NULL;
			}
			v = variable_new(type, f);
			break;
		case VT_DOUBLE:
			res = sscanf(data, "%le", &d);
			if(res != 1)
			{
				error_set_code(1, "Invalid double value");
				return NULL;
			}
			v = variable_new(type, d);
			break;
		case VT_STRING:
			v = variable_new(type, data);
			break;
		case VT_INT64:
		case VT_UINT64:
			u64 = (uint64_t)udata[0] << 56
				| (uint64_t)udata[1] << 48
				| (uint64_t)udata[2] << 40
				| (uint64_t)udata[3] << 32
				| (uint64_t)udata[4] << 24
				| (uint64_t)udata[5] << 16
				| (uint64_t)udata[6] << 8
				| (uint64_t)udata[7];
			v = variable_new(type, u64);
			break;
		case VT_BUFFER:
			/* XXX avoid copying the buffer */
			if((b = buffer_new(s - sizeof(u32), &data[sizeof(u32)]))
					== NULL)
				return NULL;
			v = variable_new(type, b);
			buffer_delete(b);
			break;
		default:
			error_set_code(1, "Unable to deserialize type %u",
					type);
			return NULL;
	}
	return v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %r12
movl %edi, %ebx
movl %edi, %ebp
cmpl $0x10, %edi
ja 0x902c
leaq 0x24ba(%rip), %rax      # 0xb4e0
movq (%rax,%rbp,8), %r15
jmp 0x902f
xorl %r15d, %r15d
cmpl $0x10, %ebx
ja 0x90ca
movl $0x103ff, %eax          # imm = 0x103FF
btl %ebx, %eax
jae 0x9073
cmpq %r15, (%r12)
movq %r15, (%r12)
jae 0x9055
leaq 0x2561(%rip), %rsi      # 0xb5b4
jmp 0x90d1
cmpl $0xd, %ebx
ja 0x90ca
leaq 0x21f7(%rip), %rax      # 0xb258
movslq (%rax,%rbp,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movsbl (%r14), %esi
jmp 0x9111
movl $0x2c00, %eax           # imm = 0x2C00
btl %ebx, %eax
jae 0x909e
movq (%r12), %rax
pushq $0x1
popq %r15
cmpq %rax, %r15
ja 0x9097
cmpb $0x0, -0x1(%r14,%r15)
je 0x9097
incq %r15
jmp 0x9085
cmpq %rax, %r15
jb 0x9042
jmp 0x90ca
cmpl $0xc, %ebx
jne 0x90ca
cmpq %r15, (%r12)
jb 0x9042
leaq 0x14(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x44b0
movl (%r13), %eax
bswapl %eax
addq %rax, %r15
jmp 0x9042
leaq 0x24c5(%rip), %rsi      # 0xb596
xorl %r14d, %r14d
pushq $0x1
popq %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x4610
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rsi
bswapq %rsi
movl %ebx, %edi
jmp 0x91c4
movzwl (%r14), %eax
rolw $0x8, %ax
movzwl %ax, %esi
jmp 0x9111
movl (%r14), %esi
bswapl %esi
movl %ebx, %edi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x46c0
leaq 0x24da(%rip), %rsi      # 0xb609
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x4550
cmpl $0x1, %eax
jne 0x920c
movsd 0x8(%rsp), %xmm0
pushq $0xb
jmp 0x91fc
addq $-0x4, %r15
addq $0x4, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x4110
testq %rax, %rax
je 0x922e
movq %rax, %rbx
pushq $0xc
popq %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x46c0
movq %rax, %r14
movq %rbx, %rdi
callq 0x4670
jmp 0x90e0
xorl %edi, %edi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x46c0
movsbl (%r14), %esi
testb $-0x2, %sil
je 0x9236
leaq 0x2420(%rip), %rsi      # 0xb5dc
jmp 0x921c
pushq $0xd
popq %rdi
movq %r14, %rsi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x46c0
leaq 0x2412(%rip), %rsi      # 0xb5f2
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x4550
cmpl $0x1, %eax
jne 0x9215
cvtss2sd 0x8(%rsp), %xmm0
pushq $0xa
popq %rdi
movb $0x1, %al
callq 0x46c0
movq %rax, %r14
jmp 0x90e0
leaq 0x23fa(%rip), %rsi      # 0xb60d
jmp 0x921c
leaq 0x23d9(%rip), %rsi      # 0xb5f5
xorl %r14d, %r14d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x4610
jmp 0x90e0
xorl %r14d, %r14d
jmp 0x90e0
pushq $0x1
popq %rdi
jmp 0x9113