// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnia/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 i;
        unsigned char plaintextblock[32], ciphertextblock[32];
        unsigned char tag[16];
        unsigned char check = 0;
        __m128i aegis128L_state[8];

        if (clen < 16) return 1; 

        aegis128L_initialization(k, npub, aegis128L_state);

        //process the associated data
        for (i = 0; i+32 <= adlen; i += 32) {
              aegis128L_enc_aut_step(ad+i, ciphertextblock, aegis128L_state);
        }

        //deal with the partial block of associated data
        //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, aegis128L_state);
        }

        //decrypt the ciphertext
        *mlen = clen - 16;  
        for (i = 0; (i+32) <= *mlen; i += 32) {
              aegis128L_dec_aut_step(m+i, c+i, aegis128L_state);
        }

        // Deal with the partial block
        // In this program, we assume that the message length is multiple of bytes.

        if (  (*mlen & 0x1f) != 0 )  {
              memset(ciphertextblock, 0, 32);
              memcpy(ciphertextblock, c+i,  *mlen & 0x1f);
              aegis128L_dec_aut_step(plaintextblock, ciphertextblock, aegis128L_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);
              aegis128L_state[0] = _mm_xor_si128( aegis128L_state[0], _mm_load_si128((__m128i*)plaintextblock)  ) ;
              aegis128L_state[4] = _mm_xor_si128( aegis128L_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, aegis128L_state);

        //verification
        for (i = 0; i  < 16; i++) check |= (tag[i] ^ c[i+*mlen]);  
        if (check == 0) return 0;
        else return 1;
}

// Assembly:
movl $0x1, %eax
cmpq $0x10, %r8
jb 0x1d04
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, %r13
movq %r8, %r14
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0x140(%rsp), %rax
movq 0x138(%rsp), %rcx
movq 0x130(%rsp), %rbp
vmovdqa (%rax), %xmm0
vmovdqa (%rcx), %xmm1
vmovdqa 0x834(%rip), %xmm10     # 0x2000
vpxor %xmm0, %xmm1, %xmm8
vpxor %xmm0, %xmm10, %xmm15
vmovdqa 0x834(%rip), %xmm11     # 0x2010
vpxor %xmm0, %xmm11, %xmm12
movl $0xa, %eax
vmovdqa %xmm15, %xmm13
vmovdqa %xmm11, %xmm14
vmovdqa %xmm8, %xmm9
vaesenc %xmm9, %xmm15, %xmm3
vaesenc %xmm15, %xmm12, %xmm15
vaesenc %xmm12, %xmm13, %xmm12
vaesenc %xmm13, %xmm8, %xmm13
vaesenc %xmm8, %xmm11, %xmm2
vaesenc %xmm11, %xmm10, %xmm11
vaesenc %xmm10, %xmm14, %xmm10
vaesenc %xmm14, %xmm9, %xmm14
vpxor %xmm1, %xmm3, %xmm9
vpxor %xmm0, %xmm2, %xmm8
decl %eax
jne 0x17f4
xorl %eax, %eax
cmpq $0x20, %rbp
jae 0x1835
jmp 0x18c0
vmovdqa %xmm15, %xmm5
vmovdqa %xmm12, %xmm1
vmovdqa %xmm13, %xmm6
vmovdqa %xmm11, %xmm2
vmovdqa %xmm10, %xmm4
vmovdqa %xmm14, %xmm3
movq %rax, %rcx
vmovdqa (%r13,%rax), %xmm0
vmovdqa 0x10(%r13,%rax), %xmm7
vaesenc %xmm15, %xmm12, %xmm15
vaesenc %xmm12, %xmm13, %xmm12
vaesenc %xmm13, %xmm8, %xmm13
vaesenc %xmm8, %xmm11, %xmm8
vaesenc %xmm11, %xmm10, %xmm11
vaesenc %xmm10, %xmm14, %xmm10
vaesenc %xmm14, %xmm9, %xmm14
vaesenc %xmm9, %xmm5, %xmm9
vpxor %xmm0, %xmm9, %xmm9
vpxor %xmm7, %xmm8, %xmm8
addq $0x20, %rax
addq $0x40, %rcx
cmpq %rbp, %rcx
jbe 0x1835
vpternlogq $0x78, %xmm5, %xmm1, %xmm6
vpternlogq $0x96, %xmm4, %xmm6, %xmm7
vmovdqa %xmm7, 0xa0(%rsp)
vpternlogq $0x6a, %xmm1, %xmm4, %xmm2
vpternlogq $0x96, %xmm3, %xmm2, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
andq $0x1f, %rbp
je 0x19de
addq %rax, %r13
movl $0x20, %edx
subq %rbp, %rdx
leaq (%rsp,%rbp), %rdi
addq $0xd0, %rdi
xorl %esi, %esi
vmovdqa %xmm10, 0x10(%rsp)
vmovdqa %xmm9, 0x60(%rsp)
vmovdqa %xmm11, 0x40(%rsp)
vmovdqa %xmm12, (%rsp)
vmovdqa %xmm13, 0x30(%rsp)
vmovdqa %xmm14, 0x20(%rsp)
vmovdqa %xmm8, 0x50(%rsp)
vmovdqa %xmm15, 0x80(%rsp)
callq 0x1030
leaq 0xd0(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1070
vmovdqa 0x20(%rsp), %xmm14
vmovdqa 0x30(%rsp), %xmm13
vmovdqa (%rsp), %xmm12
vmovdqa 0x40(%rsp), %xmm11
vmovdqa 0x10(%rsp), %xmm10
leaq 0xd0(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa 0x10(%rax), %xmm1
vmovdqa %xmm12, %xmm2
vpternlogq $0x78, %xmm11, %xmm10, %xmm2
vpternlogq $0x96, %xmm14, %xmm0, %xmm2
vmovdqa %xmm13, %xmm3
vmovdqa 0x80(%rsp), %xmm4
vpternlogq $0x78, %xmm4, %xmm12, %xmm3
vpternlogq $0x96, %xmm10, %xmm1, %xmm3
vmovdqa %xmm2, 0x90(%rsp)
vmovdqa %xmm3, 0xa0(%rsp)
vmovdqa 0x60(%rsp), %xmm5
vaesenc %xmm5, %xmm4, %xmm2
vaesenc %xmm4, %xmm12, %xmm15
vaesenc %xmm12, %xmm13, %xmm12
vmovdqa 0x50(%rsp), %xmm4
vaesenc %xmm13, %xmm4, %xmm13
vaesenc %xmm4, %xmm11, %xmm4
vaesenc %xmm11, %xmm10, %xmm11
vaesenc %xmm10, %xmm14, %xmm10
vaesenc %xmm14, %xmm5, %xmm14
vpxor %xmm0, %xmm2, %xmm9
vpxor %xmm1, %xmm4, %xmm8
addq $-0x10, %r14
movq %r14, (%r15)
cmpq $0x20, %r14
jae 0x19f2
xorl %ebp, %ebp
jmp 0x1a73
xorl %eax, %eax
vmovdqa %xmm15, %xmm0
vmovdqa (%rbx,%rax), %xmm1
vpternlogq $0x78, %xmm11, %xmm10, %xmm1
vpternlogq $0x96, %xmm12, %xmm14, %xmm1
vmovdqa 0x10(%rbx,%rax), %xmm2
vpternlogq $0x78, %xmm0, %xmm12, %xmm2
vpternlogq $0x96, %xmm13, %xmm10, %xmm2
vmovdqa %xmm1, (%r12,%rax)
vmovdqa %xmm2, 0x10(%r12,%rax)
vaesenc %xmm0, %xmm12, %xmm15
vaesenc %xmm12, %xmm13, %xmm12
vaesenc %xmm13, %xmm8, %xmm13
vaesenc %xmm8, %xmm11, %xmm3
vaesenc %xmm11, %xmm10, %xmm11
vaesenc %xmm10, %xmm14, %xmm10
vaesenc %xmm14, %xmm9, %xmm14
vaesenc %xmm9, %xmm0, %xmm0
vpxor %xmm1, %xmm0, %xmm9
vpxor %xmm2, %xmm3, %xmm8
movq (%r15), %r14
leaq 0x20(%rax), %rbp
addq $0x40, %rax
vmovdqa %xmm15, %xmm0
cmpq %r14, %rax
movq %rbp, %rax
jbe 0x19f8
movq %r14, %r13
andq $0x1f, %r13
je 0x1c7a
leaq (%rbx,%rbp), %rax
movq %rax, 0x70(%rsp)
movl $0x20, %edx
subq %r13, %rdx
leaq (%rsp,%r13), %rdi
addq $0x90, %rdi
xorl %esi, %esi
vmovdqa %xmm10, 0x10(%rsp)
vmovdqa %xmm9, 0x60(%rsp)
vmovdqa %xmm11, 0x40(%rsp)
vmovdqa %xmm12, (%rsp)
vmovdqa %xmm13, 0x30(%rsp)
vmovdqa %xmm14, 0x20(%rsp)
vmovdqa %xmm8, 0x50(%rsp)
vmovdqa %xmm15, 0x80(%rsp)
callq 0x1030
leaq 0x90(%rsp), %r14
movq %r14, %rdi
movq 0x70(%rsp), %rsi
movq %r13, %rdx
callq 0x1070
vmovdqa (%r14), %xmm1
vmovdqa 0x40(%rsp), %xmm0
vpternlogq $0x78, 0x10(%rsp), %xmm0, %xmm1
vmovdqa 0x20(%rsp), %xmm0
vpternlogq $0x96, (%rsp), %xmm0, %xmm1
vmovdqa %xmm1, 0x70(%rsp)
vmovdqa 0x10(%r14), %xmm2
vmovdqa 0x80(%rsp), %xmm3
vpternlogq $0x78, (%rsp), %xmm3, %xmm2
leaq 0xd0(%rsp), %rax
vmovdqa %xmm1, (%rax)
vmovdqa 0x30(%rsp), %xmm0
vpternlogq $0x96, 0x10(%rsp), %xmm0, %xmm2
vmovdqa %xmm2, 0xb0(%rsp)
vmovdqa %xmm2, 0x10(%rax)
vmovdqa (%rsp), %xmm0
vaesenc %xmm3, %xmm0, %xmm0
vmovdqa %xmm0, 0xc0(%rsp)
vmovdqa (%rsp), %xmm0
vmovdqa 0x30(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, (%rsp)
vmovdqa 0x30(%rsp), %xmm0
vmovdqa 0x50(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
vmovdqa 0x40(%rsp), %xmm0
vmovdqa 0x50(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, 0x50(%rsp)
vmovdqa 0x10(%rsp), %xmm0
vmovdqa 0x40(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, 0x40(%rsp)
vmovdqa 0x10(%rsp), %xmm0
vmovdqa 0x20(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
vmovdqa 0x60(%rsp), %xmm0
vmovdqa 0x20(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, 0x20(%rsp)
vmovdqa 0x60(%rsp), %xmm0
vaesenc %xmm0, %xmm3, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
addq %rbp, %r12
movq %r12, %rdi
movq %rax, %r12
movq %rax, %rsi
movq %r13, %rdx
callq 0x1070
movq (%r15), %r14
movl %r14d, %edx
andl $0x1f, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1030
vmovdqa 0x50(%rsp), %xmm8
vmovdqa 0x20(%rsp), %xmm14
vmovdqa 0x30(%rsp), %xmm13
vmovdqa (%rsp), %xmm12
vmovdqa 0x40(%rsp), %xmm11
vmovdqa 0x60(%rsp), %xmm9
vmovdqa 0x10(%rsp), %xmm10
vmovdqa 0x70(%rsp), %xmm0
vpternlogq $0x96, (%r12), %xmm0, %xmm9
vmovdqa 0xb0(%rsp), %xmm0
vpternlogq $0x96, 0x10(%r12), %xmm0, %xmm8
vmovdqa 0xc0(%rsp), %xmm15
vmovq %r14, %xmm0
vmovq 0x130(%rsp), %xmm1
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
vpsllq $0x3, %xmm0, %xmm0
vpxor %xmm0, %xmm10, %xmm0
movl $0x7, %eax
vaesenc %xmm9, %xmm15, %xmm1
vaesenc %xmm15, %xmm12, %xmm15
vaesenc %xmm12, %xmm13, %xmm12
vaesenc %xmm13, %xmm8, %xmm13
vaesenc %xmm8, %xmm11, %xmm3
vaesenc %xmm11, %xmm10, %xmm11
vaesenc %xmm10, %xmm14, %xmm10
vaesenc %xmm14, %xmm9, %xmm14
vpxor %xmm0, %xmm1, %xmm9
vpxor %xmm0, %xmm3, %xmm8
decl %eax
jne 0x1c9a
vpxor %xmm12, %xmm13, %xmm0
vpternlogq $0x96, %xmm8, %xmm0, %xmm11
vpternlogq $0x96, %xmm10, %xmm11, %xmm14
vpternlogq $0x96, (%rbx,%r14), %xmm14, %xmm9
xorl %eax, %eax
vptest %xmm9, %xmm9
setne %al
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)