// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/trivia128v2/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 Message 
   {

        Z[0] = Key_Ext64(SC_State); 

        for(i=0;i<8;i++) {m[i] = ((u8)(Z[0]>>(8*(7-i))))^(c[i]);  } 
        Block = load64(m);    
	process_block(Block, Tag, Checksum, SC_State, Z, PROC_BLOCK);

          // message computation 
	
        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);
	   }
	   ck_ctr=CHUNK_SIZE_MSG; set_zero64(Checksum,3);
	   
	   if(IT && blk_ctr!=0 && clen > 32) // 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
           	{
                
            		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);  
  
   for(i=0;i<clen-16;i++) {m[i] = ((u8)(Z[0]>>(8*(7-i))))^(c[i]);} //message for final block
   process_block(pad(m, clen-16), Tag, Checksum, SC_State, Z, PROC_BLOCK);
   *outputmlen=*outputmlen+clen-16; c=c+clen-16;
// ...And the final 3 Checksum Blocks for AD
   //for(i=0; i<3; i++){process_block(Checksum[i], Tag, Checksum, SC_State, Z+i, PROC_CHECKSUM);}
 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 AD   
   //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++)//Checking the equality of the intermediate tags
           {            
                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 $0xb8, %rsp
movq %r9, %rbx
movq %r8, %r15
movq %rcx, %r13
movq %rsi, %rax
movq %rdi, %r14
movq 0xf0(%rsp), %r12
movq 0x100(%rsp), %rsi
movq 0xf8(%rsp), %rdx
movq %rax, 0x30(%rsp)
andq $0x0, (%rax)
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
callq 0x1050
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x10c0
leaq 0x10(%rsp), %rdi
callq 0x1829
leaq 0x40(%rsp), %rdi
callq 0x183b
movq %r13, %rsi
movq %r15, %rdx
movl $0x80, %eax
movl $0x400000, %ecx         # imm = 0x400000
movq %rax, %r15
movq %rsi, %r12
movq %rcx, 0x38(%rsp)
movq %rdx, 0x28(%rsp)
leaq -0x10(%rdx), %rbx
movq %rbp, %r13
movq %rbp, %rdi
callq 0x1120
movq %rax, 0x8(%rsp)
pushq $0x38
popq %rcx
xorl %edx, %edx
cmpq $0x8, %rbx
jb 0x1d23
movq %r13, %rbx
leaq 0x10(%rsp), %rbp
cmpq $-0x8, %rcx
je 0x1bf2
shrxq %rcx, %rax, %rsi
xorb (%r12,%rdx), %sil
movb %sil, (%r14,%rdx)
incq %rdx
addq $-0x8, %rcx
jmp 0x1bd6
movq %r14, %rdi
callq 0x1080
movq %rax, %rdi
movq %rbp, %rsi
leaq 0x40(%rsp), %rdx
movq %rbx, %rcx
leaq 0x8(%rsp), %r8
xorl %r9d, %r9d
callq 0x1100
addq $0x8, %r14
movq 0x30(%rsp), %rax
addq $0x8, (%rax)
leaq 0x8(%r12), %rsi
movq 0x28(%rsp), %rax
leaq -0x8(%rax), %rdx
movq 0x38(%rsp), %rax
subq $0x1, %rax
movl $0x0, %ecx
cmovaeq %rax, %rcx
movq %r15, %rax
decq %rax
movq %rbx, %rbp
jne 0x1b9b
movq %rcx, %r15
movq %rdx, 0x60(%rsp)
movq %rsi, 0x68(%rsp)
xorl %ebx, %ebx
leaq 0x10(%rsp), %rbp
cmpq $0x3, %rbx
je 0x1c8e
movq 0x40(%rsp,%rbx,8), %rdi
movq %rbp, %rsi
leaq 0x40(%rsp), %rdx
movq %r13, %rcx
leaq 0x8(%rsp), %r8
pushq $0x1
popq %r9
callq 0x1100
incq %rbx
jmp 0x1c65
leaq 0x40(%rsp), %rdi
callq 0x183b
cmpq $0x2, 0x38(%rsp)
setb %al
movq 0x60(%rsp), %rdx
cmpq $0x21, %rdx
setb %cl
orb %al, %cl
movl $0x80, %eax
movq %r13, %rbp
movq 0x68(%rsp), %rsi
movq %r15, %rcx
jne 0x1b9b
leaq 0x18(%r12), %rbp
addq $-0x18, 0x28(%rsp)
addq $0x8, %r12
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0x1cfa
leaq (%r12,%rbx), %rdi
callq 0x10b0
leaq 0x4(%rbx), %rcx
cmpl %eax, 0x10(%rsp,%rbx)
movq %rcx, %rbx
movq %r15, %rcx
je 0x1cd6
jmp 0x1dfc
movl $0x80, %eax
movq 0x28(%rsp), %rdx
movq %rbp, %rsi
movq %r13, %rbp
jmp 0x1b9b
shrxq %rcx, %rax, %rsi
xorb (%r12,%rdx), %sil
movb %sil, (%r14,%rdx)
incq %rdx
addq $-0x8, %rcx
cmpq %rdx, %rbx
jne 0x1d0f
movq %r14, %rdi
movl %ebx, %esi
callq 0x10a0
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x8(%rsp), %rbp
xorl %r13d, %r13d
movq %rax, %rdi
movq %rdx, %r15
movq %rcx, %r14
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x1100
movq 0x30(%rsp), %rax
addq %rbx, (%rax)
addq 0x28(%rsp), %r12
pushq $0x1
popq %rbx
cmpq $0x3, %r13
je 0x1dcc
movq 0x40(%rsp,%r13,8), %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rbp, %r8
movl %ebx, %r9d
callq 0x1100
cmpq $0x2, %r13
je 0x1db0
testq %r13, %r13
jne 0x1dc7
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
leaq 0x14(%rsp), %rcx
jmp 0x1dc5
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
leaq 0x1c(%rsp), %rcx
xorl %eax, (%rcx)
incq %r13
jmp 0x1d6d
leaq -0x4(%r12), %rdi
callq 0x10b0
addq $-0x10, %r12
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0x1e01
leaq (%r12,%r14), %rdi
callq 0x10b0
leaq 0x4(%r14), %rcx
cmpl %eax, 0x10(%rsp,%r14)
movq %rcx, %r14
je 0x1ddf
pushq $-0x1
popq %rax
movl %eax, %ebx
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)