// File: /workspace/github_cpp_makefile_star2_O1/Alcaro[P]bridges/arlib/staticmap.cpp
// Function: staticmap::insert(arrayview<unsigned char>, arrayview<unsigned char>)
bytesw staticmap::insert(bytesr key, bytesr val)
{
//puts("INSERT "+tostringhex(key)+" "+tostringhex(val));
//puts("XINSERT "+tostringhex(key)+" "+tostring(val.size())+" 55");
	desync();
	
	rehash_if_needed();
	
	uint64_t hash = sm_hash(key);
	uint64_t hashmap_pos = hashmap_locate(hash, key);
	
	uint64_t prev_ptr = rd64(hashmap_pos+off_h_ptr);
	uint64_t alloc_size = bitround(off_d_keystart+key.size()+val.size());
	if (alloc_size <= sector_size && prev_ptr > 1 && val.size() == rd64(prev_ptr+off_d_valsz))
	{
		// choose in-place update if possible
		f.pwrite(prev_ptr+off_d_keystart+key.size(), val);
		return mmap.slice(prev_ptr+off_d_keystart+key.size(), val.size());
	}
	uint64_t pos = alloc(alloc_size);
	
	uint8_t head[off_d_keystart];
	wr64(head, alloc_size+t_data, key.size(), val.size());
	
	if (alloc_size <= sector_size)
	{
		iovec iov[] = { { head, off_d_keystart }, { (void*)key.ptr(), key.size() }, { (void*)val.ptr(), val.size() } };
		f.pwritev(pos, iov);
	}
	else
	{
		iovec iov[] = {                           { (void*)key.ptr(), key.size() }, { (void*)val.ptr(), val.size() } };
		f.pwritev(pos+off_d_keystart, iov);
		f.pwrite(pos, head);
	}
	
	if (prev_ptr > 1)
		free(prev_ptr);
	else
		wr64(off_r_hm_load, rd64(off_r_hm_load)+(prev_ptr==0), rd64(off_r_entries)+1);
	wr64(hashmap_pos, pos, hash);
	
	return mmap.slice(pos+off_r_entries+key.size(), val.size());
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %rbx
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0x18(%rsp)
movq %rsi, %rdi
callq 0x183fe
movq %r15, %rdi
callq 0x17f66
movq %r14, %rdi
movq %r12, %rsi
callq 0x15897
movq %rax, %rcx
shrq $0x1e, %rcx
xorq %rax, %rcx
movabsq $-0x40a7b892e31b1a47, %rax # imm = 0xBF58476D1CE4E5B9
imulq %rcx, %rax
movq %rax, %rcx
shrq $0x1b, %rcx
xorq %rax, %rcx
movabsq $-0x6b2fb644ecceee15, %rax # imm = 0x94D049BB133111EB
imulq %rcx, %rax
movq %rax, %r13
shrq $0x1f, %r13
xorq %rax, %r13
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x17e74
movq %rbx, %rdx
movq 0x8(%r15), %r8
movq (%r8,%rax), %rbp
leaq (%r12,%rbx), %rdi
addq $0x18, %rdi
leaq (%r12,%rbx), %rcx
addq $0x17, %rcx
bsrq %rcx, %rcx
movl $0x2, %esi
shlq %cl, %rsi
cmpq $0x2, %rdi
movl $0x1, %ebx
cmovaeq %rsi, %rbx
cmpq $0x2, %rbp
jb 0x18296
movl 0x1c(%r15), %ecx
cmpq %rcx, %rbx
ja 0x18296
cmpq %rdx, 0x10(%r8,%rbp)
jne 0x18296
leaq (%r12,%rbp), %rbx
addq $0x18, %rbx
movl 0x4(%r15), %edi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
movq %rdx, %r14
callq 0x30d0
addq 0x8(%r15), %rbx
xorps %xmm0, %xmm0
movq 0x18(%rsp), %rcx
movups %xmm0, (%rcx)
movq %rbx, (%rcx)
movq %r14, 0x8(%rcx)
jmp 0x183e8
movq %r14, 0x10(%rsp)
movq %rax, 0x28(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %rdx, 0x20(%rsp)
callq 0x17c82
movq 0x20(%rsp), %rcx
movq %rax, %r14
leaq 0x1(%rbx), %rax
movq %rax, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movl 0x1c(%r15), %eax
cmpq %rax, %rbx
jbe 0x1831f
leaq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
movq %r12, 0x8(%rsi)
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsi)
movq %rcx, 0x18(%rsi)
movq %r14, %rcx
addq $0x18, %rcx
movl 0x4(%r15), %edi
movl $0x2, %edx
callq 0x3260
movl 0x4(%r15), %edi
leaq 0x30(%rsp), %rsi
movl $0x18, %edx
movq %r14, %rcx
callq 0x30d0
jmp 0x1835f
leaq 0x30(%rsp), %rax
leaq 0x50(%rsp), %rsi
movq %rax, (%rsi)
movq $0x18, 0x8(%rsi)
movq 0x10(%rsp), %rax
movq %rax, 0x10(%rsi)
movq %r12, 0x18(%rsi)
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsi)
movq %rcx, 0x28(%rsi)
movl 0x4(%r15), %edi
movl $0x3, %edx
movq %r14, %rcx
callq 0x3260
cmpq $0x2, %rbp
jb 0x18372
movq %r15, %rdi
movq %rbp, %rsi
callq 0x17b5a
jmp 0x183a4
movq 0x8(%r15), %rax
xorq $0x1, %rbp
addq 0x18(%rax), %rbp
movq 0x20(%rax), %rax
incq %rax
leaq 0x50(%rsp), %rsi
movq %rbp, (%rsi)
movq %rax, 0x8(%rsi)
movl 0x4(%r15), %edi
movl $0x10, %edx
movl $0x18, %ecx
callq 0x30d0
leaq 0x50(%rsp), %rsi
movq %r14, (%rsi)
movq %r13, 0x8(%rsi)
movl 0x4(%r15), %edi
movl $0x10, %edx
movq 0x28(%rsp), %rcx
callq 0x30d0
movq 0x8(%r15), %rax
addq %r14, %rax
addq %r12, %rax
addq $0x20, %rax
xorps %xmm0, %xmm0
movq 0x18(%rsp), %rcx
movups %xmm0, (%rcx)
movq %rax, (%rcx)
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rcx)
movq %rcx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop