// File: /workspace/github_c_makefile_star2_O1/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 0x3115
movq %rax, %rbx
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
movl $0x55, %esi
callq 0x2040
testq %r15, %r15
je 0x2ffc
xorl %eax, %eax
movb %al, 0x10(%rsp,%rax)
incq %rax
cmpq %rax, %r15
jne 0x2ff0
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x342f
testq %rax, %rax
je 0x311d
movq %rax, %r14
shlq $0x3, %r15
leaq 0x35c3(%rip), %rdi      # 0x65e3
movq %r15, %rsi
xorl %eax, %eax
callq 0x2030
movl $0x10000, %edi          # imm = 0x10000
callq 0x20d0
testq %rax, %rax
je 0x3125
movq %rax, %r15
movl $0x10000, %edi          # imm = 0x10000
callq 0x20d0
testq %rax, %rax
je 0x312d
movq %rax, %r12
movl $0x10000, %r8d          # imm = 0x10000
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x3f5e
xorl %edi, %edi
callq 0x20f0
movq %r14, %rdi
xorl %esi, %esi
callq 0x3c7e
testq %rax, %rax
je 0x3135
movq %rax, %r13
movq %r15, 0x8(%rsp)
xorl %ebp, %ebp
movq %rbp, %r15
callq 0x21f0
movzbl %al, %ebp
leaq (%rbx,%r15), %rsi
leaq (%r12,%r15), %rdx
movq %r13, %rdi
movq %rbp, %rcx
callq 0x3cdb
addq %r15, %rbp
cmpq $0xff00, %rbp           # imm = 0xFF00
jb 0x3090
movl $0x10000, %ecx          # imm = 0x10000
subq %rbp, %rcx
movq %rbx, %rsi
addq %rbp, %rsi
movq %r12, %rdx
addq %rbp, %rdx
movq %r13, %rdi
callq 0x3cdb
movq %r13, %rdi
callq 0x3f3a
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 0x3593(%rip), %rax      # 0x6696
leaq 0x3583(%rip), %rdi      # 0x668d
cmovneq %rax, %rdi
callq 0x2080
jmp 0x313b
movl $0x1, %r13d
jmp 0x315b
movl $0x1, %r13d
jmp 0x3153
movl $0x1, %r13d
jmp 0x314b
movl $0x1, %r13d
jmp 0x3143
movl $0x1, %r13d
movq %r12, %rdi
callq 0x2130
movq %r15, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x34cf
movq %rbx, %rdi
callq 0x2130
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq