// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x88(%rbp)
subq $0x10, %rax
ja 0xe7a3
movq -0x88(%rbp), %rax
leaq 0x1e6d(%rip), %rcx      # 0x103d4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq $0x0, -0x38(%rbp)
jmp 0xe7a3
movq $0x1, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq $0x2, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0xea30
movw %ax, -0x3c(%rbp)
leaq -0x3c(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq $0x2, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0xea30
movw %ax, -0x3e(%rbp)
leaq -0x3e(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq $0x4, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
callq 0xd160
movl %eax, -0x44(%rbp)
leaq -0x44(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq $0x4, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
callq 0xd160
movl %eax, -0x54(%rbp)
leaq -0x54(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq $0x8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xea60
movq %rax, -0x50(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq $0x8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xea60
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
leaq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rax
movss 0x8(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movl $0x10, %esi
leaq 0x1f0f(%rip), %rdx      # 0x105a2
movb $0x1, %al
callq 0x42a0
cltq
movq %rax, -0x28(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
leaq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movl $0x10, %esi
leaq 0x1edc(%rip), %rdx      # 0x105a2
movb $0x1, %al
callq 0x42a0
cltq
movq %rax, -0x28(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4270
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4270
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
callq 0xd160
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4550
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4350
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movq $0x4, -0x28(%rbp)
movq -0x78(%rbp), %rdi
callq 0x4250
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %edi
callq 0xd160
movl %eax, -0x54(%rbp)
leaq -0x54(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xe7a3
movq $0x8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0xea60
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xe7da
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0xe7da
movq -0x10(%rbp), %rax
movl (%rax), %edx
movl $0x1, %edi
leaq 0x1de1(%rip), %rsi      # 0x105a6
movb $0x0, %al
callq 0x4630
movl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0xea1b
movq $0x0, -0x30(%rbp)
testb $0x1, -0x19(%rbp)
je 0xe888
movq -0x10(%rbp), %rax
movl (%rax), %eax
movb %al, -0x39(%rbp)
movq -0x18(%rbp), %rdi
movl $0x1, %esi
leaq -0x39(%rbp), %rdx
callq 0x4490
cmpl $0x0, %eax
je 0xe818
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xea1b
movq $0x1, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0xc, (%rax)
jne 0xe85e
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0x54(%rbp), %rdx
movl $0x4, %ecx
callq 0x40c0
cmpl $0x0, %eax
je 0xe850
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xea1b
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
jmp 0xe886
movq -0x10(%rbp), %rax
cmpl $0xe, (%rax)
je 0xe884
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x40c0
movl %eax, -0x4(%rbp)
jmp 0xea1b
jmp 0xe886
jmp 0xe888
movq -0x10(%rbp), %rax
cmpl $0xc, (%rax)
jne 0xe8d2
movq -0x18(%rbp), %rdi
movl $0x4, %esi
leaq -0x54(%rbp), %rdx
callq 0x4490
cmpl $0x0, %eax
je 0xe8b4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xea1b
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl $0x4, %esi
callq 0x40c0
movl %eax, -0x4(%rbp)
jmp 0xea1b
movq -0x10(%rbp), %rax
cmpl $0xe, (%rax)
jne 0xea05
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movb %al, -0x39(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
leaq -0x39(%rbp), %rdx
movl $0x1, %ecx
callq 0x40c0
cmpl $0x0, %eax
je 0xe91c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xea1b
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x40c0
cmpl $0x0, %eax
je 0xe94e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xea1b
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x78(%rbp), %rdi
callq 0x4340
movq %rax, -0x28(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rdi
callq 0x4250
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
jae 0xe9fc
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x45e0
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq %rax, %rdx
movq -0x28(%rbp), %rcx
callq 0x40c0
cmpl $0x0, %eax
je 0xe9dd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xea1b
movq -0x28(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xe9eb
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xe96f
movl $0x0, -0x4(%rbp)
jmp 0xea1b
jmp 0xea07
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x4490
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)