// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/deoxysneq128128v13/ref/deoxys.c
// Function: deoxys_aead_encrypt
void deoxys_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[16];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[16];
    uint8_t last_block[16];
    uint8_t Checksum[16];
    uint8_t Final[16];
    uint8_t zero_block[16];
    uint8_t Pad[16];
    uint8_t temp[16];

    
    /* Fill the tweak with zeros (no nonce !!!) */
    memset(tweak, 0, sizeof(tweak));

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

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

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

        /* For each full input blocks */
        i=0;
        while (16*(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+16*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 > 16*i ) {

            /* Prepare the last padded block */
            memset(last_block, 0, 16);
            memcpy(last_block, ass_data+16*i, ass_data_len-16*i);
            last_block[ass_data_len-16*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 */
    memset(tweak, 0, sizeof(tweak));
    set_nonce_in_tweak(tweak, nonce);
    
    memset(Checksum, 0, 16);
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (16*(i+1) <= m_len) {
        xor_values(Checksum, message+16*i );
        set_block_number_in_tweak(tweak, i );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message+16*i , tweakey, ciphertext+16*i );
        i++;
    }

    /* Process incomplete block */
    if (m_len > 16*i) {
        memset(last_block, 0, 16);
        memcpy(last_block, message +16*i, m_len-16*i);
        last_block[m_len-16*i]=0x80;
        xor_values(Checksum, last_block);

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

        /* 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-16*i; j++) {
            ciphertext[16*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);
    }
    else{
        set_block_number_in_tweak(tweak, i);
        set_stage_in_tweak(tweak, MSB_CHKSUM_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<16; i++) {
        ciphertext[m_len+i]=Final[i] ^ Auth[i];
    }

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

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
vmovups (%r8), %xmm0
movq %rdx, %r14
movq %rsi, %rbp
vmovaps %xmm0, 0x10(%rsp)
vpxor %xmm3, %xmm3, %xmm3
vxorps %xmm0, %xmm0, %xmm0
testq %rsi, %rsi
movq %rcx, 0x48(%rsp)
je 0x1390
movq %rdi, %rsi
movq %r9, 0x38(%rsp)
xorl %ecx, %ecx
cmpq $0x10, %rbp
jae 0x123b
xorl %ebx, %ebx
jmp 0x12d9
movl $0x10, %r15d
leaq 0x21(%rsp), %r13
xorl %ebx, %ebx
vmovaps %xmm0, 0x60(%rsp)
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3d9a(%rip), %ymm0, %ymm0 # 0x5000
movb $0x20, 0x20(%rsp)
movl %ecx, 0x3(%r13)
movl %ecx, (%r13)
movb %al, 0x28(%rsp)
vpmovqb %ymm0, 0x29(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x2d(%rsp)
movb %bh, 0x2e(%rsp)
movb %bl, 0x2f(%rsp)
movq %rsi, %r12
leaq -0x10(%rsi,%r15), %rsi
movl $0x100, %edi            # imm = 0x100
leaq 0x10(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
vzeroupper
callq 0x10d0
xorl %ecx, %ecx
movq %r12, %rsi
vmovaps 0x60(%rsp), %xmm0
vxorps 0xc0(%rsp), %xmm0, %xmm0
incq %rbx
addq $0x10, %r15
cmpq %rbp, %r15
jbe 0x1248
movq %rbx, %rax
shlq $0x4, %rax
subq %rax, %rbp
jbe 0x1382
vmovaps %xmm0, 0x60(%rsp)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x80(%rsp), %r15
vmovaps %xmm0, (%r15)
addq %rax, %rsi
movq %r15, %rdi
movq %rbp, %rdx
callq 0x1100
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3cda(%rip), %ymm0, %ymm0 # 0x5000
leaq 0x10(%rsp), %rdx
movb $0x60, 0x10(%rdx)
xorl %ecx, %ecx
movl %ecx, 0x11(%rdx)
movl %ecx, 0x14(%rdx)
movb %al, 0x18(%rdx)
vpmovqb %ymm0, 0x19(%rdx)
movb $-0x80, 0x80(%rsp,%rbp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x1d(%rdx)
movb %bh, 0x2e(%rsp)
movb %bl, 0x1f(%rdx)
leaq 0xc0(%rsp), %r12
movl $0x100, %edi            # imm = 0x100
movq %r15, %rsi
movq %r12, %rcx
vzeroupper
callq 0x10d0
vmovaps 0x60(%rsp), %xmm0
vxorps (%r12), %xmm0, %xmm0
movq 0x48(%rsp), %rcx
vpxor %xmm3, %xmm3, %xmm3
movq 0x38(%rsp), %r9
vmovaps %xmm0, 0x60(%rsp)
movl (%r9), %edx
vmovd %edx, %xmm0
shrb $0x4, %dl
movb 0x4(%r9), %sil
vmovd 0x1(%r9), %xmm1
vpsrlw $0x4, %xmm1, %xmm1
vpsllw $0x4, %xmm0, %xmm2
vpternlogd $0xe4, 0x3d61(%rip){1to4}, %xmm1, %xmm2 # 0x5120
movb 0x5(%r9), %r10b
movl %r10d, %eax
shrb $0x4, %al
shlb $0x4, %sil
orb %al, %sil
movb 0x6(%r9), %r8b
movl %r8d, %eax
shrb $0x4, %al
shlb $0x4, %r10b
orb %al, %r10b
movb 0x7(%r9), %bpl
movl %ebp, %eax
shrb $0x4, %al
shlb $0x4, %r8b
orb %al, %r8b
shlb $0x4, %bpl
vmovdqa %xmm3, 0x70(%rsp)
cmpq $0x10, %rcx
movb %sil, 0x38(%rsp)
vmovdqa %xmm2, 0x90(%rsp)
jae 0x1416
xorl %ebx, %ebx
jmp 0x14ef
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r14, 0x40(%rsp)
vmovaps 0x70(%rsp), %xmm0
vxorps (%r14,%r13), %xmm0, %xmm0
vmovaps %xmm0, 0x70(%rsp)
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
orb %bpl, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3bb3(%rip), %ymm0, %ymm0 # 0x5000
movb %dl, 0x20(%rsp)
vmovd %xmm2, 0x21(%rsp)
movb %sil, 0x25(%rsp)
movb %r10b, 0x26(%rsp)
movb %r8b, 0x27(%rsp)
movb %al, 0x28(%rsp)
vpmovqb %ymm0, 0x29(%rsp)
leaq (%r14,%r13), %rsi
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x2d(%rsp)
movb %bh, 0x2e(%rsp)
movb %bl, 0x2f(%rsp)
movq 0x140(%rsp), %rax
leaq (%rax,%r13), %rcx
movl %r10d, %r14d
movl $0x100, %edi            # imm = 0x100
movq %rdx, %r15
leaq 0x10(%rsp), %rdx
movq %rbx, %r12
movl %r8d, %ebx
vzeroupper
callq 0x10d0
movl %ebx, %r8d
movq %r12, %rbx
vmovdqa 0x90(%rsp), %xmm2
movl %r14d, %r10d
movq 0x40(%rsp), %r14
movb 0x38(%rsp), %sil
movq %r15, %rdx
movq 0x48(%rsp), %rcx
incq %rbx
leaq 0x10(%r13), %rax
addq $0x20, %r13
cmpq %rcx, %r13
movq %rax, %r13
jbe 0x1420
movq 0x148(%rsp), %r12
movq %rbx, %r15
shlq $0x4, %r15
subq %r15, %rcx
jbe 0x17d8
vpxor %xmm0, %xmm0, %xmm0
leaq 0x80(%rsp), %rdi
vmovdqa %xmm0, (%rdi)
addq %r15, %r14
movb %r10b, 0x40(%rsp)
movq %r14, %rsi
movq %rdx, %r14
movq %rcx, %rdx
movl %r8d, %r13d
movq %rcx, %r12
callq 0x1100
movb $-0x80, 0x80(%rsp,%r12)
vmovaps 0x70(%rsp), %xmm0
leaq 0x80(%rsp), %rax
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, 0x70(%rsp)
leaq 0xe0(%rsp), %rsi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
movq %r14, 0xb8(%rsp)
leal 0x40(%r14), %eax
movq %rbx, %r14
shrq $0x38, %r14
andb $0xf, %r14b
orb %bpl, %r14b
movq %rbx, %rcx
shrq $0x30, %rcx
movq %rbx, %rdi
shrq $0x28, %rdi
movq %rbx, %r8
shrq $0x20, %r8
movl %ebx, %r9d
shrl $0x18, %r9d
movl %ebx, %r10d
shrl $0x10, %r10d
movl %ebx, %r11d
leaq 0x10(%rsp), %rdx
movb %al, 0x10(%rdx)
vmovaps 0x90(%rsp), %xmm0
vmovss %xmm0, 0x11(%rdx)
shrl $0x8, %r11d
movb 0x38(%rsp), %al
movb %al, 0x15(%rdx)
movb 0x40(%rsp), %al
movb %al, 0x16(%rdx)
movb %r13b, 0xf(%rsp)
movb %r13b, 0x17(%rdx)
movq %rcx, %rbp
movb %r14b, 0x18(%rdx)
movb %bpl, 0x19(%rdx)
movq %rdi, 0xb0(%rsp)
movb %dil, 0x1a(%rdx)
movq %r8, 0xa8(%rsp)
movb %r8b, 0x1b(%rdx)
movl %r9d, 0x5c(%rsp)
movb %r9b, 0x1c(%rdx)
movl %r10d, 0x58(%rsp)
movb %r10b, 0x1d(%rdx)
movl %r11d, 0x54(%rsp)
movb %r11b, 0x1e(%rdx)
movb %bl, 0x1f(%rdx)
leaq 0xd0(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10d0
movq 0x140(%rsp), %r13
addq %r13, %r15
cmpq $0x1, %r12
adcq $0x0, %r12
leaq 0x1f(%r12), %rax
andq $-0x20, %rax
decq %r12
vpbroadcastq %r12, %ymm0
xorl %ecx, %ecx
vpmovsxbq 0x3acc(%rip), %ymm1 # 0x5124
vpmovsxbq 0x3ac7(%rip), %ymm2 # 0x5128
vpmovsxbq 0x3ac2(%rip), %ymm3 # 0x512c
vpmovsxbq 0x3abd(%rip), %ymm4 # 0x5130
vpmovsxbq 0x3ab8(%rip), %ymm5 # 0x5134
vpmovsxbq 0x3ab3(%rip), %ymm6 # 0x5138
vpmovsxbq 0x3aae(%rip), %ymm7 # 0x513c
vpmovsxbq 0x3aa9(%rip), %ymm8 # 0x5140
vpbroadcastq %rcx, %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 0x80(%rsp,%rcx), %ymm9 {%k1} {z}
vmovdqu8 0xd0(%rsp,%rcx), %ymm10 {%k1} {z}
vpxor %ymm9, %ymm10, %ymm9
vmovdqu8 %ymm9, (%r15,%rcx) {%k1}
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x1697
movq 0xb8(%rsp), %r12
orb $0x50, %r12b
movb %r12b, 0x20(%rsp)
vmovdqa 0x90(%rsp), %xmm0
vmovd %xmm0, 0x21(%rsp)
movb 0x38(%rsp), %al
movb %al, 0x25(%rsp)
movb 0x40(%rsp), %al
movb %al, 0x26(%rsp)
movb 0xf(%rsp), %al
movb %al, 0x27(%rsp)
movb %r14b, 0x28(%rsp)
movb %bpl, 0x29(%rsp)
movq 0xb0(%rsp), %rax
movb %al, 0x2a(%rsp)
movq 0xa8(%rsp), %rax
movb %al, 0x2b(%rsp)
movl 0x5c(%rsp), %eax
movb %al, 0x2c(%rsp)
movl 0x58(%rsp), %eax
movb %al, 0x2d(%rsp)
movl 0x54(%rsp), %eax
movb %al, 0x2e(%rsp)
movb %bl, 0x2f(%rsp)
movq 0x148(%rsp), %r12
jmp 0x1837
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
orb %bpl, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x380d(%rip), %ymm0, %ymm0 # 0x5000
orb $0x10, %dl
movb %dl, 0x20(%rsp)
vmovd %xmm2, 0x21(%rsp)
movb %sil, 0x25(%rsp)
movb %r10b, 0x26(%rsp)
movb %r8b, 0x27(%rsp)
movb %al, 0x28(%rsp)
vpmovqb %ymm0, 0x29(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x2d(%rsp)
movb %bh, 0x2e(%rsp)
movb %bl, 0x2f(%rsp)
movq 0x140(%rsp), %r13
leaq 0x70(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0xf0(%rsp), %r14
movl $0x100, %edi            # imm = 0x100
movq %r14, %rcx
vzeroupper
callq 0x10d0
vmovaps 0x60(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
movq 0x48(%rsp), %rax
vmovups %xmm0, (%r13,%rax)
addq $0x10, %rax
movq %rax, (%r12)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq