// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/joltikeq6464v13/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)
{

    uint32_t i,j;
    uint64_t hblock;
    uint8_t tweak[8];
    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 temp[8];
    uint8_t tag[8];

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

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

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

    /* The first two blocks with Nonce plaintext */
    set_stage_in_tweak(tweak, MSB_AD_N1);
    set_block_number_in_tweak_4(tweak, 0 );
    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);
        
        i = 0;
        while (8*(i+1) <= ass_data_len) {
            set_block_number_in_tweak_4 (tweak, i);
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt (TWEAKEY_STATE_SIZE, ass_data+8*i, tweakey, temp);
            xor_values (Auth, temp);
            i++;
        }
        
        /* Last incomplete BLOCK */
        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_4(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
     * first pass
     */
    
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (8*(i+1) <= m_len) {
        set_block_number_in_tweak_4(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message + 8*i, tweakey, temp);
        xor_values(Auth, temp );
        i++;
    }
    
    /* Process incomplete block */
    if (m_len >  8*i) {
        
        /* Prepare the last padded block */
        memset(last_block, 0, 8);
        memcpy(last_block, message+8*i, m_len-8*i);
        last_block[m_len-8*i]=0x80;
        
        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO );
        set_block_number_in_tweak_4(tweak, 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_4(tweak, 0 );
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, Auth, tweakey, tag );
    

    
    /* Message
     * second pass
     */
    
    i = 0;
    hblock = get_block_number_from_tag( tag );
    while (8*(i+1) <= m_len) {
        set_block_number_in_tweak_1(tweak, hblock );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, ciphertext + 8*i );
        xor_values(ciphertext + 8*i, message + 8*i );
        i++;
        hblock++;
    }
    
    /* Impartial block */
    if (m_len >  8*i) {
        set_block_number_in_tweak_1(tweak, hblock );
	set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, temp );
        
        for( j = 0; j < m_len - 8 * i; j ++)
            ciphertext[ 8 * i + j ] = message[ 8 * i + j] ^ temp[j];
    } 
    
    /* Append the authentication tag to the ciphertext */
    memcpy( ciphertext + m_len, tag, 8 );

    /* Update the size of the ciphertext (incl. tag) */
    *c_len=8+m_len;
}

// Assembly:
subq $0x88, %rsp
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %rax
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movq %r9, 0x58(%rsp)
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
movq 0x60(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
leaq 0x40(%rsp), %rdi
movl $0x30, %esi
callq 0x1a50
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a80
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
movq 0x58(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
leaq 0x40(%rsp), %rdi
movl $0x70, %esi
callq 0x1a50
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
movq 0x58(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
cmpq $0x0, 0x78(%rsp)
je 0x171f
leaq 0x40(%rsp), %rdi
movl $0x20, %esi
callq 0x1a50
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
shll $0x3, %eax
movl %eax, %eax
cmpq 0x78(%rsp), %rax
ja 0x165a
leaq 0x40(%rsp), %rdi
movl 0x54(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
movq 0x80(%rsp), %rsi
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x15e4
movq 0x78(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x171d
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
leaq 0x20(%rsp), %rdi
movq 0x80(%rsp), %rsi
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
movq 0x78(%rsp), %rdx
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
subq %rax, %rdx
callq 0x1130
movq 0x78(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
subq %rcx, %rax
movb $-0x80, 0x20(%rsp,%rax)
leaq 0x40(%rsp), %rdi
movl $0x60, %esi
callq 0x1a50
leaq 0x40(%rsp), %rdi
movl 0x54(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
jmp 0x171f
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x1a50
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
shll $0x3, %eax
movl %eax, %eax
cmpq 0x68(%rsp), %rax
ja 0x17a6
leaq 0x40(%rsp), %rdi
movl 0x54(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
movq 0x70(%rsp), %rsi
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x1733
movq 0x68(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x1866
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
leaq 0x20(%rsp), %rdi
movq 0x70(%rsp), %rsi
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
movq 0x68(%rsp), %rdx
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
subq %rax, %rdx
callq 0x1130
movq 0x68(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
subq %rcx, %rax
movb $-0x80, 0x20(%rsp,%rax)
leaq 0x40(%rsp), %rdi
movl $0x40, %esi
callq 0x1a50
leaq 0x40(%rsp), %rdi
movl 0x54(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
leaq 0x40(%rsp), %rdi
movl $0x10, %esi
callq 0x1a50
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a80
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
leaq 0x28(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq %rsp, %rcx
movl $0x80, %edi
callq 0x10d0
movl $0x0, 0x54(%rsp)
movq %rsp, %rdi
callq 0x1030
movq %rax, 0x48(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
shll $0x3, %eax
movl %eax, %eax
cmpq 0x68(%rsp), %rax
ja 0x1963
leaq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1be0
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
movq 0x58(%rsp), %rsi
leaq 0x30(%rsp), %rdx
movq 0x90(%rsp), %rcx
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rcx
movl $0x80, %edi
callq 0x10d0
movq 0x90(%rsp), %rdi
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rdi
movq 0x70(%rsp), %rsi
movl 0x54(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x1b90
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x18be
movq 0x68(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x1a1d
leaq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1be0
leaq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1b70
movq 0x58(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
movq 0x68(%rsp), %rcx
movl 0x54(%rsp), %edx
shll $0x3, %edx
movl %edx, %edx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x1a1b
movq 0x70(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x3, %ecx
addl 0x50(%rsp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl 0x50(%rsp), %ecx
movzbl 0x8(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0x90(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x3, %ecx
addl 0x50(%rsp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x19b9
jmp 0x1a1d
movq 0x90(%rsp), %rax
movq 0x68(%rsp), %rcx
movq (%rsp), %rdx
movq %rdx, (%rax,%rcx)
movq 0x68(%rsp), %rcx
addq $0x8, %rcx
movq 0x98(%rsp), %rax
movq %rcx, (%rax)
addq $0x88, %rsp
retq
nop