// File: /workspace/github_c_makefile_star2_O2/Tarsnap[P]libcperciva/tests/crypto_aesctr/main.c
// Function: selftest_unaligned_access
static size_t
selftest_unaligned_access(size_t keylen)
{
	struct crypto_aesctr * aesctr;
	struct crypto_aes_key * key_exp;
	uint8_t key_unexpanded[32];
	uint8_t * largebuf;
	uint8_t * largebuf_out1;
	uint8_t * largebuf_out2;
	size_t i;
	size_t bytes_processed;
	size_t new_chunk;
	size_t failures = 0;

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

	/* Prepare the key: 00010203... */
	for (i = 0; i < keylen; i++)
		key_unexpanded[i] = (uint8_t)i;
	if ((key_exp = crypto_aes_key_expand(key_unexpanded, keylen)) == NULL)
		goto err1;

	/* Test with a large buffer and unaligned access. */
	printf("Computing %zu-bit AES-CTR of a large buffer two "
	    "different ways...", keylen * 8);

	/* Prepare output buffers. */
	if ((largebuf_out1 = malloc(LARGE_BUFSIZE)) == NULL)
		goto err2;
	if ((largebuf_out2 = malloc(LARGE_BUFSIZE)) == NULL)
		goto err3;

	/* Encrypt with one call. */
	crypto_aesctr_buf(key_exp, 0, largebuf, largebuf_out1, LARGE_BUFSIZE);

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

	/* Encrypt with multiple calls. */
	if ((aesctr = crypto_aesctr_init(key_exp, 0)) == NULL)
		goto err4;
	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. */
		crypto_aesctr_stream(aesctr, &largebuf[bytes_processed],
		    &largebuf_out2[bytes_processed], new_chunk);
		bytes_processed += new_chunk;
	}
	new_chunk = LARGE_BUFSIZE - bytes_processed;
	crypto_aesctr_stream(aesctr, &largebuf[bytes_processed],
	    &largebuf_out2[bytes_processed], new_chunk);
	crypto_aesctr_free(aesctr);

	/* Compare ciphertexts. */
	if (memcmp(largebuf_out1, largebuf_out2, LARGE_BUFSIZE)) {
		printf(" FAILED!\n");
		failures++;
	} else
		printf(" PASSED!\n");

	/* Clean up. */
	free(largebuf_out2);
	free(largebuf_out1);
	crypto_aes_key_free(key_exp);
	free(largebuf);

	return (failures);

err4:
	free(largebuf_out2);
err3:
	free(largebuf_out1);
err2:
	crypto_aes_key_free(key_exp);
err1:
	free(largebuf);
err0:
	/* Failure! */
	return (1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
movl $0x10000, %edi          # imm = 0x10000
callq 0x20d0
testq %rax, %rax
je 0x2c1c
movq %rax, %rbx
pushq $0x55
popq %rsi
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
callq 0x2040
xorl %eax, %eax
cmpq %rax, %r15
je 0x2b59
movb %al, 0x10(%rsp,%rax)
incq %rax
jmp 0x2b4b
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x2f5a
testq %rax, %rax
je 0x2c22
movq %rax, %r14
shlq $0x3, %r15
leaq 0x2a66(%rip), %rdi      # 0x55e3
movq %r15, %rsi
xorl %eax, %eax
callq 0x2030
movl $0x10000, %edi          # imm = 0x10000
callq 0x20d0
pushq $0x1
popq %r13
testq %rax, %rax
je 0x2c91
movq %rax, %r15
movl $0x10000, %edi          # imm = 0x10000
callq 0x20d0
testq %rax, %rax
je 0x2c89
movq %rax, %r12
movl $0x10000, %r8d          # imm = 0x10000
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x398d
xorl %edi, %edi
callq 0x20f0
movq %r14, %rdi
xorl %esi, %esi
callq 0x37a4
testq %rax, %rax
je 0x2c81
movq %rax, %rbp
movq %r15, 0x8(%rsp)
xorl %r15d, %r15d
cmpq $0xfeff, %r15           # imm = 0xFEFF
ja 0x2c28
callq 0x21f0
movzbl %al, %r13d
leaq (%rbx,%r15), %rsi
leaq (%r12,%r15), %rdx
movq %rbp, %rdi
movq %r13, %rcx
callq 0x37f7
addq %r13, %r15
jmp 0x2bf2
pushq $0x1
popq %r13
jmp 0x2ca1
pushq $0x1
popq %r13
jmp 0x2c99
movl $0x10000, %ecx          # imm = 0x10000
subq %r15, %rcx
movq %rbx, %rsi
addq %r15, %rsi
movq %r12, %rdx
addq %r15, %rdx
movq %rbp, %rdi
callq 0x37f7
movq %rbp, %rdi
callq 0x396b
movl $0x10000, %edx          # imm = 0x10000
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x2170
xorl %r13d, %r13d
testl %eax, %eax
setne %r13b
leaq 0x2a25(%rip), %rax      # 0x5696
leaq 0x2a15(%rip), %rdi      # 0x568d
cmovneq %rax, %rdi
callq 0x2080
movq %r12, %rdi
callq 0x2130
movq %r15, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2ffa
movq %rbx, %rdi
callq 0x2130
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq