// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rsi, %rax
movq %rdi, %r14
movq 0x100(%rsp), %rsi
movq 0xf8(%rsp), %rdx
xorl %ebp, %ebp
movq %rax, 0x58(%rsp)
movq %rbp, (%rax)
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0x1030
movq %r13, %rdi
movq %rbx, %rsi
movq 0xf0(%rsp), %rdx
callq 0x1060
movq %r15, %rdx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
vmovaps %xmm0, 0x30(%rsp)
movq %rbp, 0x40(%rsp)
leaq -0x10(%r15), %rax
cmpq $0x8, %rax
jb 0x1e8e
movl $0x400000, %edi         # imm = 0x400000
movl $0x80, %ebp
leaq 0x20(%rsp), %rsi
leaq 0x70(%rsp), %r15
leaq 0x10(%rsp), %r13
movq %rdx, 0x60(%rsp)
movq %rdi, 0x68(%rsp)
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %rdx
movq %rcx, %rax
shldq $0x2, %rdx, %rax
movq 0x80(%rsp), %r10
shrdq $0x3c, %rdx, %r10
xorq %rax, %r10
movq 0x88(%rsp), %rax
movq 0x90(%rsp), %rdi
movq %rax, %r8
shldq $0x5, %rdi, %r8
movq %rax, %r9
shldq $0x29, %rdi, %r9
xorq %r8, %r9
xorq %r10, %r9
movq 0x98(%rsp), %r11
movq 0xa0(%rsp), %r8
shldq $0x2, %r8, %r11
movq 0xa8(%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, 0x10(%rsp)
movl $0x38, %ecx
xorl %edx, %edx
shrxq %rcx, %rax, %rdi
xorb (%r12,%rdx), %dil
movb %dil, (%r14,%rdx)
incq %rdx
addq $-0x8, %rcx
cmpq $-0x8, %rcx
jne 0x1d02
movzbl (%r14), %edi
movl $0x1, %eax
leaq 0x30(%rsp), %rdx
movq %rdi, %rcx
shlq $0x8, %rcx
movzbl (%r14,%rax), %edi
orq %rcx, %rdi
incq %rax
cmpq $0x8, %rax
jne 0x1d2a
movq %rsi, %rbx
movq %r15, %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x1090
movq 0x58(%rsp), %rax
addq $0x8, (%rax)
leaq 0x8(%r12), %r8
movq 0x60(%rsp), %rax
leaq -0x8(%rax), %rcx
movq 0x68(%rsp), %rax
subq $0x1, %rax
movl $0x0, %edi
cmovaeq %rax, %rdi
decq %rbp
je 0x1d89
movq %rbx, %rsi
jmp 0x1e74
movq %rdi, 0x50(%rsp)
movq %r12, 0xb0(%rsp)
movq %rcx, 0x8(%rsp)
movq %r8, 0x18(%rsp)
xorl %ebp, %ebp
movq %r13, %r12
movq %r15, %r13
leaq 0x30(%rsp), %r15
movq 0x30(%rsp,%rbp,8), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %r12, %r8
movl $0x1, %r9d
callq 0x1090
incq %rbp
cmpq $0x3, %rbp
jne 0x1dad
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpq $0x2, 0x68(%rsp)
setb %al
cmpq $0x21, 0x8(%rsp)
setb %cl
movl $0x80, %ebp
orb %al, %cl
movq %rbx, %rsi
jne 0x1e5f
movq 0xb0(%rsp), %r11
leaq 0x18(%r11), %r8
movq 0x60(%rsp), %r9
addq $-0x18, %r9
leaq 0x9(%r11), %rax
xorl %ecx, %ecx
movq %r13, %r15
movq %r12, %r13
movzbl 0x8(%r11,%rcx,4), %edx
movq $-0x3, %r10
movl %edx, %edi
shll $0x8, %edi
movzbl 0x3(%rax,%r10), %edx
orl %edi, %edx
incq %r10
jne 0x1e31
cmpl %edx, 0x20(%rsp,%rcx,4)
jne 0x2073
incq %rcx
addq $0x4, %rax
cmpq $0x4, %rcx
jne 0x1e24
movq %r9, %rcx
jmp 0x1e6f
movq 0x18(%rsp), %r8
movq 0x8(%rsp), %rcx
movq %r13, %r15
movq %r12, %r13
movq 0x50(%rsp), %rdi
addq $0x8, %r14
leaq -0x10(%rcx), %rax
movq %r8, %r12
movq %rcx, %rdx
cmpq $0x7, %rax
ja 0x1c6c
jmp 0x1e94
movq %rdx, %rcx
movq %r12, %r8
movq %rcx, 0x8(%rsp)
movq %r8, 0x18(%rsp)
movq 0x70(%rsp), %rdx
movq 0x78(%rsp), %rsi
movq %rdx, %rcx
shldq $0x2, %rsi, %rcx
movq 0x80(%rsp), %rdi
shrdq $0x3c, %rsi, %rdi
xorq %rcx, %rdi
movq 0x88(%rsp), %rcx
movq 0x90(%rsp), %r8
movq %rcx, %r9
shldq $0x5, %r8, %r9
movq %rcx, %r10
shldq $0x29, %r8, %r10
xorq %r9, %r10
xorq %rdi, %r10
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %r9
shldq $0x2, %r9, %rdi
movq 0xa8(%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, 0x10(%rsp)
testq %rax, %rax
je 0x1f6e
movl $0x38, %edx
xorl %esi, %esi
movq 0x18(%rsp), %r8
shrxq %rdx, %rcx, %rdi
xorb (%r8,%rsi), %dil
movb %dil, (%r14,%rsi)
incq %rsi
addq $-0x8, %rdx
cmpq %rsi, %rax
jne 0x1f34
xorl %edx, %edx
xorl %ecx, %ecx
movzbl (%r14,%rdx), %esi
orq %rsi, %rcx
shlq $0x8, %rcx
incq %rdx
cmpq %rdx, %rax
jne 0x1f51
orq $0x80, %rcx
jmp 0x1f73
movl $0x80, %ecx
shll $0x3, %eax
movb $0x38, %dl
subb %al, %dl
shlxq %rdx, %rcx, %rdi
leaq 0x20(%rsp), %rbx
leaq 0x30(%rsp), %r14
leaq 0x70(%rsp), %r15
leaq 0x10(%rsp), %r12
xorl %r13d, %r13d
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
xorl %r9d, %r9d
callq 0x1090
movq 0x58(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rsp), %rcx
addq %rcx, %rax
addq $-0x10, %rax
movq %rax, (%rdx)
leaq 0x24(%rsp), %rbp
movq 0x30(%rsp,%r13,8), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
movl $0x1, %r9d
callq 0x1090
cmpq $0x2, %r13
je 0x2002
testq %r13, %r13
jne 0x2019
movq 0x10(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x20(%rsp)
movq %rbp, %rcx
jmp 0x2017
movq 0x10(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x28(%rsp)
leaq 0x2c(%rsp), %rcx
xorl %eax, (%rcx)
incq %r13
cmpq $0x3, %r13
jne 0x1fc6
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx), %rax
addq $-0x10, %rax
addq %rdx, %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 0x2048
cmpl %esi, 0x20(%rsp,%rdx,4)
jne 0x2073
incq %rdx
addq $0x4, %rcx
cmpq $0x4, %rdx
jne 0x203d
xorl %eax, %eax
jmp 0x2078
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop