// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
pushq $0x12
popq %rbp
leaq 0x8(%rsp), %r12
subl $0x1, %ebp
jb 0x16db
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1090
jmp 0x16c9
leaq 0x10(%rsp), %rdi
callq 0x1829
leaq 0x20(%rsp), %r13
pushq $-0x80
popq %r12
leaq 0x8(%rsp), %rbp
movq %r13, %rdi
callq 0x183b
cmpq $0x8, %r14
jb 0x175f
movq %r15, %rdi
callq 0x1080
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x1100
addq $0x8, %r15
addq $-0x8, %r14
incq %r12
jne 0x16fb
xorl %r12d, %r12d
cmpq $0x3, %r12
je 0x1759
movq 0x20(%rsp,%r12,8), %rdi
leaq 0x10(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %rbp, %r8
pushq $0x1
popq %r9
callq 0x1100
incq %r12
jmp 0x1732
pushq $-0x80
popq %r12
jmp 0x16f3
movq %r15, %rdi
movl %r14d, %esi
callq 0x10a0
leaq 0x10(%rsp), %r14
leaq 0x20(%rsp), %r15
leaq 0x8(%rsp), %r12
xorl %r13d, %r13d
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x1100
pushq $0x1
popq %rbp
cmpq $0x3, %r13
je 0x17f3
movq 0x20(%rsp,%r13,8), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movq %r12, %r8
movl %ebp, %r9d
callq 0x1100
cmpl $0x2, %r13d
je 0x17d7
testl %r13d, %r13d
jne 0x17ee
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
leaq 0x14(%rsp), %rcx
jmp 0x17ec
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
leaq 0x1c(%rsp), %rcx
xorl %eax, (%rcx)
incq %r13
jmp 0x1796
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x1030
pushq $0x12
popq %rbp
leaq 0x8(%rsp), %r14
subl $0x1, %ebp
jb 0x181a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1090
jmp 0x1808
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq