// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aegis256/ref/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[16], ciphertextblock[16];
        unsigned char tag[16];
        unsigned char check = 0;
        unsigned char aegis256_state[96];

        if (clen < 16) return -1;

        aegis256_initialization(k, npub, aegis256_state);

        //process the associated data
        for (i = 0; (i+16) <= adlen; i += 16) {
              aegis256_enc_aut_step(ad+i, ciphertextblock, aegis256_state);
        }

        //deal with the partial block of associated data
        //in this program, we assume that the message length is multiple of bytes.
        if (  (adlen & 0xf) != 0 )  {
              memset(plaintextblock, 0, 16);
              memcpy(plaintextblock, ad+i, adlen & 0xf);
              aegis256_enc_aut_step(plaintextblock, ciphertextblock, aegis256_state);
        }

        *mlen = clen - 16;

        //decrypt the ciphertext
        for (i = 0; (i+16) <= *mlen; i += 16) {
              aegis256_dec_aut_step(m+i, c+i, aegis256_state);
        }

        // Deal with the partial block
        // In this program, we assume that the message length is multiple of bytes.
        if (  (*mlen & 0xf) != 0  )  {
              memset(ciphertextblock, 0, 16);
              memcpy(ciphertextblock, c+i, *mlen & 0xf);
              aegis256_dec_aut_step(plaintextblock, ciphertextblock, aegis256_state);
              memcpy(m+i, plaintextblock, *mlen & 0xf);

              //need to modify the state here (because in the last block, keystream is wrongly used to update the state)
              memset(plaintextblock, 0, *mlen & 0xf);
              ((unsigned long long*)aegis256_state)[0] ^= ((unsigned long long*)plaintextblock)[0];
              ((unsigned long long*)aegis256_state)[1] ^= ((unsigned long long*)plaintextblock)[1];
        }

        //we assume that the tag length is multiple of bytes
        aegis256_tag_generation(*mlen, adlen, 16, tag, aegis256_state);

        //verification
        for (i = 0; i  < 16; i++) check |= (tag[i] ^ c[clen - 16 + i]);
        if (check == 0) return 0;
        else return -1;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r8
jb 0x1f6a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %r9, %r12
movq %r8, %r14
movq %rsi, %r13
movq %rdi, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq 0x160(%rsp), %rdi
movq 0x158(%rsp), %rsi
movq 0x150(%rsp), %r15
leaq 0x20(%rsp), %rdx
callq 0x1080
cmpq $0x10, %r15
movq %r14, 0x8(%rsp)
jae 0x19b3
movq 0x90(%rsp), %rcx
xorl %eax, %eax
jmp 0x1b7f
movq %r13, 0xc0(%rsp)
xorl %eax, %eax
movq %r12, 0x80(%rsp)
movq 0x80(%rsp), %r14
movq %rax, %r15
movq 0x40(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x68(%rsp), %rbx
movq 0x70(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq (%r14,%r15), %rax
movq %rax, 0x110(%rsp)
movq 0x8(%r14,%r15), %rbp
leaq 0x70(%rsp), %rdi
vmovaps (%rdi), %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x60(%rsp), %r12
movq %r12, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r12, %rdi
leaq 0x50(%rsp), %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x1040
movq %r13, %rdi
leaq 0x40(%rsp), %r12
movq %r12, %rsi
movq %r13, %rdx
callq 0x1040
movq %r12, %rdi
leaq 0x30(%rsp), %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x1040
movq %r13, %rdi
leaq 0x20(%rsp), %r12
movq %r12, %rsi
movq %r13, %rdx
callq 0x1040
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x1040
vmovdqu (%r14,%r15), %xmm0
vpxor 0x20(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
leaq 0x10(%r15), %rax
addq $0x20, %r15
cmpq 0x150(%rsp), %r15
movq 0x150(%rsp), %r15
jbe 0x19cd
movq 0xe0(%rsp), %rcx
andq 0xd8(%rsp), %rcx
xorq 0xd0(%rsp), %rbx
xorq %rcx, %rbx
xorq 0xc8(%rsp), %rbp
xorq %rbx, %rbp
movq %rbp, 0x98(%rsp)
movq 0x108(%rsp), %rcx
andq 0xf8(%rsp), %rcx
movq 0x100(%rsp), %rdx
xorq 0xf0(%rsp), %rdx
xorq %rcx, %rdx
movq 0x110(%rsp), %rcx
xorq 0xe8(%rsp), %rcx
xorq %rdx, %rcx
movq 0x8(%rsp), %r14
movq 0xc0(%rsp), %r13
movq 0x80(%rsp), %r12
movq %rcx, 0x90(%rsp)
movq %r15, %rbx
andq $0xf, %rbx
je 0x1c91
addq %rax, %r12
movl $0x10, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0xb0, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1090
leaq 0x40(%rsp), %r14
leaq 0x50(%rsp), %r12
leaq 0x30(%rsp), %rbx
leaq 0x70(%rsp), %rdi
vmovdqa -0x40(%rdi), %xmm0
vmovdqa -0x20(%rdi), %xmm1
vmovaps (%rdi), %xmm2
vpand -0x30(%rdi), %xmm1, %xmm1
vpternlogq $0x96, -0x10(%rdi), %xmm0, %xmm1
vmovdqa (%r15), %xmm0
vmovdqa %xmm0, 0x80(%rsp)
vpternlogq $0x96, (%rdi), %xmm0, %xmm1
leaq 0x60(%rsp), %rbp
vmovdqa %xmm1, 0x90(%rsp)
leaq 0x10(%rsp), %r15
vmovaps %xmm2, (%r15)
movq %rbp, %rsi
movq %rdi, %rdx
callq 0x1040
movq %rbp, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1040
movq %r12, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1040
movq %r14, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1040
leaq 0x20(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1040
movq %r14, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1040
vmovdqa 0x80(%rsp), %xmm0
vpxor (%r14), %xmm0, %xmm0
vmovdqa %xmm0, (%r14)
movq 0x8(%rsp), %r14
addq $-0x10, %r14
movq %r13, %rax
movq %r14, (%r13)
cmpq $0x10, %r14
movq %r14, 0x8(%rsp)
jae 0x1cba
xorl %r15d, %r15d
movq %r14, %rbx
movq 0xa0(%rsp), %r14
jmp 0x1dd1
xorl %r12d, %r12d
movq 0xa0(%rsp), %r14
movq 0xa8(%rsp), %r13
vmovdqa 0x50(%rsp), %xmm0
vpand 0x40(%rsp), %xmm0, %xmm0
vmovdqu %xmm0, (%r13,%r12)
vpxor 0x30(%rsp), %xmm0, %xmm0
vmovdqu %xmm0, (%r13,%r12)
vpxor 0x60(%rsp), %xmm0, %xmm0
vmovdqu %xmm0, (%r13,%r12)
vpxor 0x70(%rsp), %xmm0, %xmm0
vmovdqu %xmm0, (%r13,%r12)
movq %rax, %rbx
vmovq %xmm0, %rax
xorq (%r14,%r12), %rax
movq %rax, (%r13,%r12)
vpextrq $0x1, %xmm0, %rax
xorq 0x8(%r14,%r12), %rax
movq %rax, 0x8(%r13,%r12)
leaq 0x70(%rsp), %rdi
vmovaps (%rdi), %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x60(%rsp), %r15
movq %r15, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r15, %rdi
leaq 0x50(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1040
movq %rbp, %rdi
leaq 0x40(%rsp), %r15
movq %r15, %rsi
movq %rbp, %rdx
callq 0x1040
movq %r15, %rdi
leaq 0x30(%rsp), %rbp
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1040
movq %rbp, %rdi
leaq 0x20(%rsp), %r15
movq %r15, %rsi
movq %rbp, %rdx
callq 0x1040
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x1040
movq %rbx, %rax
vmovdqu (%r13,%r12), %xmm0
vpxor 0x20(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
movq (%rbx), %rbx
leaq 0x10(%r12), %r15
addq $0x20, %r12
cmpq %rbx, %r12
movq %r15, %r12
jbe 0x1ccd
andq $0xf, %rbx
movq %r14, %r12
je 0x1f1c
leaq (%r12,%r15), %r14
movl $0x10, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
xorl %esi, %esi
movq %rax, %r13
callq 0x1030
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1090
leaq 0x40(%rsp), %r14
leaq 0x50(%rsp), %rbp
leaq 0x60(%rsp), %rbx
leaq 0x70(%rsp), %rdi
vmovdqa -0x40(%rdi), %xmm0
vmovdqa -0x20(%rdi), %xmm1
vmovaps (%rdi), %xmm2
vpand -0x30(%rdi), %xmm1, %xmm1
vpternlogq $0x96, -0x10(%rdi), %xmm0, %xmm1
vmovdqa (%rdi), %xmm0
leaq 0x90(%rsp), %rax
vpternlogq $0x96, (%rax), %xmm1, %xmm0
vmovdqa %xmm0, 0x80(%rsp)
leaq 0xb0(%rsp), %rax
vmovdqa %xmm0, (%rax)
leaq 0x10(%rsp), %rax
vmovaps %xmm2, (%rax)
movq %rbx, %rsi
movq %rdi, %rdx
callq 0x1040
movq %rbx, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1040
movq %rbp, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
movq %r14, %rdx
callq 0x1040
leaq 0x20(%rsp), %rbx
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %rdi, %rdx
callq 0x1040
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1040
movq 0xa8(%rsp), %rdi
addq %r15, %rdi
movq (%r13), %rdx
andl $0xf, %edx
leaq 0xb0(%rsp), %r14
movq %r14, %rsi
callq 0x1090
movq (%r13), %rdx
andl $0xf, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1030
movq %r13, %rax
vmovdqa (%rbx), %xmm0
vmovdqa 0x80(%rsp), %xmm1
vpternlogq $0x96, (%r14), %xmm1, %xmm0
vmovdqa %xmm0, (%rbx)
movq (%rax), %rdi
leaq 0x10(%rsp), %rbx
leaq 0x20(%rsp), %r8
movq 0x150(%rsp), %rsi
movl $0x10, %edx
movq %rbx, %rcx
callq 0x10b0
movq 0x8(%rsp), %rax
vmovdqu (%r12,%rax), %xmm0
vpxor (%rbx), %xmm0, %xmm0
xorl %eax, %eax
vptest %xmm0, %xmm0
setne %al
negl %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop