// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rax
movq %rdi, %r14
movq 0xe0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
movq %rax, 0x28(%rsp)
andq $0x0, (%rax)
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x1050
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x10c0
leaq 0x10(%rsp), %rdi
callq 0x182c
leaq 0x30(%rsp), %rbx
movabsq $0x100000000, %rbp      # imm = 0x100000000
leaq 0x8(%rsp), %r13
movq %rbx, %rdi
callq 0x183e
movq %r12, %rdi
cmpq $0x8, %r15
jb 0x1975
callq 0x1080
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x1100
movq 0x8(%rsp), %rax
pushq $0x38
popq %rcx
xorl %edx, %edx
cmpq $-0x8, %rcx
je 0x1921
shrxq %rcx, %rax, %rsi
xorb (%r12,%rdx), %sil
movb %sil, (%r14,%rdx)
incq %rdx
addq $-0x8, %rcx
jmp 0x1905
addq $0x8, %r12
addq $0x8, %r14
addq $-0x8, %r15
movq 0x28(%rsp), %rax
addq $0x8, (%rax)
decq %rbp
jne 0x18ce
xorl %ebp, %ebp
cmpq $0x3, %rbp
je 0x1966
movq 0x30(%rsp,%rbp,8), %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
leaq 0x50(%rsp), %rcx
movq %r13, %r8
pushq $0x1
popq %r9
callq 0x1100
incq %rbp
jmp 0x193d
movabsq $0x100000000, %rbp      # imm = 0x100000000
jmp 0x18c6
movl %r15d, %esi
callq 0x10a0
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x8(%rsp), %rbx
xorl %r13d, %r13d
movq %rax, %rdi
movq %rbx, %r8
xorl %r9d, %r9d
callq 0x1100
pushq $0x38
popq %rax
movq (%rbx), %rcx
cmpq %r13, %r15
jbe 0x19c8
movl %eax, %edx
andb $-0x8, %dl
shrxq %rdx, %rcx, %rdx
xorb (%r12,%r13), %dl
movb %dl, (%r14,%r13)
incq %r13
addq $-0x8, %rax
jmp 0x19a8
movq 0x28(%rsp), %rax
addq %r15, (%rax)
xorl %r12d, %r12d
leaq 0x50(%rsp), %r13
leaq 0x8(%rsp), %rbp
pushq $0x1
popq %rbx
cmpq $0x3, %r12
je 0x1a41
movq 0x30(%rsp,%r12,8), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %r13, %rcx
movq %rbp, %r8
movl %ebx, %r9d
callq 0x1100
cmpl $0x2, %r12d
je 0x1a25
testl %r12d, %r12d
jne 0x1a3c
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x10(%rsp)
leaq 0x14(%rsp), %rcx
jmp 0x1a3a
movq 0x8(%rsp), %rax
movq %rax, %rcx
shrq $0x20, %rcx
xorl %ecx, 0x18(%rsp)
leaq 0x1c(%rsp), %rcx
xorl %eax, (%rcx)
incq %r12
jmp 0x19e0
addq %r15, %r14
xorl %eax, %eax
pushq $0x3
popq %rcx
cmpq $0x4, %rax
je 0x1a6f
movl 0x10(%rsp,%rax,4), %edx
movl %ecx, %esi
testl %esi, %esi
js 0x1a66
movl %esi, %edi
movb %dl, (%r14,%rdi)
shrl $0x8, %edx
decl %esi
jmp 0x1a55
addq $0x4, %r14
incq %rax
jmp 0x1a49
movq 0x28(%rsp), %rax
addq $0x10, (%rax)
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq