// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aegis128l/ref/ecrypt.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[32], ciphertextblock[32], mac[16];
        unsigned char aegis128L_state[128];

        //initialization stage
        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);
        }

        //encrypt the plaintext
        for (i = 0; (i+32) <= mlen; i += 32) {
              aegis128L_enc_aut_step(m+i, c+i, aegis128L_state);
        }

        // Deal with the partial block
        // In this program, we assume that the message length is a multiple of bytes.
        if (  (mlen & 0x1f) != 0 )  {
              memset(plaintextblock, 0, 32);
              memcpy(plaintextblock, m+i, mlen & 0x1f);
              aegis128L_enc_aut_step(plaintextblock, ciphertextblock, aegis128L_state);
              memcpy(c+i,ciphertextblock, mlen & 0x1f);
        }

        //finalization stage, we assume that the tag length is a multiple of bytes
        aegis128L_tag_generation(mlen, adlen, 16, mac, aegis128L_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, %r14
movq %r8, %rbp
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x1040
cmpq $0x20, %r14
movq %r14, 0x8(%rsp)
jae 0x1612
movq %r14, %rcx
xorl %eax, %eax
jmp 0x1644
xorl %r12d, %r12d
leaq 0x40(%rsp), %r15
leaq 0x70(%rsp), %r14
leaq (%r12,%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1070
movq 0x8(%rsp), %rcx
leaq 0x20(%r12), %rax
addq $0x40, %r12
cmpq %rcx, %r12
movq %rax, %r12
jbe 0x161f
movq %rcx, %r15
andq $0x1f, %r15
je 0x168c
addq %rax, %rbp
movl $0x20, %edx
subq %r15, %rdx
leaq (%rsp,%r15), %rdi
addq $0x20, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x10c0
leaq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movq %r14, %rdi
callq 0x1070
cmpq $0x20, %rbx
movq 0x18(%rsp), %r12
jae 0x169b
xorl %ebp, %ebp
jmp 0x16c3
xorl %r14d, %r14d
leaq 0x70(%rsp), %r15
leaq (%r14,%r13), %rdi
leaq (%r12,%r14), %rsi
movq %r15, %rdx
callq 0x1070
leaq 0x20(%r14), %rbp
addq $0x40, %r14
cmpq %rbx, %r14
movq %rbp, %r14
jbe 0x16a3
movq %rbx, %r15
andq $0x1f, %r15
je 0x171f
addq %rbp, %r13
movl $0x20, %edx
subq %r15, %rdx
leaq (%rsp,%r15), %rdi
addq $0x20, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x10c0
leaq 0x40(%rsp), %r13
leaq 0x70(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x1070
addq %r12, %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x10c0
leaq 0x60(%rsp), %r14
leaq 0x70(%rsp), %r8
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl $0x10, %edx
movq %r14, %rcx
callq 0x1080
leaq 0x10(%rbx), %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
vmovaps (%r14), %xmm0
vmovups %xmm0, (%r12,%rbx)
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq