// File: davidgfnet[P]dns-db/dns_block.cc
// Function: DNS_DB::DnsBlock::makeRoomMove(char const*)
void DNS_DB::DnsBlock::makeRoomMove(const char * domain) {
	// Assume we do not have an empty spot for ourselves. Therefore
	// look for the place we should be and move all the nodes down one
	// position
	int p = 0;

	int res = lookupEmptyDomainSpot(domain, &p);
	assert(res == NO_EMPTY_SPOT || res == ALREADY_EXISTS);

	for (p; p < numBlocks; p++) {
		DNS_DB::DnsBlock::InternalBlock * ptr = &blockptr[p];
		// Stop after we find our spot
		if ((ptr->header & flagUsed) && (ptr->header & DNS_DB::DnsBlock::flagDomain)) {
			if ( !less(ptr->data.domain.domain, domain) ) {
				// If the domain exists move to the last block of it
				if (memcmp(ptr->data.domain.domain, domain, MAX_DNS_SIZE) == 0) {
					do {
						p++;
						ptr = &blockptr[p];
					} while (ptr != endptr && (ptr->header & DNS_DB::DnsBlock::flagUsed) && !(ptr->header & DNS_DB::DnsBlock::flagDomain));
				}
				break;
			}
		}
	}

	// We should not have space after us, otherwise why are we getting called?
	assert((p == numBlocks) || (blockptr[p].header & DNS_DB::DnsBlock::flagDomain));

	if (p == numBlocks)
		return;

	#ifdef EXTRA_CHECK
	checkBM();
	#endif

	// Look for the first empty slot, and move the N slots to make room
	int tomove = 1;
	InternalBlock * ptrl = &blockptr[p+1];
	while (ptrl != endptr) {
		if (!(ptrl->header & flagUsed)) {
			// Move N consec elements down, to make room at ptr
			memmove(&blockptr[p+1], &blockptr[p], tomove*sizeof(InternalBlock));
			memset (&blockptr[p], 0, sizeof(InternalBlock));
			break;
		}
		else
			tomove++;
			
		ptrl++;
	}
	
	if (ptrl == endptr)
		return;

	// Update the bitmask
	bitmap->setBit(p+tomove, true);
	bitmap->setBit(p, false);

	#ifdef EXTRA_CHECK
	checkBM();
	#endif

	// It might happen that we do not have room :(
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x14(%rbp), %rdx
callq 0xadc0
movl %eax, -0x18(%rbp)
movb $0x1, %al
cmpl $-0x1, -0x18(%rbp)
movb %al, -0x31(%rbp)
je 0xb344
cmpl $-0x2, -0x18(%rbp)
sete %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %al
testb $0x1, %al
jne 0xb34d
jmp 0xb34f
jmp 0xb36e
leaq 0x820c(%rip), %rdi      # 0x13562
leaq 0x7f6c(%rip), %rsi      # 0x132c9
movl $0x162, %edx            # imm = 0x162
leaq 0x8227(%rip), %rcx      # 0x13590
callq 0x41e0
jmp 0xb370
movl -0x14(%rbp), %eax
cmpl 0x10f97(%rip), %eax     # 0x1c310
jae 0xb487
movq -0x40(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
movzbl 0x10f65(%rip), %ecx     # 0x1c308
andl %ecx, %eax
cmpl $0x0, %eax
je 0xb477
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
movzbl 0x10f4d(%rip), %ecx     # 0x1c309
andl %ecx, %eax
cmpl $0x0, %eax
je 0xb477
movq -0x20(%rbp), %rdi
addq $0x1, %rdi
movq -0x10(%rbp), %rsi
movl $0x23, %edx
callq 0x4150
cmpl $0x0, %eax
jl 0xb475
movq -0x20(%rbp), %rdi
addq $0x1, %rdi
movq -0x10(%rbp), %rsi
movl $0x23, %edx
callq 0x4130
cmpl $0x0, %eax
jne 0xb473
jmp 0xb403
movq -0x40(%rbp), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x14(%rbp)
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x40(%rbp), %rdx
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0x41(%rbp)
je 0xb46a
movq -0x20(%rbp), %rax
movzbl (%rax), %ecx
movzbl 0x10ec5(%rip), %eax     # 0x1c308
andl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x41(%rbp)
je 0xb46a
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
movzbl 0x10eac(%rip), %ecx     # 0x1c309
andl %ecx, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0xb403
jmp 0xb473
jmp 0xb487
jmp 0xb477
jmp 0xb479
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xb370
movl -0x14(%rbp), %ecx
movb $0x1, %al
cmpl 0x10e7e(%rip), %ecx     # 0x1c310
movb %al, -0x42(%rbp)
je 0xb4be
movq -0x40(%rbp), %rax
movq (%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
movzbl 0x10e56(%rip), %ecx     # 0x1c309
andl %ecx, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x42(%rbp)
movb -0x42(%rbp), %al
testb $0x1, %al
jne 0xb4c7
jmp 0xb4c9
jmp 0xb4e8
leaq 0x80f2(%rip), %rdi      # 0x135c2
leaq 0x7df2(%rip), %rsi      # 0x132c9
movl $0x176, %edx            # imm = 0x176
leaq 0x80ad(%rip), %rcx      # 0x13590
callq 0x41e0
movl -0x14(%rbp), %eax
cmpl 0x10e1f(%rip), %eax     # 0x1c310
jne 0xb4f8
jmp 0xb5f7
movq -0x40(%rbp), %rax
movl $0x1, -0x24(%rbp)
movq (%rax), %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
cmpq 0x8(%rcx), %rax
je 0xb5ad
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movzbl 0x10dce(%rip), %ecx     # 0x1c308
andl %ecx, %eax
cmpl $0x0, %eax
jne 0xb593
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x6, %rcx
addq %rcx, %rdi
movq (%rax), %rsi
movslq -0x14(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rsi
movslq -0x24(%rbp), %rdx
shlq $0x6, %rdx
callq 0x4490
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movslq -0x14(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x4180
jmp 0xb5ad
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x30(%rbp)
jmp 0xb51a
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
cmpq 0x8(%rcx), %rax
jne 0xb5bd
jmp 0xb5f7
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x8a20
movq %rax, %rdi
movl -0x14(%rbp), %esi
addl -0x24(%rbp), %esi
movl $0x1, %edx
callq 0xc5d0
movq -0x40(%rbp), %rdi
addq $0x18, %rdi
callq 0x8a20
movq %rax, %rdi
movl -0x14(%rbp), %esi
xorl %edx, %edx
callq 0xc5d0
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)