// File: /workspace/github_cpp_makefile_star2_O1/rellla[P]E3DC-Rscp/RscpProtocol.cpp
// Function: RscpProtocol::createValue(SRscpValue*, unsigned int const&, std::vector<SRscpValue, std::allocator<SRscpValue>> const&)
int32_t RscpProtocol::createValue(SRscpValue* response, const SRscpTag & tag, const std::vector<SRscpValue> & value) {
	// sanity check
	if(response == NULL) {
		return RSCP::ERR_INVALID_INPUT;
	}
	// reset the memory of the response value to zero
	memset(response, 0, sizeof(SRscpValue));
	// calculate the new tag size
	uint32_t newTagLength = 0;
	for(size_t i = 0; i < value.size(); ++i) {
		newTagLength += sizeof(SRscpValue) - sizeof(value[i].data) + value[i].length;
	}
	// check boundaries
	if(newTagLength > 0xFFF8) {
		return RSCP::ERR_DATA_LIMIT_EXCEEDED;
	}
	// re-allocate the structure memory
	if(allocateMemory(response, newTagLength) == false) {
		return RSCP::ERR_NO_MEMORY;
	}
	// first add the TAG for a container
	response->tag = tag;
	response->dataType = RSCP::eTypeContainer;
	response->length = newTagLength;

	// copy data
	size_t sPos = 0;
	for(size_t i = 0; i < value.size(); ++i) {
		SRscpValue *newData = reinterpret_cast<SRscpValue *> (((uint8_t*)response->data) + sPos);
		newData->tag = value[i].tag;
		newData->dataType = value[i].dataType;
		newData->length = value[i].length;
		if(newData->length > 0) {
			// copy into the position of the data pointer and not into the data pointer itself as the data is appended
			memcpy(&newData->data, value[i].data, newData->length);
		}
		sPos += sizeof(SRscpValue) - sizeof(value[i].data) + value[i].length;
	}
	// all added successfully
	return RSCP::OK;
}

// Assembly:
testq %rsi, %rsi
je 0x57e0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
xorl %r12d, %r12d
movq %r12, 0x7(%rsi)
movq %r12, (%rsi)
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
je 0x5719
movabsq $-0x1111111111111111, %rdx # imm = 0xEEEEEEEEEEEEEEEF
imulq %rdx, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x5, %rax
xorl %r12d, %r12d
movzwl (%rax), %edx
addl %edx, %r12d
addl $0x7, %r12d
addq $0xf, %rax
decq %rcx
jne 0x5706
movl $0xfffffff9, %eax       # imm = 0xFFFFFFF9
cmpl $0xfff8, %r12d          # imm = 0xFFF8
ja 0x57d1
movl %r12d, %edx
movq %r14, %rsi
callq 0x52fa
movl %eax, %ecx
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
testb %cl, %cl
je 0x57d1
movl (%r15), %eax
movl %eax, (%r14)
movb $0xe, 0x4(%r14)
movw %r12w, 0x5(%r14)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x57cf
xorl %r15d, %r15d
movabsq $-0x1111111111111111, %r12 # imm = 0xEEEEEEEEEEEEEEEF
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x7(%r14), %rdi
movl (%rax,%r15), %eax
movl %eax, (%rdi,%rbp)
movq (%rbx), %rax
movb 0x4(%rax,%r15), %cl
movb %cl, 0x4(%rdi,%rbp)
movzwl 0x5(%rax,%r15), %edx
movw %dx, 0x5(%rdi,%rbp)
testq %rdx, %rdx
je 0x57a8
addq %rbp, %rdi
addq $0x7, %rdi
movq 0x7(%rax,%r15), %rsi
callq 0x20f0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movzwl 0x5(%rax,%r15), %edx
addq %rdx, %rbp
addq $0x7, %rbp
incq %r13
subq %rax, %rcx
imulq %r12, %rcx
addq $0xf, %r15
cmpq %rcx, %r13
jb 0x5770
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq