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

	// copy data
	size_t sPos = mainTag->length;
	for(size_t i = 0; i < value.size(); ++i) {
		SRscpValue *newData = reinterpret_cast<SRscpValue *> (response->data + response->length + 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;
	}
	// increment the total length by the new tag
	response->length += newTagLength;
	// all added successfully
	return RSCP::OK;
}

// Assembly:
testq %rsi, %rsi
je 0x5672
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r14
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
je 0x5678
movabsq $-0x1111111111111111, %rdx # imm = 0xEEEEEEEEEEEEEEEF
imulq %rdx, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x5, %rax
movl $0x7, %r12d
movzwl (%rax), %edx
addl %edx, %r12d
addl $0x7, %r12d
addq $0xf, %rax
decq %rcx
jne 0x565d
jmp 0x567e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq
movl $0x7, %r12d
movzwl 0x5(%r14), %ecx
addl %r12d, %ecx
movl $0xfffffff9, %eax       # imm = 0xFFFFFFF9
cmpl $0xfff8, %ecx           # imm = 0xFFF8
ja 0x5759
movl %ecx, %edx
movq %r14, %rsi
callq 0x5092
movl %eax, %ecx
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
testb %cl, %cl
je 0x5759
movq 0x7(%r14), %rax
movzwl 0x5(%r14), %edx
movl (%r15), %ecx
movl %ecx, (%rax,%rdx)
movb $0xe, 0x4(%rax,%rdx)
movl %r12d, %ecx
addl $-0x7, %ecx
movw %cx, 0x5(%rax,%rdx)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x5752
movzwl %cx, %r15d
xorl %r13d, %r13d
xorl %ebp, %ebp
movzwl 0x5(%r14), %edi
addq 0x7(%r14), %rdi
movl (%rax,%r13), %eax
movl %eax, (%r15,%rdi)
movq (%rbx), %rax
movb 0x4(%rax,%r13), %cl
movb %cl, 0x4(%r15,%rdi)
movzwl 0x5(%rax,%r13), %edx
movw %dx, 0x5(%r15,%rdi)
testq %rdx, %rdx
je 0x5724
addq %r15, %rdi
addq $0x7, %rdi
movq 0x7(%rax,%r13), %rsi
callq 0x20f0
movq (%rbx), %rax
movzwl 0x5(%rax,%r13), %ecx
addq %rcx, %r15
addq $0x7, %r15
incq %rbp
movq 0x8(%rbx), %rcx
subq %rax, %rcx
movabsq $-0x1111111111111111, %rdx # imm = 0xEEEEEEEEEEEEEEEF
imulq %rdx, %rcx
addq $0xf, %r13
cmpq %rcx, %rbp
jb 0x56e1
addw %r12w, 0x5(%r14)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq