// File: /workspace/github_c_makefile_star2_O1/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
cmpq $0x4, %r12
jb 0x192a
movq %r12, %r13
shrq $0x2, %r13
xorl %r15d, %r15d
leaq 0xc(%rsp), %r14
leaq 0x8(%rsp), %rbx
movl (%rbp,%r15,4), %esi
leaq 0x30(%rsp), %rdi
movq %r14, %rdx
movq %rbx, %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x10b0
incq %r15
cmpq %r15, %r13
jne 0x1901
movq %r12, %r13
andq $-0x4, %r13
cmpq %r12, %r13
je 0x196d
leaq 0x30(%rsp), %r15
leaq 0xc(%rsp), %r14
leaq 0x8(%rsp), %rbx
movzbl (%rbp,%r13), %esi
movq %r15, %rdi
movq %r14, %rdx
movq %rbx, %rcx
movl $0xff, %r8d
movl $0xff, %r9d
callq 0x1070
incq %r13
cmpq %r12, %r13
jb 0x1945
xorl %ebx, %ebx
leaq 0x30(%rsp), %r13
leaq 0xc(%rsp), %rbp
leaq 0x8(%rsp), %r15
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x4, %rbx
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r13, %rdi
movq %rbp, %rdx
movq %r15, %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x10b0
incq %rbx
cmpq $0x8, %rbx
jne 0x197e
movq 0x18(%rsp), %r12
cmpq $0x4, %r12
movq 0x20(%rsp), %r15
jb 0x19fe
movq %r12, %rbx
shrq $0x2, %rbx
xorl %r14d, %r14d
leaq 0x8(%rsp), %r13
movq 0x10(%rsp), %rbp
movl (%r15,%r14,4), %esi
leaq 0x30(%rsp), %rdi
movq %rbp, %rdx
movq %r13, %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x10b0
incq %r14
addq $0x4, %rbp
cmpq %r14, %rbx
jne 0x19d5
movq %r12, %rbx
andq $-0x4, %rbx
cmpq %r12, %rbx
movq 0x10(%rsp), %r14
je 0x1a47
leaq 0xc(%rsp), %r13
leaq 0x8(%rsp), %rbp
movzbl (%r15,%rbx), %esi
leaq 0x30(%rsp), %rdi
movq %r13, %rdx
movq %rbp, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x1070
movb 0xc(%rsp), %al
movb %al, (%r14,%rbx)
incq %rbx
cmpq %r12, %rbx
jb 0x1a19
xorl %ebx, %ebx
leaq 0x30(%rsp), %r15
leaq 0xc(%rsp), %r12
leaq 0x8(%rsp), %r13
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
cmpq $0x4, %rbx
movl $0x0, %r8d
sbbl %r8d, %r8d
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %r9d, %r9d
callq 0x10b0
incq %rbx
cmpq $0x8, %rbx
jne 0x1a58
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, (%r14,%rcx)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq