// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis256/aesni/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 long i;
        unsigned char plaintextblock[16], ciphertextblock[16], mac[16];
        __m128i aegis256_state[6];

        //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 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 $0xa8, %rsp
movq 0xe8(%rsp), %rax
movq 0xf0(%rsp), %r10
vmovdqa (%r10), %xmm2
vmovdqa 0x10(%r10), %xmm3
vmovdqa (%rax), %xmm0
movq %r9, %r12
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, 0x68(%rsp)
movq %rdi, %r14
vmovdqa 0x10(%rax), %xmm1
vmovdqa 0xced(%rip), %xmm6      # 0x2000
vpxor %xmm6, %xmm2, %xmm5
vmovdqa 0xcf1(%rip), %xmm7      # 0x2010
vpxor %xmm7, %xmm3, %xmm10
vpxor %xmm2, %xmm0, %xmm2
vpxor %xmm3, %xmm1, %xmm3
movl $0x8, %eax
vmovdqa %xmm3, %xmm8
vmovdqa %xmm2, %xmm9
vaesenc %xmm10, %xmm5, %xmm4
vaesenc %xmm5, %xmm6, %xmm5
vaesenc %xmm6, %xmm7, %xmm6
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm10, %xmm9
vpxor %xmm0, %xmm2, %xmm2
vpxor %xmm2, %xmm9, %xmm9
vaesenc %xmm4, %xmm5, %xmm10
vaesenc %xmm5, %xmm6, %xmm5
vaesenc %xmm6, %xmm7, %xmm6
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm4, %xmm4
vpxor %xmm1, %xmm3, %xmm3
vpxor %xmm3, %xmm4, %xmm9
decl %eax
jne 0x1338
cmpq $0x10, %r12
jae 0x1392
xorl %ecx, %ecx
jmp 0x13d0
xorl %eax, %eax
vmovdqa %xmm10, %xmm0
vaesenc %xmm0, %xmm5, %xmm10
vaesenc %xmm5, %xmm6, %xmm5
vaesenc %xmm6, %xmm7, %xmm6
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm0, %xmm0
vpxor (%r8,%rax), %xmm0, %xmm9
leaq 0x10(%rax), %rcx
addq $0x20, %rax
vmovdqa %xmm10, %xmm0
cmpq %r12, %rax
movq %rcx, %rax
jbe 0x1398
movq %r12, %rdx
andq $0xf, %rdx
je 0x1460
vpxor %xmm0, %xmm0, %xmm0
leaq 0x70(%rsp), %rbx
vmovdqa %xmm0, (%rbx)
addq %rcx, %r8
movq %rbx, %rdi
movq %r8, %rsi
vmovdqa %xmm7, 0x30(%rsp)
vmovdqa %xmm5, 0x20(%rsp)
vmovdqa %xmm6, 0x10(%rsp)
vmovdqa %xmm8, (%rsp)
vmovdqa %xmm9, 0x40(%rsp)
vmovdqa %xmm10, 0x50(%rsp)
callq 0x1070
vmovdqa (%rsp), %xmm8
vmovdqa 0x10(%rsp), %xmm6
vmovdqa 0x20(%rsp), %xmm5
vmovdqa 0x30(%rsp), %xmm7
vmovdqa 0x40(%rsp), %xmm2
vmovdqa 0x50(%rsp), %xmm1
vaesenc %xmm2, %xmm1, %xmm0
vaesenc %xmm1, %xmm5, %xmm10
vaesenc %xmm5, %xmm6, %xmm5
vaesenc %xmm6, %xmm7, %xmm6
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm2, %xmm8
vpxor (%rbx), %xmm0, %xmm9
cmpq $0x10, %r15
jae 0x146a
xorl %ebp, %ebp
jmp 0x14c5
xorl %eax, %eax
vmovdqa %xmm10, %xmm0
vmovdqa (%r13,%rax), %xmm1
vpand %xmm6, %xmm7, %xmm2
vpternlogq $0x96, %xmm5, %xmm0, %xmm2
vpternlogq $0x96, %xmm8, %xmm1, %xmm2
vmovdqa %xmm2, (%r14,%rax)
vaesenc %xmm0, %xmm5, %xmm10
vaesenc %xmm5, %xmm6, %xmm5
vaesenc %xmm6, %xmm7, %xmm6
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm0, %xmm0
vpxor %xmm1, %xmm0, %xmm9
leaq 0x10(%rax), %rbp
addq $0x20, %rax
vmovdqa %xmm10, %xmm0
cmpq %r15, %rax
movq %rbp, %rax
jbe 0x1470
movq %r15, %rbx
andq $0xf, %rbx
je 0x15fc
vpxor %xmm0, %xmm0, %xmm0
leaq 0x70(%rsp), %rdi
vmovdqa %xmm0, (%rdi)
addq %rbp, %r13
movq %r13, %rsi
movq %rbx, %rdx
vmovdqa %xmm7, 0x30(%rsp)
vmovdqa %xmm5, 0x20(%rsp)
vmovdqa %xmm6, 0x10(%rsp)
vmovdqa %xmm8, (%rsp)
vmovdqa %xmm9, 0x40(%rsp)
vmovdqa %xmm10, 0x50(%rsp)
callq 0x1070
leaq 0x70(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa 0x10(%rsp), %xmm1
vpand 0x30(%rsp), %xmm1, %xmm1
vmovdqa 0x50(%rsp), %xmm2
vpternlogq $0x96, 0x20(%rsp), %xmm2, %xmm1
vpternlogq $0x96, (%rsp), %xmm0, %xmm1
leaq 0x90(%rsp), %rsi
vmovdqa %xmm1, (%rsi)
vmovdqa 0x20(%rsp), %xmm1
vaesenc %xmm2, %xmm1, %xmm1
vmovdqa %xmm2, %xmm3
vmovdqa %xmm1, 0x80(%rsp)
vmovdqa 0x20(%rsp), %xmm1
vmovdqa 0x10(%rsp), %xmm2
vaesenc %xmm1, %xmm2, %xmm1
vmovdqa %xmm1, 0x20(%rsp)
vmovdqa 0x30(%rsp), %xmm1
vmovdqa 0x10(%rsp), %xmm2
vaesenc %xmm2, %xmm1, %xmm2
vmovdqa %xmm2, 0x10(%rsp)
vmovdqa 0x30(%rsp), %xmm1
vmovdqa (%rsp), %xmm2
vaesenc %xmm1, %xmm2, %xmm1
vmovdqa %xmm1, 0x30(%rsp)
vmovdqa (%rsp), %xmm1
vmovdqa 0x40(%rsp), %xmm2
vaesenc %xmm1, %xmm2, %xmm1
vmovdqa %xmm1, (%rsp)
vaesenc %xmm2, %xmm3, %xmm1
vpxor %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x40(%rsp)
addq %r14, %rbp
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x1070
vmovdqa 0x40(%rsp), %xmm9
vmovdqa (%rsp), %xmm8
vmovdqa 0x10(%rsp), %xmm6
vmovdqa 0x20(%rsp), %xmm5
vmovdqa 0x30(%rsp), %xmm7
vmovdqa 0x80(%rsp), %xmm10
vmovq %r15, %xmm0
vmovq %r12, %xmm1
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
vpsllq $0x3, %xmm0, %xmm0
movl $0x7, %eax
vmovdqa %xmm6, %xmm1
vaesenc %xmm10, %xmm5, %xmm2
vaesenc %xmm5, %xmm1, %xmm5
vaesenc %xmm1, %xmm7, %xmm1
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm10, %xmm9
vpternlogq $0x96, %xmm6, %xmm0, %xmm9
vmovdqa %xmm2, %xmm10
decl %eax
jne 0x1618
vpxor %xmm2, %xmm5, %xmm0
vpternlogq $0x96, %xmm1, %xmm0, %xmm7
vpternlogq $0x96, %xmm8, %xmm7, %xmm9
leaq 0x10(%r15), %rax
movq 0x68(%rsp), %rcx
movq %rax, (%rcx)
vmovdqu %xmm9, (%r14,%r15)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq