// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]libcperciva/tests/crc32/main.c
// Function: selftest
static int
selftest(void)
{
	CRC32C_CTX ctx;
	uint8_t cbuf[4];
	size_t i, j;
	size_t failures = 0;
	char * largebuf;
	size_t bytes_processed;
	size_t new_chunk;
	uint8_t alt_buf[4];

	/* Run regular test cases. */
	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
		printf("Computing CRC32C of \"%s\"...", tests[i].s);
		CRC32C_Init(&ctx);
		CRC32C_Update(&ctx, (const uint8_t *)tests[i].s,
		    strlen(tests[i].s));
		CRC32C_Final(cbuf, &ctx);
		if (memcmp(cbuf, tests[i].cbuf, 4)) {
			printf(" FAILED!\n");
			printf("Computed CRC32C: ");
			for (j = 0; j < 4; j++)
				printf("%02x", cbuf[j]);
			printf("\nCorrect CRC32C:  ");
			for (j = 0; j < 4; j++)
				printf("%02x", tests[i].cbuf[j]);
			printf("\n");
			failures++;
		} else {
			printf(" PASSED!\n");
		}
	}

	/* Test with a large buffer and unaligned access. */
	printf("Computing CRC32C of a large buffer two different ways...");

	/* Prepare a large buffer with repeating 01010101_2 = 85. */
	if ((largebuf = malloc(LARGE_BUFSIZE)) == NULL)
		goto err0;
	memset(largebuf, 85, LARGE_BUFSIZE);

	/* Compute checksum with one call. */
	CRC32C_Init(&ctx);
	CRC32C_Update(&ctx, (const uint8_t *)largebuf, LARGE_BUFSIZE);
	CRC32C_Final(cbuf, &ctx);

	/* Ensure we have a repeatable pattern of random values. */
	srandom(0);

	/* Compute checksum with multiple calls. */
	CRC32C_Init(&ctx);
	bytes_processed = 0;
	while (bytes_processed < LARGE_BUFSIZE - MAX_CHUNK) {
		/* Get an insecure random number; not used for cryptography. */
		new_chunk = ((unsigned long int)random()) % MAX_CHUNK;

		/* Process that amount of data. */
		CRC32C_Update(&ctx, (const uint8_t *)&largebuf[bytes_processed],
		    new_chunk);
		bytes_processed += new_chunk;
	}
	new_chunk = LARGE_BUFSIZE - bytes_processed;
	CRC32C_Update(&ctx, (const uint8_t *)&largebuf[bytes_processed],
	    new_chunk);
	CRC32C_Final(alt_buf, &ctx);

	/* Compare checksums. */
	if (memcmp(cbuf, alt_buf, 4)) {
		printf(" FAILED!\n");
		failures++;
	} else
		printf(" PASSED!\n");

	/* Clean up. */
	free(largebuf);

	/* Report overall success to exit code. */
	if (failures)
		return (1);
	else
		return (0);

err0:
	/* Failure! */
	return (1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0x4be6(%rip), %rbx      # 0x6128
leaq 0x1b26(%rip), %rbp      # 0x306f
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
cmpq $0x8, %r13
je 0x1656
movq %r13, 0x18(%rsp)
shlq $0x4, %r13
leaq 0x4bb1(%rip), %r14      # 0x6120
movq (%r13,%r14), %rsi
leaq 0x1ac6(%rip), %rdi      # 0x3041
xorl %eax, %eax
callq 0x10f0
leaq 0xc(%rsp), %r15
movq %r15, %rdi
callq 0x1818
movq (%r13,%r14), %r12
movq %r12, %rdi
callq 0x10e0
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x1a51
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x1b19
movl 0x8(%rsp), %eax
cmpl 0x8(%r13,%r14), %eax
je 0x1639
leaq 0x1b7a(%rip), %rdi      # 0x3143
callq 0x10b0
leaq 0x1a88(%rip), %rdi      # 0x305d
xorl %eax, %eax
callq 0x10f0
xorl %r12d, %r12d
movq 0x18(%rsp), %r13
cmpq $0x4, %r12
je 0x15ff
movzbl 0x8(%rsp,%r12), %esi
movq %rbp, %rdi
xorl %eax, %eax
callq 0x10f0
incq %r12
jmp 0x15e4
leaq 0x1a6e(%rip), %rdi      # 0x3074
xorl %eax, %eax
callq 0x10f0
xorl %r12d, %r12d
cmpq $0x4, %r12
je 0x162a
movzbl (%rbx,%r12), %esi
movq %rbp, %rdi
xorl %eax, %eax
callq 0x10f0
incq %r12
jmp 0x1610
pushq $0xa
popq %rdi
callq 0x1040
incq 0x10(%rsp)
jmp 0x164a
leaq 0x1afa(%rip), %rdi      # 0x313a
callq 0x10b0
movq 0x18(%rsp), %r13
incq %r13
addq $0x10, %rbx
jmp 0x1555
leaq 0x1a2a(%rip), %rdi      # 0x3087
xorl %eax, %eax
callq 0x10f0
movl $0x10000, %edi          # imm = 0x10000
callq 0x1180
testq %rax, %rax
je 0x1736
movq %rax, %rbx
pushq $0x55
popq %rsi
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
callq 0x1120
leaq 0xc(%rsp), %r14
movq %r14, %rdi
callq 0x1818
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1a51
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x1b19
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x1090
movq %r14, %rdi
callq 0x1818
cmpq $0xfeff, %r12           # imm = 0xFEFF
ja 0x16ec
callq 0x1170
movzbl %al, %r15d
leaq (%rbx,%r12), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x1a51
addq %r15, %r12
jmp 0x16c6
movl $0x10000, %edx          # imm = 0x10000
subq %r12, %rdx
movq %rbx, %rsi
addq %r12, %rsi
leaq 0xc(%rsp), %r14
movq %r14, %rdi
callq 0x1a51
leaq 0x24(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x1b19
movl 0x8(%rsp), %eax
cmpl (%r15), %eax
je 0x173b
leaq 0x1a1c(%rip), %rdi      # 0x3143
callq 0x10b0
movq 0x10(%rsp), %r14
incq %r14
jmp 0x174c
pushq $0x1
popq %rax
jmp 0x175c
leaq 0x19f8(%rip), %rdi      # 0x313a
callq 0x10b0
movq 0x10(%rsp), %r14
movq %rbx, %rdi
callq 0x1030
xorl %eax, %eax
testq %r14, %r14
setne %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq