// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnia/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[32], ciphertextblock[32], mac[16];
        __m128i aegis128L_state[8];

        //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 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 multiple of bytes
        aegis128L_tag_generation(mlen,adlen, 16, mac, aegis128L_state);
        *clen = mlen + 16;
        memcpy(c+mlen, mac, 16); 

        return 0; 
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %rax
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq %rcx, 0xe8(%rsp)
movq %r8, 0xe0(%rsp)
movq %r9, 0xd8(%rsp)
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq $0x0, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
addq $0x20, %rax
cmpq 0xd8(%rsp), %rax
ja 0x249f
movq 0xe0(%rsp), %rdi
addq 0xd0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq %rsp, %rdx
callq 0x1060
movq 0xd0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xd0(%rsp)
jmp 0x2453
movq 0xd8(%rsp), %rax
andq $0x1f, %rax
cmpq $0x0, %rax
je 0x2506
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1030
leaq 0xb0(%rsp), %rdi
movq 0xe0(%rsp), %rsi
addq 0xd0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0xb0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq %rsp, %rdx
callq 0x1060
movq $0x0, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
addq $0x20, %rax
cmpq 0xe8(%rsp), %rax
ja 0x2566
movq 0xf0(%rsp), %rdi
addq 0xd0(%rsp), %rdi
movq 0x100(%rsp), %rsi
addq 0xd0(%rsp), %rsi
movq %rsp, %rdx
callq 0x1060
movq 0xd0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xd0(%rsp)
jmp 0x2512
movq 0xe8(%rsp), %rax
andq $0x1f, %rax
cmpq $0x0, %rax
je 0x25f6
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1030
leaq 0xb0(%rsp), %rdi
movq 0xf0(%rsp), %rsi
addq 0xd0(%rsp), %rsi
movq 0xe8(%rsp), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0xb0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq %rsp, %rdx
callq 0x1060
movq 0x100(%rsp), %rdi
addq 0xd0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq 0xe8(%rsp), %rdx
andq $0x1f, %rdx
callq 0x10b0
movq 0xe8(%rsp), %rdi
movq 0xd8(%rsp), %rsi
leaq 0x80(%rsp), %rcx
movq %rsp, %r8
movl $0x10, %edx
callq 0x1070
movq 0xe8(%rsp), %rcx
addq $0x10, %rcx
movq 0xf8(%rsp), %rax
movq %rcx, (%rax)
movq 0x100(%rsp), %rax
movq 0xe8(%rsp), %rcx
movq 0x80(%rsp), %rdx
movq %rdx, (%rax,%rcx)
movq 0x88(%rsp), %rdx
movq %rdx, 0x8(%rax,%rcx)
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopw %cs:(%rax,%rax)