// File: /workspace/github_cpp_makefile_star2_O2/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 0x4e23
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), %rcx
movq 0x8(%rbx), %rax
subq %rcx, %rax
pushq $0xf
popq %rsi
cqto
idivq %rsi
addq $0x5, %rcx
pushq $0x7
popq %r13
subq $0x1, %rax
jb 0x4e0d
movzwl (%rcx), %edx
addl %edx, %r13d
addl $0x7, %r13d
addq $0xf, %rcx
jmp 0x4df7
movzwl 0x5(%r14), %eax
addl %r13d, %eax
cmpl $0xfff8, %eax           # imm = 0xFFF8
jbe 0x4e27
pushq $-0x7
jmp 0x4ecf
pushq $-0x1
popq %rax
retq
movl %eax, %edx
movq %r14, %rsi
callq 0x48f0
testb %al, %al
je 0x4ecd
movq 0x7(%r14), %rax
movzwl 0x5(%r14), %ecx
movl (%r15), %edx
movl %edx, (%rax,%rcx)
movb $0xe, 0x4(%rax,%rcx)
movl %r13d, %edx
addl $-0x7, %edx
movw %dx, 0x5(%rax,%rcx)
movzwl %dx, %r15d
movq (%rbx), %rcx
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x8(%rbx), %rax
subq %rcx, %rax
cqto
pushq $0xf
popq %rsi
idivq %rsi
cmpq %rax, %r12
jae 0x4ed2
movzwl 0x5(%r14), %edi
addq 0x7(%r14), %rdi
movl (%rcx,%rbp), %eax
movl %eax, (%r15,%rdi)
movq (%rbx), %rcx
movb 0x4(%rcx,%rbp), %al
movb %al, 0x4(%r15,%rdi)
movzwl 0x5(%rcx,%rbp), %edx
movw %dx, 0x5(%r15,%rdi)
testq %rdx, %rdx
je 0x4eb8
addq %r15, %rdi
addq $0x7, %rdi
movq 0x7(%rcx,%rbp), %rsi
callq 0x2120
movq (%rbx), %rcx
movzwl 0x5(%rcx,%rbp), %eax
addq %rax, %r15
addq $0x7, %r15
incq %r12
addq $0xf, %rbp
jmp 0x4e64
pushq $-0x2
popq %rax
jmp 0x4ed9
addw %r13w, 0x5(%r14)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq