// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/minalpherv11/ref/minalpher.c
// Function: AEAD_TGEN
static void AEAD_TGEN(
  const unsigned char     *_K,    /* I    Secret key                  */
  const unsigned char     *_N,    /* I    Nonce                       */
  const unsigned char     *_A,    /* I    Associated data             */
  unsigned long long      _alen,  /* I    Length of Associated data   */
  const unsigned char     *_C,    /* I    Ciphertext                  */
  unsigned long long      _clen,  /* I    Length of Ciphertext        */

  unsigned char           *tag_   /* O    Tag                         */
  )
{
  int d, pad;
  unsigned long long i, j, blocknum;
  const unsigned char *ap, *cp;
  unsigned char in[BLOCK_SIZE], out[BLOCK_SIZE], t[BLOCK_SIZE], zero[NONCEBYTES];

  i = 1;
  j = 1;
  ap = _A;
  cp = _C;

  /*-- Associated data --*/
  pad = _alen%BLOCK_SIZE;
  blocknum = _alen/BLOCK_SIZE;
  if( pad != 0 ) blocknum += 1;
  Clear( t, BLOCK_SIZE );
  Clear( zero, NONCEBYTES );

  while( blocknum > 1 ){

    Copy( in, ap, BLOCK_SIZE );
    TEM_ENC( _K, FLG_AD, zero, i, 0, in, out );
    Xor( t, out, BLOCK_SIZE );

    i++;
    blocknum--;
    ap += BLOCK_SIZE;
  }

  if( blocknum == 1 ){

    if( (pad == 0) && (_alen != 0) ){
      Copy( in, ap, BLOCK_SIZE );
    }else{
      Copy( in, ap, pad );
      in[pad] = 0x80;
      for( d=pad+1; d<BLOCK_SIZE; d++ ) in[d] = 0x00;
    }
    Xor( in, t, BLOCK_SIZE );

    if( pad == 0 ){
      TEM_ENC( _K, FLG_AD, zero, i-1, 1, in, out );
    }else{
      TEM_ENC( _K, FLG_AD, zero, i-1, 2, in, out );
    }

    Copy( t, out, BLOCK_SIZE );
  }


  /*-- Ciphertext --*/
  i=1;
  blocknum = _clen/BLOCK_SIZE;

  while( blocknum > 1 ){

    Copy( in, cp, BLOCK_SIZE );
    TEM_ENC( _K, FLG_M, _N, 2*i, 0, in, out );
    Xor( t, out, BLOCK_SIZE );

    i++;
    blocknum--;
    cp += BLOCK_SIZE;
  }

  /* blocknum == 1 */
  for( d=0; d<BLOCK_SIZE; d++ ) in[d] = cp[d] ^ t[d];
  TEM_ENC( _K, FLG_M, _N, 2*i-1, 1, in, out );
  Copy( tag_, out, TAGBYTES );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, %r12
movq %r8, %r15
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, 0x10(%rsp)
xorl %eax, %eax
movl %ecx, %r8d
andl $0x1f, %r8d
setne %al
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x70(%rsp)
movq %rcx, %rbx
shrq $0x5, %rbx
addq %rax, %rbx
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movq %rax, 0x65(%rsp)
leaq 0x40(%rsp), %rsi
cmpq $0x2, %rbx
jb 0x1542
movq %rcx, 0x98(%rsp)
movl %r8d, 0x1c(%rsp)
movq %r14, 0x8(%rsp)
movl $0x1, %r13d
vmovups 0x70(%rsp), %ymm0
movq 0x10(%rsp), %r14
vmovups %ymm0, 0xa0(%rsp)
vmovups (%rbp), %ymm0
vmovups %ymm0, 0x20(%rsp)
movq %rsi, (%rsp)
movq %r14, %rdi
xorl %esi, %esi
leaq 0x60(%rsp), %rdx
movq %r13, %rcx
xorl %r8d, %r8d
leaq 0x20(%rsp), %r9
vzeroupper
callq 0x1a70
vmovups 0xa0(%rsp), %ymm0
leaq 0x40(%rsp), %rsi
vxorps 0x40(%rsp), %ymm0, %ymm0
incq %r13
addq $0x20, %rbp
cmpq %r13, %rbx
jne 0x14d2
vmovups %ymm0, 0x70(%rsp)
decq %r13
movq %r14, %rbx
movl 0x1c(%rsp), %r8d
movq 0x98(%rsp), %rcx
jmp 0x1558
testq %rbx, %rbx
movq 0x10(%rsp), %rbx
je 0x1631
movq %r14, 0x8(%rsp)
xorl %r13d, %r13d
testl %r8d, %r8d
setne %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x1582
xorl %eax, %eax
movq 0x8(%rsp), %r14
movb (%rbp,%rax), %cl
movb %cl, 0x20(%rsp,%rax)
incq %rax
cmpq $0x20, %rax
jne 0x156f
jmp 0x15e9
movl %ecx, %eax
andl $0x1f, %eax
testl %r8d, %r8d
movq 0x8(%rsp), %r14
je 0x15b5
xorl %edi, %edi
movb (%rbp,%rdi), %dl
movb %dl, 0x20(%rsp,%rdi)
incq %rdi
cmpq %rdi, %rax
jne 0x1593
movl %ecx, %eax
andl $0x1f, %eax
movb $-0x80, 0x20(%rsp,%rax)
cmpl $0x1f, %r8d
jne 0x15ba
jmp 0x15e9
movb $-0x80, 0x20(%rsp,%rax)
andl $0x1f, %ecx
leaq (%rsp,%rcx), %rdi
addq $0x21, %rdi
xorl %eax, %eax
movl $0x1e, %edx
subl %r8d, %edx
cmovbl %eax, %edx
incl %edx
xorl %esi, %esi
movl %r8d, %ebp
vzeroupper
callq 0x1040
movl %ebp, %r8d
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %r9
vmovups (%r9), %ymm0
vxorps 0x70(%rsp), %ymm0, %ymm0
vmovups %ymm0, (%r9)
cmpl $0x1, %r8d
movl $0x2, %r8d
sbbq $0x0, %r8
movq %rsi, (%rsp)
leaq 0x60(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rcx
vzeroupper
callq 0x1a70
vmovups 0x40(%rsp), %ymm0
vmovups %ymm0, 0x70(%rsp)
movl $0x1, %r13d
cmpq $0x40, %r12
jb 0x16b3
shrq $0x5, %r12
vmovups 0x70(%rsp), %ymm0
leaq 0x20(%rsp), %rbp
leaq 0x40(%rsp), %rdx
vmovups %ymm0, 0xa0(%rsp)
vmovups (%r15), %ymm0
vmovups %ymm0, 0x20(%rsp)
leaq 0x1(%r13), %rcx
movq %rdx, (%rsp)
movq %rbx, %rdi
movl $0x40, %esi
movq %r14, %rdx
xorl %r8d, %r8d
movq %rbp, %r9
vzeroupper
callq 0x1a70
vmovups 0xa0(%rsp), %ymm0
leaq 0x40(%rsp), %rdx
vxorps 0x40(%rsp), %ymm0, %ymm0
decq %r12
addq $0x20, %r15
addq $0x2, %r13
cmpq $0x1, %r12
ja 0x1651
vmovups %ymm0, 0x70(%rsp)
jmp 0x16b8
leaq 0x40(%rsp), %rdx
movq 0x100(%rsp), %rbx
xorl %eax, %eax
movb 0x70(%rsp,%rax), %cl
xorb (%r15,%rax), %cl
movb %cl, 0x20(%rsp,%rax)
incq %rax
cmpq $0x20, %rax
jne 0x16c2
movq %rdx, (%rsp)
leaq 0x20(%rsp), %r9
movl $0x1, %r8d
movq 0x10(%rsp), %rdi
movl $0x40, %esi
movq %r14, %rdx
movq %r13, %rcx
vzeroupper
callq 0x1a70
vmovaps 0x40(%rsp), %xmm0
vmovups %xmm0, (%rbx)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq