// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
movq %r9, %r13
movq %r8, %rbp
movq %rcx, %rbx
movq %rdx, 0x28(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, 0x20(%rsp)
movq 0xc8(%rsp), %rcx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x70(%rsp)
vmovaps %xmm0, 0x30(%rsp)
movq 0xd0(%rsp), %rax
vmovups (%rax), %xmm0
leaq 0x50(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x40(%rsp), %r12
andl $0x0, 0xb(%r12)
movq (%rcx), %rax
movq %rax, (%r12)
movl 0x7(%rcx), %eax
movl %eax, 0x7(%r12)
movb $0x2, 0xf(%r12)
movl %r13d, %eax
andl $0xf, %eax
movq %rax, 0x18(%rsp)
xorl %esi, %esi
movq %rsp, %r14
cmpq %r13, %rsi
jae 0x1907
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
leaq 0x10(%rsi), %r15
cmpq %r13, %r15
jae 0x188a
vmovups (%rbp,%rsi), %xmm0
vmovaps %xmm0, (%rsp)
jmp 0x18d0
addq %rbp, %rsi
cmpq %r13, %r15
jne 0x18a2
vmovups (%rsi), %xmm0
vmovaps %xmm0, (%rsp)
movb $0x4, 0x4f(%rsp)
jmp 0x18d0
movq %r14, %rdi
movq %r12, %r14
movq %rbp, %r12
movq %rbx, %rbp
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x1090
movb $-0x80, (%rsp,%rbx)
movq %rbp, %rbx
movq %r12, %rbp
movq %r14, %r12
movq %rsp, %r14
movb $0x6, 0x4f(%rsp)
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1080
xorl %eax, %eax
cmpq $0x10, %rax
je 0x18f7
movb (%rsp,%rax), %cl
xorb %cl, 0x70(%rsp,%rax)
incq %rax
jmp 0x18e5
movq %r12, %rdi
callq 0x1a45
movq %r15, %rsi
jmp 0x1862
leaq 0x40(%rsp), %r13
andl $0x0, 0xb(%r13)
movb $0x0, 0xf(%r13)
movq 0xc8(%rsp), %rcx
movq (%rcx), %rax
movq %rax, (%r13)
movl 0x7(%rcx), %eax
movl %eax, 0x7(%r13)
movl %ebx, %r12d
andl $0xf, %r12d
leal (,%r12,8), %eax
movl %eax, 0x18(%rsp)
xorl %ecx, %ecx
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbp
cmpq %rbx, %rcx
jae 0x19e4
leaq 0x10(%rcx), %r14
cmpq %rbx, %r14
jbe 0x199c
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
movl 0x18(%rsp), %eax
movb %al, 0xf(%rsp)
movq %rsp, %rcx
movq %rcx, %rdi
leaq 0x50(%rsp), %rsi
movq %r13, %rdx
callq 0x1080
xorl %eax, %eax
cmpl %r12d, %eax
jae 0x19d4
movb (%rbp,%rax), %cl
xorb %cl, 0x30(%rsp,%rax)
xorb (%rsp,%rax), %cl
movb %cl, (%r15,%rax)
incq %rax
jmp 0x1983
xorl %eax, %eax
cmpq $0x10, %rax
je 0x19b1
movb (%rbp,%rax), %dl
xorb %dl, 0x30(%rsp,%rax)
incq %rax
jmp 0x199e
movq 0x28(%rsp), %rax
leaq (%rax,%rcx), %rdi
addq 0x20(%rsp), %rcx
leaq 0x50(%rsp), %rsi
movq %r13, %rdx
callq 0x1080
movq %r13, %rdi
callq 0x1a45
addq $0x10, %rbp
addq $0x10, %r15
movq %r14, %rcx
jmp 0x194b
movl %ebx, %eax
shlb $0x4, %al
incb %al
leaq 0x40(%rsp), %rdx
movb %al, 0xf(%rdx)
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rcx
movq %rcx, %rdi
callq 0x1080
movq 0x20(%rsp), %rdx
addq %rbx, %rdx
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1a25
movb 0x30(%rsp,%rax), %cl
xorb 0x70(%rsp,%rax), %cl
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1a0f
addq $0x10, %rbx
movq 0x68(%rsp), %rax
movq %rbx, (%rax)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq