// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x108, %rsp            # imm = 0x108
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %rax
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq %rcx, 0xe8(%rsp)
movq %r8, 0xe0(%rsp)
movq %r9, 0xd8(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
movq 0xe0(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x70(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x78(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x80(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x88(%rsp)
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
leaq 0xa0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a50
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a50
leaq 0xa0(%rsp), %rdi
movl $0x30, %esi
callq 0x1b30
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
movq 0xd8(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b90
leaq 0xa0(%rsp), %rdi
movl $0x70, %esi
callq 0x1b30
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
movq 0xd8(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b90
cmpq $0x0, 0xf8(%rsp)
je 0x159f
leaq 0xa0(%rsp), %rdi
movl $0x20, %esi
callq 0x1b30
movq $0x0, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0xf8(%rsp), %rax
ja 0x14b6
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1a50
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
movq 0x100(%rsp), %rsi
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b90
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
jmp 0x141e
movq 0xf8(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x159d
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
leaq 0x50(%rsp), %rdi
movq 0x100(%rsp), %rsi
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0xf8(%rsp), %rdx
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
movq 0xf8(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x50(%rsp,%rax)
leaq 0xa0(%rsp), %rdi
movl $0x60, %esi
callq 0x1b30
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1a50
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
leaq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b90
jmp 0x159f
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x1b30
movq $0x0, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0xe8(%rsp), %rax
ja 0x1652
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1a50
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
movq 0xf0(%rsp), %rsi
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b90
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
jmp 0x15ba
movq 0xe8(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x1739
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
leaq 0x50(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0xe8(%rsp), %rdx
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
movq 0xe8(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x50(%rsp,%rax)
leaq 0xa0(%rsp), %rdi
movl $0x40, %esi
callq 0x1b30
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1a50
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
leaq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1b90
leaq 0xa0(%rsp), %rdi
movl $0x10, %esi
callq 0x1b30
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a50
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
leaq 0x60(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
movq (%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movzbl 0xa0(%rsp), %eax
andl $0x7f, %eax
xorl $0x80, %eax
movb %al, 0xa0(%rsp)
movq $0x0, 0xd0(%rsp)
movq %rsp, %rdi
callq 0x1030
movq %rax, 0xc0(%rsp)
movq %rsp, %rdi
addq $0x8, %rdi
callq 0x1030
movq %rax, 0xb8(%rsp)
movq 0xd0(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0xe8(%rsp), %rax
ja 0x190b
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
movq 0xb8(%rsp), %rsi
callq 0x1a50
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
movq 0xd8(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movq 0x110(%rsp), %rcx
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
movq 0x110(%rsp), %rdi
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0xf0(%rsp), %rsi
movq 0xd0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x1b90
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
jne 0x1906
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
leaq 0xa0(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x1a50
movzbl 0xa0(%rsp), %eax
andl $0x7f, %eax
xorl $0x80, %eax
movb %al, 0xa0(%rsp)
jmp 0x17e9
movq 0xe8(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x1a0a
leaq 0xa0(%rsp), %rdi
addq $0x8, %rdi
movq 0xb8(%rsp), %rsi
callq 0x1a50
leaq 0x70(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1b60
movq 0xd8(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
movq $0x0, 0xc8(%rsp)
movq 0xc8(%rsp), %rax
movq 0xe8(%rsp), %rcx
movq 0xd0(%rsp), %rdx
shlq $0x4, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x1a08
movq 0xf0(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq $0x4, %rcx
addq 0xc8(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movq 0xc8(%rsp), %rcx
movzbl 0x10(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0x110(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq $0x4, %rcx
addq 0xc8(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0xc8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc8(%rsp)
jmp 0x197b
jmp 0x1a0a
movq 0x110(%rsp), %rax
movq 0xe8(%rsp), %rcx
movq (%rsp), %rdx
movq %rdx, (%rax,%rcx)
movq 0x8(%rsp), %rdx
movq %rdx, 0x8(%rax,%rcx)
movq 0xe8(%rsp), %rcx
addq $0x10, %rcx
movq 0x118(%rsp), %rax
movq %rcx, (%rax)
addq $0x108, %rsp            # imm = 0x108
retq
nopl (%rax,%rax)