// File: /workspace/github_c_makefile_star2_O2/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:
cmpq $0x10, %r8
jae 0x1b21
pushq $-0x1
popq %rax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %rbp
movq %r8, %r13
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq %rsp, %r14
movq %r14, %rdx
callq 0x1040
vmovdqa (%r14), %xmm4
vmovdqa 0x10(%r14), %xmm1
vmovdqa 0x20(%r14), %xmm2
vmovdqa 0x30(%r14), %xmm3
vmovdqa 0x70(%r14), %xmm0
vmovdqa 0x60(%r14), %xmm6
vmovdqa 0x50(%r14), %xmm5
vmovdqa 0x40(%r14), %xmm7
movq 0xd0(%rsp), %r14
movl $0x80, %eax
movl $0x80, %ecx
cmpq %r14, %rcx
ja 0x1c79
vaesenc %xmm0, %xmm6, %xmm8
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm3, %xmm7
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm4, %xmm1
vaesenc %xmm4, %xmm0, %xmm0
vpxor (%rbp), %xmm0, %xmm0
vpxor 0x10(%rbp), %xmm7, %xmm4
vaesenc %xmm8, %xmm6, %xmm7
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm3, %xmm4
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm0, %xmm1
vaesenc %xmm0, %xmm8, %xmm0
vpxor 0x20(%rbp), %xmm0, %xmm0
vpxor 0x30(%rbp), %xmm4, %xmm4
vaesenc %xmm7, %xmm6, %xmm8
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm3, %xmm4
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm0, %xmm1
vaesenc %xmm0, %xmm7, %xmm0
vpxor 0x40(%rbp), %xmm0, %xmm7
vpxor 0x50(%rbp), %xmm4, %xmm4
vaesenc %xmm8, %xmm6, %xmm0
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm3, %xmm9
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm7, %xmm1
vaesenc %xmm7, %xmm8, %xmm4
vpxor 0x60(%rbp), %xmm4, %xmm4
vpxor 0x70(%rbp), %xmm9, %xmm7
addq %rax, %rbp
addq %rax, %rcx
jmp 0x1b9d
vmovdqa %xmm0, 0x70(%rsp)
vmovdqa %xmm6, 0x60(%rsp)
vmovdqa %xmm5, 0x50(%rsp)
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm2, 0x20(%rsp)
vmovdqa %xmm1, 0x10(%rsp)
vmovdqa %xmm4, (%rsp)
vmovdqa %xmm7, 0x40(%rsp)
movq %r14, %rdx
andq $0x7f, %rdx
je 0x1cf6
movq %rsp, %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x10b0
vmovdqa (%r14), %xmm4
vmovdqa 0x10(%r14), %xmm1
vmovdqa 0x20(%r14), %xmm2
vmovdqa 0x30(%r14), %xmm3
vmovdqa 0x60(%r14), %xmm6
vmovdqa 0x50(%r14), %xmm5
vmovdqa 0x70(%r14), %xmm0
vmovdqa 0x40(%r14), %xmm7
movq 0xd0(%rsp), %r14
addq $-0x10, %r13
movq %r13, (%r15)
vmovdqa 0x70(%rsp), %xmm8
vmovdqa 0x60(%rsp), %xmm9
vmovdqa 0x50(%rsp), %xmm10
vmovdqa (%rsp), %xmm14
vmovdqa 0x10(%rsp), %xmm11
vmovdqa 0x20(%rsp), %xmm12
vmovdqa 0x30(%rsp), %xmm13
movl $0x80, %eax
vmovdqa 0x40(%rsp), %xmm15
movl $0x80, %edx
movq %rbx, %rcx
cmpq %r13, %rdx
ja 0x1f01
vmovdqa (%rcx), %xmm8
vpternlogq $0x78, %xmm2, %xmm3, %xmm8
vpternlogq $0x96, %xmm6, %xmm1, %xmm8
vmovdqa 0x10(%rcx), %xmm9
vpternlogq $0x78, %xmm6, %xmm0, %xmm9
vpternlogq $0x96, %xmm2, %xmm5, %xmm9
vmovdqa %xmm8, (%r12)
vmovdqa %xmm9, 0x10(%r12)
vaesenc %xmm0, %xmm6, %xmm10
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm3, %xmm7
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm4, %xmm1
vaesenc %xmm4, %xmm0, %xmm0
vpxor %xmm0, %xmm8, %xmm0
vpxor %xmm7, %xmm9, %xmm4
vmovdqa 0x20(%rcx), %xmm7
vpternlogq $0x78, %xmm3, %xmm2, %xmm7
vpternlogq $0x96, %xmm6, %xmm1, %xmm7
vmovdqa 0x30(%rcx), %xmm8
vpternlogq $0x78, %xmm10, %xmm6, %xmm8
vpternlogq $0x96, %xmm5, %xmm2, %xmm8
vmovdqa %xmm7, 0x20(%r12)
vmovdqa %xmm8, 0x30(%r12)
vaesenc %xmm10, %xmm6, %xmm9
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm3, %xmm4
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm0, %xmm1
vaesenc %xmm0, %xmm10, %xmm0
vpxor %xmm7, %xmm0, %xmm7
vpxor %xmm4, %xmm8, %xmm4
vmovdqa 0x40(%rcx), %xmm8
vpternlogq $0x78, %xmm3, %xmm2, %xmm8
vpternlogq $0x96, %xmm6, %xmm1, %xmm8
vmovdqa 0x50(%rcx), %xmm10
vpternlogq $0x78, %xmm9, %xmm6, %xmm10
vpternlogq $0x96, %xmm5, %xmm2, %xmm10
vmovdqa %xmm8, 0x40(%r12)
vmovdqa %xmm10, 0x50(%r12)
vaesenc %xmm9, %xmm6, %xmm0
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm3, %xmm4
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm7, %xmm1
vaesenc %xmm7, %xmm9, %xmm7
vpxor %xmm7, %xmm8, %xmm7
vpxor %xmm4, %xmm10, %xmm4
vmovdqa 0x60(%rcx), %xmm14
vpternlogq $0x78, %xmm3, %xmm2, %xmm14
vpternlogq $0x96, %xmm6, %xmm1, %xmm14
vmovdqa 0x70(%rcx), %xmm15
vpternlogq $0x78, %xmm0, %xmm6, %xmm15
vpternlogq $0x96, %xmm5, %xmm2, %xmm15
vmovdqa %xmm14, 0x60(%r12)
vmovdqa %xmm15, 0x70(%r12)
vaesenc %xmm0, %xmm6, %xmm8
vaesenc %xmm6, %xmm5, %xmm9
vaesenc %xmm5, %xmm4, %xmm10
vaesenc %xmm4, %xmm3, %xmm4
vaesenc %xmm3, %xmm2, %xmm13
vaesenc %xmm2, %xmm1, %xmm12
vaesenc %xmm1, %xmm7, %xmm11
vaesenc %xmm7, %xmm0, %xmm0
vpxor %xmm0, %xmm14, %xmm14
vpxor %xmm4, %xmm15, %xmm15
movq (%r15), %r13
addq %rax, %rcx
addq %rax, %r12
addq %rax, %rdx
vmovdqa %xmm15, %xmm7
vmovdqa %xmm8, %xmm0
vmovdqa %xmm13, %xmm3
vmovdqa %xmm10, %xmm5
vmovdqa %xmm12, %xmm2
vmovdqa %xmm11, %xmm1
vmovdqa %xmm9, %xmm6
vmovdqa %xmm14, %xmm4
jmp 0x1d39
vmovdqa %xmm8, 0x70(%rsp)
vmovdqa %xmm9, 0x60(%rsp)
vmovdqa %xmm10, 0x50(%rsp)
vmovdqa %xmm13, 0x30(%rsp)
vmovdqa %xmm12, 0x20(%rsp)
vmovdqa %xmm11, 0x10(%rsp)
vmovdqa %xmm14, (%rsp)
vmovdqa %xmm15, 0x40(%rsp)
movq %r13, %rdx
andq $0x7f, %rdx
je 0x1f47
movq %rsp, %rdi
movq %r12, %rsi
callq 0x10d0
movq (%r15), %r13
pushq $0x10
popq %rdx
leaq 0x80(%rsp), %rcx
movq %rsp, %r8
movq %r13, %rdi
movq %r14, %rsi
callq 0x1080
addq %r13, %rbx
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1f7e
movb (%rbx,%rax), %dl
xorb 0x80(%rsp,%rax), %dl
orb %dl, %cl
incq %rax
jmp 0x1f67
xorl %eax, %eax
negb %cl
sbbl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop