// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx), %r14
andq $-0x10, %r14
leaq 0x40(%rsp), %rdi
movq $0x0, 0x10(%rsp)
movl $0x200, %r12d           # imm = 0x200
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3170
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r14, %rsi
callq 0xbaee
movq (%r15), %r13
orq $0x2, %r14
movq %r14, (%r13)
movq $0x0, 0x8(%rsp)
cmpq 0x10(%rbx), %r12
jae 0x132f8
movq 0x8(%rbx), %rdi
movq (%rdi,%r12), %rcx
movl %ecx, %eax
andl $0xf, %eax
movq %rcx, %rdx
andq $-0x10, %rdx
movq %r12, %rbp
negq %rbp
andq %r12, %rbp
testq %rcx, %rcx
cmovneq %rdx, %rbp
cmpq $0x2, %rax
je 0x132a7
cmpl $0x1, %eax
je 0x13246
testq %rax, %rax
jne 0x132f0
bsrq %rbp, %r14
xorl $0x3f, %r14d
shll $0x3, %r14d
xorq $0x1f8, %r14            # imm = 0x1F8
movq 0x40(%rsp,%r14), %r15
cmpq $0x2, %r15
jb 0x132cd
leaq 0x10(%r15), %rsi
leaq 0x8(%r12), %rdx
movq %rbx, %rdi
callq 0x13fc2
jmp 0x132d0
addq %r12, %rdi
movq 0x8(%rdi), %rsi
addq $0x18, %rdi
movq %rdi, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
callq 0x13380
movq %rax, %r14
movq 0x28(%rsp), %r15
addq $-0x10, %r15
andq %rax, %r15
movq (%r13,%r15), %rax
testq %rax, %rax
je 0x132bc
cmpq %r14, 0x8(%r13,%r15)
jne 0x1329e
movq 0x8(%rbx), %rcx
leaq (%rcx,%rax), %rsi
addq $0x18, %rsi
movq -0x10(%rsi), %rdx
leaq 0x30(%rsp), %rdi
callq 0x4338
testb %al, %al
jne 0x132eb
addq $0x10, %r15
movq %r15, %rax
jmp 0x13263
cmpq 0x18(%rsp), %r12
movq 0x8(%rsp), %rax
cmovneq %r12, %rax
movq %rax, 0x8(%rsp)
jmp 0x132f0
incq 0x10(%rsp)
movq %r12, (%r13,%r15)
movq %r14, 0x8(%r13,%r15)
jmp 0x132f0
xorl %r15d, %r15d
movq %r12, 0x40(%rsp,%r14)
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %r14, %r8
callq 0x13fe2
jmp 0x132f0
movq %r12, 0x8(%rsp)
addq %rbp, %r12
jmp 0x131c8
leaq 0x238(%rsp), %rax
movq 0x8(%rsp), %r15
cmpq $0x0, (%rax)
jne 0x13318
movq $0x1, (%rax)
addq $-0x8, %rax
jmp 0x13305
leaq 0x58(%rsp), %rdx
movq 0x10(%rsp), %rax
movq %rax, (%rdx)
movq %rax, 0x8(%rdx)
leaq 0x4(%rbx), %r14
pushq $0x18
popq %rsi
movl $0x1e8, %ecx            # imm = 0x1E8
movq %r14, %rdi
callq 0x13f72
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x13f72
testq %r15, %r15
je 0x13364
movq %rbx, %rdi
movq %r15, %rsi
callq 0x13390
movq 0x20(%rsp), %rdi
callq 0x3430
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq