// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/acorn128/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 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 < 512/8; i++)
    {
        if ( i == 0 ) plaintextbyte = 1;
        else plaintextbyte = 0;

        if ( i < 256/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 < 512/8; i++)
    {
        if ( i == 0 ) plaintextbyte = 1;
        else plaintextbyte = 0;

        if ( i < 256/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, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
leaq 0x40(%rsp), %r15
movq %r15, %rdx
callq 0x10c0
xorl %r12d, %r12d
leaq 0xf(%rsp), %r14
leaq 0xe(%rsp), %rbx
cmpq %r12, %r13
je 0x168d
movzbl (%rbp,%r12), %esi
movq %r15, %rdi
movq %r14, %rdx
movq %rbx, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x10d0
incq %r12
jmp 0x1663
xorl %ebx, %ebx
leaq 0x40(%rsp), %r13
leaq 0xf(%rsp), %rbp
leaq 0xe(%rsp), %r15
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %r12
cmpq $0x40, %rbx
je 0x16df
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x20, %rbx
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r13, %rdi
movq %rbp, %rdx
movq %r15, %rcx
movl $0xff, %r9d
callq 0x10d0
incq %rbx
jmp 0x16a8
xorl %ebx, %ebx
leaq 0x40(%rsp), %r15
leaq 0xe(%rsp), %r13
movq 0x20(%rsp), %rbp
cmpq %rbx, %r14
je 0x1717
movzbl (%r12,%rbx), %esi
leaq (%rbx,%rbp), %rdx
movq %r15, %rdi
movq %r13, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x10d0
incq %rbx
jmp 0x16f0
xorl %ebx, %ebx
leaq 0x40(%rsp), %r12
leaq 0xf(%rsp), %r15
leaq 0xe(%rsp), %r13
cmpq $0x40, %rbx
je 0x175c
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x20, %rbx
movl $0x0, %eax
sbbl %eax, %eax
movzbl %al, %r8d
movq %r12, %rdi
movq %r15, %rdx
movq %r13, %rcx
xorl %r9d, %r9d
callq 0x10d0
incq %rbx
jmp 0x1728
leaq 0x30(%rsp), %rbx
leaq 0x40(%rsp), %r8
xorl %edx, %edx
movq %rbx, %rcx
callq 0x10a0
leaq 0x10(%r14), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
vmovaps (%rbx), %xmm0
vmovups %xmm0, (%rbp,%r14)
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq