// File: /workspace/github_c_makefile_star2_O3/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, %rbp
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %rax
movq %rdi, %r12
movq 0xf0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
xorl %ecx, %ecx
movq %rax, 0x8(%rsp)
movq %rcx, (%rax)
leaq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x1030
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1060
xorl %ecx, %ecx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovaps %xmm0, 0x30(%rsp)
movq %rcx, 0x40(%rsp)
movq %rbx, 0x20(%rsp)
cmpq $0x8, %rbx
jb 0x1acc
movl $0x80, %edi
movl $0x400000, %r10d        # imm = 0x400000
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rsp, %r8
movq 0x8(%rsp), %rbx
movq %rdi, 0x28(%rsp)
movq %r10, 0x50(%rsp)
movzbl (%r15), %edi
movl $0x1, %eax
movq %rdi, %rcx
shlq $0x8, %rcx
movzbl (%r15,%rax), %edi
orq %rcx, %rdi
incq %rax
cmpq $0x8, %rax
jne 0x194b
movq %rsi, %rbp
leaq 0x60(%rsp), %rcx
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 0x197d
leaq 0x8(%r12), %r13
addq $-0x8, 0x20(%rsp)
movq (%rbx), %rcx
addq $0x8, %rcx
movq %rcx, (%rbx)
movq 0x50(%rsp), %rax
subq $0x1, %rax
movl $0x0, %r10d
cmovaeq %rax, %r10
movq 0x28(%rsp), %rdi
decq %rdi
je 0x19da
movq %rbp, %rsi
leaq 0x30(%rsp), %rdx
movq %r14, %r8
jmp 0x1ab7
movq %r10, 0x48(%rsp)
movq %rcx, 0x58(%rsp)
movq %r15, 0x28(%rsp)
xorl %ebx, %ebx
leaq 0x60(%rsp), %r15
leaq 0x30(%rsp), %rbp
movq 0x30(%rsp,%rbx,8), %rdi
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %r14, %r8
movl $0x1, %r9d
callq 0x1090
incq %rbx
cmpq $0x3, %rbx
jne 0x19f5
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
cmpq $0x2, 0x50(%rsp)
setb %al
cmpq $0x0, 0x20(%rsp)
sete %cl
movl $0x80, %edi
orb %al, %cl
leaq 0x10(%rsp), %rsi
movq %r14, %r8
jne 0x1a9e
xorl %eax, %eax
movq 0x8(%rsp), %rbx
movq 0x28(%rsp), %r15
movq 0x48(%rsp), %r10
movl 0x10(%rsp,%rax,4), %ecx
movl $0xb, %edx
movb %cl, (%r12,%rdx)
shrl $0x8, %ecx
decq %rdx
cmpq $0x7, %rdx
jne 0x1a6c
addq $0x4, %r13
incq %rax
addq $0x4, %r12
cmpq $0x4, %rax
jne 0x1a63
movq (%rbx), %rcx
addq $0x10, %rcx
movq %rcx, (%rbx)
leaq 0x30(%rsp), %rdx
jmp 0x1ab7
movq 0x8(%rsp), %rbx
leaq 0x30(%rsp), %rdx
movq 0x28(%rsp), %r15
movq 0x58(%rsp), %rcx
movq 0x48(%rsp), %r10
addq $0x8, %r15
movq %r13, %r12
cmpq $0x7, 0x20(%rsp)
ja 0x1938
jmp 0x1ad4
movq %r12, %r13
movq 0x8(%rsp), %rbx
movq 0x20(%rsp), %r14
testq %r14, %r14
je 0x1b58
xorl %ecx, %ecx
xorl %eax, %eax
movzbl (%r15,%rcx), %edx
orq %rdx, %rax
shlq $0x8, %rax
incq %rcx
cmpq %rcx, %r14
jne 0x1ae2
orq $0x80, %rax
leal (,%r14,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 %ebx, %ebx
xorl %r9d, %r9d
callq 0x1090
movl $0x38, %eax
movl %eax, %ecx
andb $-0x8, %cl
shrxq %rcx, (%rsp), %rcx
xorb (%r15,%rbx), %cl
movb %cl, (%r13,%rbx)
incq %rbx
addq $-0x8, %rax
cmpq %rbx, %r14
jne 0x1b2e
movq 0x8(%rsp), %rbx
movq (%rbx), %rcx
jmp 0x1b82
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rcx, %r15
leaq 0x60(%rsp), %rcx
movq %rsp, %r8
xorl %r9d, %r9d
callq 0x1090
movq %r15, %rcx
addq %r14, %rcx
movq %rcx, (%rbx)
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 0x1bd7
testl %ebp, %ebp
jne 0x1bed
movq (%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
leaq 0x14(%rsp), %rcx
jmp 0x1beb
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 0x1b9c
addq 0x20(%rsp), %r13
xorl %eax, %eax
movq 0x8(%rsp), %rsi
movl 0x10(%rsp,%rax,4), %ecx
movl $0x4, %edx
movb %cl, -0x1(%r13,%rdx)
shrl $0x8, %ecx
decq %rdx
jne 0x1c0b
addq $0x4, %r13
incq %rax
cmpq $0x4, %rax
jne 0x1c02
addq $0x10, (%rsi)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq