// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/ascon128v11/ref/ascon.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
    unsigned char *c, unsigned long long *clen,
    const unsigned char *m, unsigned long long mlen,
    const unsigned char *ad, unsigned long long adlen,
    const unsigned char *nsec,
    const unsigned char *npub,
    const unsigned char *k) {

  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 = mlen / rate + 1;
  i64 l = mlen % 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;
  // pad plaintext
  for (i = 0; i < mlen; ++i)
    M[i] = m[i];
  M[mlen] = 0x80;
  for (i = mlen + 1; i < t * rate; ++i)
    M[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 != 0) {
    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) {
      S[j] ^= M[i * rate + j];
      c[i * rate + j] = S[j];
    }
    permutation(S, b);
  }
  for (j = 0; j < rate; ++j)
    S[j] ^= M[(t - 1) * rate + j];
  for (j = 0; j < l; ++j)
    c[(t - 1) * rate + j] = S[j];
  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 tag
  for (i = 0; i < klen; ++i)
    c[mlen + i] = S[rate + klen + i];
  *clen = mlen + klen;

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %r14
movq %rcx, %r15
movq %rdx, -0x30(%rbp)
movq %rsi, -0x88(%rbp)
movq %rdi, -0x80(%rbp)
movq %r9, %rax
andq $-0x8, %rax
leaq 0x8(%rax), %rbx
movq %rsp, %r12
addq $0x17, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rcx, %rax
andq $-0x8, %rax
movq %rsp, %r13
movq %rax, -0x40(%rbp)
addq $0x17, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
testq %r9, %r9
je 0x14d2
movq %r12, %rdi
movq %r8, %rsi
movq %r14, %rdx
callq 0x1080
movb $-0x80, (%r12,%r14)
leaq 0x1(%r14), %rdi
cmpq %rbx, %rdi
jge 0x14f2
addq %r12, %rdi
movl %r14d, %edx
notl %edx
andl $0x7, %edx
xorl %esi, %esi
callq 0x1030
movq -0x40(%rbp), %rax
leaq 0x8(%rax), %rbx
testq %r15, %r15
je 0x150e
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
callq 0x1080
movq 0x18(%rbp), %rcx
movb $-0x80, (%r13,%r15)
leaq 0x1(%r15), %rdi
cmpq %rbx, %rdi
jge 0x1539
addq %r13, %rdi
movl %r15d, %edx
notl %edx
andl $0x7, %edx
xorl %esi, %esi
movq %rcx, %rbx
callq 0x1030
movq %rbx, %rcx
movq %r15, -0x30(%rbp)
andl $0x7, %r15d
movq %r15, -0x78(%rbp)
leaq -0x70(%rbp), %rbx
movq $0x60c4080, (%rbx)      # imm = 0x60C4080
movq 0x20(%rbp), %r15
vmovups (%r15), %xmm0
vmovups %xmm0, 0x8(%rbx)
vmovups (%rcx), %xmm0
vmovups %xmm0, 0x18(%rbx)
movq %rbx, %rdi
movl $0xc, %esi
callq 0x1070
vmovups 0x18(%rbx), %xmm0
vxorps (%r15), %xmm0, %xmm0
vmovups %xmm0, 0x18(%rbx)
testq %r14, %r14
je 0x15af
shrq $0x3, %r14
incq %r14
xorl %r15d, %r15d
movq (%r12,%r15,8), %rax
xorq %rax, -0x70(%rbp)
movq %rbx, %rdi
movl $0x6, %esi
callq 0x1070
incq %r15
cmpq %r15, %r14
jne 0x1592
xorb $0x1, -0x49(%rbp)
cmpq $0x8, -0x30(%rbp)
movq -0x80(%rbp), %r15
jb 0x15f3
movq -0x30(%rbp), %rbx
shrq $0x3, %rbx
xorl %r12d, %r12d
leaq -0x70(%rbp), %r14
movq -0x70(%rbp), %rax
xorq (%r13,%r12,8), %rax
movq %rax, -0x70(%rbp)
movq %rax, (%r15,%r12,8)
movq %r14, %rdi
movl $0x6, %esi
callq 0x1070
incq %r12
cmpq %r12, %rbx
jne 0x15cd
leaq -0x58(%rbp), %r14
movq -0x40(%rbp), %rdi
movq (%r13,%rdi), %rax
xorq %rax, -0x18(%r14)
movq -0x78(%rbp), %rdx
testq %rdx, %rdx
je 0x1619
addq %r15, %rdi
leaq -0x70(%rbp), %rsi
callq 0x1080
movq 0x20(%rbp), %rax
vmovups (%rax), %xmm0
vmovaps %xmm0, -0x40(%rbp)
leaq -0x70(%rbp), %rbx
vxorps 0x8(%rbx), %xmm0, %xmm0
vmovups %xmm0, 0x8(%rbx)
movq %rbx, %rdi
movl $0xc, %esi
callq 0x1070
vmovaps -0x40(%rbp), %xmm0
vxorps 0x18(%rbx), %xmm0, %xmm0
vmovups %xmm0, 0x18(%rbx)
vmovups (%r14), %xmm0
movq -0x30(%rbp), %rcx
vmovups %xmm0, (%r15,%rcx)
addq $0x10, %rcx
movq -0x88(%rbp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq