// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x9d8, %rsp            # imm = 0x9D8
movq 0x9f0(%rsp), %rax
movq 0x9e8(%rsp), %rax
movq 0x9e0(%rsp), %rax
movq %rdi, 0x9a8(%rsp)
movq %rsi, 0x9a0(%rsp)
movq %rdx, 0x998(%rsp)
movq %rcx, 0x990(%rsp)
movq %r8, 0x988(%rsp)
movq %r9, 0x980(%rsp)
vpxor %xmm0, %xmm0, %xmm0
vmovups %ymm0, (%rsp)
vmovdqu %ymm0, 0x860(%rsp)
vmovdqu %ymm0, 0x840(%rsp)
vmovdqu %ymm0, 0x820(%rsp)
vmovdqu %ymm0, 0x800(%rsp)
vmovdqu %ymm0, 0x7e0(%rsp)
vmovdqu %ymm0, 0x7c0(%rsp)
vmovdqu %ymm0, 0x7a0(%rsp)
vmovdqu %ymm0, 0x780(%rsp)
movq 0x988(%rsp), %rcx
addq $-0x10, %rcx
movq 0x9a0(%rsp), %rax
movq %rcx, (%rax)
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x270(%rsp)
vmovdqa %xmm0, 0x260(%rsp)
cmpq $0x0, 0x9e0(%rsp)
jbe 0x87d1
movq $0x0, 0x258(%rsp)
movq 0x9f0(%rsp), %rdi
movq 0x9e8(%rsp), %rsi
leaq 0x280(%rsp), %rcx
movl $0x2, %edx
vzeroupper
callq 0x10c0
movq $0x0, 0x258(%rsp)
movq 0x258(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
cmpq 0x9e0(%rsp), %rax
jae 0x86a5
movq 0x980(%rsp), %rdi
addq 0x258(%rsp), %rdi
leaq 0x880(%rsp), %rsi
callq 0x10f0
leaq 0x880(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x1110
leaq 0x280(%rsp), %rdi
movq 0x258(%rsp), %rsi
callq 0x1050
leaq 0x880(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x270(%rsp), %rdx
movl $0x10, %ecx
callq 0x1040
movq 0x258(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, 0x258(%rsp)
jmp 0x8609
leaq 0x150(%rsp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1030
leaq 0x150(%rsp), %rdi
movq 0x980(%rsp), %rsi
addq 0x258(%rsp), %rsi
movq 0x9e0(%rsp), %rdx
subq 0x258(%rsp), %rdx
callq 0x1130
movq 0x9e0(%rsp), %rax
andq $0xf, %rax
cmpq $0x0, %rax
jne 0x872b
leaq 0x280(%rsp), %rdi
movq 0x9e0(%rsp), %rax
subq 0x258(%rsp), %rax
subq $0x1, %rax
shrq $0x4, %rax
movl %eax, %esi
movl $0xf, %edx
movl $0x4, %ecx
callq 0x1060
jmp 0x8774
leaq 0x280(%rsp), %rdi
movq 0x9e0(%rsp), %rax
subq 0x258(%rsp), %rax
subq $0x1, %rax
shrq $0x4, %rax
movl %eax, %esi
movl $0xf, %edx
movl $0x6, %ecx
callq 0x1060
movq 0x9e0(%rsp), %rax
subq 0x258(%rsp), %rax
movb $-0x80, 0x150(%rsp,%rax)
leaq 0x150(%rsp), %rdi
leaq 0x880(%rsp), %rsi
callq 0x10f0
leaq 0x880(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x1110
leaq 0x880(%rsp), %rdi
movq 0x9e0(%rsp), %rax
subq 0x258(%rsp), %rax
addq $0xf, %rax
shrq $0x4, %rax
movl %eax, %ecx
xorl %eax, %eax
movl %eax, %esi
leaq 0x270(%rsp), %rdx
callq 0x1040
movq 0x990(%rsp), %rax
movq 0x9a0(%rsp), %rcx
addq (%rcx), %rax
movq %rax, 0x9b8(%rsp)
movq 0x9b8(%rsp), %rax
vmovdqu (%rax), %xmm0
vpxor 0x270(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x270(%rsp)
movq $0x0, 0x148(%rsp)
movq 0x9f0(%rsp), %rdi
movq 0x9e8(%rsp), %rsi
leaq 0x280(%rsp), %rcx
xorl %edx, %edx
vzeroupper
callq 0x10c0
movq $0x0, 0x148(%rsp)
movq 0x148(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movq 0x9a0(%rsp), %rcx
cmpq (%rcx), %rax
ja 0x88ef
movq 0x990(%rsp), %rdi
addq 0x148(%rsp), %rdi
leaq 0x880(%rsp), %rsi
callq 0x10f0
leaq 0x880(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x10b0
leaq 0x280(%rsp), %rdi
movq 0x148(%rsp), %rsi
callq 0x1050
leaq 0x880(%rsp), %rdi
movq 0x9a8(%rsp), %rsi
addq 0x148(%rsp), %rsi
leaq 0x260(%rsp), %rdx
movl $0x10, %ecx
callq 0x1040
movq 0x148(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, 0x148(%rsp)
jmp 0x8844
movq 0x9a0(%rsp), %rax
movq (%rax), %rax
subq 0x148(%rsp), %rax
shrq $0x4, %rax
movl %eax, 0x144(%rsp)
movq 0x9a0(%rsp), %rax
movq (%rax), %rax
andq $0xf, %rax
cmpq $0x0, %rax
je 0x8a66
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1030
movq 0x9a0(%rsp), %rax
movq (%rax), %rax
andq $0xf, %rax
shlq $0x3, %rax
movb %al, %cl
movl 0x144(%rsp), %eax
shll $0x4, %eax
addl $0xf, %eax
cltq
movb %cl, 0x40(%rsp,%rax)
leaq 0x40(%rsp), %rdi
leaq 0x880(%rsp), %rsi
callq 0x10f0
leaq 0x880(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x1110
leaq 0x880(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1070
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movq 0x9a0(%rsp), %rcx
movq (%rcx), %rcx
andq $0xf, %rcx
cmpq %rcx, %rax
jae 0x8a2c
movq 0x990(%rsp), %rax
movq 0x9a0(%rsp), %rcx
movq (%rcx), %rcx
shrq $0x4, %rcx
shlq $0x4, %rcx
movl 0x3c(%rsp), %edx
addq %rdx, %rcx
movzbl (%rax,%rcx), %eax
movl 0x144(%rsp), %ecx
shll $0x4, %ecx
addl 0x3c(%rsp), %ecx
movl %ecx, %ecx
movzbl 0x40(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0x9a8(%rsp), %rax
movq 0x9a0(%rsp), %rcx
movq (%rcx), %rcx
shrq $0x4, %rcx
shlq $0x4, %rcx
movl 0x3c(%rsp), %esi
addq %rsi, %rcx
movb %dl, (%rax,%rcx)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x89a0
movq 0x9a8(%rsp), %rdi
movq 0x9a0(%rsp), %rax
movq (%rax), %rax
shrq $0x4, %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x9a0(%rsp), %rax
movq (%rax), %rdx
andq $0xf, %rdx
leaq 0x260(%rsp), %rsi
callq 0x1100
leaq 0x780(%rsp), %rdi
movq 0x990(%rsp), %rsi
addq 0x148(%rsp), %rsi
movl 0x144(%rsp), %eax
shll $0x4, %eax
movslq %eax, %rdx
callq 0x1130
leaq 0x780(%rsp), %rax
movl 0x144(%rsp), %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
vmovdqa 0x270(%rsp), %xmm0
movq %rax, 0x9d0(%rsp)
vmovdqa %xmm0, 0x9c0(%rsp)
vmovdqa 0x9c0(%rsp), %xmm0
movq 0x9d0(%rsp), %rax
vmovdqu %xmm0, (%rax)
leaq 0x280(%rsp), %rdi
movl 0x144(%rsp), %esi
movq 0x9a0(%rsp), %rax
movq (%rax), %rax
shlq $0x4, %rax
addq $0x1, %rax
movl $0xf, %edx
movzbl %al, %ecx
callq 0x1060
leaq 0x780(%rsp), %rdi
leaq 0x880(%rsp), %rsi
callq 0x10f0
leaq 0x880(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x10b0
leaq 0x880(%rsp), %rdi
leaq 0x780(%rsp), %rsi
movl 0x144(%rsp), %ecx
addl $0x1, %ecx
movl 0x144(%rsp), %r8d
leaq 0x260(%rsp), %rdx
callq 0x10d0
movq 0x9a8(%rsp), %rdi
addq 0x148(%rsp), %rdi
leaq 0x780(%rsp), %rsi
movl 0x144(%rsp), %eax
shll $0x4, %eax
movslq %eax, %rdx
callq 0x1130
leaq 0x780(%rsp), %rax
movl 0x144(%rsp), %ecx
shll $0x4, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
vmovdqu (%rax), %xmm1
vmovdqu 0x260(%rsp), %xmm0
vpxor %xmm1, %xmm0, %xmm0
vptest %xmm0, %xmm0
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x8be9
movq 0x9a8(%rsp), %rdi
movq 0x9a0(%rsp), %rax
movq (%rax), %rdx
xorl %esi, %esi
callq 0x1030
movl $0xffffffff, 0x9b4(%rsp) # imm = 0xFFFFFFFF
jmp 0x8bf4
movl $0x0, 0x9b4(%rsp)
movl 0x9b4(%rsp), %eax
addq $0x9d8, %rsp            # imm = 0x9D8
retq
nopw %cs:(%rax,%rax)