// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3d8, %rsp            # imm = 0x3D8
movq %rsi, %rbx
cmpl $0x3, %edi
jle 0x8b5b
leaq -0x70(%rip), %r14       # 0x8560
movl $0xf, %edi
movq %r14, %rsi
callq 0x32c0
movl $0x2, %edi
movq %r14, %rsi
callq 0x32c0
movq 0x8(%rbx), %rsi
leaq 0x78(%rsp), %rdi
leaq 0x138(%rsp), %rdx
callq 0x30d0
movq 0x10(%rbx), %rsi
leaq 0x38(%rsp), %rdi
leaq 0x138(%rsp), %rdx
callq 0x30d0
movq 0x18(%rbx), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x138(%rsp), %rdx
callq 0x30d0
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x399c
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x25d2(%rip), %rsi      # 0xac2a
leaq 0x38(%rsp), %rdi
callq 0x3070
testl %eax, %eax
je 0x86ae
leaq 0x25ff(%rip), %rsi      # 0xac6c
leaq 0x38(%rsp), %rdi
callq 0x3070
testl %eax, %eax
je 0x87c0
leaq 0x25f3(%rip), %rsi      # 0xac79
leaq 0x38(%rsp), %rdi
callq 0x3070
testl %eax, %eax
je 0x89a8
leaq 0x2619(%rip), %rsi      # 0xacb8
leaq 0x38(%rsp), %rdi
callq 0x3070
jmp 0x8ad5
movq 0x58(%rsp), %rsi
leaq 0x138(%rsp), %rdi
movl $0x8, %edx
callq 0x8c40
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq 0x2c0(%rsp), %rbx
movq %rsp, %r14
leaq 0x98(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x32f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x871f
testb $0x1, 0x6c3b(%rip)      # 0xf34b
jne 0x871f
movq %r15, %rdi
movq %r14, %rsi
callq 0x4542
jmp 0x86f0
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x876c
movl $0x1, %ebp
leaq 0xd8(%rsp), %rbx
xorl %ecx, %ecx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq %rbx, %rdi
callq 0x5550
testb %al, %al
je 0x8b3c
movl %ebp, %ecx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
incl %ebp
cmpq %rcx, %rdx
ja 0x873a
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x877a
callq 0x3220
leaq 0x646f(%rip), %rax      # 0xebf0
leaq 0x2d0(%rsp), %rbx
movq %rax, -0x10(%rbx)
leaq 0x6484(%rip), %rax      # 0xec18
movq %rax, (%rbx)
movq $0x0, -0x8(%rbx)
leaq 0x63b2(%rip), %rsi      # 0xeb58
leaq 0x138(%rsp), %rdi
callq 0x943a
movq %rbx, %rdi
callq 0x30a0
jmp 0x8ad5
leaq 0xd8(%rsp), %rsi
leaq 0x138(%rsp), %rdi
leaq 0x98(%rsp), %rcx
xorl %edx, %edx
callq 0x459c
leaq 0x150(%rsp), %rbx
movq %rsp, %r12
movq 0x67d7(%rip), %r13      # 0xefc8
leaq 0x24aa(%rip), %rbp      # 0xaca2
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
movq %rbx, %rdi
callq 0x7016
testb %al, %al
je 0x8841
movl 0x140(%rsp), %eax
movq 0x148(%rsp), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
sarq %rdx
movabsq $0x14c1bacf914c1bad, %rcx # imm = 0x14C1BACF914C1BAD
imulq %rcx, %rdx
decq %rdx
cmpq %rax, %rdx
je 0x8996
leaq 0x140(%rsp), %rdi
callq 0x8f52
movq %rbx, %rdi
leaq 0x170(%rsp), %rsi
callq 0x7120
movq %r12, %rdi
movq %rbx, %rsi
callq 0x70c4
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r13, %rdi
callq 0x3290
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x88b7
callq 0x3220
movq %r12, %rdi
movq %rbx, %rsi
callq 0x8ff4
movq (%rsp), %rdi
cmpq %rdi, 0x8(%rsp)
je 0x8978
xorl %r12d, %r12d
xorl %ebx, %ebx
movl 0x8(%rdi,%r12), %esi
movq %r13, %rdi
callq 0x3190
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3290
movq (%rsp), %rax
movl (%rax,%r12), %esi
movq %r14, %rdi
callq 0x3190
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x3290
movq (%rsp), %rax
movl 0x4(%rax,%r12), %esi
movq %r14, %rdi
callq 0x3190
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
incq %rbx
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
subq %rdi, %rax
sarq $0x2, %rax
imulq %r15, %rax
addq $0xc, %r12
cmpq %rbx, %rax
ja 0x88d6
testq %rdi, %rdi
leaq 0x150(%rsp), %rbx
movq %rsp, %r12
je 0x8802
callq 0x3220
jmp 0x8802
leaq 0x138(%rsp), %rdi
callq 0x4600
jmp 0x8ad5
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
callq 0x5804
movq %rax, %rbx
movq %r14, %rdi
callq 0x58c8
movq %rax, %r14
movq 0x65fb(%rip), %rdi      # 0xefc8
leaq 0x22ad(%rip), %rsi      # 0xac81
movl $0xe, %edx
callq 0x3290
movq 0x65e3(%rip), %rdi      # 0xefc8
movl %ebx, %esi
callq 0x33f0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq 0x65ab(%rip), %rdi      # 0xefc8
leaq 0x226c(%rip), %rsi      # 0xac90
movl $0x13, %edx
callq 0x3290
movq 0x6593(%rip), %rdi      # 0xefc8
movl %r14d, %esi
callq 0x33f0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq 0x655a(%rip), %rdi      # 0xefc8
leaq 0x222f(%rip), %rsi      # 0xaca4
movl $0x13, %edx
callq 0x3290
imull $0x64, %ebx, %eax
cvtsi2sd %eax, %xmm0
addl %ebx, %r14d
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
movq 0x652f(%rip), %rdi      # 0xefc8
callq 0x33a0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3230
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq 0x98(%rsp), %rdi
callq 0x3cec
leaq 0x20(%rsp), %rdi
callq 0x8d7e
leaq 0x98(%rsp), %rdi
callq 0x3aca
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8aff
callq 0x3220
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8b12
callq 0x3220
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8b28
callq 0x3220
xorl %eax, %eax
addq $0x3d8, %rsp            # imm = 0x3D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20f3(%rip), %rdi      # 0xac36
leaq 0x2103(%rip), %rsi      # 0xac4d
leaq 0x2104(%rip), %rcx      # 0xac55
movl $0x33, %edx
callq 0x3180
movq %rbx, %rdi
callq 0x3781
jmp 0x8b81
jmp 0x8b81
jmp 0x8b81
movq %rax, %rbx
jmp 0x8bfb
movq %rax, %rbx
jmp 0x8c0e
movq %rax, %rbx
jmp 0x8c21
movq %rax, %rbx
jmp 0x8be4
jmp 0x8b8a
jmp 0x8b8a
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x8b9b
callq 0x3220
leaq 0x138(%rsp), %rdi
callq 0x8d3a
jmp 0x8be4
jmp 0x8bae
jmp 0x8bae
movq %rax, %rbx
jmp 0x8bd7
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
jne 0x8bd2
jmp 0x8bd7
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x8bd7
callq 0x3220
leaq 0x138(%rsp), %rdi
callq 0x4600
leaq 0x20(%rsp), %rdi
callq 0x8d7e
leaq 0x98(%rsp), %rdi
callq 0x3aca
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8c0e
callq 0x3220
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8c21
callq 0x3220
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8c37
callq 0x3220
movq %rbx, %rdi
callq 0x3400
nop