// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x108, %rsp            # imm = 0x108
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %rax
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq %rdx, 0xe8(%rsp)
movq %rcx, 0xe0(%rsp)
movq %r8, 0xd8(%rsp)
movq %r9, 0xd0(%rsp)
movl $0x0, 0x4(%rsp)
movq 0xf0(%rsp), %rax
movq $0x0, (%rax)
leaq 0x90(%rsp), %rdi
movq 0x120(%rsp), %rsi
movq 0x118(%rsp), %rdx
callq 0x1050
leaq 0x90(%rsp), %rdi
movq 0xd0(%rsp), %rsi
movq 0x110(%rsp), %rdx
callq 0x10c0
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x60(%rsp)
movq $0x8000000, 0x58(%rsp)  # imm = 0x8000000
leaq 0x40(%rsp), %rdi
movl $0x4, %esi
callq 0x1ee0
leaq 0x70(%rsp), %rdi
movl $0x3, %esi
callq 0x1f20
movq 0xd8(%rsp), %rax
subq $0x10, %rax
cmpq $0x8, %rax
jb 0x2661
leaq 0x90(%rsp), %rdi
callq 0x1120
movq %rax, 0x88(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x8, 0x10(%rsp)
jae 0x2533
movq 0x88(%rsp), %rax
movl $0x7, %ecx
subq 0x10(%rsp), %rcx
shlq $0x3, %rcx
shrq %cl, %rax
movzbl %al, %eax
movq 0xe0(%rsp), %rcx
movq 0x10(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xf8(%rsp), %rax
movq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x24da
movq 0xf8(%rsp), %rdi
callq 0x1080
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x88(%rsp), %r8
xorl %r9d, %r9d
callq 0x1100
movq 0xf8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xf8(%rsp)
movq 0xe0(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xe0(%rsp)
movq 0xf0(%rsp), %rax
movq (%rax), %rcx
addq $0x8, %rcx
movq 0xf0(%rsp), %rax
movq %rcx, (%rax)
movq 0xd8(%rsp), %rax
subq $0x8, %rax
movq %rax, 0xd8(%rsp)
movq 0x60(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x58(%rsp)
jbe 0x25e6
movq 0x58(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x60(%rsp)
jne 0x265c
movq $0x0, 0x10(%rsp)
cmpq $0x3, 0x10(%rsp)
jae 0x263e
movq 0x10(%rsp), %rax
movq 0x70(%rsp,%rax,8), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x88(%rsp), %r8
movl $0x1, %r9d
callq 0x1100
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x25f7
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x60(%rsp)
leaq 0x70(%rsp), %rdi
movl $0x3, %esi
callq 0x1f20
jmp 0x24a6
leaq 0x90(%rsp), %rdi
callq 0x1120
movq %rax, 0x88(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq 0xd8(%rsp), %rcx
subq $0x10, %rcx
cmpq %rcx, %rax
jae 0x26e6
movq 0x88(%rsp), %rax
movl $0x7, %ecx
subq 0x10(%rsp), %rcx
shlq $0x3, %rcx
shrq %cl, %rax
movzbl %al, %eax
movq 0xe0(%rsp), %rcx
movq 0x10(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xf8(%rsp), %rax
movq 0x10(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x267f
movq 0xf8(%rsp), %rdi
movq 0xd8(%rsp), %rax
subq $0x10, %rax
movl %eax, %esi
callq 0x10a0
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x88(%rsp), %r8
xorl %r9d, %r9d
callq 0x1100
movq 0xf0(%rsp), %rax
movq (%rax), %rcx
addq 0xd8(%rsp), %rcx
subq $0x10, %rcx
movq 0xf0(%rsp), %rax
movq %rcx, (%rax)
movq 0xe0(%rsp), %rax
addq 0xd8(%rsp), %rax
addq $-0x10, %rax
movq %rax, 0xe0(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x3, 0x10(%rsp)
jae 0x2815
movq 0x10(%rsp), %rax
movq 0x70(%rsp,%rax,8), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x88(%rsp), %r8
movl $0x1, %r9d
callq 0x1100
cmpq $0x0, 0x10(%rsp)
jne 0x27d4
movq 0x88(%rsp), %rax
shrq $0x20, %rax
xorl 0x40(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0x88(%rsp), %rax
xorl 0x44(%rsp), %eax
movl %eax, 0x44(%rsp)
cmpq $0x2, 0x10(%rsp)
jne 0x2800
movq 0x88(%rsp), %rax
shrq $0x20, %rax
xorl 0x48(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x88(%rsp), %rax
xorl 0x4c(%rsp), %eax
movl %eax, 0x4c(%rsp)
jmp 0x2802
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x276d
movq $0x0, 0x10(%rsp)
cmpq $0x4, 0x10(%rsp)
jae 0x284a
movq 0xe0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xe0(%rsp)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x281e
movq 0xd8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xd8(%rsp)
movq 0xe0(%rsp), %rdi
addq $-0x4, %rdi
callq 0x10b0
movl %eax, 0x3c(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x4, 0x10(%rsp)
jae 0x28d3
movq 0xe0(%rsp), %rdi
addq $-0x10, %rdi
movq 0x10(%rsp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x10b0
movl %eax, 0x3c(%rsp)
movq 0x10(%rsp), %rax
movl 0x40(%rsp,%rax,4), %eax
cmpl 0x3c(%rsp), %eax
je 0x28c1
movl $0xffffffff, 0x104(%rsp) # imm = 0xFFFFFFFF
jmp 0x28de
jmp 0x28c3
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x287c
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopl (%rax)