// File: nmmmnu[P]HM3/hm3/pairblob.cc
// Function: hm3::PairBlob::create(char const*, unsigned long, char const*, unsigned long, unsigned int, unsigned int)
std::unique_ptr<PairBlob> PairBlob::create(	const char *key, size_t const keylen,
				const char *val, size_t const vallen,
				uint32_t const expires, uint32_t const created){
	if (key == nullptr || keylen == 0){
		std::logic_error exception("Key is zero size");
		throw exception;
	}

	if (keylen > MAX_KEY_SIZE){
		std::logic_error exception("Key is very long");
		throw exception;
	}

	if (vallen > MAX_VAL_SIZE){
		std::logic_error exception("Value is very long");
		throw exception;
	}

	size_t const size = sizeofBase__() + keylen + 1 + vallen + 1;

	std::unique_ptr<PairBlob>  pair{ new(size, std::nothrow) PairBlob };

	pair->created	= htobe64(getCreateTime_(created));
	pair->expires	= htobe32(expires);
	pair->vallen	= htobe32((uint32_t) vallen);
	pair->keylen	= htobe16((uint16_t) keylen);

	// memcpy so we can switch to blobs later...
	memcpy(& pair->buffer[0],		key, keylen);
	pair->buffer[keylen] = '\0';

	// this is safe with NULL pointer.
	memcpy(& pair->buffer[keylen + 1],	val, vallen);
	pair->buffer[keylen + 1 + vallen] = '\0';

	pair->checksum	= pair->calcChecksum();

	return pair;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0x58(%rsp)
movq %rdi, %rax
movq %rax, 0x60(%rsp)
movl 0xf0(%rsp), %eax
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rcx, 0xc8(%rsp)
movq %r8, 0xc0(%rsp)
movl %r9d, 0xbc(%rsp)
cmpq $0x0, 0xd8(%rsp)
je 0x46a1
cmpq $0x0, 0xd0(%rsp)
jne 0x4719
leaq 0x4a8c(%rip), %rsi      # 0x9134
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x2260
movl $0x10, %edi
callq 0x20e0
movq 0x48(%rsp), %rsi
movq %rax, %rdi
movq %rdi, 0x50(%rsp)
callq 0x2150
movq 0x50(%rsp), %rdi
movq 0x88fe(%rip), %rsi      # 0xcfe0
movq 0x88df(%rip), %rdx      # 0xcfc8
callq 0x22b0
jmp 0x4a2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x2338
jmp 0x4a20
cmpq $0x2000, 0xd0(%rsp) # imm = 0x2000
jbe 0x479f
leaq 0x4a17(%rip), %rsi      # 0x9145
leaq 0x88(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x2260
movl $0x10, %edi
callq 0x20e0
movq 0x38(%rsp), %rsi
movq %rax, %rdi
movq %rdi, 0x40(%rsp)
callq 0x2150
movq 0x40(%rsp), %rdi
movq 0x8878(%rip), %rsi      # 0xcfe0
movq 0x8859(%rip), %rdx      # 0xcfc8
callq 0x22b0
jmp 0x4a2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x2338
jmp 0x4a20
cmpq $0x3dfeb, 0xc0(%rsp) # imm = 0x3DFEB
jbe 0x481f
leaq 0x49a2(%rip), %rsi      # 0x9156
leaq 0x78(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x2260
movl $0x10, %edi
callq 0x20e0
movq 0x28(%rsp), %rsi
movq %rax, %rdi
movq %rdi, 0x30(%rsp)
callq 0x2150
movq 0x30(%rsp), %rdi
movq 0x87f5(%rip), %rsi      # 0xcfe0
movq 0x87d6(%rip), %rdx      # 0xcfc8
callq 0x22b0
jmp 0x4a2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x2338
jmp 0x4a20
callq 0x45e0
addq 0xd0(%rsp), %rax
addq $0x1, %rax
addq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
movb $0x0, 0x6f(%rsp)
movq 0x70(%rsp), %rsi
movl $0x14, %edi
callq 0x4dd0
movq %rax, %rcx
movq %rcx, 0x18(%rsp)
xorl %eax, %eax
cmpq $0x0, %rcx
movq %rax, 0x20(%rsp)
je 0x4876
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x4876
movq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x4e00
movl 0xf0(%rsp), %edi
callq 0x4ae0
movq %rax, %rdi
callq 0x4a30
movq %rax, 0x10(%rsp)
jmp 0x48a0
movq 0x58(%rsp), %rdi
callq 0x3ae0
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movl 0xbc(%rsp), %edi
callq 0x4b20
movl %eax, 0xc(%rsp)
jmp 0x48c4
movq 0x58(%rsp), %rdi
callq 0x3ae0
movl 0xc(%rsp), %ecx
movl %ecx, 0x8(%rax)
movl 0xc0(%rsp), %edi
callq 0x4b20
movl %eax, 0x8(%rsp)
jmp 0x48e7
movq 0x58(%rsp), %rdi
callq 0x3ae0
movl 0x8(%rsp), %ecx
movl %ecx, 0xc(%rax)
movzwl 0xd0(%rsp), %edi
callq 0x4b60
movw %ax, 0x6(%rsp)
jmp 0x490c
movq 0x58(%rsp), %rdi
callq 0x3ae0
movw 0x6(%rsp), %cx
movq 0x58(%rsp), %rdi
movw %cx, 0x10(%rax)
callq 0x3ae0
movq %rax, %rdi
addq $0x13, %rdi
movq 0xd8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
callq 0x2170
movq 0x58(%rsp), %rdi
callq 0x3ae0
movq 0x58(%rsp), %rdi
movq 0xd0(%rsp), %rcx
movb $0x0, 0x13(%rax,%rcx)
callq 0x3ae0
movq %rax, %rdi
addq $0x13, %rdi
movq 0xd0(%rsp), %rax
addq $0x1, %rax
addq %rax, %rdi
movq 0xc8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
callq 0x2170
movq 0x58(%rsp), %rdi
callq 0x3ae0
movq 0x58(%rsp), %rdi
movq 0xd0(%rsp), %rcx
addq $0x1, %rcx
addq 0xc0(%rsp), %rcx
movb $0x0, 0x13(%rax,%rcx)
callq 0x3ae0
movq %rax, %rdi
callq 0x4e30
movq 0x58(%rsp), %rdi
movb %al, 0x5(%rsp)
callq 0x3ae0
movb 0x5(%rsp), %cl
movb %cl, 0x12(%rax)
movb $0x1, 0x6f(%rsp)
testb $0x1, 0x6f(%rsp)
jne 0x4a13
jmp 0x4a09
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
callq 0x39d0
jmp 0x4a20
movq 0x58(%rsp), %rdi
callq 0x39d0
movq 0x60(%rsp), %rax
addq $0xe8, %rsp
retq
movq 0xa0(%rsp), %rdi
callq 0x22c0
nopl (%rax)