// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/joltikneq9696v13/ref/joltik.c
// Function: joltik_aead_encrypt
void joltik_aead_encrypt(const uint8_t *ass_data, size_t ass_data_len,
                         const uint8_t *message, size_t m_len,
                         const uint8_t *key,
                         const uint8_t *nonce,
                         uint8_t *ciphertext, size_t *c_len)
{

    uint64_t i;
    uint64_t j;
    uint8_t tweak[12];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[8];
    uint8_t last_block[8];
    uint8_t Checksum[8];
    uint8_t Final[8];
    uint8_t zero_block[8];
    uint8_t Pad[8];
    uint8_t temp[8];

    /* Fill the tweak from nonce */
    memset(tweak, 0, sizeof(tweak));

    /* Fill the key(s) in the tweakey state */
    memcpy(tweakey, key, 12);

    /* Associated data */
    memset(Auth, 0, 8);

    if(ass_data_len) {
            set_stage_in_tweak(tweak, MSB_AD);

            /* For each full input blocks */
            i=0;
            while (8*(i+1) <= ass_data_len) {

                /* Encrypt the current block */
                set_block_number_in_tweak(tweak, i);
                set_tweak_in_tweakey(tweakey, tweak);
                aesTweakEncrypt(TWEAKEY_STATE_SIZE, ass_data+8*i, tweakey, temp);

                /* Update Auth value */
                xor_values(Auth, temp);

                /* Go on with the next block */
                i++;
            }

            /* Last block if incomplete */
            if ( ass_data_len > 8*i ) {

                /* Prepare the last padded block */
                memset(last_block, 0, 8);
                memcpy(last_block, ass_data+8*i, ass_data_len-8*i);
                last_block[ass_data_len-8*i]=0x80;

                /* Encrypt the last block */
                set_stage_in_tweak(tweak, MSB_AD_LAST);
                set_block_number_in_tweak(tweak, i);
                set_tweak_in_tweakey(tweakey, tweak);
                aesTweakEncrypt(TWEAKEY_STATE_SIZE, last_block, tweakey, temp);

                /* Update the Auth value */
                xor_values(Auth, temp);
            }
    }/* if ass_data_len>0 */

    /* Message */
    set_nonce_in_tweak(tweak, nonce);

    memset(Checksum, 0, 8);
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (8*(i+1) <= m_len) {
        xor_values(Checksum, message+8*i );
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message+8*i, tweakey, ciphertext+8*i );
        i++;
    }
 
    /* Process last block */
    /* If the block is full, i.e. M_last=epsilon */
    if (m_len == 8*i) {
        set_stage_in_tweak(tweak, MSB_CHKSUM_FULL);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);

    } else { /* If the block is partial and requires padding */
        memset(last_block, 0, 8);
        memcpy(last_block, message+8*i, m_len-8*i);
        last_block[m_len-8*i]=0x80;
        xor_values(Checksum, last_block);

        /* Create the zero block for encryption */
        memset(zero_block, 0, 8);

        /* Encrypt it */
        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, zero_block, tweakey, Pad);

        for (j=0; j<m_len-8*i; j++) {
            ciphertext[8*i+j]=last_block[j] ^ Pad[j];
        }
        set_stage_in_tweak(tweak, MSB_CHKSUM_NON_FULL);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);
    }

    /* Append the authentication tag to the ciphertext */
    for (i=0; i<8; i++) {
        ciphertext[m_len+i]=Final[i] ^ Auth[i];
    }

    /* The authentication tag is one block long, i.e. 8 bytes */
    *c_len=m_len+8;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq (%r8), %rax
movq %rax, (%rsp)
movl 0x8(%r8), %eax
movl %eax, 0x8(%rsp)
testq %rsi, %rsi
movq %rcx, 0x60(%rsp)
movq %rdx, 0x20(%rsp)
je 0x12bf
movq %rsi, %r12
movq %rdi, %r13
movq %r9, 0x50(%rsp)
vxorps %xmm0, %xmm0, %xmm0
cmpq $0x8, %rsi
jae 0x12d1
xorl %ebx, %ebx
jmp 0x1368
movq %rcx, %r14
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
jmp 0x1419
movl $0x8, %r14d
leaq 0xd(%rsp), %rbp
xorl %ebx, %ebx
leaq 0x68(%rsp), %r15
vmovaps %xmm0, 0x30(%rsp)
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x1cff(%rip), %ymm0, %ymm0 # 0x3000
movb $0x20, 0xc(%rsp)
movb $0x0, 0x4(%rbp)
movl $0x0, (%rbp)
movb %al, 0x12(%rsp)
vpmovqb %ymm0, 0x13(%rsp)
movb %bl, 0x17(%rsp)
leaq -0x8(%r13,%r14), %rsi
movl $0xc0, %edi
movq %rsp, %rdx
movq %r15, %rcx
vzeroupper
callq 0x10c0
vmovsd 0x68(%rsp), %xmm0
vmovaps 0x30(%rsp), %xmm1
vxorps %xmm0, %xmm1, %xmm1
vmovaps %xmm1, 0x30(%rsp)
vmovaps 0x30(%rsp), %xmm0
incq %rbx
addq $0x8, %r14
cmpq %r12, %r14
jbe 0x12e3
leaq (,%rbx,8), %rax
subq %rax, %r12
movq 0x60(%rsp), %r14
vmovaps %xmm0, 0x30(%rsp)
jbe 0x140f
leaq 0x28(%rsp), %r15
movq $0x0, (%r15)
addq %rax, %r13
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x1110
movb $-0x80, 0x28(%rsp,%r12)
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x1c41(%rip), %ymm0, %ymm0 # 0x3000
movq %rsp, %rdx
movb $0x60, 0xc(%rdx)
movl $0x0, 0xd(%rdx)
movb $0x0, 0x11(%rdx)
movb %al, 0x12(%rdx)
vpmovqb %ymm0, 0x13(%rdx)
movb %bl, 0x17(%rdx)
leaq 0x68(%rsp), %r12
movl $0xc0, %edi
movq %r15, %rsi
movq %r12, %rcx
vzeroupper
callq 0x10c0
vmovsd (%r12), %xmm0
vmovaps 0x30(%rsp), %xmm1
vxorps %xmm1, %xmm0, %xmm1
vmovaps %xmm1, 0x30(%rsp)
movq 0x20(%rsp), %rdx
movq 0x50(%rsp), %r9
movq 0xd0(%rsp), %r8
movl (%r9), %ebp
vmovd %ebp, %xmm0
shrb $0x4, %bpl
vmovd 0x1(%r9), %xmm1
movb 0x4(%r9), %r10b
vpsrlw $0x4, %xmm1, %xmm1
vpsllw $0x4, %xmm0, %xmm2
vpternlogd $0xe4, 0x1cd5(%rip){1to4}, %xmm1, %xmm2 # 0x3120
movb 0x5(%r9), %r12b
movl %r12d, %eax
shrb $0x4, %al
shlb $0x4, %r10b
orb %al, %r10b
shlb $0x4, %r12b
movq $0x0, 0x40(%rsp)
cmpq $0x8, %r14
vmovdqa %xmm2, 0x50(%rsp)
jae 0x147d
xorl %r13d, %r13d
jmp 0x1526
movl $0x1, %ebx
xorl %eax, %eax
leaq (%rdx,%rax,8), %rsi
movq (%rsi), %rdx
xorq %rdx, 0x40(%rsp)
movq %rax, %rcx
shrq $0x28, %rcx
andb $0xf, %cl
orb %r12b, %cl
vpbroadcastq %rax, %ymm0
vpsrlvq 0x1b54(%rip), %ymm0, %ymm0 # 0x3000
movb %bpl, 0xc(%rsp)
vmovd %xmm2, 0xd(%rsp)
movb %r10b, 0x11(%rsp)
movb %cl, 0x12(%rsp)
vpmovqb %ymm0, 0x13(%rsp)
movb %al, 0x17(%rsp)
leaq (%r8,%rax,8), %rcx
movq %rbp, %r15
movl %r10d, %ebp
movl $0xc0, %edi
movq %rsp, %rdx
movl %r12d, %r13d
movq %r14, %r12
movq %r8, %r14
vzeroupper
callq 0x10c0
movq %r14, %r8
movq %r12, %r14
movl %r13d, %r12d
movq %rbx, %r13
vmovdqa 0x50(%rsp), %xmm2
movl %ebp, %r10d
movq %r15, %rbp
incq %rbx
leaq 0x8(,%r13,8), %rdx
movq %r13, %rax
cmpq %r14, %rdx
movq 0x20(%rsp), %rdx
jbe 0x1484
leaq (,%r13,8), %rbx
movq %r14, %r15
subq %rbx, %r15
jne 0x1579
orb $0x10, %bpl
movq %r13, %rax
shrq $0x28, %rax
andb $0xf, %al
orb %r12b, %al
vpbroadcastq %r13, %ymm0
vpsrlvq 0x1aab(%rip), %ymm0, %ymm0 # 0x3000
movb %bpl, 0xc(%rsp)
vmovd %xmm2, 0xd(%rsp)
movb %r10b, 0x11(%rsp)
movb %al, 0x12(%rsp)
vpmovqb %ymm0, 0x13(%rsp)
jmp 0x17a3
xorl %r14d, %r14d
leaq 0x28(%rsp), %rdi
movq %r14, (%rdi)
addq %rbx, %rdx
movb %r10b, 0x20(%rsp)
movq %rdx, %rsi
movq %r15, %rdx
callq 0x1110
movb $-0x80, 0x28(%rsp,%r15)
leaq 0x28(%rsp), %rax
movq (%rax), %rax
xorq %rax, 0x40(%rsp)
leaq 0x88(%rsp), %rsi
movq %r14, (%rsi)
leal 0x40(%rbp), %eax
movq %r13, %rcx
shrq $0x28, %rcx
andb $0xf, %cl
orb %r12b, %cl
movq %r13, %rdi
shrq $0x20, %rdi
movl %r13d, %r8d
shrl $0x18, %r8d
movl %r13d, %r9d
shrl $0x10, %r9d
movl %r13d, %r12d
shrl $0x8, %r12d
movq %rsp, %rdx
movb %al, 0xc(%rdx)
vmovaps 0x50(%rsp), %xmm0
vmovss %xmm0, 0xd(%rdx)
movb 0x20(%rsp), %al
movb %al, 0x11(%rdx)
movq %rcx, 0x78(%rsp)
movb %cl, 0x12(%rdx)
movq %rdi, 0x70(%rsp)
movb %dil, 0x13(%rdx)
movl %r8d, 0x4c(%rsp)
movb %r8b, 0x14(%rdx)
movl %r9d, 0x48(%rsp)
movb %r9b, 0x15(%rdx)
movb %r12b, 0x16(%rdx)
movb %r13b, 0x17(%rdx)
leaq 0x80(%rsp), %rcx
movl $0xc0, %edi
callq 0x10c0
addq 0xd0(%rsp), %rbx
cmpq $0x1, %r15
adcq %r14, %r15
leaq 0x1f(%r15), %rax
andq $-0x20, %rax
decq %r15
vpbroadcastq %r15, %ymm0
vpmovsxbq 0x1ac5(%rip), %ymm1 # 0x3124
vpmovsxbq 0x1ac0(%rip), %ymm2 # 0x3128
vpmovsxbq 0x1abb(%rip), %ymm3 # 0x312c
vpmovsxbq 0x1ab6(%rip), %ymm4 # 0x3130
vpmovsxbq 0x1ab1(%rip), %ymm5 # 0x3134
vpmovsxbq 0x1aac(%rip), %ymm6 # 0x3138
vpmovsxbq 0x1aa7(%rip), %ymm7 # 0x313c
vpmovsxbq 0x1aa2(%rip), %ymm8 # 0x3140
vpbroadcastq %r14, %ymm9
vpor %ymm1, %ymm9, %ymm10
vpor %ymm2, %ymm9, %ymm11
vpor %ymm3, %ymm9, %ymm12
vpor %ymm4, %ymm9, %ymm13
vpor %ymm5, %ymm9, %ymm14
vpor %ymm6, %ymm9, %ymm15
vporq %ymm7, %ymm9, %ymm16
vpor %ymm8, %ymm9, %ymm9
vpcmpleuq %ymm0, %ymm9, %k0
vpcmpleuq %ymm0, %ymm16, %k1
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpleuq %ymm0, %ymm15, %k1
vpcmpleuq %ymm0, %ymm14, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
kunpckbw %k0, %k1, %k0
vpcmpleuq %ymm0, %ymm13, %k1
vpcmpleuq %ymm0, %ymm12, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
vpcmpleuq %ymm0, %ymm11, %k2
vpcmpleuq %ymm0, %ymm10, %k3
kshiftlb $0x4, %k3, %k3
korb %k3, %k2, %k2
kunpckbw %k1, %k2, %k1
kunpckwd %k0, %k1, %k1
vmovdqu8 0x28(%rsp,%r14), %ymm9 {%k1} {z}
vmovdqu8 0x80(%rsp,%r14), %ymm10 {%k1} {z}
vpxor %ymm9, %ymm10, %ymm9
vmovdqu8 %ymm9, (%rbx,%r14) {%k1}
addq $0x20, %r14
cmpq %r14, %rax
jne 0x169e
orb $0x50, %bpl
movb %bpl, 0xc(%rsp)
vmovdqa 0x50(%rsp), %xmm0
vmovd %xmm0, 0xd(%rsp)
movb 0x20(%rsp), %al
movb %al, 0x11(%rsp)
movq 0x78(%rsp), %rax
movb %al, 0x12(%rsp)
movq 0x70(%rsp), %rax
movb %al, 0x13(%rsp)
movl 0x4c(%rsp), %eax
movb %al, 0x14(%rsp)
movl 0x48(%rsp), %eax
movb %al, 0x15(%rsp)
movb %r12b, 0x16(%rsp)
movb %r13b, 0x17(%rsp)
leaq 0x40(%rsp), %rsi
movq %rsp, %rdx
leaq 0x90(%rsp), %r14
movl $0xc0, %edi
movq %r14, %rcx
vzeroupper
callq 0x10c0
vmovsd (%r14), %xmm0
vxorps 0x30(%rsp), %xmm0, %xmm0
movq 0x60(%rsp), %rax
movq 0xd0(%rsp), %rcx
vmovlps %xmm0, (%rcx,%rax)
addq $0x8, %rax
movq 0xd8(%rsp), %rcx
movq %rax, (%rcx)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq