// File: /workspace/github_c_makefile_star2_O1/DeforaOS[P]libSystem/src/variable.c
// Function: variable_serialize
VariableError variable_serialize(Variable * variable, Buffer * buffer,
		bool prefix)
{
	size_t size = 0;
	size_t offset;
	void * p;
	uint8_t u8;
	int16_t i16;
	uint16_t u16;
	int32_t i32;
	int64_t i64;
	uint32_t u32;
	uint64_t u64;
	char buf[16];
	Array const * array;
	size_t i;

#ifdef DEBUG
	fprintf(stderr, "DEBUG: %s(%u)\n", __func__, variable->type);
#endif
	switch(variable->type)
	{
		case VT_NULL:
			p = NULL;
			break;
		case VT_BOOL:
		case VT_INT8:
		case VT_UINT8:
			size = sizeof(variable->u.int8);
			p = &variable->u.int8;
			break;
		case VT_INT16:
			size = sizeof(i16);
			i16 = _bswap16(variable->u.int16);
			p = &i16;
			break;
		case VT_UINT16:
			size = sizeof(u16);
			u16 = _bswap16(variable->u.uint16);
			p = &u16;
			break;
		case VT_INT32:
			size = sizeof(i32);
			i32 = _bswap32(variable->u.int32);
			p = &i32;
			break;
		case VT_UINT32:
			size = sizeof(u32);
			u32 = _bswap32(variable->u.uint32);
			p = &u32;
			break;
		case VT_INT64:
			size = sizeof(i64);
			i64 = _bswap64(variable->u.int64);
			p = &i64;
			break;
		case VT_UINT64:
			size = sizeof(u64);
			u64 = _bswap64(variable->u.uint64);
			p = &u64;
			break;
		case VT_FLOAT:
			size = snprintf(buf, sizeof(buf), "%.e", variable->u.f);
			p = buf;
			break;
		case VT_DOUBLE:
			size = snprintf(buf, sizeof(buf), "%.e", variable->u.d);
			p = buf;
			break;
		case VT_BUFFER:
			size = buffer_get_size(variable->u.buffer);
			u32 = buffer_get_size(variable->u.buffer);
			u32 = _bswap32(u32);
			p = (void *)buffer_get_data(variable->u.buffer);
			break;
		case VT_STRING:
			size = string_get_length(variable->u.string) + 1;
			p = variable->u.string;
			break;
		case VT_ARRAY:
			array = variable->u.array.array;
			size = sizeof(u32);
			u32 = array_count(array);
			u32 = _bswap32(u32);
			p = &u32;
			break;
		case VT_POINTER:
			size = sizeof(u64);
			u64 = (uint64_t)variable->u.pointer;
			u64 = _bswap64(u64);
			p = &u64;
			break;
	}
#ifdef DEBUG
	fprintf(stderr, "DEBUG: %s() %lu\n", __func__, size);
#endif
	if(size == 0 && variable->type != VT_NULL)
		return -error_set_code(1, "Unable to serialize type %u",
				variable->type);
	offset = 0;
	if(prefix)
	{
		/* prefix with the type */
		u8 = variable->type;
		if(buffer_set(buffer, sizeof(u8), (char const *)&u8) != 0)
			return -1;
		offset = sizeof(u8);
		if(variable->type == VT_BUFFER)
		{
			if(buffer_set_data(buffer, offset, (char const *)&u32,
						sizeof(u32)) != 0)
				return -1;
			offset += sizeof(u32);
		}
		else if(variable->type != VT_ARRAY)
			return buffer_set_data(buffer, offset, (char const *)p,
					size);
	}
	if(variable->type == VT_BUFFER)
	{
		if(buffer_set(buffer, sizeof(u32), (char const *)&u32) != 0)
			return -1;
		return buffer_set_data(buffer, sizeof(u32), (char const *)p,
				size);
	}
	else if(variable->type == VT_ARRAY)
	{
		array = variable->u.array.array;
		u8 = variable->u.array.type;
		if(buffer_set_data(buffer, offset, (char const *)&u8,
					sizeof(u8)) != 0)
			return -1;
		offset += sizeof(u8);
		if(buffer_set_data(buffer, offset, p, size) != 0)
			return -1;
		offset += sizeof(u32);
		size = array_get_size(array);
		for(i = 0; i < array_count(array); i++)
			if(buffer_set_data(buffer, offset, array_get(array, i),
						size) != 0)
				return -1;
			else
				offset += size;
		return 0;
	}
	return buffer_set(buffer, size, (char const *)p);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
movl (%rdi), %eax
xorl %r15d, %r15d
xorl %r12d, %r12d
decl %eax
cmpl $0xf, %eax
ja 0xa661
leaq 0x102f(%rip), %rcx      # 0xb59c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x8(%r14), %r15
movl $0x1, %r12d
jmp 0xa661
movq 0x8(%r14), %rax
bswapq %rax
leaq 0x10(%rsp), %r15
movq %rax, (%r15)
movl $0x8, %r12d
jmp 0xa661
movzwl 0x8(%r14), %eax
rolw $0x8, %ax
leaq 0x10(%rsp), %r15
movw %ax, (%r15)
movl $0x2, %r12d
jmp 0xa661
movq 0x8(%r14), %rdi
callq 0x4330
movq %rax, %r12
incq %r12
movq 0x8(%r14), %r15
jmp 0xa661
movl 0x8(%r14), %eax
bswapl %eax
leaq 0x10(%rsp), %r15
jmp 0xa601
movq 0x10(%r14), %rdi
callq 0x4250
bswapl %eax
leaq 0xc(%rsp), %r15
movl %eax, (%r15)
jmp 0xa604
movl 0x8(%r14), %eax
bswapl %eax
leaq 0xc(%rsp), %r15
movl %eax, (%r15)
movl $0x4, %r12d
jmp 0xa661
cvtss2sd 0x8(%r14), %xmm0
jmp 0xa643
movq 0x8(%r14), %rdi
callq 0x4270
movq %rax, %r12
movq 0x8(%r14), %rdi
callq 0x4270
bswapl %eax
movl %eax, 0xc(%rsp)
movq 0x8(%r14), %rdi
callq 0x4500
movq %rax, %r15
jmp 0xa661
movsd 0x8(%r14), %xmm0
leaq 0x10d8(%rip), %rdx      # 0xb722
leaq 0x10(%rsp), %r15
movl $0x10, %esi
movq %r15, %rdi
movb $0x1, %al
callq 0x42a0
movslq %eax, %r12
testq %r12, %r12
je 0xa70b
testb %bpl, %bpl
je 0xa6d1
movb (%r14), %al
leaq 0xb(%rsp), %rdx
movb %al, (%rdx)
movl $0x1, %r13d
movl $0x1, %esi
movq %rbx, %rdi
callq 0x4450
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xa794
movl (%r14), %eax
cmpl $0xe, %eax
je 0xa6d4
cmpl $0xc, %eax
jne 0xa77f
leaq 0xc(%rsp), %rdx
movl $0x1, %esi
movl $0x4, %ecx
movq %rbx, %rdi
callq 0x40d0
testl %eax, %eax
jne 0xa794
movl $0x5, %r13d
jmp 0xa6d4
xorl %r13d, %r13d
movl (%r14), %eax
cmpl $0xe, %eax
je 0xa72f
cmpl $0xc, %eax
jne 0xa76f
leaq 0xc(%rsp), %rdx
movl $0x4, %esi
movq %rbx, %rdi
callq 0x4450
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xa794
movl $0x4, %esi
jmp 0xa784
movl (%r14), %edx
testl %edx, %edx
je 0xa66a
leaq 0x1009(%rip), %rsi      # 0xb726
movl $0x1, %edi
xorl %eax, %eax
callq 0x45d0
movl %eax, %ebp
negl %ebp
jmp 0xa794
movq 0x10(%r14), %rbp
movb 0x8(%r14), %al
leaq 0xb(%rsp), %rdx
movb %al, (%rdx)
movl $0x1, %ecx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x40d0
testl %eax, %eax
jne 0xa768
leaq 0x1(%r13), %rsi
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x40d0
testl %eax, %eax
je 0xa7a5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xa794
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x4450
jmp 0xa792
movl $0x1, %esi
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x40d0
movl %eax, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %r12
movq %rbp, %rdi
callq 0x4320
movq %rax, %r14
movq %rbp, %rdi
callq 0x4250
testq %rax, %rax
je 0xa802
addq $0x5, %r13
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %r12, %rdi
movq %r15, %rsi
callq 0x4590
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq %r14, %rcx
callq 0x40d0
testl %eax, %eax
jne 0xa768
addq %r14, %r13
incq %r15
movq %r12, %rdi
callq 0x4250
cmpq %rax, %r15
jb 0xa7c9
jmp 0xa794
xorl %ebp, %ebp
jmp 0xa794