// File: /workspace/github_cpp_makefile_star2_O2/Alcaro[P]bridges/arlib/staticmap.cpp
// Function: staticmap::fsck()
void staticmap::fsck()
{
	array<uint64_t> objects_file;
	array<uint64_t> objects_reachable;
	uint64_t pos = 0;
	while (pos != mmap.size())
	{
		objects_file.append(pos);
		uint64_t head = rd64(pos);
		uint64_t size = head & ~t_typemask;
		if (size & (size-1)) abort(); // size must be power of two
		if ((size-1) & pos) abort(); // every object must be correctly aligned
		if (size < 32) abort(); // size must be at least 32
		pos += size;
	}
	
	objects_reachable.append(0);
	if (rd64(0) != off_r_objsize + t_meta) abort();
	if (rd64(off_r_sig) != sig_passive && rd64(off_r_sig) != sig_active) abort();
	
	uint64_t hashmap_pos = rd64(off_r_hm_ptr);
	uint64_t hashmap_head = rd64(hashmap_pos + off_header);
	objects_reachable.append(hashmap_pos);
	if ((hashmap_head & t_typemask) != t_meta) abort();
	uint64_t hashmap_size = hashmap_head & ~t_typemask;
	if (hashmap_size < 512) abort();
	
	set<bytesr> keys_seen;
	uint64_t hashmap_load = 0;
	uint64_t hashmap_items = 0;
	uint64_t hashmap_iter = 16;
	while (hashmap_iter < hashmap_size)
	{
		uint64_t ptr = rd64(hashmap_pos + hashmap_iter + off_h_ptr);
		uint64_t hash = rd64(hashmap_pos + hashmap_iter + off_h_hash);
		hashmap_iter += 16;
		
		if (ptr >= 1) hashmap_load++;
		if (ptr <= 1) continue;
		hashmap_items++;
		
		objects_reachable.append(ptr);
		uint64_t obj_head = rd64(ptr + off_header);
		if ((obj_head & t_typemask) != t_data) abort();
		uint64_t obj_size = obj_head & ~t_typemask;
		uint64_t key_size = rd64(ptr+off_d_keysz);
		uint64_t val_size = rd64(ptr+off_d_valsz);
		if (off_d_keystart+key_size+val_size > obj_size) abort();
		if (off_d_keystart+key_size+val_size <= obj_size/2) abort();
		
		bytesr by = mmap.slice(ptr+off_d_keystart, key_size);
		if (keys_seen.contains(by)) abort();
		keys_seen.add(by);
		if (hash != sm_hash(by)) abort();
	}
	
	if (hashmap_load != rd64(off_r_hm_load)) abort();
	if (hashmap_items != rd64(off_r_entries)) abort();
	
	bool last = false;
	for (int i=5;i<=63;i++)
	{
		uint64_t prev = off_r_freespace(i);
		uint64_t ptr = rd64(prev);
		if (ptr == 1) last = true;
		if (ptr == 0 && last) abort();
		if (ptr > 1)
		{
		again:
			objects_reachable.append(ptr);
			uint64_t size = uint64_t(1)<<i;
			uint64_t sibling = ptr ^ size;
			if (rd64(ptr + off_header) != size+t_free) abort();
			if (rd64(ptr + off_header) == rd64(sibling + off_header)) abort(); // should've been merged
			if (rd64(ptr + off_f_prevptr) != prev) abort();
			uint64_t next = rd64(ptr + off_f_nextobj);
			prev = ptr + off_f_nextobj;
			
			if (next != 0)
			{
				ptr = next;
				goto again;
			}
		}
	}
	
	objects_file.sort();
	objects_reachable.sort();
	if (objects_file != objects_reachable) abort();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r14
movaps %xmm0, (%r14)
movaps %xmm0, 0x20(%rsp)
xorl %eax, %eax
leaq 0x40(%rsp), %r15
movq %rax, 0x40(%rsp)
cmpq 0x10(%rbx), %rax
je 0x13c74
movq %r14, %rdi
movq %r15, %rsi
callq 0xdc48
movq 0x40(%rsp), %rcx
movq 0x8(%rbx), %rax
movq (%rax,%rcx), %rdx
movq %rdx, %rax
andq $-0x10, %rax
leaq -0x1(%rax), %rsi
testq %rsi, %rax
jne 0x13c6f
testq %rcx, %rsi
jne 0x13c6f
addq %rcx, %rax
cmpq $0x1f, %rdx
ja 0x13c2e
callq 0x3180
leaq 0x60(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x20(%rsp), %rdi
callq 0xdc3c
movq 0x8(%rbx), %rax
cmpq $0x202, (%rax)          # imm = 0x202
jne 0x13c6f
movabsq $0x100000000000000, %rcx # imm = 0x100000000000000
orq 0x8(%rax), %rcx
movabsq $0x170616d736c7261, %rdx # imm = 0x170616D736C7261
cmpq %rdx, %rcx
jne 0x13c6f
movq 0x10(%rax), %rcx
leaq 0x48(%rsp), %rsi
movq %rcx, (%rsi)
movq (%rax,%rcx), %rbp
leaq 0x20(%rsp), %rdi
callq 0xdc48
movl %ebp, %eax
andl $0xf, %eax
cmpl $0x2, %eax
jne 0x13c6f
cmpq $0x1ff, %rbp            # imm = 0x1FF
jbe 0x13c6f
andq $-0x10, %rbp
leaq 0x60(%rsp), %rdi
callq 0x140ae
pushq $0x10
popq %r12
xorl %edx, %edx
xorl %r14d, %r14d
movq %rbp, 0x10(%rsp)
cmpq %rbp, %r12
jae 0x13de9
movq 0x8(%rbx), %rax
addq 0x48(%rsp), %rax
movq (%r12,%rax), %rcx
movq %rcx, 0x38(%rsp)
cmpq $0x1, %rcx
sbbq $-0x1, %r14
cmpq $0x2, %rcx
jb 0x13de0
movq %rdx, 0x18(%rsp)
movq 0x8(%r12,%rax), %r15
leaq 0x20(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xdc48
movq 0x38(%rsp), %rdx
movq 0x8(%rbx), %rax
movq (%rax,%rdx), %rcx
movl %ecx, %esi
andl $0xf, %esi
cmpl $0x1, %esi
jne 0x13c6f
addq %rdx, %rax
movq %rcx, %rdi
andq $-0x10, %rdi
movq 0x8(%rax), %rdx
movq 0x10(%rax), %rsi
addq %rdx, %rsi
addq $0x18, %rsi
cmpq %rdi, %rsi
ja 0x13c6f
shrq %rcx
cmpq %rcx, %rsi
leaq 0x60(%rsp), %rbp
jbe 0x13c6f
addq $0x18, %rax
movq %rax, (%rsp)
movq %rdx, 0x8(%rsp)
movq %rbp, %rdi
movq %rsp, %r13
movq %r13, %rsi
callq 0x140ba
testb %al, %al
jne 0x13c6f
movq %rbp, %rdi
movq %r13, %rsi
callq 0x140ca
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x13380
cmpq %rax, %r15
jne 0x13c6f
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x10(%rsp), %rbp
addq $0x10, %r12
jmp 0x13cfa
movq 0x8(%rbx), %rax
cmpq 0x18(%rax), %r14
jne 0x13c6f
cmpq 0x20(%rax), %rdx
jne 0x13c6f
pushq $0x5
popq %r14
xorl %edx, %edx
leaq 0x20(%rsp), %r15
movq %rsp, %r12
cmpq $0x40, %r14
je 0x13eb2
movq (%rax,%r14,8), %rcx
movq %rcx, (%rsp)
cmpq $0x1, %rcx
sete 0x10(%rsp)
testq %rcx, %rcx
movl %edx, 0x18(%rsp)
je 0x13e99
cmpq $0x1, %rcx
je 0x13ea2
leaq (,%r14,8), %r13
pushq $0x1
popq %rbp
movl %r14d, %ecx
shlq %cl, %rbp
movq %r15, %rdi
movq %r12, %rsi
callq 0xdc48
movq (%rsp), %rcx
movq 0x8(%rbx), %rax
cmpq %rbp, (%rax,%rcx)
jne 0x13c6f
movq %rcx, %rdx
xorq %rbp, %rdx
cmpq (%rax,%rdx), %rbp
je 0x13c6f
cmpq %r13, 0x10(%rax,%rcx)
jne 0x13c6f
movq 0x8(%rax,%rcx), %rdx
testq %rdx, %rdx
je 0x13ea2
addq $0x8, %rcx
movq %rdx, (%rsp)
movq %rcx, %r13
jmp 0x13e4a
testb $0x1, %dl
jne 0x13c6f
movl 0x18(%rsp), %edx
orb 0x10(%rsp), %dl
incq %r14
jmp 0x13e0f
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x140d2
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x140d2
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0x140e0
testb %al, %al
jne 0x13c6f
leaq 0x60(%rsp), %rdi
callq 0x140ea
movq 0x20(%rsp), %rdi
callq 0x3430
movq 0x50(%rsp), %rdi
callq 0x3430
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop