// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aegis128/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 aegis128_state[5];  

        //initialization stage  
        aegis128_initialization(k, npub, aegis128_state);

        //process the associated data  
        for (i = 0; (i+16) <= adlen; i += 16) {  
              aegis128_enc_aut_step(ad+i, ciphertextblock, aegis128_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);    
              aegis128_enc_aut_step(plaintextblock, ciphertextblock, aegis128_state);  
        }

        //encrypt the plaintext
        for (i = 0; (i+16) <= mlen; i += 16) {  
              aegis128_enc_aut_step(m+i, c+i, aegis128_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);  
              aegis128_enc_aut_step(plaintextblock, ciphertextblock, aegis128_state);
              memcpy(c+i,ciphertextblock, mlen & 0xf);  
        }     

        //finalization stage, we assume that the tag length is multiple of bytes
        aegis128_tag_generation(mlen,adlen, 16, mac, aegis128_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 %r9, 0x8(%rsp)
movq %r8, %rbp
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, 0x20(%rsp)
movq %rdi, 0x28(%rsp)
movq 0xf0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
leaq 0x40(%rsp), %r15
movq %r15, %rdx
callq 0x1060
pushq $0x10
popq %r12
leaq 0x30(%rsp), %r14
cmpq 0x8(%rsp), %r12
ja 0x1334
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1040
addq $0x10, %rbp
addq $0x10, %r12
jmp 0x1315
movq 0x8(%rsp), %r14
andq $0xf, %r14
je 0x1379
pushq $0x10
popq %rdx
subq %r14, %rdx
leaq (%rsp,%r14), %rdi
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10b0
leaq 0x30(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x1040
pushq $0x10
popq %r14
leaq 0x40(%rsp), %r15
movq 0x28(%rsp), %r12
movq %r12, %rbp
cmpq %rbx, %r14
ja 0x13ab
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1040
addq $0x10, %rbp
addq $0x10, %r13
addq $0x10, %r14
jmp 0x138a
movq %rbx, %r15
andq $0xf, %r15
je 0x13ff
pushq $0x10
popq %rdx
subq %r15, %rdx
leaq (%rsp,%r15), %rdi
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x10b0
leaq 0x30(%rsp), %r13
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x1040
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x10b0
pushq $0x10
popq %rdx
leaq 0x90(%rsp), %r14
leaq 0x40(%rsp), %r8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rcx
callq 0x10a0
leaq 0x10(%rbx), %rax
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
vmovaps (%r14), %xmm0
vmovups %xmm0, (%r12,%rbx)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq