// File: /workspace/github_cpp_makefile_star2_O3/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 $0x278, %rsp            # imm = 0x278
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx), %r13
andq $-0x10, %r13
leaq 0x70(%rsp), %rdi
xorl %r14d, %r14d
movl $0x200, %r12d           # imm = 0x200
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3170
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rbp
movaps %xmm0, (%rbp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xdea4
movq (%rbp), %r15
movq %rbx, %rbp
orq $0x2, %r13
movq %r13, (%r15)
cmpq $0x201, 0x10(%rbx)      # imm = 0x201
jb 0x17dcc
movq $0x0, (%rsp)
movq $0x0, 0x18(%rsp)
movq %rbp, 0x8(%rsp)
movq 0x8(%rbp), %rdi
movq (%rdi,%r12), %rcx
movl %ecx, %eax
andl $0xf, %eax
movq %rcx, %rdx
andq $-0x10, %rdx
movq %r12, %rsi
negq %rsi
andq %r12, %rsi
testq %rcx, %rcx
cmovneq %rdx, %rsi
cmpq $0x2, %rax
je 0x17d72
cmpl $0x1, %eax
je 0x17c89
testq %rax, %rax
jne 0x17db7
bsrq %rsi, %rbx
xorl $0x3f, %ebx
shll $0x3, %ebx
xorq $0x1f8, %rbx            # imm = 0x1F8
movq 0x70(%rsp,%rbx), %r13
cmpq $0x2, %r13
jb 0x17d85
leaq 0x10(%r13), %rcx
leaq 0x8(%r12), %rax
movq %rax, 0x40(%rsp)
movl 0x4(%rbp), %edi
movl $0x8, %edx
movq %rsi, %r14
leaq 0x40(%rsp), %rsi
callq 0x30d0
movq %r14, %rsi
jmp 0x17d88
movq %rsi, 0x28(%rsp)
addq %r12, %rdi
movq 0x8(%rdi), %rsi
addq $0x18, %rdi
movq %rdi, 0x68(%rsp)
movq %rsi, 0x10(%rsp)
callq 0x15ad7
movq %rax, %rcx
shrq $0x1e, %rcx
xorq %rax, %rcx
movabsq $-0x40a7b892e31b1a47, %rax # imm = 0xBF58476D1CE4E5B9
imulq %rax, %rcx
movq %rcx, %rax
shrq $0x1b, %rax
xorq %rcx, %rax
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rcx, %rax
movq %rax, %r13
shrq $0x1f, %r13
xorq %rax, %r13
movq 0x38(%rsp), %rbp
addq $-0x10, %rbp
movq %r13, %rbx
andq %rbp, %rbx
movq (%r15,%rbx), %rsi
testq %rsi, %rsi
je 0x17d58
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
leaq 0x18(%rax), %rcx
movq %rcx, 0x60(%rsp)
movq 0x10(%rsp), %rcx
cmpq %r13, 0x8(%r15,%rbx)
jne 0x17d48
cmpq 0x8(%rax,%rsi), %rcx
jne 0x17d48
addq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq %r15, %r14
movq %rax, %r15
callq 0x3380
movq 0x10(%rsp), %rcx
testl %eax, %eax
movq %r15, %rax
movq %r14, %r15
je 0x17dc6
addq $0x10, %rbx
andq %rbp, %rbx
movq (%r15,%rbx), %rsi
testq %rsi, %rsi
jne 0x17d11
incq 0x18(%rsp)
movq %r12, (%r15,%rbx)
movq %r13, 0x8(%r15,%rbx)
movq 0x8(%rsp), %rbp
movq 0x28(%rsp), %rsi
jmp 0x17db7
cmpq 0x20(%rsp), %r12
movq (%rsp), %rax
cmovneq %r12, %rax
movq %rax, (%rsp)
jmp 0x17db7
xorl %r13d, %r13d
movq %r12, 0x70(%rsp,%rbx)
movq %rsi, 0x40(%rsp)
movq %r13, 0x48(%rsp)
movq %rbx, 0x50(%rsp)
movl 0x4(%rbp), %edi
movl $0x18, %edx
movq %rsi, %rbx
leaq 0x40(%rsp), %rsi
movq %r12, %rcx
callq 0x30d0
movq %rbx, %rsi
addq %rsi, %r12
cmpq 0x10(%rbp), %r12
jb 0x17c02
jmp 0x17dd0
movq %r12, (%rsp)
jmp 0x17d66
xorl %ecx, %ecx
jmp 0x17de4
cmpq $0x0, 0x268(%rsp)
movq (%rsp), %r14
movq 0x18(%rsp), %rcx
jne 0x17dfe
leaq 0x268(%rsp), %rax
movq $0x1, (%rax)
cmpq $0x0, -0x8(%rax)
leaq -0x8(%rax), %rax
je 0x17dec
leaq 0x88(%rsp), %rsi
movq %rcx, (%rsi)
movq %rcx, 0x8(%rsi)
movl 0x4(%rbp), %edi
movl $0x1e8, %edx            # imm = 0x1E8
movl $0x18, %ecx
callq 0x30d0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movl 0x4(%rbp), %edi
movq 0x20(%rsp), %rcx
callq 0x30d0
testq %r14, %r14
je 0x17e46
movq %rbp, %rdi
movq %r14, %rsi
callq 0x17e62
movq 0x30(%rsp), %rdi
callq 0x3430
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq