// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %r8, %r12
movq %rcx, %rbx
movq %rsi, %rax
movq %rdi, %r15
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
xorl %ebp, %ebp
movq %rax, 0x28(%rsp)
movq %rbp, (%rax)
leaq 0x50(%rsp), %r13
movq %r13, %rdi
callq 0x1030
movq %r13, %rdi
movq %r14, %rsi
movq 0xd0(%rsp), %rdx
callq 0x1060
movq %r12, %rdx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovaps %xmm0, 0x30(%rsp)
movq %rbp, 0x40(%rsp)
leaq -0x10(%r12), %rax
cmpq $0x8, %rax
jb 0x1db5
movabsq $0x100000000, %r13      # imm = 0x100000000
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rbp
leaq 0x50(%rsp), %r14
leaq 0x8(%rsp), %r12
movq %rdx, 0x20(%rsp)
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
movq %rcx, %rax
shldq $0x2, %rdx, %rax
movq 0x60(%rsp), %r10
shrdq $0x3c, %rdx, %r10
xorq %rax, %r10
movq 0x68(%rsp), %rax
movq 0x70(%rsp), %rdi
movq %rax, %r8
shldq $0x5, %rdi, %r8
movq %rax, %r9
shldq $0x29, %rdi, %r9
xorq %r8, %r9
xorq %r10, %r9
movq 0x78(%rsp), %r11
movq 0x80(%rsp), %r8
shldq $0x2, %r8, %r11
movq 0x88(%rsp), %r10
shrdq $0x2d, %r8, %r10
shldq $0x26, %rdx, %rcx
shldq $0x2, %rdi, %rax
andq %rcx, %rax
xorq %r11, %rax
xorq %r9, %rax
xorq %r10, %rax
movq %rax, 0x8(%rsp)
movl $0x38, %ecx
xorl %edx, %edx
shrxq %rcx, %rax, %rdi
xorb (%rbx,%rdx), %dil
movb %dil, (%r15,%rdx)
incq %rdx
addq $-0x8, %rcx
cmpq $-0x8, %rcx
jne 0x1cc6
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 0x1ce9
movq %rbp, %rdx
movq %r14, %rcx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x1090
movq 0x28(%rsp), %rax
addq $0x8, (%rax)
decq %r13
jne 0x1d8c
movq %rbx, 0x90(%rsp)
xorl %r13d, %r13d
movq %r12, %rbx
movq %r14, %r12
movq %rbp, %r14
leaq 0x10(%rsp), %rbp
movq 0x30(%rsp,%r13,8), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r12, %rcx
movq %rbx, %r8
movl $0x1, %r9d
callq 0x1090
incq %r13
cmpq $0x3, %r13
jne 0x1d39
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movabsq $0x100000000, %r13      # imm = 0x100000000
movq %r14, %rbp
movq %r12, %r14
movq %rbx, %r12
movq 0x90(%rsp), %rbx
addq $0x8, %r15
addq $0x8, %rbx
movq 0x20(%rsp), %rax
leaq -0x8(%rax), %rcx
addq $-0x18, %rax
movq %rcx, %rdx
cmpq $0x7, %rax
leaq 0x10(%rsp), %rsi
ja 0x1c41
jmp 0x1db8
movq %rdx, %rcx
movq %rcx, 0x20(%rsp)
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rsi
movq %rdx, %rcx
shldq $0x2, %rsi, %rcx
movq 0x60(%rsp), %rdi
shrdq $0x3c, %rsi, %rdi
xorq %rcx, %rdi
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r8
movq %rcx, %r9
shldq $0x5, %r8, %r9
movq %rcx, %r10
shldq $0x29, %r8, %r10
xorq %r9, %r10
xorq %rdi, %r10
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %r9
shldq $0x2, %r9, %rdi
movq 0x88(%rsp), %r11
shrdq $0x2d, %r9, %r11
shldq $0x26, %rsi, %rdx
shldq $0x2, %r8, %rcx
andq %rdx, %rcx
xorq %rdi, %rcx
xorq %r10, %rcx
xorq %r11, %rcx
movq %rcx, 0x8(%rsp)
testq %rax, %rax
je 0x1e7c
movl $0x38, %edx
xorl %esi, %esi
shrxq %rdx, %rcx, %rdi
xorb (%rbx,%rsi), %dil
movb %dil, (%r15,%rsi)
incq %rsi
addq $-0x8, %rdx
cmpq %rsi, %rax
jne 0x1e42
xorl %edx, %edx
xorl %ecx, %ecx
movzbl (%r15,%rdx), %esi
orq %rsi, %rcx
shlq $0x8, %rcx
incq %rdx
cmpq %rdx, %rax
jne 0x1e5f
orq $0x80, %rcx
jmp 0x1e81
movl $0x80, %ecx
shll $0x3, %eax
movb $0x38, %dl
subb %al, %dl
shlxq %rdx, %rcx, %rdi
leaq 0x10(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x8(%rsp), %r13
xorl %ebp, %ebp
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x1090
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
movq 0x20(%rsp), %rdx
addq %rdx, %rax
addq $-0x10, %rax
movq %rax, (%rcx)
movq 0x30(%rsp,%rbp,8), %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
movl $0x1, %r9d
callq 0x1090
testq %rbp, %rbp
je 0x1ef7
cmpq $0x2, %rbp
jne 0x1f0b
jmp 0x1f10
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
xorl %eax, 0x14(%rsp)
incq %rbp
jmp 0x1ece
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
xorl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rcx
leaq (%rcx,%rbx), %rax
addq $-0x10, %rax
addq %rbx, %rcx
addq $-0xf, %rcx
xorl %edx, %edx
movzbl (%rax,%rdx,4), %esi
movq $-0x3, %rdi
movl %esi, %r8d
shll $0x8, %r8d
movzbl 0x3(%rcx,%rdi), %esi
orl %r8d, %esi
incq %rdi
jne 0x1f45
cmpl %esi, 0x10(%rsp,%rdx,4)
jne 0x1f70
incq %rdx
addq $0x4, %rcx
cmpq $0x4, %rdx
jne 0x1f3a
xorl %eax, %eax
jmp 0x1f75
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop