// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/acorn128v2/ref/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 plaintextbyte, ciphertextbyte, ksbyte, mac[16];
    unsigned char state[293];
    unsigned char ca, cb;

    //initialization stage
    acorn128_initialization(k, npub, state);

    //process the associated data
    for (i = 0; i < adlen; i++)
    {
        acorn128_enc_onebyte(state, ad[i], &ciphertextbyte, &ksbyte, 0xff, 0xff);
    }

    for (i = 0; i < 256/8; i++)
    {
        if ( i == 0 ) plaintextbyte = 0x1;
        else plaintextbyte = 0;

        if ( i < 128/8)   ca = 0xff;
        else ca = 0;

        cb = 0xff;

        acorn128_enc_onebyte(state, plaintextbyte, &ciphertextbyte, &ksbyte, ca, cb);
    }


    //process the plaintext
    for (i = 0; i < mlen; i++)
    {
        acorn128_enc_onebyte(state, m[i], &(c[i]), &ksbyte, 0xff, 0 );
    }

    for (i = 0; i < 256/8; i++)
    {
        if (i == 0) plaintextbyte = 0x1;
        else plaintextbyte = 0;

        if ( i < 128/8)   ca = 0xff;
        else ca = 0;

        cb = 0;

        acorn128_enc_onebyte(state, plaintextbyte, &ciphertextbyte, &ksbyte, ca, cb);
    }

    //finalization stage, we assume that the tag length is a multiple of bytes
    acorn128_tag_generation(mlen, adlen, 16, mac, 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 $0x168, %rsp            # imm = 0x168
movq %r9, %r13
movq %r8, %rbp
movq %rcx, 0x28(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x10d0
testq %r13, %r13
je 0x1668
xorl %r12d, %r12d
leaq 0x40(%rsp), %r15
leaq 0xf(%rsp), %r14
leaq 0xe(%rsp), %rbx
movzbl (%rbp,%r12), %esi
movq %r15, %rdi
movq %r14, %rdx
movq %rbx, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x10e0
incq %r12
cmpq %r12, %r13
jne 0x1640
xorl %ebx, %ebx
leaq 0x40(%rsp), %r13
leaq 0xf(%rsp), %rbp
leaq 0xe(%rsp), %r15
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x10, %rbx
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r13, %rdi
movq %rbp, %rdx
movq %r15, %rcx
movl $0xff, %r9d
callq 0x10e0
incq %rbx
cmpq $0x20, %rbx
jne 0x1679
movq 0x28(%rsp), %rbp
testq %rbp, %rbp
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
je 0x16f4
xorl %ebx, %ebx
leaq 0x40(%rsp), %r15
leaq 0xe(%rsp), %r14
movzbl (%r13,%rbx), %esi
leaq (%r12,%rbx), %rdx
movq %r15, %rdi
movq %r14, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x10e0
incq %rbx
cmpq %rbx, %rbp
jne 0x16ce
xorl %ebx, %ebx
leaq 0x40(%rsp), %r15
leaq 0xf(%rsp), %r12
leaq 0xe(%rsp), %r13
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x10, %rbx
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %r9d, %r9d
callq 0x10e0
incq %rbx
cmpq $0x20, %rbx
jne 0x1705
leaq 0x30(%rsp), %rbx
leaq 0x40(%rsp), %r8
xorl %edx, %edx
movq %rbx, %rcx
callq 0x10a0
leaq 0x10(%rbp), %rax
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
vmovaps (%rbx), %xmm0
movq 0x10(%rsp), %rax
vmovups %xmm0, (%rax,%rbp)
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq