// File: /workspace/github_cpp_makefile_star2_O2/funny-falcon[P]fanom_hash/./check.inc.cpp
// Function: check_table(table*, int)
int check_table(table *tbl, int use_32) {
	uint32_t maxchain = 0;
	double expectedmaxchain = 2 * log(tbl->bins) / log(log(tbl->bins));
	std::map<uint64_t, uint32_t> counter;
	int ret = 0;
	int i;
	for (i = 0; i < tbl->bins; i++) {
		uint32_t chain = 0;
		entry* e = tbl->entries[i];
		while (e != NULL) {
			chain++;
			checksum_add(e->str, e->len);
			counter[e->hash]++;
			e = e->next;
		}
		if (chain > maxchain)
			maxchain = chain;
	}
	printf("checksum %08x %08x\n", checksum[0], checksum[1]);
	printf("maxchain: %u (expected %f)", maxchain, expectedmaxchain);
	if (maxchain > expectedmaxchain*2) {
		printf("(!!! maxchain overflow)");
		ret = 1;
	}
	printf("\n");
	if (counter.size() != tbl->size) {
		double ncoll = tbl->size - (uint32_t)counter.size();
		double expected = 0;
		uint32_t maxcnt = 0;
		if (use_32) {
			double d = pow(2,32);
			double n = tbl->size;
			/* https://en.wikipedia.org/wiki/Birthday_problem#Collision_counting */
			expected = n - d + d*pow((d-1)/d, n);
			if (expected > 0.01 && ncoll <= 2) {
				ret = 0;
			} else {
				ret = expected*4 < ncoll;
			}
		} else {
			ret = 1;
		}
		for (auto& pair: counter) {
			if (pair.second-1 > maxcnt) {
				maxcnt = pair.second-1;
			}
		}
		printf("Has full hash collision: %u same hashes for %u values (expected %f)\n",
				(uint32_t)ncoll, tbl->size, expected);
		if (maxcnt > 1) {
			printf("Max full collision: %u\n", maxcnt);
			ret = 1;
		}
	}
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, 0x10(%rsp)
movq %rdi, %rbx
movl 0x4(%rdi), %edi
callq 0x3159
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x4(%rbx), %edi
callq 0x3159
callq 0x2130
leaq 0x20(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0x18(%rsp), %r14
xorl %r15d, %r15d
movl 0x4(%rbx), %eax
cmpq %rax, %rbp
jae 0x26a8
leaq (,%rbp,8), %r12
addq 0x8(%rbx), %r12
xorl %r13d, %r13d
movq (%r12), %r12
testq %r12, %r12
je 0x269c
leaq 0x20(%r12), %rdi
movq 0x18(%r12), %rsi
callq 0x255c
movq %r14, %rdi
movq %r12, %rsi
callq 0x3166
incl %r13d
incl (%rax)
addq $0x8, %r12
jmp 0x266e
cmpl %r15d, %r13d
cmoval %r13d, %r15d
incq %rbp
jmp 0x2657
movl 0x4af2(%rip), %esi      # 0x71a0
movl 0x4af0(%rip), %edx      # 0x71a4
leaq 0x1a1c(%rip), %rdi      # 0x40d7
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2030
leaq 0x1a1f(%rip), %rdi      # 0x40eb
movl %r15d, %esi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x2030
movl %r15d, %eax
cvtsi2sd %rax, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2706
leaq 0x1a0b(%rip), %rdi      # 0x4106
xorl %eax, %eax
callq 0x2030
pushq $0x1
popq %r14
pushq $0xa
popq %rdi
callq 0x2210
movq 0x40(%rsp), %rcx
movl (%rbx), %eax
cmpq %rax, %rcx
je 0x280d
movl %eax, %r15d
subl %ecx, %r15d
cmpl $0x0, 0x10(%rsp)
je 0x27a8
movl %r15d, %ecx
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x10(%rsp)
movl %eax, %eax
cvtsi2sd %rax, %xmm1
movsd 0x18c0(%rip), %xmm0     # 0x4008
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x18b6(%rip), %xmm0     # 0x4010
callq 0x2280
mulsd 0x18b1(%rip), %xmm0     # 0x4018
addsd 0x8(%rsp), %xmm0
ucomisd 0x18ab(%rip), %xmm0     # 0x4020
setbe %al
cmpl $0x3, %r15d
setae %cl
orb %al, %cl
movsd 0x189f(%rip), %xmm1     # 0x4028
movsd %xmm0, 0x8(%rsp)
mulsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
seta %al
andb %cl, %al
movzbl %al, %r14d
jmp 0x27b6
pushq $0x1
popq %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x30(%rsp), %rax
leaq 0x20(%rsp), %rcx
cmpq %rcx, %rax
je 0x27db
movl 0x28(%rax), %ecx
decl %ecx
cmpl %ebp, %ecx
cmoval %ecx, %ebp
movq %rax, %rdi
callq 0x2100
jmp 0x27bd
movl (%rbx), %edx
leaq 0x193a(%rip), %rdi      # 0x411e
movl %r15d, %esi
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x2030
cmpl $0x1, %ebp
jbe 0x280d
leaq 0x1963(%rip), %rdi      # 0x4163
movl %ebp, %esi
xorl %eax, %eax
callq 0x2030
pushq $0x1
popq %r14
leaq 0x18(%rsp), %rdi
callq 0x31c0
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x2290