// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/trivia128v2/ref/encrypt.c
// Function: Initialize_process_AD_Reinitialize
void Initialize_process_AD_Reinitialize(u64 *SC_State, const unsigned char *ad, unsigned long long adlen)
{
   u64  Z[1], Checksum[3], Block, ck_ctr; 
   u32 dummy_word[2], Tag[4], temp; 
   u8 FINISH; 
   int i; 

   for(i=0; i<18; i++) {SC_Update64(SC_State, Z);} // Update SC_State for 18 64-rounds.


/*  PROCESS ASSOCIATED DATA BLOCKS...   */
//----------------------------------------

   ck_ctr = CHUNK_SIZE;  set_zero32(Tag,4);  set_zero64(Checksum,3);
   while(adlen >=8) // Process All But the Final Block of AD 
   {
	Block = load64(ad);
	process_block(Block, Tag, Checksum, SC_State, Z, PROC_BLOCK); // Update the Tag and Checksum
       	ad = ad+8; adlen = adlen-8; ck_ctr--;
        if(ck_ctr==0) // process the checksum blocks
   	{
	   for(i=0; i<3; i++){
		process_block(Checksum[i], Tag, Checksum, SC_State, Z, PROC_CHECKSUM);}// Update Tag 
                                                                                                // for the Checksum Block
	   ck_ctr=CHUNK_SIZE; set_zero64(Checksum,3); // Reset The Chunk Counter and Set the Checksum Array to zero
   	}
   }

   process_block(pad(ad, adlen), Tag, Checksum, SC_State, Z, PROC_BLOCK);//, PROC_AD); // Update the Tag and Checksum for the 
                                                                                   // Final Block
 
   for(i=0; i<3; i++)
	{  process_block(Checksum[i], Tag, Checksum, SC_State, Z, PROC_CHECKSUM);
	   if(i == 0) {Tag[0] ^= (u32)(Z[0]>>32); Tag[1] ^= (u32)(Z[0]);}
	   if(i == 2) {Tag[2] ^= (u32)(Z[0]>>32); Tag[3] ^= (u32)(Z[0]);}
	}


// Update the Tag for the Final 4 Checksum Blocks for AD and Get the Key for Variable Length Handling
  // for(i=0; i<4; i++){process_block(Checksum[i], Tag, Checksum, SC_State, Z+i, PROC_CHECKSUM);}

// Add the Currently Generated Key to Handle Variable Length AD
  // Tag[0] ^= (u32)(Z[0]>>32); Tag[1] ^= (u32)(Z[0]); Tag[2] ^= (u32)(Z[1]>>32); Tag[3] ^= (u32)(Z[1]); Tag[4] ^= (u32)(Z[2]>>32);

   insert_SC(SC_State, Tag); // Insert Tag for AD to streamcipher state
   for(i=0; i<18; i++) {SC_Update64(SC_State, Z);} // Update SC_State for 18 64-rounds.
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl $0x12, %ebp
leaq 0x8(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1050
decl %ebp
jne 0x16ab
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpq $0x8, %r14
jb 0x1791
movl $0x80, %eax
leaq 0x10(%rsp), %r12
leaq 0x30(%rsp), %r13
leaq 0x8(%rsp), %rbp
movq %rax, 0x28(%rsp)
movzbl (%r15), %edi
movl $0x1, %eax
movq %rdi, %rcx
shlq $0x8, %rcx
movzbl (%r15,%rax), %edi
orq %rcx, %rdi
incq %rax
cmpq $0x8, %rax
jne 0x16ff
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x1090
movq 0x28(%rsp), %rax
decq %rax
jne 0x177f
movq %r14, 0x28(%rsp)
xorl %r14d, %r14d
movq 0x30(%rsp,%r14,8), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %rbp, %r8
movl $0x1, %r9d
callq 0x1090
incq %r14
cmpq $0x3, %r14
jne 0x173d
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x80, %eax
movq 0x28(%rsp), %r14
addq $0x8, %r15
addq $-0x8, %r14
cmpq $0x7, %r14
ja 0x16f1
testq %r14, %r14
je 0x17b6
xorl %ecx, %ecx
xorl %eax, %eax
movzbl (%r15,%rcx), %edx
orq %rdx, %rax
shlq $0x8, %rax
incq %rcx
cmpq %rcx, %r14
jne 0x179a
orq $0x80, %rax
jmp 0x17bb
movl $0x80, %eax
shll $0x3, %r14d
movb $0x38, %cl
subb %r14b, %cl
shlxq %rcx, %rax, %rdi
leaq 0x10(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x8(%rsp), %r12
xorl %r13d, %r13d
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x1090
leaq 0x14(%rsp), %rbp
movq 0x30(%rsp,%r13,8), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r12, %r8
movl $0x1, %r9d
callq 0x1090
cmpl $0x2, %r13d
je 0x1830
testl %r13d, %r13d
jne 0x1847
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
movq %rbp, %rcx
jmp 0x1845
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
leaq 0x1c(%rsp), %rcx
xorl %eax, (%rcx)
incq %r13
cmpq $0x3, %r13
jne 0x17f4
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
xorq %rcx, (%rbx)
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
shlq $0x20, %rax
orq %rax, %rcx
xorq %rcx, 0x8(%rbx)
movl $0x12, %ebp
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1050
decl %ebp
jne 0x187f
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq