// File: /workspace/github_c_makefile_star2_O0/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:
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
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x30(%rsp)
movq $0x8000000, 0x28(%rsp)  # imm = 0x8000000
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
callq 0x1ee0
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x1f20
cmpq $0x8, 0xb8(%rsp)
jb 0x21ae
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 0x20c4
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 0x2072
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 0x213e
movq 0x28(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x21a9
movl $0x0, 0x4(%rsp)
cmpl $0x3, 0x4(%rsp)
jge 0x218b
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 0x214e
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x30(%rsp)
leaq 0x40(%rsp), %rdi
movl $0x3, %esi
callq 0x1f20
jmp 0x2028
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 0x2246
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 0x21ec
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 0x2314
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 0x22dd
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 0x2302
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 0x2304
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x2284
movl $0x0, 0x4(%rsp)
cmpl $0x4, 0x4(%rsp)
jge 0x235a
movq 0xd0(%rsp), %rdi
movslq 0x4(%rsp), %rax
movl 0x10(%rsp,%rax,4), %esi
callq 0x2380
movq 0xd0(%rsp), %rax
addq $0x4, %rax
movq %rax, 0xd0(%rsp)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x231c
movq 0xc8(%rsp), %rax
movq (%rax), %rcx
addq $0x10, %rcx
movq 0xc8(%rsp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
addq $0xd8, %rsp
retq
nop