// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnib/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;

    //initialization stage
    aegis128L_initialization(k, npub, state); 

    //process the associated data 
    for (i = 0; (i+32) <= adlen; i = i+32)   
    {    
        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);  
     }  

    // Deal with the partial block    
    // In this program, we assume that the message length is multiple of bytes.
    if (  (adlen & 0x1f) != 0 )  {
         memset(plaintextblock, 0, 32);
         memcpy(plaintextblock, ad+i, adlen & 0x1f);
         aegis128L_enc_aut_step(plaintextblock, ciphertextblock, state);
    }  


    // encrypt the message 
    for (i = 0; (i+32) <= mlen; i = i+32) 
    {   
        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);  
    }  

    // Deal with the partial block    
    // In this program, we assume that the message length is multiple of bytes.
    if ( (mlen & 0x1f) != 0 )  {    
         memset(plaintextblock, 0, 32);   
         memcpy(plaintextblock, m+i, mlen & 0x1f);   
         aegis128L_enc_aut_step(plaintextblock, ciphertextblock, state);  
         memcpy(c+i, ciphertextblock, mlen & 0x1f);   
    }

    // 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 $0xe8, %rsp
movq %r9, %r12
movq %r8, %rbp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, 0x88(%rsp)
movq %rdi, %r14
movq 0x128(%rsp), %rax
movq 0x130(%rsp), %rcx
vmovdqa (%rcx), %xmm0
vmovdqa (%rax), %xmm1
vmovdqa 0xb56(%rip), %xmm5      # 0x2010
vpxor %xmm0, %xmm1, %xmm11
vpxor %xmm5, %xmm0, %xmm4
vmovdqa 0xb36(%rip), %xmm6      # 0x2000
vpxor %xmm6, %xmm0, %xmm7
movl $0xa, %eax
vmovdqa %xmm4, %xmm8
vmovdqa %xmm6, %xmm9
vmovdqa %xmm11, %xmm10
vaesenc %xmm10, %xmm4, %xmm3
vaesenc %xmm4, %xmm7, %xmm4
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm11, %xmm8
vaesenc %xmm11, %xmm6, %xmm2
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm9, %xmm5
vaesenc %xmm9, %xmm10, %xmm9
vpxor %xmm1, %xmm3, %xmm10
vpxor %xmm0, %xmm2, %xmm11
decl %eax
jne 0x14e0
cmpq $0x20, %r12
jae 0x151e
xorl %ecx, %ecx
jmp 0x156c
xorl %eax, %eax
vmovdqa %xmm4, %xmm0
vaesenc %xmm11, %xmm6, %xmm1
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm9, %xmm5
vaesenc %xmm9, %xmm10, %xmm9
vaesenc %xmm10, %xmm0, %xmm2
vaesenc %xmm0, %xmm7, %xmm4
vaesenc %xmm7, %xmm8, %xmm7
vpxor (%rbp,%rax), %xmm2, %xmm10
vaesenc %xmm8, %xmm11, %xmm8
vpxor 0x10(%rbp,%rax), %xmm1, %xmm11
leaq 0x20(%rax), %rcx
addq $0x40, %rax
vmovdqa %xmm4, %xmm0
cmpq %r12, %rax
movq %rcx, %rax
jbe 0x1524
movq %r12, %rbx
andq $0x1f, %rbx
je 0x1682
addq %rcx, %rbp
movl $0x20, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
xorl %esi, %esi
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm6, 0x40(%rsp)
vmovdqa %xmm7, (%rsp)
vmovdqa %xmm8, 0x30(%rsp)
vmovdqa %xmm9, 0x20(%rsp)
vmovdqa %xmm10, 0x50(%rsp)
vmovdqa %xmm11, 0x60(%rsp)
vmovdqa %xmm4, 0x70(%rsp)
callq 0x1030
leaq 0x90(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1070
vmovdqa 0x20(%rsp), %xmm9
vmovdqa 0x30(%rsp), %xmm8
vmovdqa (%rsp), %xmm7
vmovdqa 0x40(%rsp), %xmm6
vmovdqa 0x10(%rsp), %xmm5
leaq 0x90(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa 0x10(%rax), %xmm1
vmovdqa %xmm7, %xmm2
vpternlogq $0x78, %xmm6, %xmm5, %xmm2
vpternlogq $0x96, %xmm9, %xmm0, %xmm2
vmovdqa %xmm8, %xmm3
vmovdqa 0x70(%rsp), %xmm4
vpternlogq $0x78, %xmm4, %xmm7, %xmm3
vpternlogq $0x96, %xmm5, %xmm1, %xmm3
vmovdqa %xmm2, 0xc0(%rsp)
vmovdqa %xmm3, 0xd0(%rsp)
vmovdqa 0x50(%rsp), %xmm10
vaesenc %xmm10, %xmm4, %xmm2
vaesenc %xmm4, %xmm7, %xmm3
vaesenc %xmm7, %xmm8, %xmm7
vmovdqa 0x60(%rsp), %xmm4
vaesenc %xmm8, %xmm4, %xmm8
vaesenc %xmm4, %xmm6, %xmm4
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm9, %xmm5
vaesenc %xmm9, %xmm10, %xmm9
vpxor %xmm0, %xmm2, %xmm10
vpxor %xmm1, %xmm4, %xmm11
vmovdqa %xmm3, %xmm4
cmpq $0x20, %r15
jae 0x168f
xorl %ebp, %ebp
jmp 0x171c
xorl %eax, %eax
vmovdqa %xmm4, %xmm0
vmovdqa (%r13,%rax), %xmm1
vmovdqa 0x10(%r13,%rax), %xmm2
vmovdqa %xmm7, %xmm3
vpternlogq $0x78, %xmm6, %xmm5, %xmm3
vpternlogq $0x96, %xmm9, %xmm1, %xmm3
vmovdqa %xmm8, %xmm4
vpternlogq $0x78, %xmm0, %xmm7, %xmm4
vpternlogq $0x96, %xmm5, %xmm2, %xmm4
vmovdqa %xmm3, (%r14,%rax)
vmovdqa %xmm4, 0x10(%r14,%rax)
vaesenc %xmm0, %xmm7, %xmm4
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm11, %xmm8
vaesenc %xmm11, %xmm6, %xmm3
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm9, %xmm5
vaesenc %xmm9, %xmm10, %xmm9
vaesenc %xmm10, %xmm0, %xmm0
vpxor %xmm1, %xmm0, %xmm10
vpxor %xmm2, %xmm3, %xmm11
leaq 0x20(%rax), %rbp
addq $0x40, %rax
vmovdqa %xmm4, %xmm0
cmpq %r15, %rax
movq %rbp, %rax
jbe 0x1695
movq %r15, %rbx
andq $0x1f, %rbx
je 0x18d0
addq %rbp, %r13
movl $0x20, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
xorl %esi, %esi
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm6, 0x40(%rsp)
vmovdqa %xmm7, (%rsp)
vmovdqa %xmm8, 0x30(%rsp)
vmovdqa %xmm9, 0x20(%rsp)
vmovdqa %xmm10, 0x50(%rsp)
vmovdqa %xmm11, 0x60(%rsp)
vmovdqa %xmm4, 0x70(%rsp)
callq 0x1030
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1070
leaq 0x90(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa 0x10(%rax), %xmm1
vmovdqa (%rsp), %xmm2
vmovdqa 0x40(%rsp), %xmm3
vpternlogq $0x78, 0x10(%rsp), %xmm3, %xmm2
vpternlogq $0x96, 0x20(%rsp), %xmm0, %xmm2
vmovdqa 0x30(%rsp), %xmm3
vmovdqa 0x70(%rsp), %xmm4
vpternlogq $0x78, (%rsp), %xmm4, %xmm3
vpternlogq $0x96, 0x10(%rsp), %xmm1, %xmm3
leaq 0xc0(%rsp), %rsi
vmovdqa %xmm2, (%rsi)
vmovdqa %xmm3, 0x10(%rsi)
vmovdqa (%rsp), %xmm2
vaesenc %xmm4, %xmm2, %xmm2
vmovdqa %xmm2, 0xb0(%rsp)
vmovdqa %xmm4, %xmm5
vmovdqa (%rsp), %xmm2
vmovdqa 0x30(%rsp), %xmm3
vaesenc %xmm2, %xmm3, %xmm2
vmovdqa %xmm2, (%rsp)
vmovdqa 0x30(%rsp), %xmm2
vmovdqa 0x60(%rsp), %xmm3
vaesenc %xmm2, %xmm3, %xmm2
vmovdqa %xmm2, 0x30(%rsp)
vmovdqa 0x40(%rsp), %xmm2
vaesenc %xmm3, %xmm2, %xmm2
vmovdqa 0x10(%rsp), %xmm3
vmovdqa 0x40(%rsp), %xmm4
vaesenc %xmm4, %xmm3, %xmm4
vmovdqa %xmm4, 0x40(%rsp)
vmovdqa 0x10(%rsp), %xmm3
vmovdqa 0x20(%rsp), %xmm4
vaesenc %xmm3, %xmm4, %xmm3
vmovdqa %xmm3, 0x10(%rsp)
vmovdqa 0x20(%rsp), %xmm3
vmovdqa 0x50(%rsp), %xmm4
vaesenc %xmm3, %xmm4, %xmm3
vmovdqa %xmm3, 0x20(%rsp)
vaesenc %xmm4, %xmm5, %xmm3
vpxor %xmm0, %xmm3, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vpxor %xmm1, %xmm2, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
addq %r14, %rbp
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x1070
vmovdqa 0x60(%rsp), %xmm11
vmovdqa 0x50(%rsp), %xmm10
vmovdqa 0x20(%rsp), %xmm9
vmovdqa 0x30(%rsp), %xmm8
vmovdqa (%rsp), %xmm7
vmovdqa 0x40(%rsp), %xmm6
vmovdqa 0x10(%rsp), %xmm5
vmovdqa 0xb0(%rsp), %xmm4
vmovq %r15, %xmm0
vmovq %r12, %xmm1
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
vpsllq $0x3, %xmm0, %xmm0
vpxor %xmm0, %xmm5, %xmm0
movl $0x7, %eax
vaesenc %xmm10, %xmm4, %xmm1
vaesenc %xmm4, %xmm7, %xmm4
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm11, %xmm8
vaesenc %xmm11, %xmm6, %xmm3
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm9, %xmm5
vaesenc %xmm9, %xmm10, %xmm9
vpxor %xmm0, %xmm1, %xmm10
vpxor %xmm0, %xmm3, %xmm11
decl %eax
jne 0x18ec
vpternlogq $0x96, %xmm7, %xmm8, %xmm11
vpternlogq $0x96, %xmm6, %xmm11, %xmm5
vpternlogq $0x96, %xmm9, %xmm5, %xmm10
leaq 0x10(%r15), %rax
movq 0x88(%rsp), %rcx
movq %rax, (%rcx)
vmovdqu %xmm10, (%r14,%r15)
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq