// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/acorn128v2/opt/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 mac[16];
    unsigned int plaintextword, ciphertextword, ksword;

    unsigned long long state[7];
    unsigned int ca, cb;

    //initialization stage
    acorn128_initialization_32bitversion(k, npub, state);

    //process the associated data
    ca = 0xffffffff;
    cb = 0xffffffff;
    for (i = 0; i < adlen/4; i = i+1)
    {
        encrypt_32bits(state, ((unsigned int*)ad)[i], &ciphertextword, &ksword, ca, cb);
    }

    // if adlen is not a multiple of 4, we process the remaining bytes
    for (i = adlen & 0xfffffffffffffffcULL; i < adlen; i++)
    {
        ca = 0xff;
        cb = 0xff;
        plaintextword = ad[i];
        encrypt_8bits(state, plaintextword, &ciphertextword, &ksword, ca, cb);
    }

    //256bits padding after the associated data
    for (i = 0; i < 256/32; i++)
    {
        if (i == 0) plaintextword = 0x1;
        else plaintextword = 0;

        if ( i < 128/32) ca = 0xffffffff;
        else ca = 0;

        cb = 0xffffffff;

        encrypt_32bits(state, plaintextword, &ciphertextword, &ksword, ca, cb);
    }

    //process the plaintext
    ca = 0xffffffff;
    cb = 0;
    for (i = 0; i < mlen/4; i=i+1)
    {
        encrypt_32bits(state, ((unsigned int*)m)[i], &(((unsigned int*)c)[i]), &ksword, ca, cb);  //&c[i], &ksword, ca, cb);
    }

    //if mlen is not a multiple of 32 bits, we process the remaining bytes.
    for (i = mlen & 0xfffffffffffffffcULL; i < mlen; i++)
    {
        ca = 0xff;
        cb = 0x0;
        plaintextword = m[i];
        encrypt_8bits(state, plaintextword, &ciphertextword, &ksword, ca, cb);
        c[i] = ciphertextword;
    }

    //256 bits padding after the plaintext
    for (i = 0; i < 256/32; i++)
    {
        if ( i == 0 ) plaintextword = 0x1;
        else plaintextword = 0;

        if ( i < 128/32)   ca = 0xffffffff;
        else ca = 0;

        cb = 0;

        encrypt_32bits(state, plaintextword, &ciphertextword, &ksword, ca, cb);
    }

    //finalization stage, we assume that the tag length is a multiple of bytes
    acorn128_tag_generation_32bits_version(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 $0x88, %rsp
movq %r9, %r12
movq %r8, %rbp
movq %rcx, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x10(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x1040
movq %r12, %r13
shrq $0x2, %r13
xorl %r14d, %r14d
leaq 0x8(%rsp), %rbx
pushq $-0x1
popq %r15
cmpq %r14, %r13
je 0x18cb
movl (%rbp,%r14,4), %esi
leaq 0x30(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
movl %r15d, %r8d
movl %r15d, %r9d
callq 0x10b0
incq %r14
jmp 0x18a4
movq %r12, %r13
andq $-0x4, %r13
leaq 0x30(%rsp), %r14
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %rbx
cmpq %r12, %r13
jae 0x190b
movzbl (%rbp,%r13), %esi
movq %r14, %rdi
movq %r15, %rdx
movq %rbx, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x1070
incq %r13
jmp 0x18e1
xorl %ebx, %ebx
leaq 0x30(%rsp), %r15
leaq 0xc(%rsp), %r13
leaq 0x8(%rsp), %rbp
pushq $-0x1
popq %r14
movq 0x18(%rsp), %r12
cmpq $0x8, %rbx
je 0x1957
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x4, %rbx
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r15, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movl %r14d, %r9d
callq 0x10b0
incq %rbx
jmp 0x1925
movq %r12, %rbx
shrq $0x2, %rbx
xorl %r14d, %r14d
leaq 0x8(%rsp), %r15
pushq $-0x1
popq %rbp
movq 0x10(%rsp), %r13
cmpq %r14, %rbx
je 0x199b
movq 0x20(%rsp), %rax
movl (%rax,%r14,4), %esi
leaq 0x30(%rsp), %rdi
movq %r13, %rdx
movq %r15, %rcx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x10b0
incq %r14
addq $0x4, %r13
jmp 0x196e
movq %r12, %rbx
andq $-0x4, %rbx
leaq 0x30(%rsp), %r14
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %r13
movq 0x10(%rsp), %rbp
cmpq %r12, %rbx
jae 0x19e8
movq 0x20(%rsp), %rax
movzbl (%rax,%rbx), %esi
movq %r14, %rdi
movq %r15, %rdx
movq %r13, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x1070
movb 0xc(%rsp), %al
movb %al, (%rbp,%rbx)
incq %rbx
jmp 0x19b6
xorl %ebx, %ebx
leaq 0x30(%rsp), %r15
leaq 0xc(%rsp), %r12
leaq 0x8(%rsp), %r14
cmpq $0x8, %rbx
je 0x1a2b
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x4, %rbx
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r15, %rdi
movq %r12, %rdx
movq %r14, %rcx
xorl %r9d, %r9d
callq 0x10b0
incq %rbx
jmp 0x19f9
leaq 0x70(%rsp), %rbx
leaq 0x30(%rsp), %r8
xorl %edx, %edx
movq %rbx, %rcx
callq 0x10a0
movq 0x18(%rsp), %rcx
leaq 0x10(%rcx), %rax
movq 0x28(%rsp), %rdx
movq %rax, (%rdx)
vmovaps (%rbx), %xmm0
vmovups %xmm0, (%rbp,%rcx)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq