// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xd8, %rsp
movq 0xf0(%rsp), %rax
movq 0xe8(%rsp), %rax
movq 0xe0(%rsp), %rax
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rcx, 0xb8(%rsp)
movq %r8, 0xb0(%rsp)
movq %r9, 0xa8(%rsp)
movq 0xc8(%rsp), %rax
movq $0x0, (%rax)
leaq 0x60(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
callq 0x1050
leaq 0x60(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x10c0
movq $0x80, 0x30(%rsp)
movq $0x400000, 0x28(%rsp)   # imm = 0x400000
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
callq 0x1ed0
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x1f10
cmpq $0x8, 0xb8(%rsp)
jb 0x220b
movq 0xc0(%rsp), %rdi
callq 0x1080
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0x58(%rsp), %r8
xorl %r9d, %r9d
callq 0x1100
movl $0x0, 0x4(%rsp)
cmpl $0x8, 0x4(%rsp)
jge 0x20ae
movq 0x58(%rsp), %rax
movl $0x7, %ecx
subl 0x4(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
shrq %cl, %rax
movzbl %al, %eax
movq 0xc0(%rsp), %rcx
movslq 0x4(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xd0(%rsp), %rax
movslq 0x4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x205c
movq 0xc0(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xc0(%rsp)
movq 0xd0(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xd0(%rsp)
movq 0xb8(%rsp), %rax
subq $0x8, %rax
movq %rax, 0xb8(%rsp)
movq 0xc8(%rsp), %rax
movq (%rax), %rcx
addq $0x8, %rcx
movq 0xc8(%rsp), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x28(%rsp)
jbe 0x2128
movq 0x28(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x2206
movl $0x0, 0x4(%rsp)
cmpl $0x3, 0x4(%rsp)
jge 0x2179
movslq 0x4(%rsp), %rax
movq 0x40(%rsp,%rax,8), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0x58(%rsp), %r8
movl $0x1, %r9d
callq 0x1100
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x213c
movq $0x80, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x1f10
cmpq $0x0, 0x28(%rsp)
je 0x2204
cmpq $0x0, 0xb8(%rsp)
jbe 0x2204
movl $0x0, 0x4(%rsp)
cmpl $0x4, 0x4(%rsp)
jge 0x21ea
movq 0xd0(%rsp), %rdi
movslq 0x4(%rsp), %rax
movl 0x10(%rsp,%rax,4), %esi
callq 0x23e0
movq 0xd0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xd0(%rsp)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x21ac
movq 0xc8(%rsp), %rax
movq (%rax), %rcx
addq $0x10, %rcx
movq 0xc8(%rsp), %rax
movq %rcx, (%rax)
jmp 0x2206
jmp 0x2012
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rax
movl %eax, %esi
callq 0x10a0
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0x58(%rsp), %r8
xorl %r9d, %r9d
callq 0x1100
movl $0x0, 0x4(%rsp)
movslq 0x4(%rsp), %rax
cmpq 0xb8(%rsp), %rax
jae 0x22a3
movq 0x58(%rsp), %rax
movl $0x7, %ecx
subl 0x4(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
shrq %cl, %rax
movzbl %al, %eax
movq 0xc0(%rsp), %rcx
movslq 0x4(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xd0(%rsp), %rax
movslq 0x4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x2249
movq 0xc8(%rsp), %rax
movq (%rax), %rcx
addq 0xb8(%rsp), %rcx
movq 0xc8(%rsp), %rax
movq %rcx, (%rax)
movq 0xd0(%rsp), %rax
addq 0xb8(%rsp), %rax
movq %rax, 0xd0(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x3, 0x4(%rsp)
jge 0x2371
movslq 0x4(%rsp), %rax
movq 0x40(%rsp,%rax,8), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0x58(%rsp), %r8
movl $0x1, %r9d
callq 0x1100
cmpl $0x0, 0x4(%rsp)
jne 0x233a
movq 0x58(%rsp), %rax
shrq $0x20, %rax
xorl 0x10(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x58(%rsp), %rax
xorl 0x14(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpl $0x2, 0x4(%rsp)
jne 0x235f
movq 0x58(%rsp), %rax
shrq $0x20, %rax
xorl 0x18(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x58(%rsp), %rax
xorl 0x1c(%rsp), %eax
movl %eax, 0x1c(%rsp)
jmp 0x2361
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x22e1
movl $0x0, 0x4(%rsp)
cmpl $0x4, 0x4(%rsp)
jge 0x23b7
movq 0xd0(%rsp), %rdi
movslq 0x4(%rsp), %rax
movl 0x10(%rsp,%rax,4), %esi
callq 0x23e0
movq 0xd0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xd0(%rsp)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x2379
movq 0xc8(%rsp), %rax
movq (%rax), %rcx
addq $0x10, %rcx
movq 0xc8(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
addq $0xd8, %rsp
retq
nopl (%rax,%rax)