// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnic/encrypt.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
	unsigned char *m,unsigned long long *mlen,
	unsigned char *nsec,
	const unsigned char *c,unsigned long long clen,
	const unsigned char *ad,unsigned long long adlen,
	const unsigned char *npub,
	const unsigned char *k
	)
{
    unsigned long long i;
    unsigned char plaintextblock[32], ciphertextblock[32];
    unsigned char tag[16];
    unsigned char check = 0;
    __m128i state[8], tmp, ct0, ct1, msg0, msg1, msg2, msg3;

    if (clen < 16) return -1;   

    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);     


    //decrypt the ciphertext  
    *mlen = clen - 16;  
    for (i = 0; (i+128) <= *mlen; i += 128) {           
        //decryption: the first 32 bytes            
        msg0 = _mm_load_si128((__m128i*)(c+i+0));   
        msg1 = _mm_load_si128((__m128i*)(c+i+16));  
        
        msg0 = _mm_xor_si128(msg0, state[6]);
        msg0 = _mm_xor_si128(msg0, state[1]);
        msg1 = _mm_xor_si128(msg1, state[2]);
        msg1 = _mm_xor_si128(msg1, state[5]);
        msg0 = _mm_xor_si128(msg0, _mm_and_si128(state[2], state[3])); 
        msg1 = _mm_xor_si128(msg1, _mm_and_si128(state[6], state[7])); 
        _mm_store_si128((__m128i*)(m+i+0),  msg0);   
        _mm_store_si128((__m128i*)(m+i+16), msg1);   

        //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);  


        //decryption: the second 32 bytes   
        msg2 = _mm_load_si128((__m128i*)(c+i+32));
        msg3 = _mm_load_si128((__m128i*)(c+i+48));
        
        msg2 = _mm_xor_si128(msg2, state[6]);
        msg2 = _mm_xor_si128(msg2, state[1]);
        msg3 = _mm_xor_si128(msg3, state[2]);
        msg3 = _mm_xor_si128(msg3, state[5]);
        msg2 = _mm_xor_si128(msg2, _mm_and_si128(state[2], state[3]));
        msg3 = _mm_xor_si128(msg3, _mm_and_si128(state[6], state[7]));
        _mm_store_si128((__m128i*)(m+i+32), msg2);
        _mm_store_si128((__m128i*)(m+i+48), msg3);

        //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);  

        //decryption: the third 32 bytes            
        msg0 = _mm_load_si128((__m128i*)(c+i+64));   
        msg1 = _mm_load_si128((__m128i*)(c+i+80));  
        
        msg0 = _mm_xor_si128(msg0, state[6]);
        msg0 = _mm_xor_si128(msg0, state[1]);
        msg1 = _mm_xor_si128(msg1, state[2]);
        msg1 = _mm_xor_si128(msg1, state[5]);
        msg0 = _mm_xor_si128(msg0, _mm_and_si128(state[2], state[3])); 
        msg1 = _mm_xor_si128(msg1, _mm_and_si128(state[6], state[7])); 
        _mm_store_si128((__m128i*)(m+i+64),  msg0);   
        _mm_store_si128((__m128i*)(m+i+80), msg1);   

        //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);  


        //decryption: the fourth 32 bytes   
        msg2 = _mm_load_si128((__m128i*)(c+i+96));
        msg3 = _mm_load_si128((__m128i*)(c+i+112));
        
        msg2 = _mm_xor_si128(msg2, state[6]);
        msg2 = _mm_xor_si128(msg2, state[1]);
        msg3 = _mm_xor_si128(msg3, state[2]);
        msg3 = _mm_xor_si128(msg3, state[5]);
        msg2 = _mm_xor_si128(msg2, _mm_and_si128(state[2], state[3]));
        msg3 = _mm_xor_si128(msg3, _mm_and_si128(state[6], state[7]));
        _mm_store_si128((__m128i*)(m+i+96), msg2);
        _mm_store_si128((__m128i*)(m+i+112), msg3);

        //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);  

    }

    if ( (*mlen & 0x7f) != 0 ) aegis128L_dec_partial(state, m+i, *mlen & 0x7f, c+i); 

    //we assume that the tag length is multiple of bytes
    aegis128L_tag_generation(*mlen, adlen, 16, tag, state);

    //verification
    for (i = 0; i  < 16; i++) check |= (tag[i] ^ c[i+*mlen]);
    if (check == 0) return 0;
    else return -1;  
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r8
jb 0x20ea
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, %r12
movq %rcx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %rbp
vmovdqa (%rax), %xmm8
vmovdqa (%rcx), %xmm9
vmovdqa 0x13ed(%rip), %xmm0     # 0x3000
vpxor %xmm8, %xmm9, %xmm7
vpxor %xmm0, %xmm8, %xmm6
vmovdqa 0x13ec(%rip), %xmm2     # 0x3010
vpxor %xmm2, %xmm8, %xmm1
movl $0xa, %eax
vmovdqa %xmm6, %xmm5
vmovdqa %xmm2, %xmm4
vmovdqa %xmm7, %xmm3
vaesenc %xmm3, %xmm6, %xmm10
vaesenc %xmm6, %xmm1, %xmm6
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm2, %xmm7
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm4, %xmm0
vaesenc %xmm4, %xmm3, %xmm4
vpxor %xmm9, %xmm10, %xmm3
vpxor %xmm7, %xmm8, %xmm7
decl %eax
jne 0x1c39
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm1, 0x60(%rsp)
vmovdqa %xmm5, 0x50(%rsp)
vmovdqa %xmm7, 0x40(%rsp)
vmovdqa %xmm2, 0x30(%rsp)
vmovdqa %xmm0, 0x20(%rsp)
vmovdqa %xmm4, 0x10(%rsp)
vmovdqa %xmm3, (%rsp)
xorl %eax, %eax
cmpq $0x80, %rbp
jb 0x1dcb
movq %rax, %rcx
vaesenc %xmm6, %xmm1, %xmm8
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm2, %xmm7
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm4, %xmm0
vaesenc %xmm3, %xmm6, %xmm6
vpxor (%r9,%rax), %xmm6, %xmm6
vaesenc %xmm4, %xmm3, %xmm3
vpxor 0x10(%r9,%rax), %xmm7, %xmm4
vaesenc %xmm8, %xmm1, %xmm7
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm2, %xmm4
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm3, %xmm0
vaesenc %xmm6, %xmm8, %xmm8
vpxor 0x20(%r9,%rax), %xmm8, %xmm8
vaesenc %xmm3, %xmm6, %xmm3
vpxor 0x30(%r9,%rax), %xmm4, %xmm4
vaesenc %xmm7, %xmm1, %xmm9
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm2, %xmm4
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm3, %xmm0
vaesenc %xmm8, %xmm7, %xmm6
vpxor 0x40(%r9,%rax), %xmm6, %xmm7
vaesenc %xmm3, %xmm8, %xmm8
vpxor 0x50(%r9,%rax), %xmm4, %xmm3
vaesenc %xmm9, %xmm1, %xmm6
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm3, %xmm5
vaesenc %xmm3, %xmm2, %xmm10
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm8, %xmm0
vaesenc %xmm7, %xmm9, %xmm3
vpxor 0x60(%r9,%rax), %xmm3, %xmm3
vaesenc %xmm8, %xmm7, %xmm4
vpxor 0x70(%r9,%rax), %xmm10, %xmm7
addq $0x80, %rax
addq $0x100, %rcx            # imm = 0x100
cmpq %rbp, %rcx
jbe 0x1cac
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm1, 0x60(%rsp)
vmovdqa %xmm5, 0x50(%rsp)
vmovdqa %xmm7, 0x40(%rsp)
vmovdqa %xmm2, 0x30(%rsp)
vmovdqa %xmm0, 0x20(%rsp)
vmovdqa %xmm4, 0x10(%rsp)
vmovdqa %xmm3, (%rsp)
movq %rbp, %rdx
andq $0x7f, %rdx
je 0x1e15
addq %rax, %r9
movq %rsp, %r13
movq %r13, %rdi
movq %r9, %rsi
callq 0x1090
vmovdqa 0x60(%r13), %xmm1
vmovdqa (%r13), %xmm3
vmovdqa 0x10(%r13), %xmm4
vmovdqa 0x20(%r13), %xmm0
vmovdqa 0x30(%r13), %xmm2
vmovdqa 0x50(%r13), %xmm5
vmovdqa 0x70(%r13), %xmm6
vmovdqa 0x40(%r13), %xmm7
addq $-0x10, %r12
movq %r12, (%r14)
cmpq $0x80, %r12
jae 0x1e2c
xorl %ecx, %ecx
jmp 0x1fe1
xorl %eax, %eax
vmovdqa (%rbx,%rax), %xmm8
vpternlogq $0x78, %xmm0, %xmm2, %xmm8
vpternlogq $0x96, %xmm1, %xmm4, %xmm8
vmovdqa 0x10(%rbx,%rax), %xmm9
vpternlogq $0x78, %xmm1, %xmm6, %xmm9
vpternlogq $0x96, %xmm0, %xmm5, %xmm9
vmovdqa %xmm8, (%r15,%rax)
vmovdqa %xmm9, 0x10(%r15,%rax)
vaesenc %xmm6, %xmm1, %xmm10
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm2, %xmm7
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm4, %xmm0
vaesenc %xmm4, %xmm3, %xmm4
vaesenc %xmm3, %xmm6, %xmm3
vpxor %xmm3, %xmm8, %xmm3
vpxor %xmm7, %xmm9, %xmm6
vmovdqa 0x20(%rbx,%rax), %xmm7
vpternlogq $0x78, %xmm2, %xmm0, %xmm7
vpternlogq $0x96, %xmm1, %xmm4, %xmm7
vmovdqa 0x30(%rbx,%rax), %xmm8
vpternlogq $0x78, %xmm10, %xmm1, %xmm8
vpternlogq $0x96, %xmm5, %xmm0, %xmm8
vmovdqa %xmm7, 0x20(%r15,%rax)
vmovdqa %xmm8, 0x30(%r15,%rax)
vaesenc %xmm10, %xmm1, %xmm9
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm6, %xmm5
vaesenc %xmm6, %xmm2, %xmm6
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm4, %xmm0
vaesenc %xmm4, %xmm3, %xmm4
vaesenc %xmm3, %xmm10, %xmm3
vpxor %xmm7, %xmm3, %xmm7
vpxor %xmm6, %xmm8, %xmm6
vmovdqa 0x40(%rbx,%rax), %xmm8
vpternlogq $0x78, %xmm2, %xmm0, %xmm8
vpternlogq $0x96, %xmm1, %xmm4, %xmm8
vmovdqa 0x50(%rbx,%rax), %xmm10
vpternlogq $0x78, %xmm9, %xmm1, %xmm10
vpternlogq $0x96, %xmm5, %xmm0, %xmm10
vmovdqa %xmm8, 0x40(%r15,%rax)
vmovdqa %xmm10, 0x50(%r15,%rax)
vaesenc %xmm9, %xmm1, %xmm3
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm6, %xmm5
vaesenc %xmm6, %xmm2, %xmm6
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm4, %xmm0
vaesenc %xmm4, %xmm7, %xmm4
vaesenc %xmm7, %xmm9, %xmm7
vpxor %xmm7, %xmm8, %xmm7
vpxor %xmm6, %xmm10, %xmm8
vmovdqa 0x60(%rbx,%rax), %xmm9
vpternlogq $0x78, %xmm2, %xmm0, %xmm9
vpternlogq $0x96, %xmm1, %xmm4, %xmm9
vmovdqa 0x70(%rbx,%rax), %xmm10
vpternlogq $0x78, %xmm3, %xmm1, %xmm10
vpternlogq $0x96, %xmm5, %xmm0, %xmm10
vmovdqa %xmm9, 0x60(%r15,%rax)
vmovdqa %xmm10, 0x70(%r15,%rax)
vaesenc %xmm3, %xmm1, %xmm6
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm8, %xmm5
vaesenc %xmm8, %xmm2, %xmm8
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm4, %xmm0
vaesenc %xmm4, %xmm7, %xmm4
vaesenc %xmm7, %xmm3, %xmm3
vpxor %xmm3, %xmm9, %xmm3
vpxor %xmm10, %xmm8, %xmm7
movq (%r14), %r12
leaq 0x80(%rax), %rcx
addq $0x100, %rax            # imm = 0x100
cmpq %r12, %rax
movq %rcx, %rax
jbe 0x1e2e
vmovdqa %xmm1, 0x60(%rsp)
vmovdqa %xmm4, 0x10(%rsp)
vmovdqa %xmm0, 0x20(%rsp)
vmovdqa %xmm5, 0x50(%rsp)
vmovdqa %xmm2, 0x30(%rsp)
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm7, 0x40(%rsp)
vmovdqa %xmm3, (%rsp)
movq %r12, %rdx
andq $0x7f, %rdx
je 0x2060
addq %rcx, %r15
addq %rbx, %rcx
movq %rsp, %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x10b0
movq (%r14), %r12
vmovdqa (%r13), %xmm3
vmovdqa 0x10(%r13), %xmm4
vmovdqa 0x20(%r13), %xmm0
vmovdqa 0x30(%r13), %xmm2
vmovdqa 0x70(%r13), %xmm6
vmovdqa 0x60(%r13), %xmm1
vmovdqa 0x50(%r13), %xmm5
vmovdqa 0x40(%r13), %xmm7
vmovq %r12, %xmm8
vmovq %rbp, %xmm9
vpunpcklqdq %xmm8, %xmm9, %xmm8 # xmm8 = xmm9[0],xmm8[0]
vpsllq $0x3, %xmm8, %xmm8
vpxor %xmm0, %xmm8, %xmm8
movl $0x7, %eax
vaesenc %xmm3, %xmm6, %xmm9
vaesenc %xmm6, %xmm1, %xmm6
vaesenc %xmm1, %xmm5, %xmm1
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm2, %xmm7
vaesenc %xmm2, %xmm0, %xmm2
vaesenc %xmm0, %xmm4, %xmm0
vaesenc %xmm4, %xmm3, %xmm4
vpxor %xmm8, %xmm9, %xmm3
vpxor %xmm7, %xmm8, %xmm7
decl %eax
jne 0x207e
vpxor %xmm1, %xmm5, %xmm1
vpternlogq $0x96, %xmm7, %xmm1, %xmm2
vpternlogq $0x96, %xmm0, %xmm2, %xmm4
vpternlogq $0x96, (%rbx,%r12), %xmm4, %xmm3
xorl %eax, %eax
vptest %xmm3, %xmm3
setne %al
negl %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop