// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/scream10v3/sse/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) {
  v16qi data[16];
  uint8_t buffer[16*16] = {0};
  v16qi tweakey[16*TWEAKEY_SIZE];
  *outputmlen = clen-CRYPTO_ABYTES;

  v16qi auth = CV(0);
  v16qi checksum = CV(0);

  // Associated Data
  if (adlen > 0) {
    size_t idx=0;
    tweakey_schedule(k, npub, TWEAK_AD, tweakey);
    
    for (idx=0; idx+256<adlen; idx+=16*16) {
      read128(ad+idx, data);
      encrypt_tweakey(data, tweakey);
      tweakey_increment(tweakey, idx);
      write128_checksum(data, NULL, &auth, 16);
    }

    // Final chunk
    uint8_t buffer2[16*16] = {0};
    memcpy(buffer2, ad+idx, adlen-idx);
    if ((adlen % 16) == 0) {
      tweakey_set(tweakey, (adlen-idx-1)/16, 15, TWEAK_AD_LAST_FULL);
    } else {
      tweakey_set(tweakey, (adlen-idx-1)/16, 15, TWEAK_AD_LAST_PARTIAL);
      buffer2[adlen-idx] = 0x80;
    }
    read128(buffer2, data);
    encrypt_tweakey(data, tweakey);
    write128_checksum(data, NULL, &auth, (adlen-idx+15)/16);
  }

  auth ^= sse_load(c+*outputmlen);

  // Message
  size_t idx=0;
  tweakey_schedule(k, npub, TWEAK_MESSAGE, tweakey);
  for (idx=0; idx+256 <= *outputmlen; idx+=256) {
    read128(c+idx, data);
    decrypt_tweakey(data, tweakey);
    tweakey_increment(tweakey, idx);
    write128_checksum(data, m+idx, &checksum, 16);
  }

  int fullblocks = (*outputmlen-idx)/16;

  // Partial block
  if (*outputmlen % 16) {
    // use slot fullblocks (tweak is already correct)
    uint8_t buffer2[16*16] = {0};
    buffer2[16*fullblocks+15] = 8*(*outputmlen%16);
    read128(buffer2, data);
    encrypt_tweakey(data, tweakey);
    write128(data, buffer2);
    unsigned i;
    for (i=0; i<*outputmlen%16; i++)
      m[16*(*outputmlen/16) + i] = c[16*(*outputmlen/16) + i] ^ buffer2[16*fullblocks+i];
    update_checksum(m+16*(*outputmlen/16), &checksum, *outputmlen%16);
  }

  // Last chunk: remaining full blocks, and checksum
  memcpy(buffer, c+idx, 16*fullblocks);
  sse_store(buffer+16*fullblocks, auth);
  tweakey_set(tweakey, fullblocks, 15, 2*8*(*outputmlen)+1);
  read128(buffer, data);
  decrypt_tweakey(data, tweakey);
  write128_checksum2(data, buffer, &checksum, fullblocks+1, fullblocks);
  memcpy(m+idx, buffer, 16*fullblocks);

  // Verify tag
  if (memcmp(&checksum, buffer+16*fullblocks, 16) != 0) {
    memset(m, 0, *outputmlen);
    return -1;
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x858, %rsp            # imm = 0x858
movq %rcx, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x8a0(%rsp), %rdi
movq 0x898(%rsp), %rcx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x130(%rsp)
vmovups %ymm0, 0x110(%rsp)
vmovups %ymm0, 0xf0(%rsp)
vmovups %ymm0, 0xd0(%rsp)
vmovups %ymm0, 0xb0(%rsp)
vmovups %ymm0, 0x90(%rsp)
vmovups %ymm0, 0x70(%rsp)
vmovups %ymm0, 0x50(%rsp)
addq $-0x10, %r8
movq %rsi, 0x10(%rsp)
movq %r8, (%rsi)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovaps %xmm0, 0x30(%rsp)
cmpq $0x0, 0x890(%rsp)
je 0x30d0
movq %r9, %r12
pushq $0x2
popq %rdx
leaq 0x350(%rsp), %r13
movq %rcx, %rsi
movq %r13, %rcx
vzeroupper
callq 0x10c0
movq 0x890(%rsp), %rax
leaq (%rsp,%rax), %r15
addq $0x150, %r15            # imm = 0x150
xorl %r14d, %r14d
leaq 0x250(%rsp), %rbx
leaq 0x100(%r14), %rbp
cmpq %rax, %rbp
jae 0x2fe7
leaq (%r12,%r14), %rdi
movq %rbx, %rsi
callq 0x10f0
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1110
movq %r13, %rdi
movq %r14, %rsi
callq 0x1050
movq %rbx, %rdi
xorl %esi, %esi
leaq 0x20(%rsp), %rdx
pushq $0x10
popq %rcx
callq 0x1040
movq 0x890(%rsp), %rax
addq $-0x100, %r15
movq %rbp, %r14
jmp 0x2f93
vxorps %xmm0, %xmm0, %xmm0
leaq 0x150(%rsp), %rdi
vmovups %ymm0, 0xe0(%rdi)
vmovups %ymm0, 0xc0(%rdi)
vmovups %ymm0, 0xa0(%rdi)
vmovups %ymm0, 0x80(%rdi)
vmovups %ymm0, 0x60(%rdi)
vmovups %ymm0, 0x40(%rdi)
vmovups %ymm0, 0x20(%rdi)
vmovups %ymm0, (%rdi)
addq %r14, %r12
movq %rax, %r13
subq %r14, %r13
movq %r12, %rsi
movq %r13, %rdx
movq %rax, %rbx
vzeroupper
callq 0x1130
leaq -0x1(%r13), %rsi
shrq $0x4, %rsi
leaq 0x350(%rsp), %rdi
pushq $0xf
popq %rdx
testb $0xf, %bl
je 0x3066
pushq $0x6
popq %rcx
callq 0x1060
movb $-0x80, (%r15)
jmp 0x306e
pushq $0x4
popq %rcx
callq 0x1060
leaq 0x150(%rsp), %rdi
leaq 0x250(%rsp), %rbx
movq %rbx, %rsi
callq 0x10f0
leaq 0x350(%rsp), %rsi
movq %rbx, %rdi
callq 0x1110
addq $0xf, %r13
shrq $0x4, %r13
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movl %r13d, %ecx
callq 0x1040
movq 0x10(%rsp), %rax
movq (%rax), %r8
vmovaps (%r14), %xmm0
movq 0x898(%rsp), %rcx
movq 0x8a0(%rsp), %rdi
movq 0x8(%rsp), %rax
vxorps (%rax,%r8), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
leaq 0x350(%rsp), %r15
movq %rcx, %rsi
xorl %edx, %edx
movq %r15, %rcx
vzeroupper
callq 0x10c0
leaq 0x250(%rsp), %rbx
pushq $0x10
popq %rbp
leaq 0x100(%r14), %r12
movq 0x10(%rsp), %rax
movq (%rax), %r13
cmpq %r13, %r12
ja 0x315e
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rdi
movq %rbx, %rsi
callq 0x10f0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10b0
movq %r15, %rdi
movq %r14, %rsi
callq 0x1050
addq 0x18(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movl %ebp, %ecx
callq 0x1040
movq %r12, %r14
jmp 0x3107
movq %r13, %rcx
subq %r14, %rcx
shrq $0x4, %rcx
testb $0xf, %r13b
je 0x3262
vxorps %xmm0, %xmm0, %xmm0
leaq 0x150(%rsp), %rbx
vmovups %ymm0, 0xe0(%rbx)
vmovups %ymm0, 0xc0(%rbx)
vmovups %ymm0, 0xa0(%rbx)
vmovups %ymm0, 0x80(%rbx)
vmovups %ymm0, 0x60(%rbx)
vmovups %ymm0, 0x40(%rbx)
vmovups %ymm0, 0x20(%rbx)
vmovups %ymm0, (%rbx)
leal (,%r13,8), %eax
andb $0x78, %al
movq %rcx, 0x48(%rsp)
movl %ecx, %ebp
shll $0x4, %ebp
movslq %ebp, %rcx
movb %al, 0x15f(%rsp,%rcx)
leaq 0x250(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
vzeroupper
callq 0x10f0
leaq 0x350(%rsp), %rsi
movq %r15, %rdi
callq 0x1110
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1070
subl %r14d, %r13d
andl $-0x10, %r13d
xorl %eax, %eax
movl %r13d, %ecx
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %rdx
movq (%rdx), %r13
movl %r13d, %edx
andl $0xf, %edx
movq %r13, %rdi
andq $-0x10, %rdi
cmpq %rax, %rdx
jbe 0x324e
movq 0x8(%rsp), %rdx
addq %rdi, %rdx
leal (%rcx,%rax), %esi
movb 0x150(%rsp,%rsi), %sil
xorb (%rax,%rdx), %sil
addq %r12, %rdi
movb %sil, (%rax,%rdi)
incq %rax
jmp 0x3211
addq %r12, %rdi
leaq 0x30(%rsp), %rsi
callq 0x1100
movq 0x48(%rsp), %rbx
jmp 0x326f
movl %ecx, %ebp
shll $0x4, %ebp
movq 0x18(%rsp), %r12
movq %rcx, %rbx
movq 0x8(%rsp), %rsi
addq %r14, %rsi
movslq %ebp, %rbp
leaq 0x50(%rsp), %rdi
movq %rbp, %rdx
callq 0x1130
vmovaps 0x20(%rsp), %xmm0
vmovaps %xmm0, 0x50(%rsp,%rbp)
shlb $0x4, %r13b
incb %r13b
movzbl %r13b, %ecx
leaq 0x350(%rsp), %rdi
pushq $0xf
popq %rdx
movl %ebx, %esi
callq 0x1060
leaq 0x250(%rsp), %r15
leaq 0x50(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x10f0
movq %r15, %rdi
leaq 0x350(%rsp), %rsi
callq 0x10b0
leal 0x1(%rbx), %ecx
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
movq %rdx, %r15
movl %ebx, %r8d
callq 0x10d0
addq %r12, %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1130
vmovdqa (%r15), %xmm0
vpxor 0x50(%rsp,%rbp), %xmm0, %xmm0
vptest %xmm0, %xmm0
je 0x332b
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1030
pushq $-0x1
popq %rax
jmp 0x332d
xorl %eax, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq