// File: /workspace/github_c_makefile_star2_O0/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:
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
movq $0x80, 0x60(%rsp)
movq $0x400000, 0x58(%rsp)   # imm = 0x400000
leaq 0x40(%rsp), %rdi
movl $0x4, %esi
callq 0x1ed0
leaq 0x70(%rsp), %rdi
movl $0x3, %esi
callq 0x1f10
movq 0xd8(%rsp), %rax
subq $0x10, %rax
cmpq $0x8, %rax
jb 0x2784
leaq 0x90(%rsp), %rdi
callq 0x1120
movq %rax, 0x88(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x8, 0x10(%rsp)
jae 0x258d
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 0x2534
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 0x2640
movq 0x58(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x60(%rsp)
jne 0x277f
movq $0x0, 0x10(%rsp)
cmpq $0x3, 0x10(%rsp)
jae 0x269c
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 0x2655
movq $0x80, 0x60(%rsp)
leaq 0x70(%rsp), %rdi
movl $0x3, %esi
callq 0x1f10
cmpq $0x0, 0x58(%rsp)
je 0x277d
cmpq $0x20, 0xd8(%rsp)
jbe 0x277d
movq $0x0, 0x10(%rsp)
cmpq $0x4, 0x10(%rsp)
jae 0x2704
movq 0xe0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xe0(%rsp)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x26d8
movq 0xd8(%rsp), %rax
subq $0x10, %rax
movq %rax, 0xd8(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x4, 0x10(%rsp)
jae 0x277b
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 0x2769
movl $0xffffffff, 0x104(%rsp) # imm = 0xFFFFFFFF
jmp 0x2a01
jmp 0x276b
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x2721
jmp 0x277d
jmp 0x277f
jmp 0x2500
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 0x2809
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 0x27a2
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 0x2938
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 0x28f7
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 0x2923
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 0x2925
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x2890
movq $0x0, 0x10(%rsp)
cmpq $0x4, 0x10(%rsp)
jae 0x296d
movq 0xe0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xe0(%rsp)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x2941
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 0x29f6
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 0x29e4
movl $0xffffffff, 0x104(%rsp) # imm = 0xFFFFFFFF
jmp 0x2a01
jmp 0x29e6
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x299f
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq