// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/minalpherv11/ref/minalpher.c
// Function: MAC_GEN
int MAC_GEN(
  const unsigned char     *_K,    /* I    Secret key              */
  const unsigned char     *_M,    /* I    Message                 */
  unsigned long long      _mlen,  /* I    Length of Message       */

  unsigned char           *C_,    /* O    Tag                     */
  unsigned long long      *clen_  /* O    Length of Tag           */
  )
{
  int d, pad;
  unsigned long long i, blocknum;
  const unsigned char *mp;
  unsigned char *cp, in[BLOCK_SIZE], out[BLOCK_SIZE], t[BLOCK_SIZE], zero[NONCEBYTES];

  i = 1;
  mp = _M;
  cp = C_;

  pad = _mlen%BLOCK_SIZE;
  blocknum = _mlen/BLOCK_SIZE;
  if( (pad!=0) || (_mlen==0) ) blocknum += 1;
  Clear( t, BLOCK_SIZE );
  Clear( zero, NONCEBYTES );

  while( blocknum > 1 ){

    Copy( in, mp, BLOCK_SIZE );
    TEM_ENC( _K, FLG_MAC, zero, i, 0, in, out );
    Xor( t, out, BLOCK_SIZE );

    i++;
    blocknum--;
    mp += BLOCK_SIZE;
  }

  /* blocknum == 1 */
  if( (pad==0) && (_mlen!=0) ){
    Copy( in, mp, BLOCK_SIZE );
  }else{
    Copy( in, mp, pad );
    in[pad] = 0x80;
    for( d=pad+1; d<BLOCK_SIZE; d++ ) in[d] = 0x00;
  }

  Xor( in, t, BLOCK_SIZE );
  if( (pad==0) && (_mlen!=0) ){
    TEM_ENC( _K, FLG_MAC, zero, i-1, 1, in, out );
  }else{
    TEM_ENC( _K, FLG_MAC, zero, i-1, 2, in, out );
  }
  Copy( cp, out, TAGBYTES );
  *clen_ = TAGBYTES;

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, 0x8(%rsp)
testb $0x1f, %r15b
setne %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
movzbl %cl, %eax
xorl %ecx, %ecx
cmpq $0x20, %rcx
je 0x1817
movb $0x0, 0x70(%rsp,%rcx)
incq %rcx
jmp 0x1807
movq %r15, %r13
shrq $0x5, %r13
xorl %ecx, %ecx
cmpq $0xd, %rcx
je 0x1830
movb $0x0, 0x23(%rsp,%rcx)
incq %rcx
jmp 0x1820
addq %rax, %r13
pushq $0x1
popq %r12
leaq 0x23(%rsp), %rbx
leaq 0x30(%rsp), %r14
cmpq $0x2, %r13
jb 0x18a1
xorl %eax, %eax
cmpq $0x20, %rax
je 0x185c
movb (%rbp,%rax), %cl
movb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x1849
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movl $0x80, %esi
movq %rbx, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
movq %r14, %r9
callq 0x1973
xorl %eax, %eax
cmpq $0x20, %rax
je 0x1895
movb 0x50(%rsp,%rax), %cl
xorb %cl, 0x70(%rsp,%rax)
incq %rax
jmp 0x1882
incq %r12
decq %r13
addq $0x20, %rbp
jmp 0x1841
testb $0x1f, %r15b
sete %cl
testq %r15, %r15
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0x18c9
xorl %ecx, %ecx
cmpq $0x20, %rcx
je 0x18f3
movb (%rbp,%rcx), %dl
movb %dl, 0x30(%rsp,%rcx)
incq %rcx
jmp 0x18b6
andl $0x1f, %r15d
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x18e1
movb (%rbp,%rcx), %dl
movb %dl, 0x30(%rsp,%rcx)
incq %rcx
jmp 0x18cf
movb $-0x80, %cl
movb %cl, 0x30(%rsp,%r15)
incq %r15
xorl %ecx, %ecx
cmpq $0x20, %r15
jne 0x18e3
xorl %ecx, %ecx
cmpq $0x20, %rcx
je 0x1908
movb 0x70(%rsp,%rcx), %dl
xorb %dl, 0x30(%rsp,%rcx)
incq %rcx
jmp 0x18f5
decq %r12
movzbl %al, %eax
pushq $0x2
popq %r8
subq %rax, %r8
leaq 0x50(%rsp), %rax
movq %rax, (%rsp)
leaq 0x23(%rsp), %rdx
leaq 0x30(%rsp), %r9
movq 0x8(%rsp), %rdi
movl $0x80, %esi
movq %r12, %rcx
callq 0x1973
xorl %eax, %eax
movq 0x10(%rsp), %rdx
cmpq $0x10, %rax
je 0x1953
movb 0x50(%rsp,%rax), %cl
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1941
movq 0x18(%rsp), %rax
movq $0x10, (%rax)
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq