// File: /workspace/github_c_makefile_star2_O3/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 %r14d, %r14d
movq %r14, 0x7(%r15)
movq %r14, (%r15)
leaq 0x10(%rsp), %rsi
leaq 0xc0(%rsp), %r15
movl $0x180, %edi            # imm = 0x180
movq %r15, %rcx
callq 0x10c0
movzbl (%r15), %eax
movl %eax, %r10d
orb $-0x80, %r10b
movl 0x8(%r15), %ecx
shlq $0x38, %rcx
movzbl 0x9(%r15), %edx
shlq $0x30, %rdx
orq %rcx, %rdx
movzbl 0xa(%r15), %ecx
shlq $0x28, %rcx
orq %rdx, %rcx
movzbl 0xb(%r15), %edx
shlq $0x20, %rdx
orq %rcx, %rdx
movl 0xc(%r15), %ecx
shll $0x18, %ecx
orq %rdx, %rcx
movzbl 0xd(%r15), %edx
shll $0x10, %edx
orq %rcx, %rdx
movzbl 0xe(%r15), %ecx
shll $0x8, %ecx
orq %rdx, %rcx
movzbl 0xf(%r15), %ebx
xorq %rcx, %rbx
movb 0x1(%r15), %r9b
movb 0x2(%r15), %r11b
movb 0x3(%r15), %sil
movb 0x4(%r15), %dil
movb 0x5(%r15), %r8b
movb 0x6(%r15), %cl
movb 0x7(%r15), %bpl
cmpq $0x10, 0x58(%rsp)
jb 0x181e
movl %edi, %r15d
movzbl %r9b, %edi
movl %r8d, %r12d
movzbl %r11b, %r8d
movq %rsi, 0x60(%rsp)
movzbl 0x60(%rsp), %esi
movzbl %r15b, %edx
movl %ebp, %r14d
movl %ecx, %ebp
movzbl %r12b, %ecx
shlq $0x30, %rdi
shlq $0x38, %rax
orq %rdi, %rax
movl %r15d, %edi
movq %r13, %r15
movl %ebp, 0xc(%rsp)
movzbl %bpl, %r13d
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, %r13d
orq %rcx, %r13
xorq %rax, %r13
movl $0x1, %eax
xorl %r14d, %r14d
movq 0x110(%rsp), %r12
movq %r14, %rcx
movq %rax, 0x68(%rsp)
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3932(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %edx
shrl $0x10, %edx
movq %r10, 0xb8(%rsp)
movb %r10b, 0x40(%rsp)
movq %r9, 0xb0(%rsp)
movb %r9b, 0x41(%rsp)
movq %r11, 0xa8(%rsp)
movb %r11b, 0x42(%rsp)
movq %rsi, 0x60(%rsp)
movb %sil, 0x43(%rsp)
movl %edi, %r14d
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 (%r15,%rax), %cl
xorb %cl, (%r12,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x176c
incq %rbx
jne 0x17c3
incq %r13
movq %r13, %r10
shrq $0x38, %r10
movq %r13, %r9
shrq $0x30, %r9
movq %r13, %r11
shrq $0x28, %r11
movq %r13, %rsi
shrq $0x20, %rsi
movl %r13d, %edi
shrl $0x18, %edi
movl %r13d, %r8d
shrl $0x10, %r8d
movl %r13d, %eax
shrl $0x8, %eax
movl %eax, 0xc(%rsp)
orb $-0x80, %r10b
movb %r13b, 0xb(%rsp)
jmp 0x17e6
movq 0xb0(%rsp), %r9
movq 0xb8(%rsp), %r10
movq 0xa8(%rsp), %r11
movq 0x60(%rsp), %rsi
movl %r14d, %edi
movl %ebp, %r8d
movq 0x68(%rsp), %r14
leaq 0x1(%r14), %rax
movq %rax, %rcx
shlq $0x4, %rcx
addq $0x10, %r12
addq $0x10, %r15
cmpq 0x58(%rsp), %rcx
jbe 0x16b7
shlq $0x4, %r14
movq 0x80(%rsp), %r13
movl 0xc(%rsp), %ecx
movb 0xb(%rsp), %bpl
movq 0x118(%rsp), %r15
movq 0x58(%rsp), %r12
subq %r14, %r12
jbe 0x18cb
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x37bd(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x20(%rsp), %rdx
movb %r10b, 0x20(%rdx)
movb %r9b, 0x21(%rdx)
movb %r11b, 0x22(%rdx)
movb %sil, 0x23(%rdx)
movb %dil, 0x24(%rdx)
movb %r8b, 0x25(%rdx)
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, %r12
adcq $0x0, %r12
movq 0x110(%rsp), %rax
addq %r14, %rax
addq %r14, %r13
xorl %ecx, %ecx
movb 0x70(%rsp,%rcx), %dl
xorb (%r13,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq %rcx, %r12
jne 0x18b7
vmovaps 0xc0(%rsp), %xmm0
movq 0x58(%rsp), %rax
movq 0x110(%rsp), %rcx
vmovups %xmm0, (%rcx,%rax)
addq $0x10, %rax
movq %rax, (%r15)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq