// File: /workspace/github_c_makefile_star2_O2/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 %rsi, -0x78(%rbp)
movq %rdi, -0x58(%rbp)
movq 0x20(%rbp), %r13
movq 0x18(%rbp), %rax
shrq $0x3, %rcx
movq %rcx, -0x88(%rbp)
movq %r9, %rcx
andq $-0x8, %rcx
leaq 0x8(%rcx), %rsi
movq %rsp, %rbx
addq $0x17, %rcx
andq $-0x10, %rcx
subq %rcx, %rbx
movq %rbx, %rsp
movq %r15, %rdi
andq $-0x8, %rdi
leaq 0x8(%rdi), %rcx
movq %rsp, %r12
movq %rdi, -0x70(%rbp)
addq $0x17, %rdi
andq $-0x10, %rdi
subq %rdi, %r12
movq %r12, %rsp
xorl %edi, %edi
cmpq %rdi, %r14
je 0x13d5
movb (%r8,%rdi), %r9b
movb %r9b, (%rbx,%rdi)
incq %rdi
jmp 0x13c3
movq %r14, %rdi
shrq $0x3, %rdi
movq %rdi, -0x60(%rbp)
movb $-0x80, %r8b
movq %r14, %rdi
movb %r8b, (%rbx,%rdi)
incq %rdi
xorl %r8d, %r8d
cmpq %rsi, %rdi
jl 0x13e6
xorl %esi, %esi
cmpq %rsi, %r15
je 0x1409
movb (%rdx,%rsi), %dil
movb %dil, (%r12,%rsi)
incq %rsi
jmp 0x13f7
movb $-0x80, %sil
movq %r15, %rdx
movb %sil, (%r12,%rdx)
incq %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
jl 0x140f
movl $0x60c4080, -0x50(%rbp) # imm = 0x60C4080
pushq $0x4
popq %rcx
cmpq $0x8, %rcx
je 0x1437
movb $0x0, -0x50(%rbp,%rcx)
incq %rcx
jmp 0x1427
pushq $0x8
popq %rcx
cmpq $0x18, %rcx
je 0x144e
movb -0x8(%r13,%rcx), %dl
movb %dl, -0x50(%rbp,%rcx)
incq %rcx
jmp 0x143a
xorl %ecx, %ecx
cmpq $0x10, %rcx
je 0x1462
movb (%rax,%rcx), %dl
movb %dl, -0x38(%rbp,%rcx)
incq %rcx
jmp 0x1450
leaq -0x50(%rbp), %rdi
pushq $0xc
popq %rsi
callq 0x1070
pushq $0x18
popq %rax
cmpq $0x28, %rax
je 0x1485
movb -0x18(%r13,%rax), %cl
xorb %cl, -0x50(%rbp,%rax)
incq %rax
jmp 0x1471
movq %r15, -0x80(%rbp)
andl $0x7, %r15d
movq %r15, -0x68(%rbp)
testq %r14, %r14
je 0x14d3
incq -0x60(%rbp)
xorl %r15d, %r15d
leaq -0x50(%rbp), %r14
pushq $0x6
popq %r13
cmpq -0x60(%rbp), %r15
je 0x14d3
xorl %eax, %eax
cmpq $0x8, %rax
je 0x14bf
movb (%rbx,%rax), %cl
xorb %cl, -0x50(%rbp,%rax)
incq %rax
jmp 0x14ad
movq %r14, %rdi
movl %r13d, %esi
callq 0x1070
incq %r15
addq $0x8, %rbx
jmp 0x14a5
leaq -0x50(%rbp), %rax
xorb $0x1, 0x27(%rax)
xorl %ebx, %ebx
pushq $0x6
popq %r13
movq -0x58(%rbp), %r14
movq %r12, %r15
cmpq -0x88(%rbp), %rbx
je 0x1527
xorl %eax, %eax
cmpq $0x8, %rax
je 0x150e
movb -0x50(%rbp,%rax), %cl
xorb (%r15,%rax), %cl
movb %cl, -0x50(%rbp,%rax)
movb %cl, (%r14,%rax)
incq %rax
jmp 0x14f3
leaq -0x50(%rbp), %rdi
movl %r13d, %esi
callq 0x1070
incq %rbx
addq $0x8, %r15
addq $0x8, %r14
jmp 0x14e8
movq -0x70(%rbp), %rdx
addq %rdx, %r12
xorl %eax, %eax
movq -0x80(%rbp), %rbx
movq -0x58(%rbp), %r14
movq 0x20(%rbp), %r15
cmpq $0x8, %rax
je 0x154f
movb (%r12,%rax), %cl
xorb %cl, -0x50(%rbp,%rax)
incq %rax
jmp 0x153c
addq %r14, %rdx
xorl %eax, %eax
movq -0x68(%rbp), %rsi
cmpq %rax, %rsi
je 0x1569
movb -0x50(%rbp,%rax), %cl
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1558
pushq $0x8
popq %rax
cmpq $0x18, %rax
je 0x1580
movb -0x8(%r15,%rax), %cl
xorb %cl, -0x50(%rbp,%rax)
incq %rax
jmp 0x156c
leaq -0x50(%rbp), %rdi
pushq $0xc
popq %rsi
callq 0x1070
xorl %eax, %eax
cmpq $0x10, %rax
je 0x15a1
movb (%r15,%rax), %cl
xorb %cl, -0x38(%rbp,%rax)
incq %rax
jmp 0x158e
addq %rbx, %r14
xorl %eax, %eax
cmpq $0x10, %rax
je 0x15b9
movb -0x38(%rbp,%rax), %cl
movb %cl, (%r14,%rax)
incq %rax
jmp 0x15a6
addq $0x10, %rbx
movq -0x78(%rbp), %rax
movq %rbx, (%rax)
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq