// File: Alcaro[P]bridges/arlib/staticmap.cpp
// Function: staticmap::free(unsigned long)
void staticmap::free(uint64_t addr)
{
//printf("free %lu\n", addr);
	uint64_t size = rd64(addr+off_header) & ~t_typemask;
again:
	uint64_t other = addr ^ size;
	if (rd64(other+off_header) == size + t_free)
	{
		uint64_t next_obj = rd64(other+off_f_nextobj);
		f.pwrite(rd64(other+off_f_prevptr), mmap.slice(other+off_f_nextobj, 8));  // *(other.prev_ptr) = other.next_obj;
		if (next_obj)                                                             // if (other.next_obj)
			f.pwrite(next_obj+off_f_prevptr, mmap.slice(other+off_f_prevptr, 8)); //   other.next_obj->prev_ptr = other.prev_ptr;
		
		addr &= ~size;
		size *= 2;
		goto again;
	}
	
	uint64_t bucket_pos = off_r_freespace(ilog2(size));
	// bucket_pos can be 1 if the freed space is combined with a sibling, such that the entire right half of the file is now free
	uint64_t prev_free = rd64(bucket_pos) & ~1;
	if (prev_free > 1)
		wr64(prev_free+off_f_prevptr, addr+off_f_nextobj);
	wr64(addr, size+t_free, prev_free, bucket_pos);
	wr64(bucket_pos, addr);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x80(%rbp)
movq -0x10(%rbp), %rsi
addq $0x0, %rsi
callq 0x353f0
andq $-0x10, %rax
movq %rax, -0x18(%rbp)
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
xorq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
addq $0x0, %rsi
callq 0x353f0
movq -0x18(%rbp), %rcx
addq $0x0, %rcx
cmpq %rcx, %rax
jne 0x338cf
movq -0x80(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x8, %rsi
callq 0x353f0
movq -0x80(%rbp), %rdi
movq %rax, -0x28(%rbp)
movq %rdi, %rax
addq $0x4, %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rsi
addq $0x10, %rsi
callq 0x353f0
movq -0x80(%rbp), %rsi
movq %rax, -0x88(%rbp)
addq $0x8, %rsi
movq -0x20(%rbp), %rdx
addq $0x8, %rdx
leaq -0x48(%rbp), %rdi
movl $0x8, %ecx
callq 0xc4c0
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rsi
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x35470
cmpq $0x0, -0x28(%rbp)
je 0x338af
movq -0x80(%rbp), %rsi
movq %rsi, %rax
addq $0x4, %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x98(%rbp)
addq $0x8, %rsi
movq -0x20(%rbp), %rdx
addq $0x10, %rdx
leaq -0x68(%rbp), %rdi
movl $0x8, %ecx
callq 0xc4c0
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x35470
movq -0x18(%rbp), %rax
xorq $-0x1, %rax
andq -0x10(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x18(%rbp), %rax
shlq %rax
movq %rax, -0x18(%rbp)
jmp 0x33790
movq -0x18(%rbp), %rdi
callq 0x336f0
movzbl %al, %eax
movl %eax, %edi
callq 0x336d0
movq -0x80(%rbp), %rdi
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rsi
callq 0x353f0
andq $-0x2, %rax
movq %rax, -0x78(%rbp)
cmpq $0x1, -0x78(%rbp)
jbe 0x3391b
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
addq $0x10, %rsi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
callq 0x356d0
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
addq $0x0, %rdx
movq -0x78(%rbp), %rcx
movq -0x70(%rbp), %r8
callq 0x35770
movq -0x80(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0x356d0
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)