// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnic/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
	)
        //void aegis128L_enc_aut(unsigned char *key, unsigned char *iv, unsigned char *ad, unsigned char *plaintext,
        //unsigned char *ciphertext, unsigned char *mac, unsigned char maclen, unsigned long msglen, unsigned long adlen)
{
    unsigned long long i;  
    unsigned char plaintextblock[32], ciphertextblock[32], mac[16];
    __m128i state[8], tmp, ct0, ct1, msg0, msg1,msg2,msg3;

    //initialization stage
    aegis128L_initialization(k, npub, state); 

    //process the associated data 
    for (i = 0; (i+128) <= adlen; i = i+128)   
    {   
        //The first 32-byte    
        msg0 = _mm_load_si128((__m128i*)(ad+i));
        msg1 = _mm_load_si128((__m128i*)(ad+i+16));

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg0);
        state[4] = _mm_xor_si128(state[4],msg1);  

        
        //The second 32-byte 
        msg2 = _mm_load_si128((__m128i*)(ad+i+32));
        msg3 = _mm_load_si128((__m128i*)(ad+i+48));

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg2);
        state[4] = _mm_xor_si128(state[4],msg3);    


        //The third 32-byte    
        msg0 = _mm_load_si128((__m128i*)(ad+i+64));
        msg1 = _mm_load_si128((__m128i*)(ad+i+80));

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg0);
        state[4] = _mm_xor_si128(state[4],msg1);  

        
        //The fourth 32-byte 
        msg2 = _mm_load_si128((__m128i*)(ad+i+96));
        msg3 = _mm_load_si128((__m128i*)(ad+i+112));

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg2);
        state[4] = _mm_xor_si128(state[4],msg3);    
     }  

    // Deal with the partial block    
    // In this program, we assume that the message length is multiple of bytes.
    if (  (adlen & 0x7f) != 0 ) aegis128L_ad_partial(state, ad + i, adlen & 0x7f);     

    // encrypt the message  
    for (i = 0; (i+128) <= mlen; i = i+128) 
    {   
        //encrypt 32 bytes: the first  
        msg0 = _mm_load_si128((__m128i*)(m+i)); 
        msg1 = _mm_load_si128((__m128i*)(m+i+16)); 

        ct0 = _mm_xor_si128(msg0, state[6]);
        ct0 = _mm_xor_si128(ct0,  state[1]);
        ct1 = _mm_xor_si128(msg1, state[2]);
        ct1 = _mm_xor_si128(ct1,  state[5]);
        ct0 = _mm_xor_si128(ct0,  _mm_and_si128(state[2], state[3]));
        ct1 = _mm_xor_si128(ct1,  _mm_and_si128(state[6], state[7]));
    	_mm_store_si128((__m128i*)(c+i), ct0);
    	_mm_store_si128((__m128i*)(c+i+16), ct1);

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg0);
        state[4] = _mm_xor_si128(state[4],msg1);  


        //encrypt 32 bytes: the second  
        msg2 = _mm_load_si128((__m128i*)(m+i+32)); 
        msg3 = _mm_load_si128((__m128i*)(m+i+48)); 

        ct0 = _mm_xor_si128(msg2, state[6]);
        ct0 = _mm_xor_si128(ct0,  state[1]);
        ct1 = _mm_xor_si128(msg3, state[2]);
        ct1 = _mm_xor_si128(ct1,  state[5]);
        ct0 = _mm_xor_si128(ct0,  _mm_and_si128(state[2], state[3]));
        ct1 = _mm_xor_si128(ct1,  _mm_and_si128(state[6], state[7]));
    	_mm_store_si128((__m128i*)(c+i+32), ct0);
    	_mm_store_si128((__m128i*)(c+i+48), ct1);

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg2);
        state[4] = _mm_xor_si128(state[4],msg3);  

        //encrypt 32 bytes: the third  
        msg0 = _mm_load_si128((__m128i*)(m+i+64)); 
        msg1 = _mm_load_si128((__m128i*)(m+i+80)); 

        ct0 = _mm_xor_si128(msg0, state[6]);
        ct0 = _mm_xor_si128(ct0,  state[1]);
        ct1 = _mm_xor_si128(msg1, state[2]);
        ct1 = _mm_xor_si128(ct1,  state[5]);
        ct0 = _mm_xor_si128(ct0,  _mm_and_si128(state[2], state[3]));
        ct1 = _mm_xor_si128(ct1,  _mm_and_si128(state[6], state[7]));
    	_mm_store_si128((__m128i*)(c+i+64), ct0);
    	_mm_store_si128((__m128i*)(c+i+80), ct1);

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg0);
        state[4] = _mm_xor_si128(state[4],msg1);  


        //encrypt 32 bytes: the fourth 
        msg2 = _mm_load_si128((__m128i*)(m+i+96)); 
        msg3 = _mm_load_si128((__m128i*)(m+i+112)); 

        ct0 = _mm_xor_si128(msg2, state[6]);
        ct0 = _mm_xor_si128(ct0,  state[1]);
        ct1 = _mm_xor_si128(msg3, state[2]);
        ct1 = _mm_xor_si128(ct1,  state[5]);
        ct0 = _mm_xor_si128(ct0,  _mm_and_si128(state[2], state[3]));
        ct1 = _mm_xor_si128(ct1,  _mm_and_si128(state[6], state[7]));
    	_mm_store_si128((__m128i*)(c+i+96), ct0);
    	_mm_store_si128((__m128i*)(c+i+112), ct1);

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg2);
        state[4] = _mm_xor_si128(state[4],msg3);    
    }  

    // Deal with the partial block    
    // In this program, we assume that the message length is multiple of bytes.   
    if (  (mlen & 0x7f) != 0 ) aegis128L_enc_partial(state, m+i, mlen & 0x7f, c+i);      

    // finalization stage, we assume that the tag length is multiple of bytes
    aegis128L_tag_generation(mlen,adlen, 16, mac, state);
    *clen = mlen + 16;
    memcpy(c+mlen, mac, 16); 
          
    return 0; 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %r12
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
movq 0xc8(%rsp), %rax
movq 0xd0(%rsp), %rcx
vmovdqa (%rcx), %xmm8
vmovdqa (%rax), %xmm9
vmovdqa 0x1917(%rip), %xmm2     # 0x3000
vpxor %xmm8, %xmm9, %xmm7
vpxor %xmm2, %xmm8, %xmm6
vmovdqa 0x1916(%rip), %xmm3     # 0x3010
vpxor %xmm3, %xmm8, %xmm0
movl $0xa, %eax
vmovdqa %xmm6, %xmm1
vmovdqa %xmm3, %xmm5
vmovdqa %xmm7, %xmm4
vaesenc %xmm4, %xmm6, %xmm10
vaesenc %xmm6, %xmm0, %xmm6
vaesenc %xmm0, %xmm1, %xmm0
vaesenc %xmm1, %xmm7, %xmm1
vaesenc %xmm7, %xmm3, %xmm7
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm5, %xmm2
vaesenc %xmm5, %xmm4, %xmm5
vpxor %xmm9, %xmm10, %xmm4
vpxor %xmm7, %xmm8, %xmm7
decl %eax
jne 0x170f
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm0, 0x60(%rsp)
vmovdqa %xmm1, 0x50(%rsp)
vmovdqa %xmm7, 0x40(%rsp)
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm2, 0x20(%rsp)
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm4, (%rsp)
xorl %eax, %eax
cmpq $0x80, %r12
jb 0x18a1
movq %rax, %rcx
vaesenc %xmm6, %xmm0, %xmm8
vaesenc %xmm0, %xmm1, %xmm0
vaesenc %xmm1, %xmm7, %xmm1
vaesenc %xmm7, %xmm3, %xmm7
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm5, %xmm2
vaesenc %xmm4, %xmm6, %xmm6
vpxor (%r8,%rax), %xmm6, %xmm6
vaesenc %xmm5, %xmm4, %xmm4
vpxor 0x10(%r8,%rax), %xmm7, %xmm5
vaesenc %xmm8, %xmm0, %xmm7
vaesenc %xmm0, %xmm1, %xmm0
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm3, %xmm5
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm4, %xmm2
vaesenc %xmm6, %xmm8, %xmm8
vpxor 0x20(%r8,%rax), %xmm8, %xmm8
vaesenc %xmm4, %xmm6, %xmm4
vpxor 0x30(%r8,%rax), %xmm5, %xmm5
vaesenc %xmm7, %xmm0, %xmm9
vaesenc %xmm0, %xmm1, %xmm0
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm3, %xmm5
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm4, %xmm2
vaesenc %xmm8, %xmm7, %xmm6
vpxor 0x40(%r8,%rax), %xmm6, %xmm7
vaesenc %xmm4, %xmm8, %xmm8
vpxor 0x50(%r8,%rax), %xmm5, %xmm4
vaesenc %xmm9, %xmm0, %xmm6
vaesenc %xmm0, %xmm1, %xmm0
vaesenc %xmm1, %xmm4, %xmm1
vaesenc %xmm4, %xmm3, %xmm10
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm8, %xmm2
vaesenc %xmm7, %xmm9, %xmm4
vpxor 0x60(%r8,%rax), %xmm4, %xmm4
vaesenc %xmm8, %xmm7, %xmm5
vpxor 0x70(%r8,%rax), %xmm10, %xmm7
addq $0x80, %rax
addq $0x100, %rcx            # imm = 0x100
cmpq %r12, %rcx
jbe 0x1782
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm0, 0x60(%rsp)
vmovdqa %xmm1, 0x50(%rsp)
vmovdqa %xmm7, 0x40(%rsp)
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm2, 0x20(%rsp)
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm4, (%rsp)
movq %r12, %rdx
andq $0x7f, %rdx
je 0x18e3
addq %rax, %r8
movq %rsp, %rbp
movq %rbp, %rdi
movq %r8, %rsi
callq 0x1090
vmovdqa 0x60(%rbp), %xmm0
vmovdqa (%rbp), %xmm4
vmovdqa 0x10(%rbp), %xmm5
vmovdqa 0x20(%rbp), %xmm2
vmovdqa 0x30(%rbp), %xmm3
vmovdqa 0x50(%rbp), %xmm1
vmovdqa 0x70(%rbp), %xmm6
vmovdqa 0x40(%rbp), %xmm7
cmpq $0x80, %rbx
jae 0x18f3
xorl %ecx, %ecx
jmp 0x1ac2
xorl %eax, %eax
vmovdqa (%r13,%rax), %xmm9
vmovdqa 0x10(%r13,%rax), %xmm10
vaesenc %xmm4, %xmm6, %xmm8
vpxor %xmm9, %xmm8, %xmm8
vmovdqa %xmm9, %xmm11
vpternlogq $0x78, %xmm2, %xmm3, %xmm11
vpternlogq $0x96, %xmm0, %xmm5, %xmm11
vaesenc %xmm7, %xmm3, %xmm9
vpxor %xmm10, %xmm9, %xmm9
vpternlogq $0x78, %xmm0, %xmm6, %xmm10
vmovdqa %xmm11, (%r14,%rax)
vpternlogq $0x96, %xmm2, %xmm1, %xmm10
vmovdqa %xmm10, 0x10(%r14,%rax)
vaesenc %xmm6, %xmm0, %xmm6
vaesenc %xmm0, %xmm1, %xmm10
vaesenc %xmm1, %xmm7, %xmm0
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm5, %xmm2
vmovdqa 0x20(%r13,%rax), %xmm7
vmovdqa 0x30(%r13,%rax), %xmm11
vaesenc %xmm5, %xmm4, %xmm4
vaesenc %xmm10, %xmm0, %xmm1
vaesenc %xmm0, %xmm9, %xmm5
vmovdqa %xmm0, %xmm12
vpternlogq $0x78, %xmm6, %xmm10, %xmm12
vaesenc %xmm6, %xmm10, %xmm0
vpternlogq $0x78, %xmm3, %xmm2, %xmm10
vpternlogq $0x96, %xmm4, %xmm7, %xmm10
vpternlogq $0x96, %xmm2, %xmm11, %xmm12
vmovdqa %xmm10, 0x20(%r14,%rax)
vmovdqa %xmm12, 0x30(%r14,%rax)
vaesenc %xmm9, %xmm3, %xmm9
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm4, %xmm2
vaesenc %xmm4, %xmm8, %xmm10
vaesenc %xmm8, %xmm6, %xmm4
vpxor %xmm7, %xmm4, %xmm6
vpxor %xmm11, %xmm9, %xmm8
vmovdqa 0x40(%r13,%rax), %xmm9
vmovdqa 0x50(%r13,%rax), %xmm11
vaesenc %xmm1, %xmm5, %xmm7
vaesenc %xmm5, %xmm8, %xmm12
vmovdqa %xmm5, %xmm13
vpternlogq $0x78, %xmm0, %xmm1, %xmm13
vaesenc %xmm0, %xmm1, %xmm4
vmovdqa %xmm1, %xmm14
vpternlogq $0x78, %xmm3, %xmm2, %xmm14
vpternlogq $0x96, %xmm10, %xmm9, %xmm14
vpternlogq $0x96, %xmm2, %xmm11, %xmm13
vaesenc %xmm8, %xmm3, %xmm1
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm10, %xmm2
vaesenc %xmm10, %xmm6, %xmm5
vaesenc %xmm6, %xmm0, %xmm10
vpxor %xmm1, %xmm11, %xmm8
vaesenc %xmm7, %xmm12, %xmm0
vaesenc %xmm12, %xmm8, %xmm1
vpternlogq $0x78, %xmm4, %xmm7, %xmm12
vaesenc %xmm4, %xmm7, %xmm6
vmovdqa %xmm14, 0x40(%r14,%rax)
vmovdqa %xmm13, 0x50(%r14,%rax)
vmovdqa 0x60(%r13,%rax), %xmm11
vmovdqa 0x70(%r13,%rax), %xmm13
vpxor %xmm9, %xmm10, %xmm9
vpternlogq $0x78, %xmm3, %xmm2, %xmm7
vpternlogq $0x96, %xmm5, %xmm11, %xmm7
vmovdqa %xmm7, 0x60(%r14,%rax)
vpternlogq $0x96, %xmm2, %xmm13, %xmm12
vmovdqa %xmm12, 0x70(%r14,%rax)
vaesenc %xmm8, %xmm3, %xmm7
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm5, %xmm2
vaesenc %xmm5, %xmm9, %xmm5
vaesenc %xmm9, %xmm4, %xmm4
vpxor %xmm4, %xmm11, %xmm4
vpxor %xmm7, %xmm13, %xmm7
leaq 0x80(%rax), %rcx
addq $0x100, %rax            # imm = 0x100
cmpq %rbx, %rax
movq %rcx, %rax
jbe 0x18f5
vmovdqa %xmm0, 0x60(%rsp)
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm2, 0x20(%rsp)
vmovdqa %xmm1, 0x50(%rsp)
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm7, 0x40(%rsp)
vmovdqa %xmm4, (%rsp)
movq %rbx, %rdx
andq $0x7f, %rdx
je 0x1b36
addq %rcx, %r13
addq %r14, %rcx
movq %rsp, %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x1050
vmovdqa (%rbp), %xmm4
vmovdqa 0x10(%rbp), %xmm5
vmovdqa 0x20(%rbp), %xmm2
vmovdqa 0x30(%rbp), %xmm3
vmovdqa 0x70(%rbp), %xmm6
vmovdqa 0x60(%rbp), %xmm0
vmovdqa 0x50(%rbp), %xmm1
vmovdqa 0x40(%rbp), %xmm7
vmovq %rbx, %xmm8
vmovq %r12, %xmm9
vpunpcklqdq %xmm8, %xmm9, %xmm8 # xmm8 = xmm9[0],xmm8[0]
vpsllq $0x3, %xmm8, %xmm8
vpxor %xmm2, %xmm8, %xmm8
movl $0x7, %eax
vaesenc %xmm4, %xmm6, %xmm9
vaesenc %xmm6, %xmm0, %xmm6
vaesenc %xmm0, %xmm1, %xmm0
vaesenc %xmm1, %xmm7, %xmm1
vaesenc %xmm7, %xmm3, %xmm7
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm5, %xmm2
vaesenc %xmm5, %xmm4, %xmm5
vpxor %xmm8, %xmm9, %xmm4
vpxor %xmm7, %xmm8, %xmm7
decl %eax
jne 0x1b54
vpternlogq $0x96, %xmm0, %xmm1, %xmm7
vpternlogq $0x96, %xmm3, %xmm7, %xmm2
vpternlogq $0x96, %xmm5, %xmm2, %xmm4
leaq 0x10(%rbx), %rax
movq %rax, (%r15)
vmovdqu %xmm4, (%r14,%rbx)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq