// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/scream12v2/ref/tae.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
                        unsigned char *c,unsigned long long *clen,
                        const unsigned char *m, unsigned long long mlen,
                        const unsigned char *ad, unsigned long long adlen,
                        const unsigned char *nsec,
                        const unsigned char *npub,
                        const unsigned char *k
                        )
{
    unsigned char K[blocklen];
    unsigned char T[blocklen] = {0};
    unsigned char auth[blocklen] = {0};
    unsigned char sum[blocklen] = {0};
    unsigned long long i;
    unsigned j;
    memcpy(K, k, blocklen);
    memcpy(T, npub, CRYPTO_NPUBBYTES);
    T[15]  = 0x02;           /* control byte for a.d. */

    /* Process Associated Data */
    for (i=0; i<adlen; i+=blocklen) {

      unsigned char block[blocklen] = {0};

      if (i+blocklen < adlen) {
	/* Normal block */
	memcpy(block, ad+i, blocklen);
      } else if (i+blocklen == adlen) {
	/* Final block: full */
	memcpy(block, ad+i, blocklen);
	T[15] = 0x04;	
      } else {
	/* Final block: partial with padding */
	memcpy(block, ad+i, adlen % blocklen);
	block[adlen % blocklen] = 0x80;
	T[15] = 0x06;
      }

      LS_encrypt(block, K, T, block);
      for (j = 0; j < blocklen; j++)
	auth[j] ^= block[j];
      increment(T);
    }

    
    /* Process Message */
    memset(T, 0, blocklen);
    memcpy(T, npub, CRYPTO_NPUBBYTES);

    for (i=0; i < mlen; i+=blocklen) {
      if (i+blocklen <= mlen) {
	/* Full block (can be last block) */
	for (j=0; j < blocklen; j++)
	  sum[j] ^= m[i+j];
	LS_encrypt(m+i, K, T, c+i);

	increment(T);
      } else {
	/* Partial block (final) */
	unsigned char block[blocklen] = {0};
	block[15] = 8*(mlen % blocklen);
	LS_encrypt(block, K, T, block);

	for (j=0; j < mlen%blocklen; j++) {
	  sum[j] ^= m[i+j];
	  c[i+j] = block[j] ^ m[i+j];
	}
      }
    }

    /* Generate Tag */
    T[15] = ((8*(mlen % blocklen)) << 1) | 1;

    LS_encrypt(sum, K, T, sum);
    for (i = 0; i < CRYPTO_ABYTES; i++)
      c[mlen+i] = auth[i] ^ sum[i];

    *clen = mlen + CRYPTO_ABYTES;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movq %rdx, 0x38(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdi, 0x28(%rsp)
movq 0xb8(%rsp), %rcx
movq 0xc0(%rsp), %rax
vxorps %xmm1, %xmm1, %xmm1
vmovaps %xmm1, 0x40(%rsp)
vmovups (%rax), %xmm0
vmovaps %xmm0, 0x50(%rsp)
movl $0x0, 0xb(%rsp)
movb $0x0, 0xf(%rsp)
movq (%rcx), %rax
movq %rax, (%rsp)
movl 0x7(%rcx), %eax
movl %eax, 0x7(%rsp)
movb $0x2, 0xf(%rsp)
testq %r9, %r9
je 0x1c0f
movq %r9, %r13
movq %r8, %rbp
movl %r13d, %r14d
andl $0xf, %r14d
xorl %esi, %esi
leaq 0x10(%rsp), %r15
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x10(%rsi), %r12
cmpq %r13, %r12
vmovaps %xmm1, 0x60(%rsp)
jae 0x1ba8
vmovups (%rbp,%rsi), %xmm0
vmovaps %xmm0, 0x10(%rsp)
jmp 0x1bd7
addq %rbp, %rsi
cmpq %r13, %r12
jne 0x1bc1
vmovups (%rsi), %xmm0
vmovaps %xmm0, 0x10(%rsp)
movb $0x4, 0xf(%rsp)
jmp 0x1bd7
movq %r15, %rdi
movq %r14, %rdx
callq 0x1090
movb $-0x80, 0x10(%rsp,%r14)
movb $0x6, 0xf(%rsp)
movq %r15, %rdi
leaq 0x50(%rsp), %rsi
movq %rsp, %rdx
movq %r15, %rcx
callq 0x1080
vmovaps 0x60(%rsp), %xmm1
vxorps 0x10(%rsp), %xmm1, %xmm1
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x1bfb
movq %r12, %rsi
cmpq %r13, %r12
jb 0x1b81
vmovaps %xmm1, 0x60(%rsp)
movb $0x0, 0xf(%rsp)
movl $0x0, 0xb(%rsp)
movq 0xb8(%rsp), %rcx
movq (%rcx), %rax
movq %rax, (%rsp)
movl 0x7(%rcx), %eax
movl %eax, 0x7(%rsp)
testq %rbx, %rbx
je 0x1d02
movl %ebx, %r15d
andl $0xf, %r15d
leal (,%r15,8), %eax
movl %eax, 0x34(%rsp)
xorl %ecx, %ecx
leaq 0x10(%rsp), %r14
movq 0x38(%rsp), %r12
movq 0x28(%rsp), %r13
leaq 0x10(%rcx), %rbp
cmpq %rbx, %rbp
jbe 0x1cb5
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movl 0x34(%rsp), %eax
movb %al, 0x1f(%rsp)
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
movq %rsp, %rdx
movq %r14, %rcx
callq 0x1080
testq %r15, %r15
je 0x1cee
xorl %eax, %eax
movb (%r12,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
xorb 0x10(%rsp,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
cmpq %rax, %r15
jne 0x1c9a
jmp 0x1cee
vmovaps 0x40(%rsp), %xmm0
movq 0x38(%rsp), %rax
leaq (%rax,%rcx), %rdi
vxorps (%rax,%rcx), %xmm0, %xmm0
vmovaps %xmm0, 0x40(%rsp)
addq 0x28(%rsp), %rcx
leaq 0x50(%rsp), %rsi
movq %rsp, %rdx
callq 0x1080
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x1ce6
addq $0x10, %r13
addq $0x10, %r12
movq %rbp, %rcx
cmpq %rbx, %rbp
jb 0x1c65
movl %ebx, %eax
shlb $0x4, %al
incb %al
movq %rsp, %rdx
movb %al, 0xf(%rdx)
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r14, %rcx
callq 0x1080
vmovaps 0x60(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
movq 0x28(%rsp), %rax
vmovups %xmm0, (%rax,%rbx)
addq $0x10, %rbx
movq 0x70(%rsp), %rax
movq %rbx, (%rax)
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq