// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/omdsha512k128n128tau128v2/ref/omdsha512.c
// Function: hash
void
hash (hashblock taga, const hashblock key, const unsigned char *ad,
      unsigned long long int adlen)
{
   hashblock lstar, lzero;
   hashblock delta;
   hashblock left, right;
   hashblock xor_res, key_func_res;
   hashblock taures;
   unsigned int  modlen;
   long long int i, l;
   size_t b;

   /* CHANGE FROM V1 TO V2 HERE */
   /* L_* = F_K(0^n, <tau>_m) */
   l2b (taures, OMD_tau);
   key_func (lstar, block0s, key, taures);
   /* L[0] = 4.L* */
   double_block (lzero, lstar);
   double_block (lzero, lzero);

   /*b = n+m*/
   b = OMD_n + OMD_m;

   /* A_1 || A_2 ··· A_{l−1} || A_l = A, where |A_i| = b for 1 ≤ i ≤ l−1
      and |A_l| ≤ b */
   /* l=ceil(adlen/b) */
   l = (adlen + b - 1)/b;

   /* Tag_a = 0^n  */
   memset (taga, 0x00, OMD_n);
   /* If AD="" => Tag_a = 0^n */
   if (adlen == 0)
      {
	 return;
      }

   /* \bar{\delta}_{0,0} = 0^n */
   memset (delta, 0x00, OMD_n);

   /** for i = 1 to l-1 **/
   /**********************/
   for (i = 1; i < l; i++)
      {
	 /*  \bar{\delta}_{i, 0} =  \bar{\delta}{i-1, 0} ^ L[ntz(i)]  */
	 increment_masking_associated_data (delta, delta, lzero, i);

	 /* Left = A_l[b-1...m]; */
	 memcpy (left, &ad[(i-1)*b],  OMD_n);
	 /* Right = A_l[m-1...0]  */
	 memcpy (right,&ad[(i-1)*b+OMD_n],OMD_m);

	 /* Tag_a = Tag_a ^ F_K(Left ^ \delta_{i, 0}, right) */
	 xor_block (xor_res, delta, left);
	 key_func (key_func_res, xor_res, key, right);
	 xor_block (taga, taga, key_func_res);
      }
   /**************************/
   /******* endfor ***********/

   /* here adlen!=0, we process last ad block*/

   modlen = adlen%b;

   /* |A_l| = b then */
   if (modlen == 0)
      {
	 /* last mask = \bar{\delta}{l,0}  */
	 increment_masking_associated_data (delta, delta, lzero,l);

	 memcpy (left, &ad[(l-1)*b],      OMD_n);
	 memcpy (right,&ad[(l-1)*b+OMD_n],OMD_m);
      }
   /* |A_l| < b then */
   else
      {
	 /* last mask = \bar{\delta}{l-1,1}  */
	 final_masking_associated_data (delta, delta, lstar);

	 memset (left , 0x00, OMD_n);
	 memset (right, 0x00, OMD_m);

	 memcpy (left, &ad[(l-1)*b], (modlen < OMD_n ? modlen : OMD_n));
	 memcpy (right, &ad[(l-1)*b+OMD_n], (modlen < OMD_n
					     ? 0
					     : modlen - OMD_n));

	 if (modlen < OMD_n)
	    /* pad last block */
	    left[modlen] = 0x80;
	 else
	    right[modlen-OMD_n] = 0x80;
   }

   /* Tag_a = Tag_a ^ F_K(Left ^ \delta_{l, 0 or 1}, right) */
   xor_block (xor_res, delta, left);
   key_func (key_func_res, xor_res, key, right);
   xor_block (taga, taga, key_func_res);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x1f0(%rsp), %r14
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x1130
leaq 0x1b21(%rip), %rsi      # 0x3000
leaq 0x1b0(%rsp), %r15
movq %r15, %rdi
movq %r12, 0x10(%rsp)
movq %r12, %rdx
movq %r14, %rcx
callq 0x10a0
leaq 0xf0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x1070
movq %r14, %rdi
movq %r14, %rsi
callq 0x1070
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x20(%rbx)
movq %rbx, 0x18(%rsp)
vmovups %ymm0, (%rbx)
testq %r13, %r13
je 0x1778
movq %r13, 0x8(%rsp)
addq $0x7f, %r13
movq %r13, 0x20(%rsp)
shrq $0x7, %r13
movq %rbp, %rax
leaq 0xb0(%rsp), %rbp
vmovups %ymm0, 0x20(%rbp)
vmovups %ymm0, (%rbp)
movq %rax, 0x28(%rsp)
leaq 0x40(%rax), %rbx
pushq $0x1
popq %r15
leaq 0x170(%rsp), %r14
leaq 0x130(%rsp), %r12
cmpq %r13, %r15
jae 0x160b
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0xf0(%rsp), %rdx
movl %r15d, %ecx
vzeroupper
callq 0x1050
vmovups -0x40(%rbx), %ymm0
vmovups -0x20(%rbx), %ymm1
vmovups %ymm1, 0x90(%rsp)
vmovups %ymm0, 0x70(%rsp)
vmovups (%rbx), %ymm0
vmovups 0x20(%rbx), %ymm1
vmovups %ymm0, 0x30(%rsp)
vmovups %ymm1, 0x50(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
leaq 0x70(%rsp), %rdx
vzeroupper
callq 0x1120
movq %r12, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x10a0
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r12, %rdx
callq 0x1120
incq %r15
subq $-0x80, %rbx
jmp 0x1577
movq 0x20(%rsp), %rcx
andq $-0x80, %rcx
movq 0x28(%rsp), %rax
leaq (%rax,%rcx), %rbp
movq 0x8(%rsp), %r14
leaq (%rax,%rcx), %rbx
addq $-0x80, %rbx
andl $0x7f, %r14d
je 0x16d3
xorl %r13d, %r13d
movl %r14d, %r15d
subl $0x40, %r15d
pushq $0x40
popq %r12
cmovbl %r14d, %r12d
cmovbl %r13d, %r15d
pushq $0x40
popq %rax
subq %r12, %rax
cmpl $0x40, %r14d
cmovbq %rax, %r13
leaq 0xb0(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
movq %rdi, %rsi
vzeroupper
callq 0x1120
vxorps %xmm0, %xmm0, %xmm0
leaq 0x30(%rsp), %rax
vmovups %ymm0, 0x20(%rax)
vmovups %ymm0, (%rax)
leaq (%rsp,%r12), %rdi
addq $0x70, %rdi
xorl %esi, %esi
movq %r13, %rdx
vzeroupper
callq 0x1040
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1140
addq $-0x40, %rbp
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1140
cmpl $0x3f, %r14d
ja 0x1721
movq 0x8(%rsp), %rax
andl $0x7f, %eax
movb $-0x80, 0x70(%rsp,%rax)
jmp 0x172b
leaq 0xb0(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
movq %rdi, %rsi
movl %r13d, %ecx
vzeroupper
callq 0x1050
vmovups (%rbx), %ymm0
vmovups 0x20(%rbx), %ymm1
vmovups %ymm1, 0x90(%rsp)
vmovups %ymm0, 0x70(%rsp)
vmovups -0x40(%rbp), %ymm0
vmovups -0x20(%rbp), %ymm1
vmovups %ymm0, 0x30(%rsp)
vmovups %ymm1, 0x50(%rsp)
jmp 0x172b
addl $-0x40, %r14d
movb $-0x80, 0x30(%rsp,%r14)
leaq 0x170(%rsp), %r14
leaq 0xb0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movq %r14, %rdi
vzeroupper
callq 0x1120
leaq 0x130(%rsp), %r15
leaq 0x30(%rsp), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
callq 0x10a0
movq 0x18(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
callq 0x1120
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq