// File: Alcaro[P]bridges/arlib/staticmap.cpp
// Function: staticmap::recover()
void staticmap::recover()
{
	// at unclean shutdown,
	// - hashmap contents are untrustworthy
	// - freelist contents are untrustworthy
	// - hashmap size/load may be off by one
	// however, we still know that
	// - every object is valid and contains its size
	// - every data object contains proper data
	// - there are no mergeable freelist objects
	// - the hashmap pointer is valid
	//     though there may be an extra hashmap object at another address
	
	uint64_t hashmap_pos = rd64(off_r_hm_ptr);
	uint64_t hashmap_size = rd64(hashmap_pos+off_header) & ~t_typemask;
	uint64_t free_this = 0; // either a duplicate hashmap or a duplicate data node; there can only be one
	
	uint8_t new_root[off_r_objsize];
	memset(new_root, 0, sizeof(new_root));
	
	bytearray new_map_buf;
	new_map_buf.resize(hashmap_size);
	uint8_t* new_map = new_map_buf.ptr();
	wr64(new_map, hashmap_size+t_meta);
	
	uint64_t num_obj = 0;
	uint64_t pos = off_r_objsize;
	while (pos < mmap.size())
	{
		uint64_t head = rd64(pos+off_header);
		uint64_t type = head & t_typemask;
		uint64_t size = head & ~t_typemask;
		if (head == 0) // if recently expanded, create a freelist object here
			size = (pos & -pos); // this will blow up if the file size can be not power of two
		if (type == t_free)
		{
			uint64_t freelist_ptr = off_r_freespace(ilog2(size));
			uint64_t prev_obj = readu_le64(new_root+freelist_ptr);
			if (prev_obj > 1)
				wr64(prev_obj+off_f_prevptr, pos+off_f_nextobj);
			else
				prev_obj = 0;
			wr64(new_root+freelist_ptr, pos);
			wr64(pos, size+t_free, prev_obj, freelist_ptr);
		}
		else if (type == t_data)
		{
			uint64_t keylen = rd64(pos+off_d_keysz);
			bytesr key = mmap.slice(pos+off_d_keystart, keylen);
			uint64_t hash = sm_hash(key);
			
			uint64_t dst_off = hash & (new_map_buf.size()-off_h_nodesize);
			while (true)
			{
				// this reads the object header if dst_off = 0; this is fine, it's nonzero
				uint64_t node_addr = readu_le64(new_map+dst_off+off_h_ptr);
				if (node_addr == 0)
					break;
				if (readu_le64(new_map+dst_off+off_h_hash) == hash)
				{
					bytesr node_key = mmap.slice(node_addr+off_d_keystart, rd64(node_addr+off_d_keysz));
					if (key == node_key)
					{
						free_this = pos;
						goto skip;
					}
				}
				dst_off = (dst_off+off_h_nodesize) & (new_map_buf.size()-off_h_nodesize);
			}
			num_obj++;
			wr64(new_map+dst_off, pos, hash);
		skip: ;
		}
		else if (type == t_meta && pos != hashmap_pos)
		{
			free_this = pos;
		}
		// else it's the actual hashmap, so do nothing
		pos += size;
	}
	for (int i=63;readu_le64(new_root+off_r_freespace(i))==0;i--)
		writeu_le64(new_root+off_r_freespace(i), 1);
	wr64(new_root+off_r_hm_load, num_obj);
	wr64(new_root+off_r_entries, num_obj);
	f.pwrite(off_r_hm_load, bytesr(new_root).skip(off_r_hm_load));
	f.pwrite(hashmap_pos, new_map_buf);
	
	if (free_this != 0)
		free(free_this);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3c0, %rsp            # imm = 0x3C0
movq %rdi, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq %rdi, -0x380(%rbp)
movl $0x10, %esi
callq 0x353f0
movq -0x380(%rbp), %rdi
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rsi
addq $0x0, %rsi
callq 0x353f0
andq $-0x10, %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
leaq -0x270(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x4170
leaq -0x280(%rbp), %rdi
callq 0x74b0
movq -0x68(%rbp), %rsi
leaq -0x280(%rbp), %rdi
callq 0x1ce60
leaq -0x280(%rbp), %rdi
callq 0x7490
movq -0x380(%rbp), %rdi
movq %rax, -0x288(%rbp)
movq -0x288(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq $0x2, %rdx
callq 0x35670
movq $0x0, -0x290(%rbp)
movq $0x200, -0x298(%rbp) # imm = 0x200
movq -0x380(%rbp), %rdi
movq -0x298(%rbp), %rax
movq %rax, -0x388(%rbp)
addq $0x8, %rdi
callq 0x6290
movq %rax, %rcx
movq -0x388(%rbp), %rax
cmpq %rcx, %rax
jae 0x3351a
movq -0x380(%rbp), %rdi
movq -0x298(%rbp), %rsi
addq $0x0, %rsi
callq 0x353f0
movq %rax, -0x2a0(%rbp)
movq -0x2a0(%rbp), %rax
andq $0xf, %rax
movq %rax, -0x2a8(%rbp)
movq -0x2a0(%rbp), %rax
andq $-0x10, %rax
movq %rax, -0x2b0(%rbp)
cmpq $0x0, -0x2a0(%rbp)
jne 0x33186
movq -0x298(%rbp), %rax
xorl %ecx, %ecx
subq -0x298(%rbp), %rcx
andq %rcx, %rax
movq %rax, -0x2b0(%rbp)
cmpq $0x0, -0x2a8(%rbp)
jne 0x33264
movq -0x2b0(%rbp), %rdi
callq 0x336f0
movzbl %al, %eax
movl %eax, %edi
callq 0x336d0
movq %rax, -0x2b8(%rbp)
leaq -0x270(%rbp), %rax
addq -0x2b8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2c0(%rbp)
cmpq $0x1, -0x2c0(%rbp)
jbe 0x33207
movq -0x380(%rbp), %rdi
movq -0x2c0(%rbp), %rsi
addq $0x10, %rsi
movq -0x298(%rbp), %rdx
addq $0x8, %rdx
callq 0x356d0
jmp 0x33212
movq $0x0, -0x2c0(%rbp)
movq -0x380(%rbp), %rdi
leaq -0x270(%rbp), %rsi
addq -0x2b8(%rbp), %rsi
movq -0x298(%rbp), %rdx
callq 0x35670
movq -0x380(%rbp), %rdi
movq -0x298(%rbp), %rsi
movq -0x2b0(%rbp), %rdx
addq $0x0, %rdx
movq -0x2c0(%rbp), %rcx
movq -0x2b8(%rbp), %r8
callq 0x35770
jmp 0x33500
cmpq $0x1, -0x2a8(%rbp)
jne 0x334da
movq -0x380(%rbp), %rdi
movq -0x298(%rbp), %rsi
addq $0x8, %rsi
callq 0x353f0
movq -0x380(%rbp), %rsi
movq %rax, -0x2c8(%rbp)
addq $0x8, %rsi
movq -0x298(%rbp), %rdx
addq $0x18, %rdx
movq -0x2c8(%rbp), %rcx
leaq -0x2e8(%rbp), %rdi
callq 0xc4c0
movq -0x2e8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq -0x2e0(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movq -0x2d8(%rbp), %rax
movq %rax, -0x300(%rbp)
movq -0x2d0(%rbp), %rax
movq %rax, -0x2f8(%rbp)
movq -0x300(%rbp), %rdi
movq -0x2f8(%rbp), %rsi
callq 0x33710
movq %rax, -0x2f0(%rbp)
movq -0x2f0(%rbp), %rax
movq %rax, -0x390(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0x390(%rbp), %rax
subq $0x10, %rcx
andq %rcx, %rax
movq %rax, -0x308(%rbp)
jmp 0x3333f
movq -0x288(%rbp), %rax
addq -0x308(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x310(%rbp)
cmpq $0x0, -0x310(%rbp)
jne 0x33376
jmp 0x3349e
movq -0x288(%rbp), %rax
addq -0x308(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x2f0(%rbp), %rax
jne 0x33463
movq -0x380(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x3a0(%rbp)
movq -0x310(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x398(%rbp)
movq -0x310(%rbp), %rsi
addq $0x8, %rsi
callq 0x353f0
movq -0x3a0(%rbp), %rsi
movq -0x398(%rbp), %rdx
movq %rax, %rcx
leaq -0x330(%rbp), %rdi
callq 0xc4c0
movq -0x330(%rbp), %rax
movq %rax, -0x320(%rbp)
movq -0x328(%rbp), %rax
movq %rax, -0x318(%rbp)
movq -0x320(%rbp), %rax
movq %rax, -0x340(%rbp)
movq -0x318(%rbp), %rax
movq %rax, -0x338(%rbp)
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
leaq -0x2d8(%rbp), %rdi
callq 0x60c0
testb $0x1, %al
jne 0x33454
jmp 0x33461
movq -0x298(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x334d8
jmp 0x33463
movq -0x308(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x3a8(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0x3a8(%rbp), %rax
subq $0x10, %rcx
andq %rcx, %rax
movq %rax, -0x308(%rbp)
jmp 0x3333d
movq -0x380(%rbp), %rdi
movq -0x290(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x290(%rbp)
movq -0x288(%rbp), %rsi
addq -0x308(%rbp), %rsi
movq -0x298(%rbp), %rdx
movq -0x2f0(%rbp), %rcx
callq 0x358a0
jmp 0x334fe
cmpq $0x2, -0x2a8(%rbp)
jne 0x334fc
movq -0x298(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x334fc
movq -0x298(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x334fe
jmp 0x33500
movq -0x2b0(%rbp), %rax
addq -0x298(%rbp), %rax
movq %rax, -0x298(%rbp)
jmp 0x330ef
movl $0x3f, -0x344(%rbp)
leaq -0x270(%rbp), %rax
movq %rax, -0x3b0(%rbp)
movslq -0x344(%rbp), %rdi
callq 0x336d0
movq %rax, %rcx
movq -0x3b0(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x335bb
leaq -0x270(%rbp), %rax
movq %rax, -0x3b8(%rbp)
movslq -0x344(%rbp), %rdi
callq 0x336d0
movq %rax, %rcx
movq -0x3b8(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq $0x1, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, (%rax)
movl -0x344(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x344(%rbp)
jmp 0x33524
movq -0x380(%rbp), %rdi
leaq -0x270(%rbp), %rsi
addq $0x18, %rsi
movq -0x290(%rbp), %rdx
callq 0x35670
movq -0x380(%rbp), %rdi
leaq -0x270(%rbp), %rsi
addq $0x20, %rsi
movq -0x290(%rbp), %rdx
callq 0x35670
movq -0x380(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x3c0(%rbp)
leaq -0x368(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x35930
leaq -0x368(%rbp), %rdi
movl $0x18, %esi
callq 0xc500
movq -0x3c0(%rbp), %rdi
movq %rax, -0x358(%rbp)
movq %rdx, -0x350(%rbp)
movq -0x358(%rbp), %rdx
movq -0x350(%rbp), %rcx
movl $0x18, %esi
callq 0x35470
movq -0x380(%rbp), %rdi
addq $0x4, %rdi
movq -0x60(%rbp), %rsi
movq -0x280(%rbp), %rax
movq %rax, -0x378(%rbp)
movq -0x278(%rbp), %rax
movq %rax, -0x370(%rbp)
movq -0x378(%rbp), %rdx
movq -0x370(%rbp), %rcx
callq 0x35470
cmpq $0x0, -0x70(%rbp)
je 0x336af
movq -0x380(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x33760
leaq -0x280(%rbp), %rdi
callq 0x1d720
addq $0x3c0, %rsp            # imm = 0x3C0
popq %rbp
retq
nopw %cs:(%rax,%rax)