// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rsi, %r15
movq %rdi, (%rsp)
movq 0x8a0(%rsp), %rdi
movq 0x898(%rsp), %rsi
movq 0x890(%rsp), %rbx
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 %r8, (%r15)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovaps %xmm0, 0x10(%rsp)
testq %rbx, %rbx
movq %r15, 0x8(%rsp)
je 0x31a0
movq %r9, %r12
movq %r14, 0x38(%rsp)
leaq 0x350(%rsp), %rcx
movl $0x2, %edx
vzeroupper
callq 0x10a0
movq %rbx, %r15
cmpq $0x101, %rbx            # imm = 0x101
jb 0x30ab
xorl %ebx, %ebx
leaq 0x250(%rsp), %r14
leaq 0x350(%rsp), %r13
leaq 0x20(%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 0x3055
jmp 0x30ad
xorl %eax, %eax
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 %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 0x3133
movl $0x6, %ecx
callq 0x1050
movb $-0x80, 0x150(%rsp,%r13)
jmp 0x313d
movl $0x4, %ecx
callq 0x1050
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %r15
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 0x20(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movl %r13d, %ecx
movl %r13d, %r8d
callq 0x10b0
movq 0x898(%rsp), %rsi
movq 0x8a0(%rsp), %rdi
movq (%r15), %rax
vmovdqa 0x20(%rsp), %xmm0
movq %r14, %rbx
vpxor (%r14,%rax), %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
leaq 0x350(%rsp), %rcx
xorl %edx, %edx
vzeroupper
callq 0x10a0
movq %r15, %r14
movq (%r15), %rax
cmpq $0x100, %rax            # imm = 0x100
jb 0x324d
xorl %r13d, %r13d
leaq 0x250(%rsp), %rbp
movq (%rsp), %r15
leaq (%rbx,%r13), %rdi
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
movq %rbp, %rdi
leaq 0x350(%rsp), %r12
movq %r12, %rsi
callq 0x1090
movq %r12, %rdi
movq %r13, %rsi
callq 0x1040
leaq (%r15,%r13), %rsi
movq %rbp, %rdi
leaq 0x10(%rsp), %rdx
movl $0x10, %ecx
movl $0x10, %r8d
callq 0x10b0
movq (%r14), %rax
leaq 0x100(%r13), %r12
addq $0x200, %r13            # imm = 0x200
cmpq %rax, %r13
movq %r12, %r13
jbe 0x31ea
movq %rax, %r13
subq %r12, %r13
shrq $0x4, %r13
testb $0xf, %al
movq %r14, %r15
movq %rbx, %rbp
je 0x3397
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, %al
andb $0x78, %al
movl %r13d, %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
movb %al, 0x15f(%rsp,%rcx)
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 (%r15), %rax
movq %rax, %rcx
andq $0xf, %rcx
je 0x3343
movl %r13d, %edx
shll $0x4, %edx
xorl %esi, %esi
movq (%rsp), %r8
andq $-0x10, %rax
leaq (%rax,%rbp), %rcx
leal (%rdx,%rsi), %edi
movb 0x150(%rsp,%rdi), %dil
xorb (%rsi,%rcx), %dil
addq %r8, %rax
movb %dil, (%rsi,%rax)
movq (%r15), %rax
movl %eax, %ecx
andl $0xf, %ecx
incq %rsi
cmpq %rsi, %rcx
ja 0x3313
jmp 0x3347
movq (%rsp), %r8
testq %rcx, %rcx
je 0x3397
andq $-0x10, %rax
addq %r8, %rax
vmovdqa 0x10(%rsp), %xmm0
xorl %edx, %edx
vmovdqa 0x11ad(%rip), %xmm1     # 0x4510
movl %edx, %esi
andl $0xf, %esi
vmovdqa %xmm0, 0x40(%rsp)
movb 0x40(%rsp,%rsi), %sil
xorb (%rax,%rdx), %sil
vpbroadcastb %edx, %xmm2
vpcmpeqb %xmm1, %xmm2, %k1
vpbroadcastb %esi, %xmm0 {%k1}
incq %rdx
cmpq %rdx, %rcx
jne 0x3363
vmovdqa %xmm0, 0x10(%rsp)
addq %r12, %rbp
movq %rbp, %rsi
movslq %r13d, %rbp
shlq $0x4, %rbp
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rdx
callq 0x10f0
vmovaps 0x20(%rsp), %xmm0
vmovaps %xmm0, 0x50(%rsp,%rbp)
movb (%r15), %al
shlb $0x4, %al
incb %al
movzbl %al, %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 0x10(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movl %r13d, %r8d
callq 0x10b0
movq (%rsp), %r15
addq %r15, %r12
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x10f0
leaq 0x10(%rsp), %rax
vmovdqa (%rax), %xmm0
vpxor 0x50(%rsp,%rbp), %xmm0, %xmm0
vptest %xmm0, %xmm0
je 0x3462
movq 0x8(%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