// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %r9, %r12
movq %r8, %r15
movq %rdx, %r13
movq %rdi, 0x8(%rsp)
xorl %eax, %eax
testb $0x1f, %cl
setne %al
xorl %edx, %edx
cmpq $0x20, %rdx
je 0x1435
movb $0x0, 0x80(%rsp,%rdx)
incq %rdx
jmp 0x1422
movq %rcx, %r14
shrq $0x5, %r14
xorl %edx, %edx
cmpq $0xd, %rdx
je 0x144e
movb $0x0, 0x73(%rsp,%rdx)
incq %rdx
jmp 0x143e
movq %rcx, 0x58(%rsp)
movq %rsi, 0x68(%rsp)
addq %rax, %r14
pushq $0x1
popq %rbp
leaq 0x10(%rsp), %rbx
movq %r14, 0x60(%rsp)
cmpq $0x2, %r14
jb 0x14cb
xorl %eax, %eax
cmpq $0x20, %rax
je 0x1484
movb (%r13,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x1470
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
xorl %esi, %esi
leaq 0x73(%rsp), %rdx
movq %rbp, %rcx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x1973
xorl %eax, %eax
cmpq $0x20, %rax
je 0x14bf
movb 0x30(%rsp,%rax), %cl
xorb %cl, 0x80(%rsp,%rax)
incq %rax
jmp 0x14a9
incq %rbp
decq %r14
addq $0x20, %r13
jmp 0x1468
cmpq $0x0, 0x60(%rsp)
movq 0x68(%rsp), %r14
je 0x1597
movq 0x58(%rsp), %rsi
testb $0x1f, %sil
setne %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x1508
xorl %eax, %eax
cmpq $0x20, %rax
je 0x1533
movb (%r13,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x14f4
movl %esi, %eax
andl $0x1f, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1522
movb (%r13,%rcx), %dl
movb %dl, 0x10(%rsp,%rcx)
incq %rcx
jmp 0x150f
movb $-0x80, %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x20, %rax
jne 0x1524
xorl %eax, %eax
cmpq $0x20, %rax
je 0x154b
movb 0x80(%rsp,%rax), %cl
xorb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x1535
andl $0x1f, %esi
decq %rbp
cmpq $0x1, %rsi
pushq $0x2
popq %r8
sbbq $0x0, %r8
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
xorl %ebx, %ebx
leaq 0x73(%rsp), %rdx
leaq 0x10(%rsp), %r9
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movq %rbp, %rcx
callq 0x1973
cmpq $0x20, %rbx
je 0x1597
movb 0x30(%rsp,%rbx), %al
movb %al, 0x80(%rsp,%rbx)
incq %rbx
jmp 0x1581
shrq $0x5, %r12
pushq $0x1
popq %rbx
pushq $0x40
popq %rbp
leaq 0x10(%rsp), %r13
xorl %eax, %eax
cmpq $0x2, %r12
jae 0x15bb
jmp 0x1619
movb (%r15,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
cmpq $0x20, %rax
jne 0x15b0
leaq (%rbx,%rbx), %rcx
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %r8d, %r8d
movq %r13, %r9
callq 0x1973
xorl %eax, %eax
cmpq $0x20, %rax
je 0x15fb
movb 0x30(%rsp,%rax), %cl
xorb %cl, 0x80(%rsp,%rax)
incq %rax
jmp 0x15e5
incq %rbx
decq %r12
addq $0x20, %r15
jmp 0x15a6
movb 0x80(%rsp,%rax), %cl
xorb (%r15,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
cmpq $0x20, %rax
jne 0x1607
leaq -0x1(,%rbx,2), %rcx
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
pushq $0x40
popq %rsi
pushq $0x1
popq %r8
leaq 0x10(%rsp), %r9
movq 0x8(%rsp), %rdi
movq %r14, %rdx
callq 0x1973
xorl %eax, %eax
movq 0xe0(%rsp), %rdx
cmpq $0x10, %rax
je 0x1665
movb 0x30(%rsp,%rax), %cl
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1653
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq