// File: davidgfnet[P]dns-db/main.cc
// Function: main
int main(int argc, char ** argv) {
	if (argc < 4) {
		fprintf(stderr, "Usage: %s dbpath command (args...)\n", argv[0]);
		fprintf(stderr, " Commands:\n");
		fprintf(stderr, "  * add-domains file\n");
		fprintf(stderr, "  * crawl bw(kbps)\n");
		exit(0);
	}

	signal(SIGTERM, sigterm);
	signal(SIGINT,  sigterm);

	std::string pathdb  = std::string(argv[1]);
	std::string command = std::string(argv[2]);
	std::string arg0    = std::string(argv[3]);

	DNS_DB db(pathdb);

	std::vector <std::string> check;
	
	if (command == "add-domains") {
		igzstream fin (arg0.c_str());
		std::string domain;
		while (fin >> domain && !doexit) {
			DNS_DB::queryError r = db.addDomain(domain);
			#ifdef EXTRA_CHECK
			assert(r == DNS_DB::resOK || r == DNS_DB::resAlreadyExists || r == DNS_DB::resDomainTooLong);
			if (r == DNS_DB::resOK)
				check.push_back(domain);
			#endif
		}

		for (unsigned int i = 0; i < check.size(); i++)
			assert(db.hasDomain(check[i]));
	}
	else if (command == "list-domains") {
		DNS_DB::DomainIterator it = db.getDomainIterator();
		while (!it.end()) {
			it.next();
			std::cout << it.getDomain() << std::endl;
			std::vector <IPv4_Record> r = it.getIpsv4();
			for (int i = 0; i < r.size(); i++) {
				std::cout << r[i].ip << " " << r[i].first_seen << " " << r[i].last_seen <<  std::endl;
			}
		}
	}
	else if (command == "summary") {
		int r = db.getNumberRecords();
		int f = db.getNumberFreeRecords();
		std::cout << "Total records " << r << std::endl;
		std::cout << "Total free records " << f << std::endl;
		std::cout << "Storage efficiency " << double(100*r)/(r+f) << std::endl;
		db.check();
	}
	else if (command == "crawl") {
		
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5b0, %rsp            # imm = 0x5B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
jge 0x110ed
movq 0xaf4a(%rip), %rax      # 0x1bfd0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2b0f(%rip), %rsi      # 0x13ba6
movb $0x0, %al
callq 0x43f0
movq 0xaf2b(%rip), %rax      # 0x1bfd0
movq (%rax), %rdi
leaq 0x2b1b(%rip), %rsi      # 0x13bca
movb $0x0, %al
callq 0x43f0
movq 0xaf13(%rip), %rax      # 0x1bfd0
movq (%rax), %rdi
leaq 0x2b0f(%rip), %rsi      # 0x13bd6
movb $0x0, %al
callq 0x43f0
movq 0xaefb(%rip), %rax      # 0x1bfd0
movq (%rax), %rdi
leaq 0x2b0d(%rip), %rsi      # 0x13bec
movb $0x0, %al
callq 0x43f0
xorl %edi, %edi
callq 0x4330
leaq -0xd4(%rip), %rsi       # 0x11020
movq %rsi, -0x4d8(%rbp)
movl $0xf, %edi
callq 0x4320
movq -0x4d8(%rbp), %rsi
movl $0x2, %edi
callq 0x4320
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x4d0(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x4c8(%rbp)
callq 0x4500
movq -0x4d0(%rbp), %rsi
movq -0x4c8(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x43b0
jmp 0x1114e
leaq -0x31(%rbp), %rdi
callq 0x4300
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x4e8(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x4e0(%rbp)
callq 0x4500
movq -0x4e8(%rbp), %rsi
movq -0x4e0(%rbp), %rdx
leaq -0x68(%rbp), %rdi
callq 0x43b0
jmp 0x1118f
leaq -0x69(%rbp), %rdi
callq 0x4300
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x4f8(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0x4f0(%rbp)
callq 0x4500
movq -0x4f8(%rbp), %rsi
movq -0x4f0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x43b0
jmp 0x111d6
leaq -0x91(%rbp), %rdi
callq 0x4300
leaq -0x138(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4820
jmp 0x111f4
leaq -0x150(%rbp), %rdi
callq 0x118d0
leaq 0x29f9(%rip), %rsi      # 0x13c00
leaq -0x68(%rbp), %rdi
callq 0x118f0
movb %al, -0x4f9(%rbp)
jmp 0x11218
movb -0x4f9(%rbp), %al
testb $0x1, %al
jne 0x11227
jmp 0x11446
leaq -0x90(%rbp), %rdi
callq 0x4100
movq %rax, %rsi
leaq -0x3f0(%rbp), %rdi
movl $0x8, %edx
callq 0x11920
jmp 0x11249
leaq -0x410(%rbp), %rdi
callq 0x43c0
leaq -0x268(%rbp), %rdi
leaq -0x410(%rbp), %rsi
callq 0x4360
movq %rax, -0x508(%rbp)
jmp 0x11271
movq -0x508(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x4350
movb %al, -0x509(%rbp)
jmp 0x1128f
movb -0x509(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x50a(%rbp)
jne 0x112a4
jmp 0x112b2
movb 0xb131(%rip), %al       # 0x1c3db
xorb $-0x1, %al
movb %al, -0x50a(%rbp)
movb -0x50a(%rbp), %al
testb $0x1, %al
jne 0x112c1
jmp 0x11389
leaq -0x138(%rbp), %rdi
leaq -0x410(%rbp), %rsi
callq 0x50d0
movl %eax, -0x510(%rbp)
jmp 0x112dc
movl -0x510(%rbp), %eax
movl %eax, -0x414(%rbp)
jmp 0x11255
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x4300
jmp 0x118c7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x4300
jmp 0x118be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x91(%rbp), %rdi
callq 0x4300
jmp 0x118b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x118a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x11891
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x410(%rbp), %rdi
callq 0x41c0
leaq -0x3f0(%rbp), %rdi
callq 0x11ab0
jmp 0x11891
movl $0x0, -0x418(%rbp)
movl -0x418(%rbp), %eax
movq %rax, -0x518(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x11a30
movq %rax, %rcx
movq -0x518(%rbp), %rax
cmpq %rcx, %rax
jae 0x11429
movl -0x418(%rbp), %eax
movl %eax, %esi
leaq -0x150(%rbp), %rdi
callq 0x11a90
movq %rax, %rsi
leaq -0x138(%rbp), %rdi
callq 0x11a50
movb %al, -0x519(%rbp)
jmp 0x113e6
movb -0x519(%rbp), %al
testb $0x1, %al
jne 0x113f2
jmp 0x113f4
jmp 0x11413
leaq 0x2811(%rip), %rdi      # 0x13c0c
leaq 0x2821(%rip), %rsi      # 0x13c23
movl $0x33, %edx
leaq 0x281d(%rip), %rcx      # 0x13c2b
callq 0x41e0
jmp 0x11415
movl -0x418(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x418(%rbp)
jmp 0x11393
leaq -0x410(%rbp), %rdi
callq 0x41c0
leaq -0x3f0(%rbp), %rdi
callq 0x11ab0
jmp 0x1184f
leaq 0x27f5(%rip), %rsi      # 0x13c42
leaq -0x68(%rbp), %rdi
callq 0x118f0
movb %al, -0x51a(%rbp)
jmp 0x1145e
movb -0x51a(%rbp), %al
testb $0x1, %al
jne 0x1146d
jmp 0x116ad
leaq -0x478(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x11af0
jmp 0x11482
jmp 0x11484
leaq -0x478(%rbp), %rdi
callq 0x11b30
movb %al, -0x51b(%rbp)
jmp 0x11498
movb -0x51b(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x114a9
jmp 0x1168b
leaq -0x478(%rbp), %rdi
callq 0x11b60
jmp 0x114b7
leaq -0x498(%rbp), %rdi
leaq -0x478(%rbp), %rsi
callq 0x11ba0
jmp 0x114cc
movq 0xaaf5(%rip), %rdi      # 0x1bfc8
leaq -0x498(%rbp), %rsi
callq 0x4280
movq %rax, -0x528(%rbp)
jmp 0x114e8
movq -0x528(%rbp), %rdi
movq 0xaaba(%rip), %rsi      # 0x1bfb0
callq 0x42f0
jmp 0x114fd
leaq -0x498(%rbp), %rdi
callq 0x41c0
leaq -0x4b0(%rbp), %rdi
leaq -0x478(%rbp), %rsi
callq 0x11bd0
jmp 0x1151e
movl $0x0, -0x4b4(%rbp)
movslq -0x4b4(%rbp), %rax
movq %rax, -0x530(%rbp)
leaq -0x4b0(%rbp), %rdi
callq 0xd470
movq %rax, %rcx
movq -0x530(%rbp), %rax
cmpq %rcx, %rax
jae 0x1167a
movslq -0x4b4(%rbp), %rsi
leaq -0x4b0(%rbp), %rdi
callq 0x11c00
movl 0x8(%rax), %esi
movq 0xaa56(%rip), %rdi      # 0x1bfc8
callq 0x4390
movq %rax, -0x538(%rbp)
jmp 0x11580
movq -0x538(%rbp), %rdi
leaq 0x26ea(%rip), %rsi      # 0x13c78
callq 0x42a0
movq %rax, -0x540(%rbp)
jmp 0x1159c
movslq -0x4b4(%rbp), %rsi
leaq -0x4b0(%rbp), %rdi
callq 0x11c00
movq -0x540(%rbp), %rdi
movl (%rax), %esi
callq 0x4390
movq %rax, -0x548(%rbp)
jmp 0x115c6
movq -0x548(%rbp), %rdi
leaq 0x26a4(%rip), %rsi      # 0x13c78
callq 0x42a0
movq %rax, -0x550(%rbp)
jmp 0x115e2
movslq -0x4b4(%rbp), %rsi
leaq -0x4b0(%rbp), %rdi
callq 0x11c00
movq -0x550(%rbp), %rdi
movl 0x4(%rax), %esi
callq 0x4390
movq %rax, -0x558(%rbp)
jmp 0x1160d
movq -0x558(%rbp), %rdi
movq 0xa995(%rip), %rsi      # 0x1bfb0
callq 0x42f0
jmp 0x11622
jmp 0x11624
movl -0x4b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4b4(%rbp)
jmp 0x11528
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1169c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x498(%rbp), %rdi
callq 0x41c0
jmp 0x1169c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x4b0(%rbp), %rdi
callq 0xc740
jmp 0x1169c
leaq -0x4b0(%rbp), %rdi
callq 0xc740
jmp 0x11484
leaq -0x478(%rbp), %rdi
callq 0x5250
jmp 0x1184d
leaq -0x478(%rbp), %rdi
callq 0x5250
jmp 0x11891
leaq 0x259b(%rip), %rsi      # 0x13c4f
leaq -0x68(%rbp), %rdi
callq 0x118f0
movb %al, -0x559(%rbp)
jmp 0x116c5
movb -0x559(%rbp), %al
testb $0x1, %al
jne 0x116d4
jmp 0x11823
leaq -0x138(%rbp), %rdi
callq 0x11c20
movq %rax, -0x568(%rbp)
jmp 0x116e9
movq -0x568(%rbp), %rax
movl %eax, -0x4b8(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x11c40
movq %rax, -0x570(%rbp)
jmp 0x1170b
movq -0x570(%rbp), %rax
movl %eax, -0x4bc(%rbp)
movq 0xa8a9(%rip), %rdi      # 0x1bfc8
leaq 0x2531(%rip), %rsi      # 0x13c57
callq 0x42a0
movq %rax, -0x578(%rbp)
jmp 0x11734
movq -0x578(%rbp), %rdi
movl -0x4b8(%rbp), %esi
callq 0x44d0
movq %rax, -0x580(%rbp)
jmp 0x1174f
movq -0x580(%rbp), %rdi
movq 0xa853(%rip), %rsi      # 0x1bfb0
callq 0x42f0
jmp 0x11764
movq 0xa85d(%rip), %rdi      # 0x1bfc8
leaq 0x24f4(%rip), %rsi      # 0x13c66
callq 0x42a0
movq %rax, -0x588(%rbp)
jmp 0x11780
movq -0x588(%rbp), %rdi
movl -0x4bc(%rbp), %esi
callq 0x44d0
movq %rax, -0x590(%rbp)
jmp 0x1179b
movq -0x590(%rbp), %rdi
movq 0xa807(%rip), %rsi      # 0x1bfb0
callq 0x42f0
jmp 0x117b0
movq 0xa811(%rip), %rdi      # 0x1bfc8
leaq 0x24bc(%rip), %rsi      # 0x13c7a
callq 0x42a0
movq %rax, -0x598(%rbp)
jmp 0x117cc
movq -0x598(%rbp), %rdi
movl -0x4b8(%rbp), %eax
imull $0x64, %eax, %ecx
cvtsi2sd %ecx, %xmm0
movl -0x4bc(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x4570
movq %rax, -0x5a0(%rbp)
jmp 0x117fe
movq -0x5a0(%rbp), %rdi
movq 0xa7a4(%rip), %rsi      # 0x1bfb0
callq 0x42f0
jmp 0x11813
leaq -0x138(%rbp), %rdi
callq 0x4ae0
jmp 0x11821
jmp 0x1184b
leaq 0x2464(%rip), %rsi      # 0x13c8e
leaq -0x68(%rbp), %rdi
callq 0x118f0
movb %al, -0x5a1(%rbp)
jmp 0x1183b
movb -0x5a1(%rbp), %al
testb $0x1, %al
jne 0x11847
jmp 0x11849
jmp 0x11849
jmp 0x1184b
jmp 0x1184d
jmp 0x1184f
leaq -0x150(%rbp), %rdi
callq 0x11c60
leaq -0x138(%rbp), %rdi
callq 0x4930
leaq -0x90(%rbp), %rdi
callq 0x41c0
leaq -0x68(%rbp), %rdi
callq 0x41c0
leaq -0x30(%rbp), %rdi
callq 0x41c0
movl -0x4(%rbp), %eax
addq $0x5b0, %rsp            # imm = 0x5B0
popq %rbp
retq
leaq -0x150(%rbp), %rdi
callq 0x11c60
leaq -0x138(%rbp), %rdi
callq 0x4930
leaq -0x90(%rbp), %rdi
callq 0x41c0
leaq -0x68(%rbp), %rdi
callq 0x41c0
leaq -0x30(%rbp), %rdi
callq 0x41c0
movq -0x40(%rbp), %rdi
callq 0x44f0