// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xa8, %rsp
movq 0xc0(%rsp), %rax
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rax
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movq %rcx, 0x88(%rsp)
movq %r8, 0x80(%rsp)
movq %r9, 0x78(%rsp)
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1040
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq $0x0, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
shrq $0x2, %rcx
cmpq %rcx, %rax
jae 0x2490
leaq 0x10(%rsp), %rdi
movq 0x80(%rsp), %rax
movq 0x70(%rsp), %rcx
movl (%rax,%rcx,4), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x58(%rsp), %rdx
leaq 0x54(%rsp), %rcx
callq 0x10b0
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x243f
movq 0x78(%rsp), %rax
andq $-0x4, %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
jae 0x2501
movl $0xff, 0xc(%rsp)
movl $0xff, 0x8(%rsp)
movq 0x80(%rsp), %rax
movq 0x70(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x5c(%rsp)
leaq 0x10(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x58(%rsp), %rdx
leaq 0x54(%rsp), %rcx
callq 0x1070
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x249e
movq $0x0, 0x70(%rsp)
cmpq $0x8, 0x70(%rsp)
jae 0x2580
cmpq $0x0, 0x70(%rsp)
jne 0x2524
movl $0x1, 0x5c(%rsp)
jmp 0x252c
movl $0x0, 0x5c(%rsp)
cmpq $0x4, 0x70(%rsp)
jae 0x253e
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x2546
movl $0x0, 0xc(%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
leaq 0x10(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x58(%rsp), %rdx
leaq 0x54(%rsp), %rcx
callq 0x10b0
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x250a
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0x0, 0x8(%rsp)
movq $0x0, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq 0x88(%rsp), %rcx
shrq $0x2, %rcx
cmpq %rcx, %rax
jae 0x25fc
leaq 0x10(%rsp), %rdi
movq 0x90(%rsp), %rax
movq 0x70(%rsp), %rcx
movl (%rax,%rcx,4), %esi
movq 0xa0(%rsp), %rdx
movq 0x70(%rsp), %rax
shlq $0x2, %rax
addq %rax, %rdx
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x54(%rsp), %rcx
callq 0x10b0
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x2599
movq 0x88(%rsp), %rax
andq $-0x4, %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
cmpq 0x88(%rsp), %rax
jae 0x2689
movl $0xff, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq 0x70(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x5c(%rsp)
leaq 0x10(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x58(%rsp), %rdx
leaq 0x54(%rsp), %rcx
callq 0x1070
movl 0x58(%rsp), %eax
movb %al, %dl
movq 0xa0(%rsp), %rax
movq 0x70(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x260d
movq $0x0, 0x70(%rsp)
cmpq $0x8, 0x70(%rsp)
jae 0x2708
cmpq $0x0, 0x70(%rsp)
jne 0x26ac
movl $0x1, 0x5c(%rsp)
jmp 0x26b4
movl $0x0, 0x5c(%rsp)
cmpq $0x4, 0x70(%rsp)
jae 0x26c6
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x26ce
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0xc(%rsp), %r8d
movl 0x8(%rsp), %r9d
leaq 0x58(%rsp), %rdx
leaq 0x54(%rsp), %rcx
callq 0x10b0
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x2692
movq 0x88(%rsp), %rdi
movq 0x78(%rsp), %rsi
leaq 0x60(%rsp), %rcx
leaq 0x10(%rsp), %r8
movl $0x10, %edx
callq 0x10a0
movq 0x88(%rsp), %rcx
addq $0x10, %rcx
movq 0x98(%rsp), %rax
movq %rcx, (%rax)
movq 0xa0(%rsp), %rax
movq 0x88(%rsp), %rcx
movq 0x60(%rsp), %rdx
movq %rdx, (%rax,%rcx)
movq 0x68(%rsp), %rdx
movq %rdx, 0x8(%rax,%rcx)
xorl %eax, %eax
addq $0xa8, %rsp
retq
nopl (%rax)