// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis256/ref/encrypt.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
	unsigned char *c,unsigned long long *clen,
	const unsigned char *m,unsigned long long mlen,
	const unsigned char *ad,unsigned long long adlen,
	const unsigned char *nsec,
	const unsigned char *npub,
	const unsigned char *k
	)
{
        unsigned long i;
        unsigned char plaintextblock[16], ciphertextblock[16], mac[16];
        unsigned char aegis256_state[96];

        //initialization stage
        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);
        }

        //encrypt the plaintext
        for (i = 0; (i+16) <= mlen; i += 16) {
              aegis256_enc_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(plaintextblock, 0, 16);
              memcpy(plaintextblock, m+i, mlen & 0xf);
              aegis256_enc_aut_step(plaintextblock, ciphertextblock, aegis256_state);
              memcpy(c+i,ciphertextblock, mlen & 0xf);
        }

        //finalization stage, we assume that the tag length is a multiple of bytes
        aegis256_tag_generation(mlen, adlen, 16, mac, aegis256_state);
        *clen = mlen + 16;
        memcpy(c+mlen, mac, 16);

        return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, (%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, 0xd0(%rsp)
movq %rdi, 0x98(%rsp)
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x1080
cmpq $0x10, %rbx
movq %rbx, 0x8(%rsp)
jae 0x13c7
xorl %eax, %eax
jmp 0x14ec
xorl %eax, %eax
movq %r14, 0xa0(%rsp)
movq 0xa0(%rsp), %r15
movq %rax, %rbx
movq 0x30(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x50(%rsp), %r12
movq 0x60(%rsp), %rbp
movq 0x70(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq (%r15,%rbx), %rax
movq %rax, 0xf0(%rsp)
leaq 0x70(%rsp), %rdi
vmovaps (%rdi), %xmm0
vmovaps %xmm0, 0x10(%rsp)
leaq 0x60(%rsp), %r14
movq %r14, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x50(%rsp), %r13
movq %r13, %rsi
movq %r14, %rdx
callq 0x1040
movq %r13, %rdi
leaq 0x40(%rsp), %r14
movq %r14, %rsi
movq %r13, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x30(%rsp), %r13
movq %r13, %rsi
movq %r14, %rdx
callq 0x1040
movq %r13, %rdi
leaq 0x20(%rsp), %r14
movq %r14, %rsi
movq %r13, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x1040
vmovups (%r15,%rbx), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
leaq 0x10(%rbx), %rax
addq $0x20, %rbx
cmpq 0x8(%rsp), %rbx
movq 0x8(%rsp), %rbx
jbe 0x13d9
andq 0xd8(%rsp), %r12
xorq 0xe8(%rsp), %rbp
xorq %r12, %rbp
movq 0xf0(%rsp), %r12
xorq 0xe0(%rsp), %r12
xorq %rbp, %r12
movq 0xa0(%rsp), %r14
movq %r12, 0xb0(%rsp)
andq $0xf, %rbx
je 0x15f6
addq %rax, %r14
movl $0x10, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0xc0, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
movq %r14, %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, 0xa0(%rsp)
vpternlogq $0x96, (%rdi), %xmm0, %xmm1
leaq 0x60(%rsp), %r13
vmovdqa %xmm1, 0xb0(%rsp)
leaq 0x10(%rsp), %r15
vmovaps %xmm2, (%r15)
movq %r13, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r13, %rdi
movq %r12, %rsi
movq %r13, %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
vmovaps 0xa0(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
vmovaps %xmm0, (%r14)
movq (%rsp), %rax
cmpq $0x10, %rax
jae 0x1618
xorl %r12d, %r12d
movq 0x98(%rsp), %rbx
movq 0x80(%rsp), %r14
jmp 0x1722
leaq 0x70(%rsp), %r13
xorl %ebp, %ebp
leaq 0x20(%rsp), %r15
movq 0x98(%rsp), %rbx
movq 0x80(%rsp), %r14
vmovdqa 0x30(%rsp), %xmm0
vmovdqa 0x50(%rsp), %xmm1
vpternlogq $0x6c, 0x40(%rsp), %xmm0, %xmm1
vmovdqa 0x60(%rsp), %xmm0
vpternlogq $0x96, 0x70(%rsp), %xmm1, %xmm0
vmovdqu %xmm0, (%rbx,%rbp)
vmovq %xmm0, %rax
xorq (%r14,%rbp), %rax
movq %rax, (%rbx,%rbp)
vpextrq $0x1, %xmm0, %rax
xorq 0x8(%r14,%rbp), %rax
movq %rax, 0x8(%rbx,%rbp)
vmovaps (%r13), %xmm0
vmovaps %xmm0, 0x10(%rsp)
movq %r13, %rdi
leaq 0x60(%rsp), %r12
movq %r12, %rsi
movq %r13, %rdx
callq 0x1040
movq %r12, %rdi
leaq 0x50(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x40(%rsp), %r12
movq %r12, %rsi
movq %r14, %rdx
callq 0x1040
movq %r12, %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x1040
movq %r14, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1040
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
movq %r15, %rdx
callq 0x1040
movq 0x80(%rsp), %r14
movq (%rsp), %rax
vmovups (%r14,%rbp), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x20(%rsp)
leaq 0x10(%rbp), %r12
addq $0x20, %rbp
cmpq %rax, %rbp
movq %r12, %rbp
jbe 0x1634
movq %rax, %r15
andq $0xf, %r15
je 0x1842
addq %r12, %r14
movl $0x10, %edx
subq %r15, %rdx
leaq (%rsp,%r15), %rdi
addq $0xc0, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1090
leaq 0x50(%rsp), %rbp
leaq 0x60(%rsp), %r14
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 (%r13), %xmm0
vmovdqa %xmm0, 0x80(%rsp)
vpternlogq $0x96, (%rdi), %xmm0, %xmm1
leaq 0xb0(%rsp), %rax
vmovdqa %xmm1, (%rax)
leaq 0x10(%rsp), %r13
vmovaps %xmm2, (%r13)
movq %r14, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r14, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1040
movq %rbp, %rdi
leaq 0x40(%rsp), %r14
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x30(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1040
leaq 0x20(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1040
movq %r14, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1040
vmovaps 0x80(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
vmovaps %xmm0, (%r14)
addq %rbx, %r12
movq %r12, %rdi
leaq 0xb0(%rsp), %rsi
movq %r15, %rdx
callq 0x1090
leaq 0x10(%rsp), %r15
leaq 0x20(%rsp), %r8
movq (%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl $0x10, %edx
movq %r15, %rcx
callq 0x10b0
leaq 0x10(%r14), %rax
movq 0xd0(%rsp), %rcx
movq %rax, (%rcx)
vmovaps (%r15), %xmm0
vmovups %xmm0, (%rbx,%r14)
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq