// File: /workspace/github_c_makefile_star2_O3/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, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x18(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x1040
cmpq $0x4, %r12
jb 0x18e5
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 0x18bc
movq %r12, %r13
andq $-0x4, %r13
cmpq %r12, %r13
je 0x1928
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 0x1900
xorl %ebx, %ebx
leaq 0x30(%rsp), %r13
leaq 0xc(%rsp), %rbp
leaq 0x8(%rsp), %r15
movq 0x10(%rsp), %r14
cmpq $0x3, %rbx
ja 0x195e
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
movq %r13, %rdi
movq %rbp, %rdx
movq %r15, %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
jmp 0x196c
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0x10b0
incq %rbx
cmpq $0x8, %rbx
jne 0x193e
cmpq $0x4, %r14
movq 0x20(%rsp), %r12
jb 0x19cd
movq 0x10(%rsp), %rbx
shrq $0x2, %rbx
xorl %r14d, %r14d
leaq 0x30(%rsp), %r15
leaq 0x8(%rsp), %r13
movq 0x18(%rsp), %rbp
movl (%r12,%r14,4), %esi
movq %r15, %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 0x19a6
movq 0x10(%rsp), %r14
movq %r14, %rbx
andq $-0x4, %rbx
cmpq %r14, %rbx
je 0x1a1d
leaq 0x30(%rsp), %r15
leaq 0xc(%rsp), %r13
leaq 0x8(%rsp), %rbp
movzbl (%r12,%rbx), %esi
movq %r15, %rdi
movq %r13, %rdx
movq %rbp, %rcx
movl $0xff, %r8d
xorl %r9d, %r9d
callq 0x1070
movb 0xc(%rsp), %al
movq 0x18(%rsp), %rcx
movb %al, (%rcx,%rbx)
incq %rbx
cmpq %r14, %rbx
jb 0x19ed
xorl %ebx, %ebx
leaq 0x30(%rsp), %r12
leaq 0xc(%rsp), %r15
leaq 0x8(%rsp), %r13
cmpq $0x3, %rbx
ja 0x1a4e
xorl %esi, %esi
testq %rbx, %rbx
sete %sil
movq %r12, %rdi
movq %r15, %rdx
movq %r13, %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
jmp 0x1a5c
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x10b0
incq %rbx
cmpq $0x8, %rbx
jne 0x1a2e
leaq 0x70(%rsp), %rbx
leaq 0x30(%rsp), %r8
xorl %edx, %edx
movq %rbx, %rcx
callq 0x10a0
movq 0x10(%rsp), %rcx
leaq 0x10(%rcx), %rax
movq 0x28(%rsp), %rdx
movq %rax, (%rdx)
vmovaps (%rbx), %xmm0
movq 0x18(%rsp), %rax
vmovups %xmm0, (%rax,%rcx)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq