// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/trivia128v2/ref/encrypt.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
unsigned char *c,unsigned long long *clen,
const unsigned char *m,unsigned long long mlen,
const unsigned char *ad,unsigned long long adlen,
const unsigned char *nsec,
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; 
   int i; 
   *clen=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 MESSAGE BLOCKS.       */
//--------------------------------//


   ck_ctr = CHUNK_SIZE_MSG; blk_ctr= MAX_IT;  set_zero32(Tag,4); set_zero64(Checksum,3);
   while(mlen >=8) // Process ALL but the Final Block of Message 
   {

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

        m = m+8; c= c+8; mlen = mlen-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 && mlen > 0) // In case of Itag, we compute intermediate tag.  // CHANGE
	   {
		for(i=0;i<4;i++){store32(c, Tag[i]); c= c+4;} *clen= *clen+16;
	   }
   	}
   }

// Process the Final Block (possibly emty) of Mesage after we apply 10* pad
   process_block(pad(m, mlen), Tag, Checksum, SC_State, Z, PROC_BLOCK);
   for(i=0;i<mlen;i++) {c[i] = ((u8)(Z[0]>>(8*(7-i))))^(m[i]);} //ciphertext for final block 


   *clen= *clen+mlen; c=c+mlen; 

// ...And the final 4 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++){store32(c, Tag[i]); c= c+4;} *clen= *clen+16; 

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, (%rsp)
movq %r8, %r15
movq %rcx, %rbp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
andq $0x0, (%r13)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
callq 0x1050
movq %rbx, %rdi
movq %r15, %rsi
movq (%rsp), %rdx
callq 0x10c0
leaq 0x10(%rsp), %rdi
callq 0x1829
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x183b
movl $0x400000, %ecx         # imm = 0x400000
movl $0x80, %eax
leaq 0x8(%rsp), %r15
movq %r13, 0x20(%rsp)
movq %rbp, (%rsp)
cmpq $0x8, %rbp
jb 0x19fc
movq %rcx, %r13
movq %rax, 0x28(%rsp)
movq %r12, %rdi
callq 0x1080
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r15, %r8
xorl %r9d, %r9d
callq 0x1100
pushq $0x38
popq %rax
xorl %ecx, %ecx
cmpq $-0x8, %rax
je 0x192a
shrxq %rax, 0x8(%rsp), %rdx
xorb (%r12,%rcx), %dl
movb %dl, (%r14,%rcx)
incq %rcx
addq $-0x8, %rax
jmp 0x190c
addq $0x8, %r12
movq 0x20(%rsp), %rax
addq $0x8, (%rax)
addq $0x8, %r14
movq (%rsp), %rbp
addq $-0x8, %rbp
movq %r13, %rax
subq $0x1, %rax
movl $0x0, %ecx
cmovaeq %rax, %rcx
movq 0x28(%rsp), %rax
decq %rax
jne 0x18ce
movq %rcx, 0x28(%rsp)
movq %rbp, (%rsp)
xorl %ebp, %ebp
cmpq $0x3, %rbp
je 0x1995
movq 0x30(%rsp,%rbp,8), %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r15, %r8
pushq $0x1
popq %r9
callq 0x1100
incq %rbp
jmp 0x196c
movq %rbx, %rdi
callq 0x183b
cmpq $0x2, %r13
setb %al
movq (%rsp), %rbp
testq %rbp, %rbp
sete %cl
orb %al, %cl
movl $0x80, %eax
movq 0x28(%rsp), %rcx
jne 0x18ce
xorl %r13d, %r13d
leaq (%r14,%r13), %rdi
cmpq $0x10, %r13
je 0x19dd
movl 0x10(%rsp,%r13), %esi
callq 0x1b06
addq $0x4, %r13
jmp 0x19c3
movq 0x20(%rsp), %rax
addq $0x10, (%rax)
movl $0x80, %eax
movq %rdi, %r14
movq (%rsp), %rbp
movq 0x28(%rsp), %rcx
jmp 0x18ce
movq %r12, %rdi
movq (%rsp), %r15
movl %r15d, %esi
callq 0x10a0
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x8(%rsp), %r8
xorl %ebx, %ebx
movq %rax, %rdi
xorl %r9d, %r9d
callq 0x1100
pushq $0x38
popq %rax
cmpq %rbx, %r15
jbe 0x1a51
movl %eax, %ecx
andb $-0x8, %cl
shrxq %rcx, 0x8(%rsp), %rcx
xorb (%r12,%rbx), %cl
movb %cl, (%r14,%rbx)
incq %rbx
addq $-0x8, %rax
jmp 0x1a2f
movq 0x20(%rsp), %rax
addq %r15, (%rax)
xorl %ebx, %ebx
leaq 0x30(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x8(%rsp), %r13
pushq $0x1
popq %rbp
cmpq $0x3, %rbx
je 0x1aca
movq 0x30(%rsp,%rbx,8), %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
movl %ebp, %r9d
callq 0x1100
cmpl $0x2, %ebx
je 0x1aae
testl %ebx, %ebx
jne 0x1ac5
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
leaq 0x14(%rsp), %rcx
jmp 0x1ac3
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
leaq 0x1c(%rsp), %rcx
xorl %eax, (%rcx)
incq %rbx
jmp 0x1a6d
addq (%rsp), %r14
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
cmpq $0x10, %rbx
je 0x1aee
leaq (%r14,%rbx), %rdi
movl 0x10(%rsp,%rbx), %esi
callq 0x1b06
addq $0x4, %rbx
jmp 0x1ad5
addq $0x10, (%r15)
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq