// File: /workspace/github_cpp_makefile_star2_O1/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, %rbp
movq 0x8(%rdi), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx), %r15
andq $-0x10, %r15
leaq 0x70(%rsp), %rdi
movq $0x0, 0x8(%rsp)
movl $0x200, %r12d           # imm = 0x200
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3170
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r13
movaps %xmm0, (%r13)
movq %r13, %rdi
movq %r15, %rsi
callq 0xda72
movq (%r13), %r14
orq $0x2, %r15
movq %r15, (%r14)
cmpq $0x201, 0x10(%rbp)      # imm = 0x201
jb 0x17ac5
xorl %r8d, %r8d
movq %rbp, 0x10(%rsp)
movq 0x8(%rbp), %rax
movq (%rax,%r12), %rdx
movl %edx, %ecx
andl $0xf, %ecx
movq %rdx, %rsi
andq $-0x10, %rsi
movq %r12, %rdi
negq %rdi
andq %r12, %rdi
testq %rdx, %rdx
cmovneq %rsi, %rdi
cmpq $0x2, %rcx
je 0x17a75
cmpl $0x1, %ecx
je 0x17957
testq %rcx, %rcx
jne 0x17ab6
movq %r8, (%rsp)
bsrq %rdi, %r15
xorl $0x3f, %r15d
shll $0x3, %r15d
xorq $0x1f8, %r15            # imm = 0x1F8
movq 0x70(%rsp,%r15), %r13
cmpq $0x2, %r13
jb 0x17a80
leaq 0x10(%r13), %rcx
leaq 0x8(%r12), %rax
movq %rax, 0x30(%rsp)
movq %rdi, %rbx
movl 0x4(%rbp), %edi
movl $0x8, %edx
leaq 0x30(%rsp), %rsi
callq 0x30d0
movq %rbx, %rdi
jmp 0x17a83
movq %rdi, 0x50(%rsp)
leaq (%rax,%r12), %rdi
addq $0x18, %rdi
movq -0x10(%rdi), %r13
movq %rdi, (%rsp)
movq %r13, %rsi
movq %r8, %r15
callq 0x15897
movq %r15, %r8
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, %rbx
shrq $0x1f, %rbx
xorq %rax, %rbx
movq 0x28(%rsp), %rdi
addq $-0x10, %rdi
movq %rbx, %r15
andq %rdi, %r15
movq 0x8(%rbp), %r9
leaq 0x18(%r9), %rax
movq %rax, 0x58(%rsp)
movq %rdi, 0x68(%rsp)
movq %r13, 0x60(%rsp)
movq (%r14,%r15), %rsi
testq %rsi, %rsi
je 0x17a2b
cmpq %rbx, 0x8(%r14,%r15)
jne 0x17a49
cmpq 0x8(%r9,%rsi), %r13
jne 0x17a32
addq 0x58(%rsp), %rsi
movq (%rsp), %rdi
movq %r13, %rdx
movq %r14, %r13
movq %r8, %r14
movq %r12, %rbp
movq %r9, %r12
callq 0x3380
movq %r12, %r9
movq %rbp, %r12
movq 0x68(%rsp), %rdi
movq %r14, %r8
movq %r13, %r14
movq 0x60(%rsp), %r13
testl %eax, %eax
sete %cl
jmp 0x17a34
movl $0x5, %eax
jmp 0x17a52
xorl %ecx, %ecx
movl $0x0, %eax
testb %cl, %cl
movl $0x6, %ecx
cmovnel %ecx, %eax
cmovneq %r12, %r8
jne 0x17a52
addq $0x10, %r15
andq %rdi, %r15
xorl %eax, %eax
testl %eax, %eax
je 0x179da
cmpl $0x6, %eax
movq 0x50(%rsp), %rdi
je 0x17a6e
incq 0x8(%rsp)
movq %r12, (%r14,%r15)
movq %rbx, 0x8(%r14,%r15)
movq 0x10(%rsp), %rbp
jmp 0x17ab6
cmpq 0x18(%rsp), %r12
cmovneq %r12, %r8
jmp 0x17ab6
xorl %r13d, %r13d
leaq 0x30(%rsp), %rsi
movq %r12, 0x70(%rsp,%r15)
movq %rdi, 0x30(%rsp)
movq %r13, 0x38(%rsp)
movq %r15, 0x40(%rsp)
movq %rdi, %rbx
movl 0x4(%rbp), %edi
movl $0x18, %edx
movq %r12, %rcx
callq 0x30d0
movq %rbx, %rdi
movq (%rsp), %r8
addq %rdi, %r12
cmpq 0x10(%rbp), %r12
jb 0x178ca
jmp 0x17ac8
xorl %r8d, %r8d
movq %r8, %rbx
cmpq $0x0, 0x268(%rsp)
jne 0x17af0
leaq 0x268(%rsp), %rax
movq $0x1, (%rax)
cmpq $0x0, -0x8(%rax)
leaq -0x8(%rax), %rax
je 0x17ade
leaq 0x88(%rsp), %rsi
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
movq %rax, 0x8(%rsi)
movl 0x4(%rbp), %edi
movl $0x1e8, %edx            # imm = 0x1E8
movl $0x18, %ecx
callq 0x30d0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl 0x4(%rbp), %edi
movq 0x18(%rsp), %rcx
callq 0x30d0
testq %rbx, %rbx
je 0x17b3d
movq %rbx, %rsi
movq %rbp, %rdi
callq 0x17b5a
movq 0x20(%rsp), %rdi
callq 0x3430
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop