// File: 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 %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq %rdi, -0x98(%rbp)
movl $0x10, %esi
callq 0x353f0
movq -0x98(%rbp), %rdi
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
addq $0x0, %rsi
callq 0x353f0
movq -0x98(%rbp), %rdi
andq $-0x10, %rax
movq %rax, -0x28(%rbp)
movl $0x20, %esi
callq 0x353f0
movq -0x28(%rbp), %rcx
shrq $0x4, %rcx
shrq %rcx
cmpq %rcx, %rax
jbe 0x33e26
movq -0x28(%rbp), %rax
shrq $0x4, %rax
shlq %rax
movq %rax, -0x30(%rbp)
jmp 0x33e61
movq -0x98(%rbp), %rdi
movl $0x18, %esi
callq 0x353f0
movq -0x28(%rbp), %rcx
shrq $0x4, %rcx
imulq $0x3, %rcx, %rcx
shrq $0x2, %rcx
cmpq %rcx, %rax
jbe 0x33e5a
movq -0x28(%rbp), %rax
shrq $0x4, %rax
movq %rax, -0x30(%rbp)
jmp 0x33e5f
jmp 0x340be
jmp 0x33e61
leaq -0x40(%rbp), %rdi
callq 0x74b0
movq -0x30(%rbp), %rsi
shlq $0x4, %rsi
leaq -0x40(%rbp), %rdi
callq 0x1ce60
leaq -0x40(%rbp), %rdi
callq 0x7490
movq -0x98(%rbp), %rdi
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq $0x2, %rdx
callq 0x35670
movq $0x10, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x33fa1
movq -0x98(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq -0x50(%rbp), %rsi
addq $0x0, %rsi
callq 0x353f0
movq -0x98(%rbp), %rdi
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rsi
addq -0x50(%rbp), %rsi
addq $0x8, %rsi
callq 0x353f0
movq %rax, -0x60(%rbp)
cmpq $0x1, -0x58(%rbp)
ja 0x33efe
jmp 0x33f90
movq -0x60(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0xa0(%rbp), %rax
subq $0x10, %rcx
andq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x33f74
movq -0x68(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xa8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0xa8(%rbp), %rax
subq $0x10, %rcx
andq %rcx, %rax
movq %rax, -0x68(%rbp)
jmp 0x33f27
movq -0x98(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq -0x68(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x358a0
movq -0x50(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x50(%rbp)
jmp 0x33eac
movq -0x28(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
jne 0x3401a
movq -0x98(%rbp), %rdi
addq $0x4, %rdi
movq -0x20(%rbp), %rsi
movq -0x40(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rcx
callq 0x35470
movq -0x98(%rbp), %rdi
movl $0x20, %esi
callq 0x353f0
movq -0x98(%rbp), %rdi
movq %rax, %rdx
movl $0x18, %esi
callq 0x356d0
jmp 0x340b5
leaq -0x40(%rbp), %rdi
callq 0x6290
movq -0x98(%rbp), %rdi
movq %rax, %rsi
callq 0x33960
movq -0x98(%rbp), %rdi
movq %rax, -0x80(%rbp)
addq $0x4, %rdi
movq -0x80(%rbp), %rsi
movq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x35470
movq -0x98(%rbp), %rdi
movq -0x80(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x20, %esi
callq 0x353f0
movq -0xb8(%rbp), %rdx
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl $0x10, %esi
callq 0x35b30
movq -0x98(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x33760
leaq -0x40(%rbp), %rdi
callq 0x1d720
addq $0xc0, %rsp
popq %rbp
retq
nopw (%rax,%rax)