// File: davidgfnet[P]dns-db/dns_index.cc
// Function: DNS_DB::DnsIndex::addDomain(char const*)
DNS_DB::queryError DNS_DB::DnsIndex::addDomain(const char * domain) {
	char domint[MAX_DNS_SIZE];
	if (!domain2idom(domain, domint))
		return resDomainTooLong;

	int n = lookupNode(domint);
	DNS_DB::DnsBlockPtr blk = database->getBlock(nodes[n].dnsblock_id);

	queryError res = blk->addDomain(domint);

	if (res == resNoSpaceLeft) {
		// Ops, just split the Block in two, must be full
		unsigned int nwblk_id = this->current_id++;
		DnsBlockPtr newblk = database->getBlock(nwblk_id);
		blk->splitBlock(domint, newblk);

		char nodemax[MAX_DNS_SIZE];
		char dommax [MAX_DNS_SIZE];
		getBlkMax(n, nodemax);
		newblk->getMinDomain(dommax);

		// Set new block boundaries
		addBlock(nwblk_id, dommax, nodemax);

		// Just recalculate the max for the other block
		setBlkMinMax(n, 0, dommax);

		// Redo
		n = lookupNode(domint);
		blk = database->getBlock(nodes[n].dnsblock_id);

		res = blk->addDomain(domint);
		assert(res != resNoSpaceLeft);
	}

	// Make sure the blog minimum is consistent
	#ifdef EXTRA_CHECK
	char tmpd[MAX_DNS_SIZE];
	blk->getIterator(database, 0).getDomain(tmpd);
	assert(less_eq(nodes[n].min, tmpd));

	// Make sure it is allright
	char prev[MAX_DNS_SIZE] = {0};
	DnsBlock::Iterator it = blk->getIterator(database, 0);
	while (!it.end()) {
		char curr[MAX_DNS_SIZE];
		it.getDomain(curr);
		assert(less_eq(prev, curr));
		memcpy(prev,curr,MAX_DNS_SIZE);
		it.next();
	}
	#endif

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xd720
testb $0x1, %al
jne 0x80fb
movl $0x3, -0x4(%rbp)
jmp 0x8338
movq -0xf8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq %rsi, -0x108(%rbp)
callq 0x7420
movq -0xf8(%rbp), %rdi
movl %eax, -0x44(%rbp)
movq 0x18(%rdi), %rax
movq %rax, -0x118(%rbp)
movslq -0x44(%rbp), %rsi
callq 0x8610
movq -0x118(%rbp), %rsi
movl 0x46(%rax), %edx
leaq -0x58(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x8650
movq -0x110(%rbp), %rdi
callq 0x8680
movq -0x108(%rbp), %rsi
movq %rax, %rdi
callq 0xb180
movl %eax, -0xfc(%rbp)
jmp 0x816d
movl -0xfc(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x1, -0x5c(%rbp)
jne 0x831c
movq -0xf8(%rbp), %rax
movl 0x20(%rax), %ecx
movl %ecx, %edx
incl %edx
movl %edx, 0x20(%rax)
movl %ecx, -0x70(%rbp)
movq 0x18(%rax), %rsi
movl -0x70(%rbp), %edx
leaq -0x80(%rbp), %rdi
callq 0x8650
jmp 0x81a6
leaq -0x58(%rbp), %rdi
callq 0x8680
movq %rax, %rdi
leaq -0x40(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0xba70
jmp 0x81c1
movq -0xf8(%rbp), %rdi
movl -0x44(%rbp), %esi
leaq -0xb0(%rbp), %rdx
callq 0x7de0
jmp 0x81d9
leaq -0x80(%rbp), %rdi
callq 0x8680
movq %rax, %rdi
leaq -0xe0(%rbp), %rsi
callq 0xbcd0
jmp 0x81f3
movq -0xf8(%rbp), %rdi
movl -0x70(%rbp), %esi
leaq -0xe0(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
callq 0x7e30
jmp 0x8212
movq -0xf8(%rbp), %rdi
movl -0x44(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
leaq -0xe0(%rbp), %rcx
callq 0x7f60
jmp 0x822e
movq -0xf8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x7420
movq -0xf8(%rbp), %rdi
movl %eax, -0x44(%rbp)
movq 0x18(%rdi), %rax
movq %rax, -0x120(%rbp)
movslq -0x44(%rbp), %rsi
callq 0x8610
movq -0x120(%rbp), %rsi
movl 0x46(%rax), %edx
leaq -0xf0(%rbp), %rdi
callq 0x8650
jmp 0x8274
leaq -0x58(%rbp), %rdi
movq %rdi, -0x130(%rbp)
leaq -0xf0(%rbp), %rsi
movq %rsi, -0x138(%rbp)
callq 0x8760
movq -0x138(%rbp), %rdi
callq 0x5f20
movq -0x130(%rbp), %rdi
callq 0x8680
movq %rax, %rdi
leaq -0x40(%rbp), %rsi
callq 0xb180
movl %eax, -0x124(%rbp)
jmp 0x82be
movl -0x124(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x1, -0x5c(%rbp)
je 0x82cf
jmp 0x8313
leaq 0xaf18(%rip), %rdi      # 0x131ee
leaq 0xae3d(%rip), %rsi      # 0x1311a
movl $0xce, %edx
leaq 0xaf1b(%rip), %rcx      # 0x13204
callq 0x41e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x832d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x5f20
jmp 0x832d
leaq -0x80(%rbp), %rdi
callq 0x5f20
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5f20
jmp 0x8338
leaq -0x58(%rbp), %rdi
callq 0x5f20
jmp 0x8344
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x44f0
nopl (%rax)