// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/deoxyseq256128v13/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;
    uint64_t hblock1,hblock2;
    uint8_t tweak[16];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[16];
    uint8_t last_block[16];
    uint8_t Final[16];
    uint8_t zero_block[16];
    uint8_t Pad[16];
    uint8_t temp[16];
    uint8_t tag[16];
    
    /* Fill the tweak with zeros (no nonce !!!) */
    memset(tweak, 0, sizeof(tweak));

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

    /* Associated data */
    memset(Auth, 0, 16);
    
    /* The first two blocks with Nonce plaintext */
    set_block_number_in_tweak(tweak + 0 , 0 );
    set_block_number_in_tweak(tweak + 8 , 0 );
    set_stage_in_tweak(tweak, MSB_AD_N1);
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce, tweakey, temp);
    xor_values(Auth, temp);

    set_stage_in_tweak(tweak, MSB_AD_N2);
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce, tweakey, temp);
    xor_values(Auth, temp);

    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 + 8, 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 + 8 , 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 
     * first pass
     */
    
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (16*(i+1) <= m_len) {
        set_block_number_in_tweak(tweak + 8, i);
	set_tweak_in_tweakey(tweakey, tweak);
	aesTweakEncrypt(TWEAKEY_STATE_SIZE, message + 16*i, tweakey, temp);
        xor_values(Auth, temp );
        i++;
	
    }
    


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

        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO );
        set_block_number_in_tweak(tweak + 8 , i );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, last_block, tweakey, temp);
	
        xor_values(Auth, temp );
    } 
    
    /* Last encryption */
    set_stage_in_tweak(tweak, MSB_M_LAST_ZERO );
    set_block_number_in_tweak(tweak + 8 , 0 );
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, Auth, tweakey, tag );
    
    
    /* Message 
     * second pass
     */
    memcpy(tweak, tag, 16 );
    tweak[0] = 0x80 ^ (tweak[0] & 0x7f);
    
    i = 0;
    hblock1 = get_block_number_from_tag( tag + 0 );
    hblock2 = get_block_number_from_tag( tag + 8 );
    while (16*(i+1) <= m_len) {
      
        set_block_number_in_tweak(tweak + 8, hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, ciphertext + 16*i );
        xor_values(ciphertext + 16*i, message + 16*i );
        i++;
        hblock2++;
        if( hblock2 == 0 ){
            hblock1 ++;
            set_block_number_in_tweak(tweak , hblock1 );
            tweak[0] = 0x80 ^ (tweak[0] & 0x7f);
        }
    }
    
    /* Impartial block */
    if (m_len >  16*i) {
        set_block_number_in_tweak(tweak + 8 , hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, temp );
        for( j = 0; j < m_len - 16 * i; j ++)
            ciphertext[ 16 * i + j ] = message[ 16 * i + j] ^ temp[j];

    } 
    
    
    /* Append the authentication tag to the ciphertext */
    memcpy( ciphertext + m_len, tag, 16 ); 

    /* 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 $0xd8, %rsp
movq %r9, %r15
movq %rcx, %rbp
movq %rdx, 0x80(%rsp)
movq %rsi, %r12
movq %rdi, 0x68(%rsp)
vmovups (%r8), %ymm0
leaq 0x41(%rsp), %rax
vmovups %ymm0, -0x21(%rax)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movb $0x30, -0x1(%rax)
xorl %ebx, %ebx
movq %rbx, (%rax)
movq %rbx, 0x7(%rax)
leaq 0x20(%rsp), %r14
leaq 0x70(%rsp), %r13
movl $0x180, %edi            # imm = 0x180
movq %r9, %rsi
movq %r14, %rdx
movq %r13, %rcx
vzeroupper
callq 0x10c0
vmovaps 0x10(%rsp), %xmm0
vxorps (%r13), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movb $0x70, 0x20(%r14)
movq %rbx, 0x21(%r14)
movq %rbx, 0x28(%r14)
movl $0x180, %edi            # imm = 0x180
movq %r15, 0x88(%rsp)
movq %r15, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x10c0
vmovdqa 0x10(%rsp), %xmm0
vpxor (%r13), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
testq %r12, %r12
movq %rbp, 0x58(%rsp)
je 0x143a
xorl %r15d, %r15d
cmpq $0x10, %r12
jae 0x1301
xorl %ebx, %ebx
jmp 0x138c
movl $0x10, %ebp
movq 0x68(%rsp), %r14
xorl %ebx, %ebx
leaq 0x41(%rsp), %r13
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3cdf(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
movb $0x20, 0x40(%rsp)
movl %r15d, 0x3(%r13)
movl %r15d, (%r13)
vpmovqb %ymm0, 0x48(%rsp)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x4c(%rsp)
movb %cl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
movl $0x180, %edi            # imm = 0x180
movq %r14, %rsi
leaq 0x20(%rsp), %rdx
leaq 0x70(%rsp), %rcx
vzeroupper
callq 0x10c0
vmovdqa 0x10(%rsp), %xmm0
vpxor 0x70(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
incq %rbx
addq $0x10, %r14
addq $0x10, %rbp
cmpq %r12, %rbp
jbe 0x1312
movq %rbx, %rax
shlq $0x4, %rax
subq %rax, %r12
movq 0x58(%rsp), %rbp
jbe 0x143a
vpxor %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r14
vmovdqa %xmm0, (%r14)
movq 0x68(%rsp), %rsi
addq %rax, %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x10f0
movb $-0x80, 0x90(%rsp,%r12)
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3c23(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x20(%rsp), %rdx
movb $0x60, 0x20(%rdx)
xorl %ecx, %ecx
leaq 0x41(%rsp), %rsi
movl %ecx, 0x3(%rsi)
movl %ecx, (%rsi)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x70(%rsp), %r12
movl $0x180, %edi            # imm = 0x180
movq %r14, %rsi
movq %r12, %rcx
vzeroupper
callq 0x10c0
vmovdqa 0x10(%rsp), %xmm0
vpxor (%r12), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
cmpq $0x10, %rbp
jae 0x1447
xorl %ebx, %ebx
jmp 0x14d3
movl $0x10, %r15d
xorl %ebx, %ebx
leaq 0x20(%rsp), %r12
leaq 0x70(%rsp), %r13
movq 0x80(%rsp), %r14
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3b90(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
movq $0x0, 0x40(%rsp)
vpmovqb %ymm0, 0x48(%rsp)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x4c(%rsp)
movb %cl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
movl $0x180, %edi            # imm = 0x180
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
vzeroupper
callq 0x10c0
vmovdqa 0x10(%rsp), %xmm0
vpxor 0x70(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
incq %rbx
addq $0x10, %r14
addq $0x10, %r15
cmpq %rbp, %r15
jbe 0x1461
movq %rbx, %rsi
shlq $0x4, %rsi
subq %rsi, %rbp
movq 0x80(%rsp), %r13
leaq 0x41(%rsp), %r15
jbe 0x1580
vpxor %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r14
vmovdqa %xmm0, (%r14)
addq %r13, %rsi
movq %r14, %rdi
movq %rbp, %rdx
callq 0x10f0
movb $-0x80, 0x90(%rsp,%rbp)
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3ada(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x20(%rsp), %rdx
movb $0x40, 0x20(%rdx)
xorl %ecx, %ecx
movl %ecx, 0x3(%r15)
movl %ecx, (%r15)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x70(%rsp), %r12
movl $0x180, %edi            # imm = 0x180
movq %r14, %rsi
movq %r12, %rcx
vzeroupper
callq 0x10c0
vmovdqa 0x10(%rsp), %xmm0
vpxor (%r12), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
leaq 0x20(%rsp), %rdx
movb $0x10, 0x20(%rdx)
xorl %r12d, %r12d
movq %r12, 0x7(%r15)
movq %r12, (%r15)
leaq 0x10(%rsp), %rsi
leaq 0xc0(%rsp), %rbx
movl $0x180, %edi            # imm = 0x180
movq %rbx, %rcx
callq 0x10c0
movzbl (%rbx), %eax
movb 0x1(%rbx), %r9b
movb 0x2(%rbx), %r10b
movb 0x3(%rbx), %sil
movb 0x4(%rbx), %dil
movb 0x5(%rbx), %r8b
movb 0x6(%rbx), %r15b
movb 0x7(%rbx), %bpl
movl %eax, %r11d
orb $-0x80, %r11b
movl 0x8(%rbx), %ecx
shlq $0x38, %rcx
movzbl 0x9(%rbx), %edx
shlq $0x30, %rdx
orq %rcx, %rdx
movzbl 0xa(%rbx), %ecx
shlq $0x28, %rcx
orq %rdx, %rcx
movzbl 0xb(%rbx), %edx
shlq $0x20, %rdx
orq %rcx, %rdx
movl 0xc(%rbx), %ecx
shll $0x18, %ecx
orq %rdx, %rcx
movzbl 0xd(%rbx), %edx
shll $0x10, %edx
orq %rcx, %rdx
movzbl 0xe(%rbx), %ecx
shll $0x8, %ecx
orq %rdx, %rcx
movzbl 0xf(%rbx), %ebx
xorq %rcx, %rbx
cmpq $0x10, 0x58(%rsp)
movl %r15d, 0xc(%rsp)
jb 0x180d
movl %ebp, %r14d
movl %edi, %ebp
movzbl %r9b, %edi
movl %r8d, %r12d
movzbl %r10b, %r8d
movq %rsi, 0x60(%rsp)
movzbl 0x60(%rsp), %esi
movzbl %bpl, %edx
movzbl %r12b, %ecx
shlq $0x30, %rdi
shlq $0x38, %rax
orq %rdi, %rax
movl %ebp, %edi
movzbl %r15b, %r15d
shlq $0x28, %r8
orq %rax, %r8
movb %r14b, 0xb(%rsp)
movzbl %r14b, %eax
shlq $0x20, %rsi
orq %r8, %rsi
movl %r12d, %r8d
shll $0x18, %edx
orq %rsi, %rdx
movq 0x60(%rsp), %rsi
shll $0x10, %ecx
orq %rdx, %rcx
shll $0x8, %r15d
orq %rcx, %r15
xorq %rax, %r15
movl $0x1, %eax
xorl %r12d, %r12d
movq 0x110(%rsp), %r14
movq %r12, %rcx
movq %rax, 0x68(%rsp)
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3941(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %edx
shrl $0x10, %edx
movq %r11, 0xb8(%rsp)
movb %r11b, 0x40(%rsp)
movq %r9, 0xb0(%rsp)
movb %r9b, 0x41(%rsp)
movq %r10, 0xa8(%rsp)
movb %r10b, 0x42(%rsp)
movq %rsi, 0x60(%rsp)
movb %sil, 0x43(%rsp)
movl %edi, %r12d
movb %dil, 0x44(%rsp)
movl %r8d, %ebp
movb %r8b, 0x45(%rsp)
movl 0xc(%rsp), %esi
movb %sil, 0x46(%rsp)
movb 0xb(%rsp), %sil
movb %sil, 0x47(%rsp)
vpmovqb %ymm0, 0x48(%rsp)
movb %al, 0x4c(%rsp)
movb %dl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
shlq $0x4, %rcx
addq 0x110(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
leaq 0x20(%rsp), %rdx
vzeroupper
callq 0x10c0
xorl %eax, %eax
movb (%r13,%rax), %cl
xorb %cl, (%r14,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x175d
incq %rbx
jne 0x17b5
incq %r15
movq %r15, %r11
shrq $0x38, %r11
movq %r15, %r9
shrq $0x30, %r9
movq %r15, %r10
shrq $0x28, %r10
movq %r15, %rsi
shrq $0x20, %rsi
movl %r15d, %edi
shrl $0x18, %edi
movl %r15d, %r8d
shrl $0x10, %r8d
movl %r15d, %eax
shrl $0x8, %eax
movl %eax, 0xc(%rsp)
orb $-0x80, %r11b
movb %r15b, 0xb(%rsp)
jmp 0x17d8
movq 0xb0(%rsp), %r9
movq 0xa8(%rsp), %r10
movq 0xb8(%rsp), %r11
movq 0x60(%rsp), %rsi
movl %r12d, %edi
movl %ebp, %r8d
movq 0x68(%rsp), %r12
leaq 0x1(%r12), %rax
movq %rax, %rcx
shlq $0x4, %rcx
addq $0x10, %r14
addq $0x10, %r13
cmpq 0x58(%rsp), %rcx
jbe 0x16a8
shlq $0x4, %r12
movq 0x80(%rsp), %r13
movb 0xb(%rsp), %bpl
movq 0x118(%rsp), %r14
movq 0x58(%rsp), %r15
subq %r12, %r15
jbe 0x18be
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x37ce(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x20(%rsp), %rdx
movb %r11b, 0x20(%rdx)
movb %r9b, 0x21(%rdx)
movb %r10b, 0x22(%rdx)
movb %sil, 0x23(%rdx)
movb %dil, 0x24(%rdx)
movb %r8b, 0x25(%rdx)
movl 0xc(%rsp), %ecx
movb %cl, 0x26(%rdx)
movb %bpl, 0x27(%rdx)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x70(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
vzeroupper
callq 0x10c0
cmpq $0x1, %r15
adcq $0x0, %r15
movq 0x110(%rsp), %rax
addq %r12, %rax
addq %r12, %r13
xorl %ecx, %ecx
movb 0x70(%rsp,%rcx), %dl
xorb (%r13,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq %rcx, %r15
jne 0x18aa
vmovaps 0xc0(%rsp), %xmm0
movq 0x58(%rsp), %rax
movq 0x110(%rsp), %rcx
vmovups %xmm0, (%rcx,%rax)
addq $0x10, %rax
movq %rax, (%r14)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq