// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/joltikeq6464v13/ref/joltik.c
// Function: joltik_aead_decrypt
int joltik_aead_decrypt(const uint8_t *ass_data, size_t ass_data_len,
                        uint8_t *message, size_t *m_len,
                        const uint8_t *key,
                        const uint8_t *nonce,
                        const 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 zero_block[8];
    uint8_t Pad[8];
    uint8_t Tag[8], tag[8];
    uint8_t temp[8];
    
    /* Get the tag from the last 8 bytes of the ciphertext */
    memcpy(Tag, ciphertext+c_len-8, 8);

    /* Update c_len to the actual size of the ciphertext (i.e., without the tag) */
    c_len-=8;
    
    /* Fill the tweak with zeros (no 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 */
    

    
    /* Get the message
     * (from the second pass)
     */
    
    i = 0;
    hblock = get_block_number_from_tag( Tag );
    while (8*(i+1) <= c_len) {
        set_block_number_in_tweak_1(tweak, hblock );
	set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, message + 8*i );
        xor_values( message + 8*i  , ciphertext + 8*i);
        i++;
        hblock++;
    }
    
    /* Impartial block */
    if (c_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 < c_len - 8 * i; j ++)
            message[ 8 * i + j ] = ciphertext[ 8 * i + j] ^ temp[j];
        
    } 
    

    
    /* Message
     * first pass
     */
    
    
    memset(tweak, 0, sizeof(tweak));
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (8*(i+1) <= c_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 (c_len >  8*i) {
        
        /* Prepare the last padded block */
        memset(last_block, 0, 8);
        memcpy(last_block, message+8*i, c_len-8*i);
        last_block[c_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 );
    
    
    /* If the tags does not match, return error -1 */
    if( 0 != memcmp_const(Tag, tag, sizeof(Tag)) ) {
        memset( message, 0, c_len );
        return -1;
    }
    
    *m_len=c_len;
    return 0;

}

// Assembly:
subq $0xb8, %rsp
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rax
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq -0x8(%rax,%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0xc8(%rsp), %rax
subq $0x8, %rax
movq %rax, 0xc8(%rsp)
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
movq 0x88(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
leaq 0x68(%rsp), %rdi
movl $0x30, %esi
callq 0x1a50
leaq 0x68(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a80
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
movq 0x80(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
leaq 0x68(%rsp), %rdi
movl $0x70, %esi
callq 0x1a50
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
movq 0x80(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
cmpq $0x0, 0xa0(%rsp)
je 0x1f74
leaq 0x68(%rsp), %rdi
movl $0x20, %esi
callq 0x1a50
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
shll $0x3, %eax
movl %eax, %eax
cmpq 0xa0(%rsp), %rax
ja 0x1ea6
leaq 0x68(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
movq 0xa8(%rsp), %rsi
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
leaq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x1e2d
movq 0xa0(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x1f72
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
leaq 0x40(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
movq 0xa0(%rsp), %rdx
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
subq %rax, %rdx
callq 0x1130
movq 0xa0(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
subq %rcx, %rax
movb $-0x80, 0x40(%rsp,%rax)
leaq 0x68(%rsp), %rdi
movl $0x60, %esi
callq 0x1a50
leaq 0x68(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
leaq 0x40(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
jmp 0x1f74
movl $0x0, 0x7c(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x1030
movq %rax, 0x70(%rsp)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
shll $0x3, %eax
movl %eax, %eax
cmpq 0xc8(%rsp), %rax
ja 0x2039
leaq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1be0
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
movq 0x80(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq 0x98(%rsp), %rcx
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rcx
movl $0x80, %edi
callq 0x10d0
movq 0x98(%rsp), %rdi
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rdi
movq 0xc0(%rsp), %rsi
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x1b90
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x1f8b
movq 0xc8(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x20ff
leaq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1be0
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
movq 0x80(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
movq 0xc8(%rsp), %rcx
movl 0x7c(%rsp), %edx
shll $0x3, %edx
movl %edx, %edx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x20fd
movq 0xc0(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
addl 0x78(%rsp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl 0x78(%rsp), %ecx
movzbl 0x8(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0x98(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
addl 0x78(%rsp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x2095
jmp 0x20ff
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x1a50
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
shll $0x3, %eax
movl %eax, %eax
cmpq 0xc8(%rsp), %rax
ja 0x219d
leaq 0x68(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
movq 0x98(%rsp), %rsi
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
leaq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x2124
movq 0xc8(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x2269
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1050
leaq 0x40(%rsp), %rdi
movq 0x98(%rsp), %rsi
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
addq %rax, %rsi
movq 0xc8(%rsp), %rdx
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
subq %rax, %rdx
callq 0x1130
movq 0xc8(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
subq %rcx, %rax
movb $-0x80, 0x40(%rsp,%rax)
leaq 0x68(%rsp), %rdi
movl $0x40, %esi
callq 0x1a50
leaq 0x68(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, %esi
callq 0x1a80
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
leaq 0x40(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1b90
leaq 0x68(%rsp), %rdi
movl $0x10, %esi
callq 0x1a50
leaq 0x68(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a80
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1b70
leaq 0x48(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x80, %edi
callq 0x10d0
leaq 0x18(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x8, %edx
callq 0x10f0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x22ee
movq 0x98(%rsp), %rdi
movq 0xc8(%rsp), %rdx
xorl %esi, %esi
callq 0x1050
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x230c
movq 0xc8(%rsp), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopl (%rax,%rax)