// File: davidgfnet[P]dns-db/dns_block.cc
// Function: DNS_DB::DnsBlock::addDomainIpv4_int(char const*, IPv4_Record const&, bool)
bool DNS_DB::DnsBlock::addDomainIpv4_int(const char * domint, const IPv4_Record & iprec, bool ret) {
	DNS_DB::DnsBlock::InternalBlock * ptr = lookupDomain(domint);
	if (ptr == 0)
		return false;

	// Take a look to see whether we can make use of an existing record chain
	do {
		if (ptr->header & DNS_DB::DnsBlock::flagDomain) {
			for (int i = 0; i < 2; i++)
				if (ptr->data.domain.records[i].ip == 0) {
					ptr->data.domain.records[i] = iprec;
					return true;
				}
		}
		else {
			for (int i = 0; i < 5; i++)
				if (ptr->data.records.records[i].ip == 0) {
					ptr->data.records.records[i] = iprec;
					return true;
				}
		}

		ptr++;
	} while ( ptr != endptr && (ptr->header & DNS_DB::DnsBlock::flagUsed) && !(ptr->header & DNS_DB::DnsBlock::flagDomain));

	// We reached here, there is no block which can be reused, try to allocate
	// the next block
	if (ptr != endptr && !(ptr->header & DNS_DB::DnsBlock::flagUsed)) {
		memset(ptr, 0, sizeof(InternalBlock));
		ptr->header = DNS_DB::DnsBlock::flagUsed;
		ptr->data.records.records[0] = iprec;
		updateBM(); // FIXME do this with bit set!
		return true;
	}
	else if (ret) {
		// Try to move blocks away, only once!
		makeRoomMove(domint);
		// Try again now
		return addDomainIpv4_int(domint, iprec, false);
	}
	return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movb %cl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movq -0x18(%rbp), %rsi
callq 0xacd0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xb880
movb $0x0, -0x1(%rbp)
jmp 0xba5e
jmp 0xb882
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movzbl 0x10a79(%rip), %ecx     # 0x1c309
andl %ecx, %eax
cmpl $0x0, %eax
je 0xb900
movl $0x0, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
jge 0xb8fe
movq -0x30(%rbp), %rax
addq $0x1, %rax
addq $0x23, %rax
movslq -0x34(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jne 0xb8f1
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
addq $0x1, %rax
addq $0x23, %rax
movslq -0x34(%rbp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
movb $0x1, -0x1(%rbp)
jmp 0xba5e
jmp 0xb8f3
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xb89e
jmp 0xb969
movl $0x0, -0x38(%rbp)
cmpl $0x5, -0x38(%rbp)
jge 0xb967
movq -0x30(%rbp), %rax
addq $0x1, %rax
addq $0x3, %rax
movslq -0x38(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jne 0xb95a
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
addq $0x1, %rax
addq $0x3, %rax
movslq -0x38(%rbp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
movb $0x1, -0x1(%rbp)
jmp 0xba5e
jmp 0xb95c
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xb907
jmp 0xb969
movq -0x30(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0x41(%rbp)
je 0xb9bd
movq -0x30(%rbp), %rax
movzbl (%rax), %ecx
movzbl 0x10972(%rip), %eax     # 0x1c308
andl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x41(%rbp)
je 0xb9bd
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movzbl 0x10959(%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 0xb882
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
cmpq 0x8(%rcx), %rax
je 0xba2b
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movzbl 0x10924(%rip), %ecx     # 0x1c308
andl %ecx, %eax
cmpl $0x0, %eax
jne 0xba2b
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x4180
movq -0x40(%rbp), %rdi
movb 0x10903(%rip), %cl      # 0x1c308
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, 0x4(%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0xc(%rax)
callq 0xab10
movb $0x1, -0x1(%rbp)
jmp 0xba5e
testb $0x1, -0x21(%rbp)
je 0xba58
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xb300
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0xb840
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0xba5e
jmp 0xba5a
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)