// File: /workspace/github_cpp_makefile_star2_O2/funny-falcon[P]fanom_hash/test_fanom.cpp
// Function: main
int main(int argc, char** argv) {
	unsigned i;
	int hex = 0;
	int use_std = 0;
	int check = 0;
	int binary = 0;
	int only_hash = 2;
	uint32_t sum = 0;
	ssize_t lsize;
	char *lbuf = NULL;
	size_t lcapa = 0;
	FILE* r;
	table tbl = {0, 0, NULL};
	std::unordered_set<std::string> set;

	for (i=1; i<argc; i++) {
		if (strcmp(argv[i], "--help") == 0) {
			goto usage;
		} else if (strcmp(argv[i], "-s") == 0) {
			use_std = 1;
		} else if (strcmp(argv[i], "-c") == 0) {
			check = 1;
		} else if (strcmp(argv[i], "-32") == 0) {
			use_32 = 1;
		} else if (strcmp(argv[i], "-x") == 0) {
			hex = 1;
			fill_dehex();
		} else if (strcmp(argv[i], "-r") == 0) {
			custom_seed = -1;
		} else if (strcmp(argv[i], "-speed") == 0) {
			only_hash = 1;
		} else if (strcmp(argv[i], "-table") == 0) {
			only_hash = 0;
		} else if (strcmp(argv[i], "-seed") == 0) {
			uint64_t s;
			i++;
			if (i == argc)
				goto usage;
			if (custom_seed == sizeof(seed)/sizeof(seed[0]))
				goto usage;
			s = strtoll(argv[i], NULL, 0); /* strtol recognize hex */
			seed[custom_seed++] = s;
		} else if (strcmp(argv[i], "-b") == 0) {
			i++;
			if (i == argc)
				goto usage;
			binary = atoi(argv[i]);
		} else {
			goto usage;
		}
	}

	if (only_hash != 2) {
		setbuffer(stdin, buffer, sizeof(buffer));
	}

	if (custom_seed == 0 && !only_hash || custom_seed == -1) {
		r = fopen("/dev/urandom", "rb");
		if (r == NULL) {
			perror("open /dev/urandom");
			return 1;
		}
		if (fread(&seed, sizeof(seed), 1, r) != 1) {
			perror("Could not read seed from /dev/urandom");
			return 1;
		}
		fclose(r);
	} else if (custom_seed == 1) {
		const uint64_t c = ((uint64_t)0x6385a7c9<<32)|0xeb0d2f41;
		seed[1] = seed[0] ^ c;
	}
	

	if (binary) {
		lbuf = (char*)malloc(binary+1);
		while ((lsize = fread(lbuf, 1, binary, stdin)) > 0) {
			if (only_hash) {
				uint64_t hash;
				if (use_32 == 0) {
					hash = fanom64_string_hash2(lbuf, lsize, seed[0], seed[1]);
				} else {
					hash = fanom32_string_hash2(lbuf, lsize, seed[0], seed[1]);
				}
				if (only_hash == 2) {
					printf("%0*" PRIx64 "\n", use_32?8:16, hash);
				}
				sum += hash;
			} else if (use_std == 0) {
				table_insert(&tbl, lbuf, lsize);
			} else {
				set.insert(std::string(lbuf, lsize));
			}
		}
	} else {
		char* copy;
		while ((lsize = getline(&lbuf, &lcapa, stdin)) != -1) {
			lsize--;
			if (only_hash == 2) {
				copy = strndup(lbuf, lsize);
			}
			if (hex) {
				assert((lsize&1) == 0);
				dehexify(lbuf, lsize);
				lsize /= 2;
			}
			if (only_hash) {
				uint64_t hash;
				if (use_32 == 0) {
					hash = fanom64_string_hash2(lbuf, lsize, seed[0], seed[1]);
				} else {
					hash = fanom32_string_hash2(lbuf, lsize, seed[0], seed[1]);
				}
				if (only_hash == 2) {
					printf("%0*" PRIx64 "\t%s\n", use_32?8:16, hash, copy);
					free(copy);
				}
				sum += hash;
			} else if (use_std == 0) {
				table_insert(&tbl, lbuf, lsize);
			} else {
				set.insert(std::string(lbuf, lsize));
			}
		}
	}

	if (only_hash) {
		if (only_hash == 1)
			printf("Sum = %x\n", sum);
		return 0;
	}

	if (use_std == 0) {
		if (tbl.size == 0) {
			goto usage;
		}

		printf("tbl->size == %u\n", tbl.size);
		if (check) {
			return check_table(&tbl, use_32);
		}
	} else {
		if (set.size() == 0) {
			goto usage;
		}
		printf("tbl->size == %zu\n", set.size());
		if (check) {
			for (auto& s: set) {
				checksum_add(s.data(), s.size());
			}
			printf("checksum %08x %08x\n", checksum[0], checksum[1]);
		}
	}
	return 0;
usage:
	printf("Usage: %s [-seed N] [-x] [-b N] [-32] [-speed|-table] [-s] [-c] < input_file\n%s", argv[0],
	"  Default, \"test vector mode\": reads lines from stdin and report checksum for every line.\n" \
	"  With -speed , only checksumming performed.\n" \
	"  With -table , puts them into chained hash table, on exit it \n" \
	"    calculates and outputs checksum and table size.\n" \
	"\t--help  - this help.\n" \
	"\t-seed N - use custom seed instead of random.\n" \
	"\t          Every invocation fills next seed array element,\n" \
	"\t          so you may give this option 1 or 2 times.\n" \
	"\t-b N    - input is binary, slice it to chunks of fixed size N\n" \
	"\t-x      - input lines are hexified, so de-hexify it\n" \
	"\t-r      - force random seed for \"test vector\"\n" \
	"\t-32     - use 32bit fanom hash function\n" \
	"\t-speed  - test speed only\n" \
	"\t-table  - test hash table (all options below for this mode)\n" \
	"\t-s      - use c++ set to verify hash table implementation\n" \
	"\t-c      - at the end, detect collisions and chain length, and\n" \
	"\t          calculate independent checksum for all inserted strings\n");
	printf("  example for \"test vector\":\n" \
		"\t%s < test_fansom.cpp\n" \
		"\techo 'a1b2bc3def' | %s \n" \
		"\techo 'a1b2bc3def' | %s -seed 2 -seed 3\n" \
		"\techo 'a1b2bc3def' | %s -x -seed 2 -seed 3\n",
		argv[0], argv[0], argv[0], argv[0]
		);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
andq $0x0, 0x8(%rsp)
andq $0x0, 0x68(%rsp)
movq %rsi, 0x20(%rsp)
movl %edi, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
leaq 0x80(%rsp), %rax
movups %xmm0, (%rax)
movl $0x3f800000, 0x10(%rax) # imm = 0x3F800000
movups %xmm0, 0x18(%rax)
pushq $0x1
popq %r12
movq $0x0, 0x28(%rsp)
pushq $0x2
popq %r13
leaq 0x18cf(%rip), %r15      # 0x417b
movl $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x1c(%rsp)
cmpl %ebp, %r12d
jae 0x2a60
movl %r12d, %eax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2220
testl %eax, %eax
je 0x2df2
movb (%r14), %bl
cmpb $0x2d, %bl
jne 0x2918
cmpb $0x73, 0x1(%r14)
jne 0x2906
cmpb $0x0, 0x2(%r14)
je 0x29eb
cmpb $0x63, 0x1(%r14)
jne 0x2918
cmpb $0x0, 0x2(%r14)
je 0x29f4
movq %r14, %rdi
leaq 0x1860(%rip), %rsi      # 0x4182
callq 0x2220
testl %eax, %eax
je 0x29d7
cmpb $0x2d, %bl
jne 0x2958
cmpb $0x78, 0x1(%r14)
jne 0x2946
cmpb $0x0, 0x2(%r14)
je 0x2a48
cmpb $0x72, 0x1(%r14)
jne 0x2958
cmpb $0x0, 0x2(%r14)
je 0x2a51
movq %r14, %rdi
leaq 0x1824(%rip), %rsi      # 0x4186
callq 0x2220
testl %eax, %eax
je 0x29e0
movq %r14, %rdi
leaq 0x1818(%rip), %rsi      # 0x418d
callq 0x2220
testl %eax, %eax
je 0x29e6
movq %r14, %rdi
leaq 0x180c(%rip), %rsi      # 0x4194
callq 0x2220
testl %eax, %eax
je 0x29fd
cmpb $0x2d, %bl
jne 0x2df2
cmpb $0x62, 0x1(%r14)
jne 0x2df2
cmpb $0x0, 0x2(%r14)
jne 0x2df2
incl %r12d
cmpl %ebp, %r12d
je 0x2df2
movl %r12d, %eax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
callq 0x2250
movq %rax, 0x28(%rsp)
jmp 0x2a58
movb $0x1, 0x47ca(%rip)      # 0x71a8
jmp 0x2a58
pushq $0x1
popq %r13
jmp 0x2a58
xorl %r13d, %r13d
jmp 0x2a58
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
jmp 0x2a58
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
jmp 0x2a58
incl %r12d
cmpl %ebp, %r12d
je 0x2df2
cmpl $0x2, 0x479c(%rip)      # 0x71ac
je 0x2df2
movl %r12d, %eax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2050
movslq 0x477a(%rip), %rcx      # 0x71ac
leal 0x1(%rcx), %edx
movl %edx, 0x4771(%rip)      # 0x71ac
leaq 0x472e(%rip), %rdx      # 0x7170
movq %rax, (%rdx,%rcx,8)
jmp 0x2a58
pushq $0x1
popq %rax
movl %eax, 0x1c(%rsp)
jmp 0x2a58
orl $-0x1, 0x4754(%rip)     # 0x71ac
incl %r12d
jmp 0x28c4
cmpl $0x2, %r13d
je 0x2a81
movq 0x4573(%rip), %rax      # 0x6fe0
movq (%rax), %rdi
leaq 0x4739(%rip), %rsi      # 0x71b0
movl $0x100000, %edx         # imm = 0x100000
callq 0x2080
movl 0x4725(%rip), %eax      # 0x71ac
movl %eax, %ecx
orl %r13d, %ecx
sete %cl
cmpl $-0x1, %eax
movq 0x28(%rsp), %rbx
je 0x2abc
testb %cl, %cl
jne 0x2abc
cmpl $0x1, %eax
jne 0x2b02
movabsq $0x6385a7c9eb0d2f41, %rax # imm = 0x6385A7C9EB0D2F41
xorq 0x46bd(%rip), %rax      # 0x7170
movq %rax, 0x46be(%rip)      # 0x7178
jmp 0x2b02
leaq 0x1705(%rip), %rdi      # 0x41c8
leaq 0x16d0(%rip), %rsi      # 0x419a
callq 0x21b0
testq %rax, %rax
je 0x2e6a
movq %rax, %r14
leaq 0x468e(%rip), %rdi      # 0x7170
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2240
cmpq $0x1, %rax
jne 0x2e73
movq %r14, %rdi
callq 0x2170
testl %ebx, %ebx
je 0x2c05
leal 0x1(%rbx), %eax
movslq %eax, %rdi
callq 0x2200
movq %rax, %rdi
movq %rax, 0x8(%rsp)
movslq %ebx, %r14
leaq 0x48(%rsp), %r15
xorl %ebp, %ebp
movq 0x44b2(%rip), %rax      # 0x6fe0
movq (%rax), %rcx
pushq $0x1
popq %rsi
movq %r14, %rdx
callq 0x2240
testq %rax, %rax
jle 0x2d3f
testl %r13d, %r13d
je 0x2b73
movb 0x4658(%rip), %bl       # 0x71a8
movq 0x8(%rsp), %rdi
movq 0x4614(%rip), %rdx      # 0x7170
testb %bl, %bl
je 0x2bae
movl 0x4612(%rip), %ecx      # 0x7178
movq %rax, %rsi
callq 0x3032
movl %eax, %r12d
jmp 0x2bc0
cmpl $0x0, 0x14(%rsp)
je 0x2bf1
movq 0x8(%rsp), %rsi
leaq 0x58(%rsp), %rcx
movq %rcx, 0x48(%rsp)
addq %rsi, %rax
movq %r15, %rdi
movq %rax, %rdx
callq 0x352c
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x35c2
movq %r15, %rdi
callq 0x20d0
jmp 0x2be7
movq 0x45c3(%rip), %rcx      # 0x7178
movq %rax, %rsi
callq 0x2ed0
movq %rax, %r12
cmpl $0x2, %r13d
jne 0x2be4
xorb $0x1, %bl
movzbl %bl, %eax
leal 0x8(,%rax,8), %esi
leaq 0x15fb(%rip), %rdi      # 0x41d5
movq %r12, %rdx
xorl %eax, %eax
callq 0x2030
addl %r12d, %ebp
movq 0x8(%rsp), %rdi
jmp 0x2b27
movq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdi
movq %rax, %rdx
callq 0x2478
jmp 0x2be7
movq 0x43d4(%rip), %rbx      # 0x6fe0
leaq 0x48(%rsp), %r15
xorl %ebp, %ebp
movq (%rbx), %rdx
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x21d0
movq %rax, %r14
cmpq $-0x1, %rax
je 0x2d3f
decq %r14
cmpl $0x2, %r13d
jne 0x2c4b
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x2040
movq %rax, %r12
cmpl $0x0, 0x1c(%rsp)
je 0x2c6c
testb $0x1, %r14b
jne 0x2e85
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x25a1
sarq %r14
testl %r13d, %r13d
jne 0x2cc6
cmpl $0x0, 0x14(%rsp)
je 0x2caf
movq 0x8(%rsp), %rsi
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
addq %rsi, %r14
movq %r15, %rdi
movq %r14, %rdx
callq 0x352c
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x35c2
movq %r15, %rdi
callq 0x20d0
jmp 0x2c13
movq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdi
movq %r14, %rdx
callq 0x2478
jmp 0x2c13
movb 0x44dc(%rip), %bl       # 0x71a8
movq 0x8(%rsp), %rdi
movq 0x4498(%rip), %rdx      # 0x7170
testb %bl, %bl
je 0x2cef
movl 0x4496(%rip), %ecx      # 0x7178
movq %r14, %rsi
callq 0x3032
movl %eax, %r14d
jmp 0x2d01
movq 0x4482(%rip), %rcx      # 0x7178
movq %r14, %rsi
callq 0x2ed0
movq %rax, %r14
cmpl $0x2, %r13d
jne 0x2d30
xorb $0x1, %bl
movzbl %bl, %eax
leal 0x8(,%rax,8), %esi
leaq 0x14f6(%rip), %rdi      # 0x4211
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2030
movq %r12, %rdi
callq 0x21c0
addl %r14d, %ebp
movq 0x42a6(%rip), %rbx      # 0x6fe0
jmp 0x2c13
testl %r13d, %r13d
je 0x2d69
xorl %r14d, %r14d
cmpl $0x1, %r13d
jne 0x2e2c
leaq 0x14c3(%rip), %rdi      # 0x421b
xorl %r14d, %r14d
movl %ebp, %esi
xorl %eax, %eax
callq 0x2030
jmp 0x2e2c
cmpl $0x0, 0x14(%rsp)
je 0x2dbc
movq 0x88(%rsp), %rsi
testq %rsi, %rsi
je 0x2df2
leaq 0x14b2(%rip), %rdi      # 0x4236
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2030
cmpl $0x0, 0x18(%rsp)
je 0x2e2c
leaq 0x80(%rsp), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x2e4b
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rsi
callq 0x255c
jmp 0x2da1
movl 0x30(%rsp), %esi
testl %esi, %esi
je 0x2df2
leaq 0x145a(%rip), %rdi      # 0x4225
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2030
cmpl $0x0, 0x18(%rsp)
je 0x2e2c
movzbl 0x43c5(%rip), %esi      # 0x71a8
leaq 0x30(%rsp), %rdi
callq 0x25ef
movl %eax, %r14d
jmp 0x2e2c
movq 0x20(%rsp), %rbx
movq (%rbx), %rsi
leaq 0x1447(%rip), %rdi      # 0x4248
leaq 0x1490(%rip), %rdx      # 0x4298
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2030
movq (%rbx), %rcx
leaq 0x180c(%rip), %rdi      # 0x4628
movq %rcx, %rsi
movq %rcx, %rdx
movq %rcx, %r8
xorl %eax, %eax
callq 0x2030
leaq 0x70(%rsp), %rdi
callq 0x3208
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x434f(%rip), %esi      # 0x71a0
movl 0x434d(%rip), %edx      # 0x71a4
leaq 0x1279(%rip), %rdi      # 0x40d7
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2030
jmp 0x2e2c
leaq 0x132c(%rip), %rdi      # 0x419d
jmp 0x2e7a
leaq 0x1335(%rip), %rdi      # 0x41af
callq 0x2150
pushq $0x1
popq %r14
jmp 0x2e2c
leaq 0x1350(%rip), %rdi      # 0x41dc
leaq 0x1358(%rip), %rsi      # 0x41eb
leaq 0x1360(%rip), %rcx      # 0x41fa
movl $0x84, %edx
callq 0x20f0
jmp 0x2ebb
jmp 0x2eaa
jmp 0x2ebb
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x20d0
jmp 0x2ebe
jmp 0x2ebb
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3208
movq %rbx, %rdi
callq 0x2290