// File: /workspace/github_cpp_makefile_star2_O1/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), %r13
movq %r13, %r15
shrq $0x4, %r15
movq %r13, %rcx
shrq $0x5, %rcx
cmpq %rcx, 0x20(%rax)
jbe 0x17f9b
addq %r15, %r15
jmp 0x17fad
leaq (%r15,%r15,2), %rcx
shrq $0x2, %rcx
cmpq %rcx, 0x18(%rax)
jbe 0x180c0
andq $-0x10, %r13
xorps %xmm0, %xmm0
movq %rsp, %r12
movaps %xmm0, (%r12)
shlq $0x4, %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0xda72
movq (%r12), %rax
orq $0x2, %r15
movq %r15, (%rax)
cmpq $0x11, %r13
jb 0x18027
movl $0x10, %ecx
leaq (%rcx,%r14), %rsi
movq 0x8(%rbx), %rdi
movq (%rdi,%rsi), %rdx
cmpq $0x2, %rdx
jb 0x1801e
movq 0x8(%rdi,%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 0x18004
movq %rdx, (%rax,%r8)
movq %rsi, 0x8(%rax,%r8)
addq $0x10, %rcx
cmpq %r13, %rcx
jb 0x17fe1
movq 0x8(%rsp), %rdx
cmpq %rdx, %r13
jne 0x18064
movq (%rsp), %rsi
movl 0x4(%rbx), %edi
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 0x180b7
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x17c82
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 0x17b5a
movq (%rsp), %rdi
callq 0x3430
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq