// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/scream12v2/ref/tae.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
                        unsigned char *m,unsigned long long *outputmlen,
                        unsigned char *nsec,
                        const unsigned char *c,unsigned long long clen,
                        const unsigned char *ad,unsigned long long adlen,
                        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);

    if (clen < CRYPTO_ABYTES) return -1;
    *outputmlen = clen - CRYPTO_ABYTES;

    /* Process Associated Data */
    memcpy(T, npub, CRYPTO_NPUBBYTES);
    T[15]  = 0x02;           /* control byte for a.d. */
    
    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 < *outputmlen; i+=blocklen) {
      if (i+blocklen <= *outputmlen) {
	/* Full block (can be last block) */
	LS_decrypt(c+i, K, T, m+i);
	for (j=0; j < blocklen; j++)
	  sum[j] ^= m[i+j];

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

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

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

    LS_encrypt(sum, K, T, sum);
    for (i = 0; i < CRYPTO_ABYTES; i++)
      if (c[*outputmlen+i] != (auth[i] ^ sum[i])) {
	memset(m, 0, *outputmlen);
	return -1;
      }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, 0x18(%rsp)
movq 0xc0(%rsp), %rax
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovaps %xmm0, 0x60(%rsp)
vmovaps %xmm0, 0x40(%rsp)
vmovups (%rax), %xmm0
vmovaps %xmm0, 0x30(%rsp)
pushq $-0x1
popq %rax
cmpq $0x10, %r8
jb 0x1cbc
movq %r9, %r13
movq %rsi, %r12
movq %rcx, 0x10(%rsp)
movq 0xb8(%rsp), %rcx
movq 0xb0(%rsp), %rbp
addq $-0x10, %r8
movq %r8, (%rsi)
movl 0x7(%rcx), %eax
leaq 0x20(%rsp), %rbx
movl %eax, 0x7(%rbx)
movq (%rcx), %rax
movq %rax, (%rbx)
movb $0x2, 0xf(%rbx)
movl %ebp, %eax
andl $0xf, %eax
movq %rax, 0x58(%rsp)
xorl %esi, %esi
leaq 0x30(%rsp), %r15
cmpq %rbp, %rsi
jae 0x1b89
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
leaq 0x10(%rsi), %r14
cmpq %rbp, %r14
jae 0x1b0b
vmovups (%r13,%rsi), %xmm0
vmovaps %xmm0, (%rsp)
jmp 0x1b54
addq %r13, %rsi
cmpq %rbp, %r14
jne 0x1b23
vmovups (%rsi), %xmm0
vmovaps %xmm0, (%rsp)
movb $0x4, 0x2f(%rsp)
jmp 0x1b54
movq %rsp, %rdi
movq %rbp, %r15
movq %rbx, %rbp
movq %r13, %rbx
movq 0x58(%rsp), %r13
movq %r13, %rdx
callq 0x1090
movb $-0x80, (%rsp,%r13)
movq %rbx, %r13
movq %rbp, %rbx
movq %r15, %rbp
leaq 0x30(%rsp), %r15
movb $0x6, 0x2f(%rsp)
movq %rsp, %rcx
movq %rcx, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1080
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1b79
movb (%rsp,%rax), %cl
xorb %cl, 0x60(%rsp,%rax)
incq %rax
jmp 0x1b67
movq %rbx, %rdi
callq 0x1a45
movq %r14, %rsi
jmp 0x1ae2
leaq 0x20(%rsp), %r13
movb $0x0, 0xf(%r13)
andl $0x0, 0xb(%r13)
movq 0xb8(%rsp), %rcx
movq (%rcx), %rax
movq %rax, (%r13)
movl 0x7(%rcx), %eax
movl %eax, 0x7(%r13)
xorl %ecx, %ecx
movq %rsp, %rbx
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
movq (%r12), %rax
cmpq %rax, %rcx
jae 0x1c62
leaq 0x10(%rcx), %rbp
cmpq %rax, %rbp
jbe 0x1c1a
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rsp)
shlb $0x3, %al
andb $0x78, %al
movb %al, 0xf(%rsp)
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1080
xorl %eax, %eax
movq (%r12), %rcx
andl $0xf, %ecx
cmpq %rax, %rcx
jbe 0x1c52
movb (%r14,%rax), %cl
xorb (%rsp,%rax), %cl
movb %cl, (%r15,%rax)
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x1bfa
movq 0x10(%rsp), %rax
leaq (%rax,%rcx), %rdi
addq 0x18(%rsp), %rcx
leaq 0x30(%rsp), %rsi
movq %r13, %rdx
callq 0x1060
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1c4a
movb (%r15,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x1c37
movq %r13, %rdi
callq 0x1a45
addq $0x10, %r15
addq $0x10, %r14
movq %rbp, %rcx
jmp 0x1bbd
shlb $0x4, %al
incb %al
leaq 0x20(%rsp), %rdx
movb %al, 0xf(%rdx)
leaq 0x30(%rsp), %rsi
leaq 0x40(%rsp), %rcx
movq %rcx, %rdi
callq 0x1080
movq (%r12), %rdx
movq 0x10(%rsp), %rdi
addq %rdx, %rdi
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1cba
movb 0x40(%rsp,%rax), %cl
xorb 0x60(%rsp,%rax), %cl
leaq 0x1(%rax), %rsi
cmpb %cl, (%rdi,%rax)
movq %rsi, %rax
je 0x1c8f
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x1030
pushq $-0x1
popq %rax
jmp 0x1cbc
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop