// File: /workspace/github_cpp_makefile_star2_O3/Alcaro[P]bridges/arlib/staticmap.cpp
// Function: staticmap::rehash_if_needed()
void staticmap::rehash_if_needed()
{
	uint64_t hashmap_pos = rd64(off_r_hm_ptr);
	uint64_t hashmap_size = rd64(hashmap_pos+off_header) & ~t_typemask;
	
	uint64_t new_num_nodes;
	if (rd64(off_r_entries) > hashmap_size/off_h_nodesize/2)
		new_num_nodes = hashmap_size/off_h_nodesize*2;
	else if (rd64(off_r_hm_load) > hashmap_size/off_h_nodesize*3/4)
		new_num_nodes = hashmap_size/off_h_nodesize;
	else return;
	
	bytearray new_map_buf;
	new_map_buf.resize(new_num_nodes * off_h_nodesize);
	
	uint8_t* new_map = new_map_buf.ptr();
	wr64(new_map, new_num_nodes*off_h_nodesize+t_meta);
	
	for (uint64_t src_off=16;src_off<hashmap_size;src_off+=16)
	{
		uint64_t node_addr = rd64(hashmap_pos+src_off+off_h_ptr);
		uint64_t node_hash = rd64(hashmap_pos+src_off+off_h_hash);
		if (node_addr <= 1) continue;
		
		uint64_t dst_off = node_hash & (new_map_buf.size()-off_h_nodesize);
		while (readu_le64(new_map+dst_off+off_h_ptr) != 0) // this reads the object header if dst_off = 0; this is fine, it's nonzero
			dst_off = (dst_off+off_h_nodesize) & (new_map_buf.size()-off_h_nodesize);
		wr64(new_map+dst_off, node_addr, node_hash);
	}
	
	if (hashmap_size == new_map_buf.size())
	{
		f.pwrite(hashmap_pos, new_map_buf);
		wr64(off_r_hm_load, rd64(off_r_entries));
	}
	else
	{
		uint64_t new_pos = alloc(new_map_buf.size());
		f.pwrite(new_pos, new_map_buf);
		wr64(off_r_hm_ptr, new_pos, rd64(off_r_entries));
		free(hashmap_pos);
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x10(%rax), %r14
movq (%rax,%r14), %r15
movq %r15, %r12
shrq $0x4, %r12
movq %r15, %rcx
shrq $0x5, %rcx
cmpq %rcx, 0x20(%rax)
jbe 0x18265
addq %r12, %r12
jmp 0x18277
leaq (%r12,%r12,2), %rcx
shrq $0x2, %rcx
cmpq %rcx, 0x18(%rax)
jbe 0x18389
andq $-0x10, %r15
xorps %xmm0, %xmm0
movq %rsp, %r13
movaps %xmm0, (%r13)
shlq $0x4, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xdea4
movq (%r13), %rax
orq $0x2, %r12
movq %r12, (%rax)
cmpq $0x11, %r15
jb 0x182f0
movl $0x10, %ecx
movq 0x8(%rbx), %rsi
addq %rcx, %rsi
movq (%r14,%rsi), %rdx
cmpq $0x2, %rdx
jb 0x182e7
movq 0x8(%r14,%rsi), %rsi
movq 0x8(%rsp), %rdi
addq $-0x10, %rdi
movq %rsi, %r9
movq %r9, %r8
andq %rdi, %r8
leaq 0x10(%r8), %r9
cmpq $0x0, (%rax,%r8)
jne 0x182cd
movq %rdx, (%rax,%r8)
movq %rsi, 0x8(%rax,%r8)
addq $0x10, %rcx
cmpq %r15, %rcx
jb 0x182ab
movq 0x8(%rsp), %rsi
cmpq %rsi, %r15
jne 0x18330
movq (%rsp), %rsi
movl 0x4(%rbx), %edi
movq %r15, %rdx
movq %r14, %rcx
callq 0x30d0
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movl 0x4(%rbx), %edi
movl $0x8, %edx
movl $0x18, %ecx
callq 0x30d0
jmp 0x18380
movq %rbx, %rdi
callq 0x17f70
movq %rax, %r15
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movl 0x4(%rbx), %edi
movq %rax, %rcx
callq 0x30d0
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
leaq 0x10(%rsp), %rsi
movq %r15, (%rsi)
movq %rax, 0x8(%rsi)
movl 0x4(%rbx), %edi
movl $0x10, %edx
movl $0x10, %ecx
callq 0x30d0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17e62
movq (%rsp), %rdi
callq 0x3430
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop