// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x78(%rbp)
movl $0x10000, %edi          # imm = 0x10000
callq 0x20b0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x2ec0
jmp 0x30b5
movq -0x48(%rbp), %rdi
movl $0x55, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x2040
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x2f01
movq -0x60(%rbp), %rax
movb %al, %cl
movq -0x60(%rbp), %rax
movb %cl, -0x40(%rbp,%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x2edb
leaq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x35c0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x2f1d
jmp 0x30ac
movq -0x10(%rbp), %rsi
shlq $0x3, %rsi
leaq 0x66f9(%rip), %rdi      # 0x9625
movb $0x0, %al
callq 0x2030
movl $0x10000, %edi          # imm = 0x10000
callq 0x20b0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x2f4c
jmp 0x30a3
movl $0x10000, %edi          # imm = 0x10000
callq 0x20b0
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x2f65
jmp 0x309a
movq -0x20(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
movl $0x10000, %r8d          # imm = 0x10000
callq 0x5d20
xorl %edi, %edi
callq 0x20d0
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5930
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x2fa3
jmp 0x3091
movq $0x0, -0x68(%rbp)
cmpq $0xff00, -0x68(%rbp)    # imm = 0xFF00
jae 0x2fef
callq 0x21b0
andq $0xff, %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq -0x68(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq -0x68(%rbp), %rdx
movq -0x70(%rbp), %rcx
callq 0x59b0
movq -0x70(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x2fab
movl $0x10000, %eax          # imm = 0x10000
subq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq -0x68(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq -0x68(%rbp), %rdx
movq -0x70(%rbp), %rcx
callq 0x59b0
movq -0x18(%rbp), %rdi
callq 0x5cb0
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x21a0
cmpl $0x0, %eax
je 0x3055
leaq 0x6076(%rip), %rdi      # 0x90b6
movb $0x0, %al
callq 0x2030
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x3063
leaq 0x6081(%rip), %rdi      # 0x90dd
movb $0x0, %al
callq 0x2030
movq -0x58(%rbp), %rdi
callq 0x2100
movq -0x50(%rbp), %rdi
callq 0x2100
movq -0x20(%rbp), %rdi
callq 0x36d0
movq -0x48(%rbp), %rdi
callq 0x2100
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x30bd
movq -0x58(%rbp), %rdi
callq 0x2100
movq -0x50(%rbp), %rdi
callq 0x2100
movq -0x20(%rbp), %rdi
callq 0x36d0
movq -0x48(%rbp), %rdi
callq 0x2100
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)