// File: /workspace/github_c_makefile_star2_O1/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 $0xa8, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %rbp
movq %rdx, %r15
movq %rsi, %rax
movq %rdi, %r12
movq 0xf0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
xorl %ecx, %ecx
movq %rax, 0x28(%rsp)
movq %rcx, (%rax)
leaq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x1030
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1060
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovaps %xmm0, 0x30(%rsp)
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movq %rbp, 0x8(%rsp)
cmpq $0x8, %rbp
jb 0x1a8e
movl $0x80, %ebp
movl $0x400000, %edi         # imm = 0x400000
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %rsp, %r8
movq %rdi, 0x58(%rsp)
movzbl (%r15), %edi
movl $0x1, %eax
movq %rdi, %r9
shlq $0x8, %r9
movzbl (%r15,%rax), %edi
orq %r9, %rdi
incq %rax
cmpq $0x8, %rax
jne 0x1945
movq %rsi, %rbx
movq %r8, %r14
xorl %r9d, %r9d
callq 0x1090
movl $0x38, %eax
xorl %ecx, %ecx
shrxq %rax, (%rsp), %rdx
xorb (%r15,%rcx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
addq $-0x8, %rax
cmpq $0x8, %rcx
jne 0x1972
movq 0x28(%rsp), %rax
addq $0x8, (%rax)
leaq 0x8(%r12), %r13
addq $-0x8, 0x8(%rsp)
movq 0x58(%rsp), %rax
subq $0x1, %rax
movl $0x0, %edi
cmovaeq %rax, %rdi
decq %rbp
je 0x19cd
movq %rbx, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %r14, %r8
jmp 0x1a79
movq %rdi, 0x50(%rsp)
xorl %ebp, %ebp
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %r14, %r8
movq 0x30(%rsp,%rbp,8), %rdi
movq %rbx, %rsi
movl $0x1, %r9d
callq 0x1090
movq %r14, %r8
leaq 0x60(%rsp), %rcx
leaq 0x30(%rsp), %rdx
incq %rbp
cmpq $0x3, %rbp
jne 0x19e1
movq %rbx, %rsi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpq $0x2, 0x58(%rsp)
setb %al
cmpq $0x0, 0x8(%rsp)
sete %dil
movl $0x80, %ebp
orb %al, %dil
jne 0x1a74
xorl %eax, %eax
movl 0x10(%rsp,%rax,4), %r9d
movl $0xb, %edi
movb %r9b, (%r12,%rdi)
shrl $0x8, %r9d
decq %rdi
cmpq $0x7, %rdi
jne 0x1a49
addq $0x4, %r13
incq %rax
addq $0x4, %r12
cmpq $0x4, %rax
jne 0x1a3f
movq 0x28(%rsp), %rax
addq $0x10, (%rax)
movq 0x50(%rsp), %rdi
addq $0x8, %r15
movq %r13, %r12
cmpq $0x7, 0x8(%rsp)
ja 0x1937
jmp 0x1a91
movq %r12, %r13
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x1abb
xorl %ecx, %ecx
xorl %eax, %eax
movzbl (%r15,%rcx), %edx
orq %rdx, %rax
shlq $0x8, %rax
incq %rcx
cmpq %rcx, %rbx
jne 0x1a9f
orq $0x80, %rax
jmp 0x1ac0
movl $0x80, %eax
leal (,%rbx,8), %ecx
movb $0x38, %dl
subb %cl, %dl
shlxq %rdx, %rax, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %rsp, %r8
xorl %r9d, %r9d
callq 0x1090
testq %rbx, %rbx
je 0x1b16
movl $0x38, %eax
xorl %ecx, %ecx
movl %eax, %edx
andb $-0x8, %dl
shrxq %rdx, (%rsp), %rdx
xorb (%r15,%rcx), %dl
movb %dl, (%r13,%rcx)
incq %rcx
addq $-0x8, %rax
cmpq %rcx, %rbx
jne 0x1af6
movq 0x28(%rsp), %rax
addq %rbx, (%rax)
xorl %ebp, %ebp
leaq 0x10(%rsp), %rbx
leaq 0x30(%rsp), %r14
leaq 0x60(%rsp), %r15
movq %rsp, %r12
movq 0x30(%rsp,%rbp,8), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq %r12, %r8
movl $0x1, %r9d
callq 0x1090
cmpl $0x2, %ebp
je 0x1b6d
testl %ebp, %ebp
jne 0x1b83
movq (%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
leaq 0x14(%rsp), %rcx
jmp 0x1b81
movq (%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
leaq 0x1c(%rsp), %rcx
xorl %eax, (%rcx)
incq %rbp
cmpq $0x3, %rbp
jne 0x1b32
addq 0x8(%rsp), %r13
xorl %eax, %eax
movq 0x28(%rsp), %rsi
movl 0x10(%rsp,%rax,4), %ecx
movl $0x4, %edx
movb %cl, -0x1(%r13,%rdx)
shrl $0x8, %ecx
decq %rdx
jne 0x1ba1
addq $0x4, %r13
incq %rax
cmpq $0x4, %rax
jne 0x1b98
addq $0x10, (%rsi)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq