// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnib/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;

    if (clen < 16) return 1; 

    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);
    }  


    //decrypt the ciphertext 
    *mlen = clen - 16;  
    for (i = 0; (i+32) <= *mlen; i += 32) {
        msg0 = _mm_load_si128((__m128i*)(c+i));
        msg1 = _mm_load_si128((__m128i*)(c+i+16));
        
        //decryption
        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), 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);   
    }

    // Deal with the partial block  
    // In this program, we assume that the message length is a multiple of bytes.
    if (  (*mlen & 0x1f) != 0 )  {  
        memset(ciphertextblock, 0, 32);  
        memcpy(ciphertextblock, c+i,  *mlen & 0x1f);  
        aegis128L_dec_aut_step(plaintextblock, ciphertextblock, state);  
        memcpy(m+i, plaintextblock, *mlen & 0x1f);  

        //need to modify the state here (because in the last block, keystream is wrongly used to update the state)
        memset(plaintextblock, 0, *mlen & 0x1f);  
        state[0] = _mm_xor_si128( state[0], _mm_load_si128((__m128i*)plaintextblock)  ) ;  
        state[4] = _mm_xor_si128( state[4], _mm_load_si128((__m128i*)(plaintextblock+16) )  ) ;
    }

    //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:
subq $0x5a8, %rsp            # imm = 0x5A8
movq 0x5c0(%rsp), %rax
movq 0x5b8(%rsp), %rax
movq 0x5b0(%rsp), %rax
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movq %rdx, 0x150(%rsp)
movq %rcx, 0x148(%rsp)
movq %r8, 0x140(%rsp)
movq %r9, 0x138(%rsp)
movb $0x0, 0xdf(%rsp)
cmpq $0x10, 0x140(%rsp)
jae 0x3872
movl $0x1, 0x16c(%rsp)
jmp 0x4378
movq 0x5c0(%rsp), %rdi
movq 0x5b8(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x1040
movq $0x0, 0x130(%rsp)
movq 0x130(%rsp), %rax
addq $0x20, %rax
cmpq 0x5b0(%rsp), %rax
ja 0x3ba4
movq 0x138(%rsp), %rax
addq 0x130(%rsp), %rax
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movq 0x138(%rsp), %rax
addq 0x130(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x190(%rsp)
movq 0x190(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm0, (%rsp)
vmovdqa 0xc0(%rsp), %xmm0
vmovdqa %xmm0, 0x40(%rsp)
vmovdqa 0xb0(%rsp), %xmm1
vmovdqa 0xc0(%rsp), %xmm0
vmovdqa %xmm1, 0x510(%rsp)
vmovdqa %xmm0, 0x500(%rsp)
vmovdqa 0x510(%rsp), %xmm0
vmovdqa 0x500(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0xc0(%rsp)
vmovdqa 0xa0(%rsp), %xmm1
vmovdqa 0xb0(%rsp), %xmm0
vmovdqa %xmm1, 0x4f0(%rsp)
vmovdqa %xmm0, 0x4e0(%rsp)
vmovdqa 0x4f0(%rsp), %xmm0
vmovdqa 0x4e0(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0xb0(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa 0xa0(%rsp), %xmm0
vmovdqa %xmm1, 0x4d0(%rsp)
vmovdqa %xmm0, 0x4c0(%rsp)
vmovdqa 0x4d0(%rsp), %xmm0
vmovdqa 0x4c0(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0xa0(%rsp)
vmovdqa 0x80(%rsp), %xmm1
vmovdqa 0x90(%rsp), %xmm0
vmovdqa %xmm1, 0x4b0(%rsp)
vmovdqa %xmm0, 0x4a0(%rsp)
vmovdqa 0x4b0(%rsp), %xmm0
vmovdqa 0x4a0(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
vmovdqa 0x70(%rsp), %xmm1
vmovdqa 0x80(%rsp), %xmm0
vmovdqa %xmm1, 0x490(%rsp)
vmovdqa %xmm0, 0x480(%rsp)
vmovdqa 0x490(%rsp), %xmm0
vmovdqa 0x480(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x80(%rsp)
vmovdqa 0x60(%rsp), %xmm1
vmovdqa 0x70(%rsp), %xmm0
vmovdqa %xmm1, 0x470(%rsp)
vmovdqa %xmm0, 0x460(%rsp)
vmovdqa 0x470(%rsp), %xmm0
vmovdqa 0x460(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp)
vmovdqa 0x50(%rsp), %xmm1
vmovdqa 0x60(%rsp), %xmm0
vmovdqa %xmm1, 0x450(%rsp)
vmovdqa %xmm0, 0x440(%rsp)
vmovdqa 0x450(%rsp), %xmm0
vmovdqa 0x440(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
vmovdqa 0x40(%rsp), %xmm1
vmovdqa 0x50(%rsp), %xmm0
vmovdqa %xmm1, 0x430(%rsp)
vmovdqa %xmm0, 0x420(%rsp)
vmovdqa 0x430(%rsp), %xmm0
vmovdqa 0x420(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x50(%rsp), %xmm1
vmovdqa 0x10(%rsp), %xmm0
vmovdqa %xmm1, 0x310(%rsp)
vmovdqa %xmm0, 0x300(%rsp)
vmovdqa 0x310(%rsp), %xmm0
vpxor 0x300(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa (%rsp), %xmm0
vmovdqa %xmm1, 0x2f0(%rsp)
vmovdqa %xmm0, 0x2e0(%rsp)
vmovdqa 0x2f0(%rsp), %xmm0
vpxor 0x2e0(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movq 0x130(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x130(%rsp)
jmp 0x3898
movq 0x5b0(%rsp), %rax
andq $0x1f, %rax
cmpq $0x0, %rax
je 0x3c0d
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1030
leaq 0x110(%rsp), %rdi
movq 0x138(%rsp), %rsi
addq 0x130(%rsp), %rsi
movq 0x5b0(%rsp), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x1060
movq 0x140(%rsp), %rcx
subq $0x10, %rcx
movq 0x158(%rsp), %rax
movq %rcx, (%rax)
movq $0x0, 0x130(%rsp)
movq 0x130(%rsp), %rax
addq $0x20, %rax
movq 0x158(%rsp), %rcx
cmpq (%rcx), %rax
ja 0x415b
movq 0x148(%rsp), %rax
addq 0x130(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm0, 0x10(%rsp)
movq 0x148(%rsp), %rax
addq 0x130(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x180(%rsp)
movq 0x180(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm0, (%rsp)
vmovdqa 0x10(%rsp), %xmm1
vmovdqa 0xb0(%rsp), %xmm0
vmovdqa %xmm1, 0x2d0(%rsp)
vmovdqa %xmm0, 0x2c0(%rsp)
vmovdqa 0x2d0(%rsp), %xmm0
vpxor 0x2c0(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
vmovdqa 0x10(%rsp), %xmm1
vmovdqa 0x60(%rsp), %xmm0
vmovdqa %xmm1, 0x2b0(%rsp)
vmovdqa %xmm0, 0x2a0(%rsp)
vmovdqa 0x2b0(%rsp), %xmm0
vpxor 0x2a0(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
vmovdqa (%rsp), %xmm1
vmovdqa 0x70(%rsp), %xmm0
vmovdqa %xmm1, 0x290(%rsp)
vmovdqa %xmm0, 0x280(%rsp)
vmovdqa 0x290(%rsp), %xmm0
vpxor 0x280(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
vmovdqa (%rsp), %xmm1
vmovdqa 0xa0(%rsp), %xmm0
vmovdqa %xmm1, 0x270(%rsp)
vmovdqa %xmm0, 0x260(%rsp)
vmovdqa 0x270(%rsp), %xmm0
vpxor 0x260(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
vmovdqa 0x10(%rsp), %xmm1
vmovdqa 0x70(%rsp), %xmm2
vmovdqa 0x80(%rsp), %xmm0
vmovdqa %xmm2, 0x590(%rsp)
vmovdqa %xmm0, 0x580(%rsp)
vmovdqa 0x590(%rsp), %xmm0
vpand 0x580(%rsp), %xmm0, %xmm0
vmovdqa %xmm1, 0x250(%rsp)
vmovdqa %xmm0, 0x240(%rsp)
vmovdqa 0x250(%rsp), %xmm0
vpxor 0x240(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
vmovdqa (%rsp), %xmm1
vmovdqa 0xb0(%rsp), %xmm2
vmovdqa 0xc0(%rsp), %xmm0
vmovdqa %xmm2, 0x570(%rsp)
vmovdqa %xmm0, 0x560(%rsp)
vmovdqa 0x570(%rsp), %xmm0
vpand 0x560(%rsp), %xmm0, %xmm0
vmovdqa %xmm1, 0x230(%rsp)
vmovdqa %xmm0, 0x220(%rsp)
vmovdqa 0x230(%rsp), %xmm0
vpxor 0x220(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movq 0x160(%rsp), %rax
addq 0x130(%rsp), %rax
vmovdqa 0x10(%rsp), %xmm0
movq %rax, 0x558(%rsp)
vmovdqa %xmm0, 0x540(%rsp)
vmovdqa 0x540(%rsp), %xmm0
movq 0x558(%rsp), %rax
vmovdqa %xmm0, (%rax)
movq 0x160(%rsp), %rax
addq 0x130(%rsp), %rax
addq $0x10, %rax
vmovdqa (%rsp), %xmm0
movq %rax, 0x538(%rsp)
vmovdqa %xmm0, 0x520(%rsp)
vmovdqa 0x520(%rsp), %xmm0
movq 0x538(%rsp), %rax
vmovdqa %xmm0, (%rax)
vmovdqa 0xc0(%rsp), %xmm0
vmovdqa %xmm0, 0x40(%rsp)
vmovdqa 0xb0(%rsp), %xmm1
vmovdqa 0xc0(%rsp), %xmm0
vmovdqa %xmm1, 0x410(%rsp)
vmovdqa %xmm0, 0x400(%rsp)
vmovdqa 0x410(%rsp), %xmm0
vmovdqa 0x400(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0xc0(%rsp)
vmovdqa 0xa0(%rsp), %xmm1
vmovdqa 0xb0(%rsp), %xmm0
vmovdqa %xmm1, 0x3f0(%rsp)
vmovdqa %xmm0, 0x3e0(%rsp)
vmovdqa 0x3f0(%rsp), %xmm0
vmovdqa 0x3e0(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0xb0(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa 0xa0(%rsp), %xmm0
vmovdqa %xmm1, 0x3d0(%rsp)
vmovdqa %xmm0, 0x3c0(%rsp)
vmovdqa 0x3d0(%rsp), %xmm0
vmovdqa 0x3c0(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0xa0(%rsp)
vmovdqa 0x80(%rsp), %xmm1
vmovdqa 0x90(%rsp), %xmm0
vmovdqa %xmm1, 0x3b0(%rsp)
vmovdqa %xmm0, 0x3a0(%rsp)
vmovdqa 0x3b0(%rsp), %xmm0
vmovdqa 0x3a0(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
vmovdqa 0x70(%rsp), %xmm1
vmovdqa 0x80(%rsp), %xmm0
vmovdqa %xmm1, 0x390(%rsp)
vmovdqa %xmm0, 0x380(%rsp)
vmovdqa 0x390(%rsp), %xmm0
vmovdqa 0x380(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x80(%rsp)
vmovdqa 0x60(%rsp), %xmm1
vmovdqa 0x70(%rsp), %xmm0
vmovdqa %xmm1, 0x370(%rsp)
vmovdqa %xmm0, 0x360(%rsp)
vmovdqa 0x370(%rsp), %xmm0
vmovdqa 0x360(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp)
vmovdqa 0x50(%rsp), %xmm1
vmovdqa 0x60(%rsp), %xmm0
vmovdqa %xmm1, 0x350(%rsp)
vmovdqa %xmm0, 0x340(%rsp)
vmovdqa 0x350(%rsp), %xmm0
vmovdqa 0x340(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
vmovdqa 0x40(%rsp), %xmm1
vmovdqa 0x50(%rsp), %xmm0
vmovdqa %xmm1, 0x330(%rsp)
vmovdqa %xmm0, 0x320(%rsp)
vmovdqa 0x330(%rsp), %xmm0
vmovdqa 0x320(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x50(%rsp), %xmm1
vmovdqa 0x10(%rsp), %xmm0
vmovdqa %xmm1, 0x210(%rsp)
vmovdqa %xmm0, 0x200(%rsp)
vmovdqa 0x210(%rsp), %xmm0
vpxor 0x200(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x90(%rsp), %xmm1
vmovdqa (%rsp), %xmm0
vmovdqa %xmm1, 0x1f0(%rsp)
vmovdqa %xmm0, 0x1e0(%rsp)
vmovdqa 0x1f0(%rsp), %xmm0
vpxor 0x1e0(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movq 0x130(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x130(%rsp)
jmp 0x3c30
movq 0x158(%rsp), %rax
movq (%rax), %rax
andq $0x1f, %rax
cmpq $0x0, %rax
je 0x42ba
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1030
leaq 0xf0(%rsp), %rdi
movq 0x148(%rsp), %rsi
addq 0x130(%rsp), %rsi
movq 0x158(%rsp), %rax
movq (%rax), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0x110(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x10a0
movq 0x160(%rsp), %rdi
addq 0x130(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movq 0x158(%rsp), %rax
movq (%rax), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0x110(%rsp), %rdi
movq 0x158(%rsp), %rax
movq (%rax), %rdx
andq $0x1f, %rdx
xorl %esi, %esi
callq 0x1030
vmovdqa 0x50(%rsp), %xmm1
leaq 0x110(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm1, 0x1d0(%rsp)
vmovdqa %xmm0, 0x1c0(%rsp)
vmovdqa 0x1d0(%rsp), %xmm0
vpxor 0x1c0(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x50(%rsp)
vmovdqa 0x90(%rsp), %xmm1
leaq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x170(%rsp)
movq 0x170(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm1, 0x1b0(%rsp)
vmovdqa %xmm0, 0x1a0(%rsp)
vmovdqa 0x1b0(%rsp), %xmm0
vpxor 0x1a0(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movq 0x158(%rsp), %rax
movq (%rax), %rdi
movq 0x5b0(%rsp), %rsi
leaq 0xe0(%rsp), %rcx
leaq 0x50(%rsp), %r8
movl $0x10, %edx
callq 0x1070
movq $0x0, 0x130(%rsp)
cmpq $0x10, 0x130(%rsp)
jae 0x4353
movq 0x130(%rsp), %rax
movzbl 0xe0(%rsp,%rax), %ecx
movq 0x148(%rsp), %rax
movq 0x130(%rsp), %rdx
movq 0x158(%rsp), %rsi
addq (%rsi), %rdx
movzbl (%rax,%rdx), %eax
xorl %eax, %ecx
movzbl 0xdf(%rsp), %eax
orl %ecx, %eax
movb %al, 0xdf(%rsp)
movq 0x130(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x130(%rsp)
jmp 0x42f0
movzbl 0xdf(%rsp), %eax
cmpl $0x0, %eax
jne 0x436d
movl $0x0, 0x16c(%rsp)
jmp 0x4378
movl $0x1, 0x16c(%rsp)
movl 0x16c(%rsp), %eax
addq $0x5a8, %rsp            # imm = 0x5A8
retq
nopw (%rax,%rax)