// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/trivia0v2/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 Padded Message 
   {
        Block = load64(m);    
	process_block(Block, Tag, Checksum, SC_State, Z, PROC_BLOCK); //Update the Tag and the Checksum
        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); // Update Tag 
                                                                                                 // for the Checksum Block
	   }
	   ck_ctr=CHUNK_SIZE_MSG; set_zero64(Checksum,3); // Reset The Chunk Counter and Set the Checksum Array to zero
	   
	   /* if(IT && blk_ctr!=0) //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); // Update the Tag and Checksum for the 
                                                                                  // Final 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;

// Update the Tag for the Final 3 Checksum Blocks for Message and Get the Key for Variable Length Handling
   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 Message
   //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, %rbx
movq %r8, %r15
movq %rcx, %rbp
movq %rdx, %r12
movq %rsi, %rax
movq %rdi, %r14
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
xorl %ecx, %ecx
movq %rax, 0x20(%rsp)
movq %rcx, (%rax)
leaq 0x50(%rsp), %r13
movq %r13, %rdi
callq 0x1030
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1060
xorl %edi, %edi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovaps %xmm0, 0x30(%rsp)
movq %rdi, 0x40(%rsp)
movq %rbp, 0x28(%rsp)
cmpq $0x8, %rbp
jb 0x1a2f
movabsq $0x100000000, %r13      # imm = 0x100000000
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x8(%rsp), %r8
movzbl (%r12), %edi
movl $0x1, %eax
movq %rdi, %r9
shlq $0x8, %r9
movzbl (%r12,%rax), %edi
orq %r9, %rdi
incq %rax
cmpq $0x8, %rax
jne 0x1947
movq %rsi, %rbp
movq %rdx, %rbx
movq %r8, %r15
xorl %r9d, %r9d
callq 0x1090
movl $0x38, %eax
movq 0x8(%rsp), %rcx
xorl %edx, %edx
shrxq %rax, %rcx, %rsi
xorb (%r12,%rdx), %sil
movb %sil, (%r14,%rdx)
incq %rdx
addq $-0x8, %rax
cmpq $0x8, %rdx
jne 0x197c
movq 0x20(%rsp), %rax
movq (%rax), %rdi
addq $0x8, %rdi
movq %rdi, (%rax)
decq %r13
movq %rbp, %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r15, %r8
jne 0x1a0f
movq %rdi, 0x48(%rsp)
xorl %r13d, %r13d
movq 0x30(%rsp,%r13,8), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl $0x1, %r9d
callq 0x1090
movq %r15, %r8
leaq 0x50(%rsp), %rcx
incq %r13
cmpq $0x3, %r13
jne 0x19c0
movq %rbx, %rdx
movq %rbp, %rsi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movabsq $0x100000000, %r13      # imm = 0x100000000
movq 0x48(%rsp), %rdi
addq $0x8, %r12
addq $0x8, %r14
movq 0x28(%rsp), %rax
addq $-0x8, %rax
movq %rax, 0x28(%rsp)
cmpq $0x7, %rax
ja 0x193d
movq 0x28(%rsp), %r15
testq %r15, %r15
je 0x1ab5
xorl %ecx, %ecx
xorl %eax, %eax
movzbl (%r12,%rcx), %edx
orq %rdx, %rax
shlq $0x8, %rax
incq %rcx
cmpq %rcx, %r15
jne 0x1a3d
orq $0x80, %rax
leal (,%r15,8), %ecx
movb $0x38, %dl
subb %cl, %dl
shlxq %rdx, %rax, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x8(%rsp), %r8
xorl %ebx, %ebx
xorl %r9d, %r9d
callq 0x1090
movl $0x38, %eax
movl %eax, %ecx
andb $-0x8, %cl
shrxq %rcx, 0x8(%rsp), %rcx
xorb (%r12,%rbx), %cl
movb %cl, (%r14,%rbx)
incq %rbx
addq $-0x8, %rax
cmpq %rbx, %r15
jne 0x1a8b
movq 0x20(%rsp), %rax
movq (%rax), %rbx
jmp 0x1ae3
movq %rdi, %rbx
movabsq $-0x8000000000000000, %rdi # imm = 0x8000000000000000
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x8(%rsp), %r8
xorl %r9d, %r9d
callq 0x1090
movq 0x20(%rsp), %rax
addq %r15, %rbx
movq %rbx, (%rax)
xorl %ebp, %ebp
leaq 0x10(%rsp), %rbx
leaq 0x30(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x8(%rsp), %r13
movq 0x30(%rsp,%rbp,8), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
movl $0x1, %r9d
callq 0x1090
cmpl $0x2, %ebp
je 0x1b3b
testl %ebp, %ebp
jne 0x1b52
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
leaq 0x14(%rsp), %rcx
jmp 0x1b50
movq 0x8(%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 0x1aff
addq 0x28(%rsp), %r14
xorl %eax, %eax
movq 0x20(%rsp), %rsi
movl 0x10(%rsp,%rax,4), %ecx
movl $0x4, %edx
movb %cl, -0x1(%r14,%rdx)
shrl $0x8, %ecx
decq %rdx
jne 0x1b70
addq $0x4, %r14
incq %rax
cmpq $0x4, %rax
jne 0x1b67
addq $0x10, (%rsi)
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq