// File: /workspace/github_c_makefile_star2_O3/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 %rsi, %rbx
movq %rdi, 0x18(%rsp)
movq 0x8a0(%rsp), %rdi
movq 0x898(%rsp), %rsi
movq 0x890(%rsp), %r15
vpxor %xmm0, %xmm0, %xmm0
vmovdqu %ymm0, 0x130(%rsp)
vmovdqu %ymm0, 0x110(%rsp)
vmovdqu %ymm0, 0xf0(%rsp)
vmovdqu %ymm0, 0xd0(%rsp)
vmovdqu %ymm0, 0xb0(%rsp)
vmovdqu %ymm0, 0x90(%rsp)
vmovdqu %ymm0, 0x70(%rsp)
vmovdqu %ymm0, 0x50(%rsp)
addq $-0x10, %r8
movq %r8, (%rbx)
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
vmovdqa %xmm0, 0x20(%rsp)
testq %r15, %r15
movq %rbx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
je 0x3195
movq %r9, %r12
leaq 0x350(%rsp), %rcx
movl $0x2, %edx
vzeroupper
callq 0x10a0
cmpq $0x101, %r15            # imm = 0x101
jb 0x3095
xorl %ebx, %ebx
leaq 0x250(%rsp), %r14
leaq 0x350(%rsp), %r13
leaq 0x30(%rsp), %rbp
leaq (%r12,%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %r14, %rdi
movq %r13, %rsi
callq 0x10d0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1040
movq %r14, %rdi
xorl %esi, %esi
movq %rbp, %rdx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
leaq 0x100(%rbx), %rax
addq $0x200, %rbx            # imm = 0x200
cmpq %r15, %rbx
movq %rax, %rbx
jb 0x303f
jmp 0x3097
xorl %eax, %eax
vpxor %xmm0, %xmm0, %xmm0
leaq 0x150(%rsp), %rdi
vmovdqu %ymm0, 0xe0(%rdi)
vmovdqu %ymm0, 0xc0(%rdi)
vmovdqu %ymm0, 0xa0(%rdi)
vmovdqu %ymm0, 0x80(%rdi)
vmovdqu %ymm0, 0x60(%rdi)
vmovdqu %ymm0, 0x40(%rdi)
vmovdqu %ymm0, 0x20(%rdi)
vmovdqu %ymm0, (%rdi)
addq %rax, %r12
movq %r15, %r13
subq %rax, %r13
movq %r12, %rsi
movq %r13, %rdx
vzeroupper
callq 0x10f0
leaq -0x1(%r13), %rsi
shrq $0x4, %rsi
leaq 0x350(%rsp), %rdi
movl $0xf, %edx
testb $0xf, %r15b
je 0x311d
movl $0x6, %ecx
callq 0x1050
movb $-0x80, 0x150(%rsp,%r13)
jmp 0x3127
movl $0x4, %ecx
callq 0x1050
leaq 0x150(%rsp), %rdi
leaq 0x250(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
leaq 0x350(%rsp), %rsi
movq %rbx, %rdi
callq 0x10d0
addq $0xf, %r13
shrq $0x4, %r13
leaq 0x30(%rsp), %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movl %r13d, %ecx
movl %r13d, %r8d
callq 0x10b0
movq 0x10(%rsp), %rbx
movq (%rbx), %r8
vmovdqa (%r14), %xmm0
movq 0x8(%rsp), %rcx
movq 0x898(%rsp), %rsi
movq 0x8a0(%rsp), %rdi
vpxor (%rcx,%r8), %xmm0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
xorl %r12d, %r12d
leaq 0x350(%rsp), %rcx
xorl %edx, %edx
vzeroupper
callq 0x10a0
movq (%rbx), %r15
cmpq $0x100, %r15            # imm = 0x100
jb 0x323f
xorl %r13d, %r13d
leaq 0x250(%rsp), %rbp
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
leaq (%r14,%r13), %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %rbp, %rdi
leaq 0x350(%rsp), %r15
movq %r15, %rsi
callq 0x1090
movq %r15, %rdi
movq %r13, %rsi
callq 0x1040
movq 0x18(%rsp), %rax
leaq (%rax,%r13), %rsi
movq %rbp, %rdi
leaq 0x20(%rsp), %rdx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
movq (%rbx), %r15
leaq 0x100(%r13), %r12
addq $0x200, %r13            # imm = 0x200
cmpq %r15, %r13
movq %r12, %r13
jbe 0x31d7
movq %r15, %r13
subq %r12, %r13
shrq $0x4, %r13
testb $0xf, %r15b
je 0x339c
vpxor %xmm0, %xmm0, %xmm0
leaq 0x150(%rsp), %rbx
vmovdqu %ymm0, 0xe0(%rbx)
vmovdqu %ymm0, 0xc0(%rbx)
vmovdqu %ymm0, 0xa0(%rbx)
vmovdqu %ymm0, 0x80(%rbx)
vmovdqu %ymm0, 0x60(%rbx)
vmovdqu %ymm0, 0x40(%rbx)
vmovdqu %ymm0, 0x20(%rbx)
vmovdqu %ymm0, (%rbx)
shlb $0x3, %r15b
andb $0x78, %r15b
movl %r13d, %ebp
shll $0x4, %ebp
movslq %ebp, %rax
movb %r15b, 0x15f(%rsp,%rax)
leaq 0x250(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
vzeroupper
callq 0x10e0
leaq 0x350(%rsp), %rsi
movq %r14, %rdi
callq 0x10d0
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
movq 0x10(%rsp), %r8
movq (%r8), %r15
testb $0xf, %r15b
je 0x33a2
movl %r13d, %eax
shll $0x4, %eax
xorl %ecx, %ecx
movq 0x18(%rsp), %rdi
movq 0x8(%rsp), %rsi
andq $-0x10, %r15
leaq (%rsi,%r15), %rdx
leal (%rax,%rcx), %r9d
movb 0x150(%rsp,%r9), %r9b
xorb (%rcx,%rdx), %r9b
addq %rdi, %r15
movb %r9b, (%rcx,%r15)
movq (%r8), %r15
movl %r15d, %edx
andl $0xf, %edx
incq %rcx
cmpq %rcx, %rdx
ja 0x3311
testq %rdx, %rdx
je 0x33a7
movq %r15, %rax
andq $-0x10, %rax
addq %rdi, %rax
vmovdqa 0x20(%rsp), %xmm0
movl %r15d, %ecx
andl $0xf, %ecx
xorl %edx, %edx
vmovdqa 0x11aa(%rip), %xmm1     # 0x4510
movl %edx, %edi
andl $0xf, %edi
vmovdqa %xmm0, 0x40(%rsp)
movb 0x40(%rsp,%rdi), %dil
xorb (%rax,%rdx), %dil
vpbroadcastb %edx, %xmm2
vpcmpeqb %xmm1, %xmm2, %k1
vpbroadcastb %edi, %xmm0 {%k1}
incq %rdx
cmpq %rdx, %rcx
jne 0x3366
vmovdqa %xmm0, 0x20(%rsp)
jmp 0x33a7
movl %r13d, %ebp
shll $0x4, %ebp
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movslq %ebp, %rbp
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rdx
callq 0x10f0
vmovaps 0x30(%rsp), %xmm0
vmovaps %xmm0, 0x50(%rsp,%rbp)
shlb $0x4, %r15b
incb %r15b
movzbl %r15b, %ecx
leaq 0x350(%rsp), %rdi
movl %r13d, %esi
movl $0xf, %edx
callq 0x1050
xorl %ebx, %ebx
leaq 0x250(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %r15, %rdi
leaq 0x350(%rsp), %rsi
callq 0x1090
leal 0x1(%r13), %ecx
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movl %r13d, %r8d
callq 0x10b0
movq 0x18(%rsp), %r15
addq %r15, %r12
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x10f0
leaq 0x20(%rsp), %rax
vmovdqa (%rax), %xmm0
vpxor 0x50(%rsp,%rbp), %xmm0, %xmm0
vptest %xmm0, %xmm0
je 0x346c
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1030
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x858, %rsp            # imm = 0x858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq