// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/ascon128v11/ref/ascon.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
    unsigned char *m, unsigned long long *mlen,
    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) {

  *mlen = 0;
  if (clen < CRYPTO_KEYBYTES)
    return -1;

  int klen = CRYPTO_KEYBYTES;
  //int nlen = CRYPTO_NPUBBYTES;
  int size = 320 / 8;
  int rate = 64 / 8;
  int capacity = size - rate;
  int a = 12;
  int b = 6;
  i64 s = adlen / rate + 1;
  i64 t = (clen - klen) / rate + 1;
  i64 l = (clen - klen) % rate;

  u8 S[size];
  u8 A[s * rate];
  u8 M[t * rate];
  i64 i, j;

  // pad associated data
  for (i = 0; i < adlen; ++i)
    A[i] = ad[i];
  A[adlen] = 0x80;
  for (i = adlen + 1; i < s * rate; ++i)
    A[i] = 0;

  // initialization
  S[0] = klen * 8;
  S[1] = rate * 8;
  S[2] = a;
  S[3] = b;
  for (i = 4; i < size - 2 * klen; ++i)
    S[i] = 0;
  for (i = 0; i < klen; ++i)
    S[size - 2 * klen + i] = k[i];
  for (i = 0; i < klen; ++i)
    S[size - klen + i] = npub[i];
  printstate("initial value:", S);
  permutation(S, a);
  for (i = 0; i < klen; ++i)
    S[rate + klen + i] ^= k[i];
  printstate("initialization:", S);

  // process associated data
  if (adlen) {
    for (i = 0; i < s; ++i) {
      for (j = 0; j < rate; ++j)
        S[j] ^= A[i * rate + j];
      permutation(S, b);
    }
  }
  S[size - 1] ^= 1;
  printstate("process associated data:", S);

  // process plaintext
  for (i = 0; i < t - 1; ++i) {
    for (j = 0; j < rate; ++j) {
      M[i * rate + j] = S[j] ^ c[i * rate + j];
      S[j] = c[i * rate + j];
    }
    permutation(S, b);
  }
  for (j = 0; j < l; ++j)
    M[(t - 1) * rate + j] = S[j] ^ c[(t - 1) * rate + j];
  for (j = 0; j < l; ++j)
    S[j] = c[(t - 1) * rate + j];
  S[l] ^= 0x80;
  printstate("process plaintext:", S);

  // finalization
  for (i = 0; i < klen; ++i)
    S[rate + i] ^= k[i];
  permutation(S, a);
  for (i = 0; i < klen; ++i)
    S[rate + klen + i] ^= k[i];
  printstate("finalization:", S);

  // return -1 if verification fails
  for (i = 0; i < klen; ++i)
    if (c[clen - klen + i] != S[rate + klen + i])
      return -1;

  // return plaintext
  *mlen = clen - klen;
  for (i = 0; i < *mlen; ++i)
    m[i] = M[i];

  return 0;
}

// Assembly:
movq $0x0, (%rsi)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r8
jb 0x1933
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r15
movq %rdi, -0x48(%rbp)
movq %rsi, -0x50(%rbp)
movq %rsp, -0x68(%rbp)
movq 0x10(%rbp), %r14
leaq -0x10(%r8), %rdx
movq %rsp, %r13
addq $-0x30, %r13
movq %r13, %rsp
movq %r14, %rax
andq $-0x8, %rax
leaq 0x8(%rax), %rsi
movq %rsp, %r12
addq $0x17, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rdx, %rax
andq $-0x8, %rax
movq %rsp, %rcx
movq %rax, -0x58(%rbp)
addq $0x17, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
testq %r14, %r14
movq %rdx, -0x30(%rbp)
je 0x1727
movq %r12, %rdi
movq %rsi, -0x40(%rbp)
movq %r9, %rsi
movq %r14, %rdx
movq %r8, %rbx
callq 0x1080
movq -0x40(%rbp), %rsi
movq %rbx, %r8
movq 0x20(%rbp), %rbx
movq 0x18(%rbp), %rax
movb $-0x80, (%r12,%r14)
leaq 0x1(%r14), %rdi
cmpq %rsi, %rdi
jge 0x175d
addq %r12, %rdi
movl %r14d, %edx
notl %edx
andl $0x7, %edx
xorl %esi, %esi
movq %r8, %rbx
callq 0x1030
movq 0x18(%rbp), %rax
movq %rbx, %r8
movq 0x20(%rbp), %rbx
andl $0x7, %r8d
movq %r8, -0x40(%rbp)
movq $0x60c4080, (%r13)      # imm = 0x60C4080
vmovups (%rbx), %xmm0
vmovups %xmm0, 0x8(%r13)
vmovups (%rax), %xmm0
vmovups %xmm0, 0x18(%r13)
movq %r13, %rdi
movl $0xc, %esi
callq 0x1070
vmovdqu 0x18(%r13), %xmm0
vpxor (%rbx), %xmm0, %xmm0
vmovdqu %xmm0, 0x18(%r13)
testq %r14, %r14
je 0x17c9
shrq $0x3, %r14
incq %r14
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rax
xorq %rax, (%r13)
movq %r13, %rdi
movl $0x6, %esi
callq 0x1070
incq %rbx
cmpq %rbx, %r14
jne 0x17ac
xorb $0x1, 0x27(%r13)
cmpq $0x8, -0x30(%rbp)
movq -0x60(%rbp), %rbx
jb 0x1814
movq -0x30(%rbp), %r14
shrq $0x3, %r14
cmpq $0x1, %r14
adcq $0x0, %r14
xorl %r12d, %r12d
movq (%r15,%r12,8), %rax
movq (%r13), %rcx
xorq %rax, %rcx
movq %rcx, (%rbx,%r12,8)
movq %rax, (%r13)
movq %r13, %rdi
movl $0x6, %esi
callq 0x1070
incq %r12
cmpq %r12, %r14
jne 0x17ec
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
je 0x1896
leaq -0x1(%rdx), %rax
vpbroadcastq %rax, %ymm0
vpcmpnltuq 0x80e(%rip), %ymm0, %k0 # 0x2040
vpcmpnltuq 0x823(%rip), %ymm0, %k1 # 0x2060
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpnltuq 0x82e(%rip), %ymm0, %k1 # 0x2080
vpcmpnltuq 0x843(%rip), %ymm0, %k2 # 0x20a0
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
kunpckbw %k0, %k1, %k1
movq -0x58(%rbp), %rax
leaq (%r15,%rax), %rsi
vmovdqu8 (%r15,%rax), %xmm0 {%k1} {z}
vpxor (%r13), %xmm0, %xmm0
vmovdqu8 %xmm0, (%rbx,%rax) {%k1}
movq %r13, %rdi
vzeroupper
callq 0x1080
movq -0x40(%rbp), %rdx
leaq 0x18(%r13), %r14
xorb $-0x80, (%r13,%rdx)
movq 0x20(%rbp), %rax
vmovups (%rax), %xmm0
vmovaps %xmm0, -0x40(%rbp)
vxorps 0x8(%r13), %xmm0, %xmm0
vmovups %xmm0, 0x8(%r13)
movq %r13, %rdi
movl $0xc, %esi
callq 0x1070
vmovaps -0x40(%rbp), %xmm0
vxorps 0x18(%r13), %xmm0, %xmm0
vmovups %xmm0, 0x18(%r13)
addq -0x30(%rbp), %r15
xorl %eax, %eax
movb (%r15,%rax), %cl
cmpb (%r14,%rax), %cl
jne 0x1918
incq %rax
cmpq $0x10, %rax
jne 0x18dd
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
testq %rax, %rax
je 0x191f
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x48(%rbp), %rdi
movb (%rbx,%rcx), %dl
movb %dl, (%rdi,%rcx)
incq %rcx
cmpq (%rsi), %rcx
jb 0x1908
jmp 0x1921
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1921
xorl %eax, %eax
movq -0x68(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq