// File: /workspace/github_c_makefile_star2_O1/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 $0x2a8, %rsp            # imm = 0x2A8
movq %rcx, %r12
movq %rdx, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x100(%rsp)
vmovups %ymm0, 0xe0(%rsp)
movb $-0x80, %cl
movl $0x40, %eax
movb %cl, 0xdf(%rsp,%rax)
movl $0x0, %ecx
decq %rax
jne 0x14da
vmovups (%r14), %ymm0
vmovups 0x20(%r14), %ymm1
leaq 0x1e0(%rsp), %rdx
vmovups %ymm1, 0x20(%rdx)
vmovups %ymm0, (%rdx)
vmovups 0xe0(%rsp), %ymm0
vmovups 0x100(%rsp), %ymm1
vmovups %ymm0, 0x40(%rdx)
vmovups %ymm1, 0x60(%rdx)
leaq 0x1ad6(%rip), %rsi      # 0x3000
leaq 0x260(%rsp), %r15
movq %r15, %rdi
vzeroupper
callq 0x10b0
leaq 0x1a0(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x1070
movq %r13, %rdi
movq %r13, %rsi
callq 0x1070
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x20(%rbx)
vmovups %ymm0, (%rbx)
testq %r12, %r12
je 0x1887
movq %r12, 0x18(%rsp)
leaq 0x7f(%r12), %rax
movq %rax, %r13
shrq $0x7, %r13
vmovups %ymm0, 0xc0(%rsp)
vmovups %ymm0, 0xa0(%rsp)
movq %rax, 0x10(%rsp)
cmpq $0x100, %rax            # imm = 0x100
jb 0x16b5
movl $0x1, %ebp
leaq 0x220(%rsp), %r12
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r15, %rsi
leaq 0x1a0(%rsp), %rdx
movl %ebp, %ecx
vzeroupper
callq 0x1050
movq %rbp, %rax
shlq $0x7, %rax
movq 0x8(%rsp), %rcx
addq %rcx, %rax
addq $-0x40, %rax
vmovups -0x40(%rax), %ymm0
vmovups -0x20(%rax), %ymm1
vmovups %ymm1, 0x80(%rsp)
vmovups %ymm0, 0x60(%rsp)
vmovups (%rax), %ymm0
vmovups 0x20(%rax), %ymm1
vmovups %ymm0, 0x20(%rsp)
vmovups %ymm1, 0x40(%rsp)
xorl %ecx, %ecx
vmovups 0x60(%rsp,%rcx), %ymm0
vxorps 0xa0(%rsp,%rcx), %ymm0, %ymm0
vmovups %ymm0, 0x160(%rsp,%rcx)
addq $0x20, %rcx
cmpq $0x40, %rcx
jne 0x1615
vmovups (%r14), %ymm0
vmovups 0x20(%r14), %ymm1
vmovups %ymm1, 0x200(%rsp)
vmovups %ymm0, 0x1e0(%rsp)
vmovups (%rax), %ymm0
vmovups 0x20(%rax), %ymm1
vmovups %ymm1, 0x20(%r12)
vmovups %ymm0, (%r12)
leaq 0x120(%rsp), %rdi
leaq 0x160(%rsp), %rsi
leaq 0x1e0(%rsp), %rdx
vzeroupper
callq 0x10b0
xorl %eax, %eax
vmovups 0x120(%rsp,%rax), %ymm0
vxorps (%rbx,%rax), %ymm0, %ymm0
vmovups %ymm0, (%rbx,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x168c
incq %rbp
cmpq %r13, %rbp
jne 0x15ba
movq 0x18(%rsp), %r15
andl $0x7f, %r15d
je 0x178f
xorl %eax, %eax
vmovups 0x260(%rsp,%rax), %ymm0
vxorps 0xa0(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0xa0(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x16c6
xorl %edx, %edx
movl %r15d, %r12d
subl $0x40, %r12d
movl $0x40, %r13d
cmovbl %r15d, %r13d
cmovbl %edx, %r12d
movl $0x40, %eax
subq %r13, %rax
vxorps %xmm0, %xmm0, %xmm0
leaq 0x20(%rsp), %rcx
vmovups %ymm0, 0x20(%rcx)
vmovups %ymm0, (%rcx)
cmpl $0x40, %r15d
cmovbq %rax, %rdx
movq 0x10(%rsp), %rcx
andq $-0x80, %rcx
movq 0x8(%rsp), %rax
leaq (%rax,%rcx), %rbp
addq $-0x80, %rbp
leaq (%rsp,%r13), %rdi
addq $0x60, %rdi
addq %rcx, %rax
addq $-0x40, %rax
movq %rax, 0x8(%rsp)
xorl %esi, %esi
vzeroupper
callq 0x1040
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1100
leaq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
callq 0x1100
cmpl $0x3f, %r15d
ja 0x17f0
movq 0x18(%rsp), %rax
andl $0x7f, %eax
movb $-0x80, 0x60(%rsp,%rax)
jmp 0x17fa
leaq 0xa0(%rsp), %rdi
leaq 0x1a0(%rsp), %rdx
movq %rdi, %rsi
movl %r13d, %ecx
vzeroupper
callq 0x1050
movq 0x10(%rsp), %rcx
andq $-0x80, %rcx
movq 0x8(%rsp), %rax
vmovups -0x80(%rax,%rcx), %ymm0
vmovups -0x60(%rax,%rcx), %ymm1
vmovups %ymm1, 0x80(%rsp)
vmovups %ymm0, 0x60(%rsp)
vmovups -0x40(%rax,%rcx), %ymm0
vmovups -0x20(%rax,%rcx), %ymm1
vmovups %ymm0, 0x20(%rsp)
vmovups %ymm1, 0x40(%rsp)
jmp 0x17fa
addl $-0x40, %r15d
movb $-0x80, 0x20(%rsp,%r15)
xorl %eax, %eax
vmovups 0x60(%rsp,%rax), %ymm0
vxorps 0xa0(%rsp,%rax), %ymm0, %ymm0
vmovups %ymm0, 0x160(%rsp,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x17fc
vmovups (%r14), %ymm0
vmovups 0x20(%r14), %ymm1
leaq 0x1e0(%rsp), %rdx
vmovups %ymm1, 0x20(%rdx)
vmovups %ymm0, (%rdx)
vmovups 0x20(%rsp), %ymm0
vmovups 0x40(%rsp), %ymm1
vmovups %ymm0, 0x40(%rdx)
vmovups %ymm1, 0x60(%rdx)
leaq 0x120(%rsp), %rdi
leaq 0x160(%rsp), %rsi
vzeroupper
callq 0x10b0
xorl %eax, %eax
vmovups 0x120(%rsp,%rax), %ymm0
vxorps (%rbx,%rax), %ymm0, %ymm0
vmovups %ymm0, (%rbx,%rax)
addq $0x20, %rax
cmpq $0x40, %rax
jne 0x186a
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq