// File: /workspace/github_c_makefile_star2_O1/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 0x956b
movl %ebx, %eax
leaq 0x207b(%rip), %rcx      # 0xb5e0
movq (%rcx,%rax,8), %r15
jmp 0x956e
xorl %r15d, %r15d
cmpl $0x10, %ebx
ja 0x9617
movl $0x103ff, %eax          # imm = 0x103FF
btl %ebx, %eax
jae 0x95b7
cmpq %r15, (%r12)
movq %r15, (%r12)
jae 0x9597
leaq 0x2122(%rip), %rsi      # 0xb6b4
jmp 0x961e
cmpl $0xd, %ebx
ja 0x9617
movl %ebx, %eax
leaq 0x1cb3(%rip), %rcx      # 0xb258
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movsbl (%r14), %esi
jmp 0x965f
movl $0x2c00, %eax           # imm = 0x2C00
btl %ebx, %eax
jae 0x95e7
movq (%r12), %rax
movl $0x1, %r15d
testq %rax, %rax
je 0x95e0
cmpb $0x0, -0x1(%r14,%r15)
je 0x95e0
incq %r15
cmpq %rax, %r15
jbe 0x95d0
cmpq %rax, %r15
jb 0x9581
jmp 0x9617
cmpl $0xc, %ebx
jne 0x9617
cmpq %r15, (%r12)
jb 0x9581
leaq 0xc(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4470
movl (%r13), %eax
bswapl %eax
movl %eax, (%r13)
addq %rax, %r15
jmp 0x9581
leaq 0x2078(%rip), %rsi      # 0xb696
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 0x9714
movzwl (%r14), %eax
rolw $0x8, %ax
movzwl %ax, %esi
jmp 0x965f
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 0x208d(%rip), %rsi      # 0xb709
movq %rsp, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x4510
cmpl $0x1, %eax
jne 0x975a
movsd (%rsp), %xmm0
movl $0xb, %edi
jmp 0x974b
addq $-0x4, %r15
addq $0x4, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x4110
testq %rax, %rax
je 0x977e
movq %rax, %rbx
movl $0xc, %edi
movq %rax, %rsi
xorl %eax, %eax
callq 0x4670
movq %rax, %r14
movq %rbx, %rdi
callq 0x4630
jmp 0x962f
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 0x9786
leaq 0x1fd2(%rip), %rsi      # 0xb6dc
jmp 0x976a
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 0x1fc3(%rip), %rsi      # 0xb6f2
movq %rsp, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x4510
cmpl $0x1, %eax
jne 0x9763
cvtss2sd (%rsp), %xmm0
movl $0xa, %edi
movb $0x1, %al
callq 0x4670
movq %rax, %r14
jmp 0x962f
leaq 0x1fac(%rip), %rsi      # 0xb70d
jmp 0x976a
leaq 0x1f8b(%rip), %rsi      # 0xb6f5
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x45d0
jmp 0x962f
xorl %r14d, %r14d
jmp 0x962f
movl $0x1, %edi
jmp 0x9661