// File: rellla[P]E3DC-Rscp/RscpProtocol.cpp
// Function: RscpProtocol::appendValue(SRscpValue*, std::vector<SRscpValue, std::allocator<SRscpValue>> const&)
int32_t RscpProtocol::appendValue(SRscpValue* response, const std::vector<SRscpValue> & value) {
	// sanity check
	if(response == NULL) {
		return RSCP::ERR_INVALID_INPUT;
	}
	// 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(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;
	}
	// copy data
	size_t sPos = 0;
	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:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x50(%rsp)
jne 0x92c2
movl $0xffffffff, 0x64(%rsp) # imm = 0xFFFFFFFF
jmp 0x94b8
movl $0x0, 0x44(%rsp)
movq $0x0, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x48(%rsp), %rdi
callq 0x5e80
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0x9328
movq 0x48(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x95d0
movzwl 0x5(%rax), %eax
movl %eax, %ecx
addq $0x7, %rcx
movl 0x44(%rsp), %eax
addq %rcx, %rax
movl %eax, 0x44(%rsp)
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x92d3
movq 0x50(%rsp), %rax
movzwl 0x5(%rax), %eax
addl 0x44(%rsp), %eax
cmpl $0xfff8, %eax           # imm = 0xFFF8
jbe 0x9349
movl $0xfffffff9, 0x64(%rsp) # imm = 0xFFFFFFF9
jmp 0x94b8
movq 0x18(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x50(%rsp), %rax
movzwl 0x5(%rax), %eax
addl 0x44(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
callq 0x85e0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x9380
movl $0xfffffffe, 0x64(%rsp) # imm = 0xFFFFFFFE
jmp 0x94b8
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rdi
callq 0x5e80
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x949d
movq 0x50(%rsp), %rax
movq 0x7(%rax), %rax
movq 0x50(%rsp), %rcx
movzwl 0x5(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x95d0
movl (%rax), %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax)
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x95d0
movb 0x4(%rax), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x4(%rax)
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x95d0
movw 0x5(%rax), %cx
movq 0x20(%rsp), %rax
movw %cx, 0x5(%rax)
movq 0x20(%rsp), %rax
movzwl 0x5(%rax), %eax
cmpl $0x0, %eax
jle 0x9469
movq 0x20(%rsp), %rax
addq $0x7, %rax
movq %rax, (%rsp)
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x95d0
movq (%rsp), %rdi
movq 0x7(%rax), %rsi
movq 0x20(%rsp), %rax
movzwl 0x5(%rax), %eax
movl %eax, %edx
callq 0x2130
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x95d0
movzwl 0x5(%rax), %eax
addq $0x7, %rax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x28(%rsp)
jmp 0x9392
movl 0x44(%rsp), %edx
movq 0x50(%rsp), %rax
movzwl 0x5(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x5(%rax)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)