// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/trivia0v2/ref/encrypt.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
       unsigned char *m,unsigned long long *outputmlen,
       unsigned char *nsec,
       const unsigned char *c,unsigned long long clen,
       const unsigned char *ad,unsigned long long adlen,
       const unsigned char *npub,
       const unsigned char *k
     )
{
   u64 SC_State[8], Z[1], Checksum[3], Block, ck_ctr, blk_ctr; 
   u32 dummy_word[2], Tag[4],   temp; 
   u8 FINISH, *tempvar; 
   u64 t1, t2, t3, i, j;
   int ctr_tag=0;
   *outputmlen=0;

   

   load_SC(SC_State, k, npub);  // Load State Blocks with Nonce and key


   Initialize_process_AD_Reinitialize(SC_State, ad, adlen); //Initialize, Process AD and Reinitialize */

/*  PROCESS CIPHERTEXT BLOCKS.       */
//--------------------------------//

   ck_ctr = CHUNK_SIZE_MSG; blk_ctr= MAX_IT;  set_zero32(Tag,4); set_zero64(Checksum,3);
   while(clen-16 >=8) // Process ALL but the Final Block of the Ciphertext
   {

        Z[0] = Key_Ext64(SC_State); //Extract the Encryption Key First

        for(i=0;i<8;i++) {m[i] = ((u8)(Z[0]>>(8*(7-i))))^(c[i]);  } // Decrypt the Ciphertext
        Block = load64(m);    
	process_block(Block, Tag, Checksum, SC_State, Z, PROC_BLOCK); //Update the Tag in the Decryptor Side

	
        m = m+8; c= c+8; *outputmlen = *outputmlen+8; clen = clen-8; 
        ck_ctr--; if(blk_ctr>0) blk_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_MSG; set_zero64(Checksum,3); // Reset The Chunk Counter and Set the Checksum Array to zero
	   
	   /*if(IT && blk_ctr!=0) // In case of Itag, we compute intermediate tag. // CHANGE
	   {
		for(i=0;i<4;i++){ c= c+4;} clen= clen-16;
	   }

           for(i=0;i<4;i++)//Checking the equality of the intermediate tags // CHANGE
           {
                
                temp=load32((c-16+i*4));
                if((Tag[i])!= temp) return -1;
           }*/
           
   	}        
   }



// Process the Final Block (possibly empty) of cipheretxt 
   Z[0] = Key_Ext64(SC_State);  //Extract the Key for the Final Block
  
   for(i=0;i<clen-16;i++) {m[i] = ((u8)(Z[0]>>(8*(7-i))))^(c[i]);} //Decrypt to the Final Message Block
   process_block(pad(m, clen-16), Tag, Checksum, SC_State, Z, PROC_BLOCK); //Update the Tag in the Decryptor Side
   *outputmlen=*outputmlen+clen-16; c=c+clen-16;

// Update the Tag for the Final 4 Checksum Blocks and Get the Key for Variable Length Handling
    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]);}

	}

// Add the Currently Generated Key to Handle Variable Length Message
 //  Tag[0] ^= (u32)(Z[0]>>32); Tag[1] ^= (u32)Z[0]; Tag[2] ^= (u32)(Z[2]>>32); Tag[3] ^= (u32)Z[2]; 
   for(i=0;i<4;i++){ c= c+4;} clen=clen-16;
   temp=load32(c-4);

   for(i=0;i<4;i++)//Check the Equality of the Final Tag
           {            
                temp=load32((c-16+i*4));
                if((Tag[i])!= temp)  return -1;
           }

return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %r15
movq %r8, %r14
movq %rcx, %rbx
movq %rsi, %rax
movq %rdi, %r12
movq 0xd0(%rsp), %rbp
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
movq %rax, 0x20(%rsp)
andq $0x0, (%rax)
leaq 0x50(%rsp), %r13
movq %r13, %rdi
callq 0x1050
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x10c0
leaq 0x10(%rsp), %rdi
callq 0x182c
leaq 0x30(%rsp), %rbp
movabsq $0x100000000, %r15      # imm = 0x100000000
movq %rbp, %rdi
callq 0x183e
movq %r13, %rdi
movq %r14, 0x28(%rsp)
addq $-0x10, %r14
callq 0x1120
movq %rax, 0x8(%rsp)
pushq $0x38
popq %rcx
xorl %edx, %edx
cmpq $0x8, %r14
jb 0x1bd4
movq 0x28(%rsp), %r14
cmpq $-0x8, %rcx
je 0x1b4f
shrxq %rcx, %rax, %rsi
xorb (%rbx,%rdx), %sil
movb %sil, (%r12,%rdx)
incq %rdx
addq $-0x8, %rcx
jmp 0x1b33
movq %r12, %rdi
callq 0x1080
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
leaq 0x8(%rsp), %r8
xorl %r9d, %r9d
callq 0x1100
addq $0x8, %r12
addq $0x8, %rbx
movq 0x20(%rsp), %rax
addq $0x8, (%rax)
addq $-0x8, %r14
decq %r15
jne 0x1b09
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x1af7
movq 0x30(%rsp,%r15,8), %rdi
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
leaq 0x8(%rsp), %r8
pushq $0x1
popq %r9
callq 0x1100
incq %r15
jmp 0x1b93
shrxq %rcx, %rax, %rsi
xorb (%rbx,%rdx), %sil
movb %sil, (%r12,%rdx)
incq %rdx
addq $-0x8, %rcx
cmpq %r14, %rdx
jb 0x1bc0
movq %r12, %rdi
movl %r14d, %esi
callq 0x10a0
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x8(%rsp), %r8
xorl %r12d, %r12d
movq %rax, %rdi
movq %rcx, %r15
movq %r8, %r13
xorl %r9d, %r9d
callq 0x1100
movq 0x20(%rsp), %rax
addq %r14, (%rax)
addq 0x28(%rsp), %rbx
leaq 0x14(%rsp), %r14
pushq $0x1
popq %rbp
cmpq $0x3, %r12
je 0x1c80
movq 0x30(%rsp,%r12,8), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %r13, %r8
movl %ebp, %r9d
callq 0x1100
cmpq $0x2, %r12
je 0x1c64
testq %r12, %r12
jne 0x1c7b
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
movq %r14, %rcx
jmp 0x1c79
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
leaq 0x1c(%rsp), %rcx
xorl %eax, (%rcx)
incq %r12
jmp 0x1c21
leaq -0x4(%rbx), %rdi
callq 0x10b0
addq $-0x10, %rbx
xorl %r14d, %r14d
pushq $-0x1
popq %rbp
xorl %r15d, %r15d
cmpq $0x10, %r15
je 0x1cb6
leaq (%rbx,%r15), %rdi
callq 0x10b0
leaq 0x4(%r15), %rcx
cmpl %eax, 0x10(%rsp,%r15)
movq %rcx, %r15
je 0x1c96
movl %ebp, %r14d
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop