// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r14
movq %rsi, %r12
movl %edi, %ebx
cmpl $0x10, %edi
ja 0x95bc
movl %ebx, %eax
leaq 0x1efa(%rip), %rcx      # 0xb4b0
movq (%rcx,%rax,8), %r15
jmp 0x95bf
xorl %r15d, %r15d
cmpl $0x10, %ebx
ja 0x9664
movl $0x103ff, %eax          # imm = 0x103FF
btl %ebx, %eax
jae 0x9608
cmpq %r15, (%r12)
movq %r15, (%r12)
jae 0x95e8
leaq 0x1fa1(%rip), %rsi      # 0xb584
jmp 0x966b
cmpl $0xd, %ebx
ja 0x9664
movl %ebx, %eax
leaq 0x1c62(%rip), %rcx      # 0xb258
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsbl (%r14), %esi
jmp 0x96ac
movl $0x2c00, %eax           # imm = 0x2C00
btl %ebx, %eax
jae 0x9638
movq (%r12), %rax
movl $0x1, %r15d
testq %rax, %rax
je 0x9631
cmpb $0x0, -0x1(%r14,%r15)
je 0x9631
incq %r15
cmpq %rax, %r15
jbe 0x9621
cmpq %rax, %r15
jb 0x95d2
jmp 0x9664
cmpl $0xc, %ebx
jne 0x9664
cmpq %r15, (%r12)
jb 0x95d2
leaq 0xc(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4470
movl (%r13), %eax
bswapl %eax
addq %rax, %r15
jmp 0x95d2
leaq 0x1efb(%rip), %rsi      # 0xb566
xorl %r14d, %r14d
movl $0x1, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x45d0
movq %r14, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq (%r14), %rsi
bswapq %rsi
movl %ebx, %edi
jmp 0x9761
movzwl (%r14), %eax
rolw $0x8, %ax
movzwl %ax, %esi
jmp 0x96ac
movl (%r14), %esi
bswapl %esi
movl %ebx, %edi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x4670
leaq 0x1f10(%rip), %rsi      # 0xb5d9
movq %rsp, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x4510
cmpl $0x1, %eax
jne 0x97a7
movsd (%rsp), %xmm0
movl $0xb, %edi
jmp 0x9798
addq $-0x4, %r15
addq $0x4, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x4110
testq %rax, %rax
je 0x97cb
movq %rax, %rbx
movl $0xc, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x4670
movq %rax, %r14
movq %rbx, %rdi
callq 0x4630
jmp 0x967c
xorl %edi, %edi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x4670
movsbl (%r14), %esi
testb $-0x2, %sil
je 0x97d3
leaq 0x1e55(%rip), %rsi      # 0xb5ac
jmp 0x97b7
movl $0xd, %edi
movq %r14, %rsi
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x4670
leaq 0x1e46(%rip), %rsi      # 0xb5c2
movq %rsp, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x4510
cmpl $0x1, %eax
jne 0x97b0
cvtss2sd (%rsp), %xmm0
movl $0xa, %edi
movb $0x1, %al
callq 0x4670
movq %rax, %r14
jmp 0x967c
leaq 0x1e2f(%rip), %rsi      # 0xb5dd
jmp 0x97b7
leaq 0x1e0e(%rip), %rsi      # 0xb5c5
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x45d0
jmp 0x967c
xorl %r14d, %r14d
jmp 0x967c
movl $0x1, %edi
jmp 0x96ae