// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rax
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
movq %r8, 0x90(%rsp)
movq %r9, 0x88(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq %rsp, %rdx
callq 0x1060
movq $0x0, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x10, %rax
cmpq 0x88(%rsp), %rax
ja 0x1dac
movq 0x90(%rsp), %rdi
addq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq 0x80(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x80(%rsp)
jmp 0x1d63
movq 0x88(%rsp), %rax
andq $0xf, %rax
cmpq $0x0, %rax
je 0x1e07
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1030
leaq 0x70(%rsp), %rdi
movq 0x90(%rsp), %rsi
addq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
andq $0xf, %rdx
callq 0x10b0
leaq 0x70(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq $0x0, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x10, %rax
cmpq 0x98(%rsp), %rax
ja 0x1e67
movq 0xa0(%rsp), %rdi
addq 0x80(%rsp), %rdi
movq 0xb0(%rsp), %rsi
addq 0x80(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq 0x80(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x80(%rsp)
jmp 0x1e13
movq 0x98(%rsp), %rax
andq $0xf, %rax
cmpq $0x0, %rax
je 0x1ee8
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1030
leaq 0x70(%rsp), %rdi
movq 0xa0(%rsp), %rsi
addq 0x80(%rsp), %rsi
movq 0x98(%rsp), %rdx
andq $0xf, %rdx
callq 0x10b0
leaq 0x70(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq 0xb0(%rsp), %rdi
addq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq 0x98(%rsp), %rdx
andq $0xf, %rdx
callq 0x10b0
movq 0x98(%rsp), %rdi
movq 0x88(%rsp), %rsi
leaq 0x50(%rsp), %rcx
movq %rsp, %r8
movl $0x10, %edx
callq 0x10a0
movq 0x98(%rsp), %rcx
addq $0x10, %rcx
movq 0xa8(%rsp), %rax
movq %rcx, (%rax)
movq 0xb0(%rsp), %rax
movq 0x98(%rsp), %rcx
movq 0x50(%rsp), %rdx
movq %rdx, (%rax,%rcx)
movq 0x58(%rsp), %rdx
movq %rdx, 0x8(%rax,%rcx)
xorl %eax, %eax
addq $0xb8, %rsp
retq
nop