// File: /workspace/github_c_makefile_star2_O1/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 0x1a59
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
movq %r8, -0x40(%rbp)
leaq -0x10(%r8), %rsi
movq %rsp, %r12
addq $-0x30, %r12
movq %r12, %rsp
movq %r14, %rax
andq $-0x8, %rax
leaq 0x8(%rax), %rdx
movq %rsp, %r13
addq $0x17, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq %rsi, %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 %rsi, -0x30(%rbp)
je 0x184e
movq %r13, %rdi
movq %r9, %rsi
movq %rdx, %rbx
movq %r14, %rdx
callq 0x1080
movq %rbx, %rdx
movq 0x20(%rbp), %rbx
movq 0x18(%rbp), %rcx
movb $-0x80, (%r13,%r14)
leaq 0x1(%r14), %rdi
cmpq %rdx, %rdi
jge 0x187b
addq %r13, %rdi
movl %r14d, %edx
notl %edx
andl $0x7, %edx
xorl %esi, %esi
callq 0x1030
movq 0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
andl $0x7, %eax
movq %rax, -0x40(%rbp)
movq $0x60c4080, (%r12)      # imm = 0x60C4080
vmovups (%rbx), %xmm0
vmovups %xmm0, 0x8(%r12)
vmovups (%rcx), %xmm0
vmovups %xmm0, 0x18(%r12)
movq %r12, %rdi
movl $0xc, %esi
callq 0x1070
vmovdqu 0x18(%r12), %xmm0
vpxor (%rbx), %xmm0, %xmm0
vmovdqu %xmm0, 0x18(%r12)
testq %r14, %r14
je 0x18ef
shrq $0x3, %r14
incq %r14
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rax
xorq %rax, (%r12)
movq %r12, %rdi
movl $0x6, %esi
callq 0x1070
incq %rbx
cmpq %rbx, %r14
jne 0x18d1
xorb $0x1, 0x27(%r12)
cmpq $0x8, -0x30(%rbp)
movq -0x60(%rbp), %rbx
jb 0x193b
movq -0x30(%rbp), %r14
shrq $0x3, %r14
cmpq $0x1, %r14
adcq $0x0, %r14
xorl %r13d, %r13d
movq (%r15,%r13,8), %rax
movq (%r12), %rcx
xorq %rax, %rcx
movq %rcx, (%rbx,%r13,8)
movq %rax, (%r12)
movq %r12, %rdi
movl $0x6, %esi
callq 0x1070
incq %r13
cmpq %r13, %r14
jne 0x1913
movq -0x40(%rbp), %r13
testq %r13, %r13
je 0x19bc
leaq -0x1(%r13), %rax
vpbroadcastq %rax, %ymm0
vpcmpnltuq 0x6a7(%rip), %ymm0, %k0 # 0x2000
vpcmpnltuq 0x6bc(%rip), %ymm0, %k1 # 0x2020
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpnltuq 0x6c7(%rip), %ymm0, %k1 # 0x2040
vpcmpnltuq 0x6dc(%rip), %ymm0, %k2 # 0x2060
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 (%r12), %xmm0, %xmm0
vmovdqu8 %xmm0, (%rbx,%rax) {%k1}
movq %r12, %rdi
movq %r13, %rdx
vzeroupper
callq 0x1080
xorb $-0x80, (%r12,%r13)
movq 0x20(%rbp), %rax
vmovups (%rax), %xmm0
vmovaps %xmm0, -0x40(%rbp)
vxorps 0x8(%r12), %xmm0, %xmm0
vmovups %xmm0, 0x8(%r12)
movq %r12, %rdi
movl $0xc, %esi
callq 0x1070
vmovaps -0x40(%rbp), %xmm0
vxorps 0x18(%r12), %xmm0, %xmm0
vmovups %xmm0, 0x18(%r12)
addq -0x30(%rbp), %r15
xorl %eax, %eax
movb (%r15,%rax), %cl
cmpb 0x18(%r12,%rax), %cl
jne 0x1a3e
incq %rax
cmpq $0x10, %rax
jne 0x1a02
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
testq %rax, %rax
je 0x1a45
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x48(%rbp), %rdi
movb (%rbx,%rcx), %dl
movb %dl, (%rdi,%rcx)
incq %rcx
cmpq (%rsi), %rcx
jb 0x1a2e
jmp 0x1a47
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1a47
xorl %eax, %eax
movq -0x68(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop