// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/deoxysneq128128v13/ref/deoxys.c
// Function: deoxys_aead_decrypt
int deoxys_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)
{

    uint64_t i;
    uint64_t j;
    uint8_t tweak[16];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[16];
    uint8_t last_block[16];
    uint8_t Checksum[16];
    uint8_t Final[16];
    uint8_t zero_block[16];
    uint8_t Pad[16];
    uint8_t Tag[16];
    uint8_t temp[16];

    /* Get the tag from the last 16 bytes of the ciphertext */
    memcpy(Tag, ciphertext+c_len-16, 16);

    /* Update c_len to the actual size of the ciphertext (i.e., without the tag) */
    c_len-=16;

    /* Fill the tweak with zeros (no nonce !!!) */
    memset(tweak, 0, sizeof(tweak));


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

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

    if(ass_data_len) {

        set_stage_in_tweak(tweak, MSB_AD);
        i=0;
        while (16*(i+1) <= ass_data_len) {
            set_block_number_in_tweak(tweak, i );
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt(TWEAKEY_STATE_SIZE, ass_data+16*i, tweakey, temp);
            xor_values(Auth, temp);
            i++;
        }

        /* Last block if incomplete */
        if ( ass_data_len > 16*i ) {
            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;
            set_stage_in_tweak(tweak, MSB_AD_LAST);
            set_block_number_in_tweak(tweak, i );
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt(TWEAKEY_STATE_SIZE, last_block, tweakey, temp);
            xor_values(Auth, temp);
        }

    } /* if ass_data_len>0 */

    /* Ciphertext */
    memset(tweak, 0, sizeof(tweak));
    set_nonce_in_tweak(tweak, nonce);
    
    
    memset(Checksum, 0, 16);
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while(16*(i+1)<=c_len) {
        set_tweak_in_tweakey(tweakey, tweak);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakDecrypt(TWEAKEY_STATE_SIZE, ciphertext+16*i, tweakey, message+16*i);
        xor_values(Checksum, message+16*i);
        i++;
    }

    /* Last block */
    /* If the block is full, i.e. M_last=epsilon */
    if (c_len == 16*i) {
        set_block_number_in_tweak(tweak, i);
        set_stage_in_tweak(tweak, MSB_CHKSUM_FULL);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);
        xor_values(Final, Auth);
        
        /* If the tags does not match, return error -1 */
        if( 0 != memcmp_const(Final, Tag, sizeof(Tag)) ) {
            memset( message, 0, c_len);
            return -1;
        }
        
    }
    else {
        
        /* Prepare the full-zero block */
        memset(zero_block, 0, 16);

        /* Prepare the tweak */
        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);

        /* Encrypt */
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, zero_block, tweakey, Pad);

        /* XOR the partial ciphertext */
        memset(last_block, 0, 16);
        memcpy(last_block, ciphertext+16*i, c_len-16*i);
        memset(Pad+c_len-16*i, 0, 16-(c_len-16*i));
        xor_values(last_block, Pad);
        last_block[c_len-16*i]=0x80;

        for (j=0; j<c_len-16*i; j++) {
            message[16*i+j]=last_block[j];
        }

        /* Update checksum */
        xor_values(Checksum, last_block);

        /* Verify the tag */
        set_stage_in_tweak(tweak, MSB_CHKSUM_NON_FULL);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);
        xor_values(Final, Auth);

        /* If the tags does not match, return error -1 */
        if( 0 != memcmp_const(Final, Tag, sizeof(Tag)) ) {
	    memset( message, 0, c_len );
            return -1;
        }
    }

    *m_len=c_len;
    return 0;
}

// Assembly:
subq $0xf8, %rsp
movq 0x108(%rsp), %rax
movq 0x100(%rsp), %rax
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq %rdx, 0xd8(%rsp)
movq %rcx, 0xd0(%rsp)
movq %r8, 0xc8(%rsp)
movq %r9, 0xc0(%rsp)
movq 0x100(%rsp), %rax
movq 0x108(%rsp), %rcx
movq -0x10(%rax,%rcx), %rdx
movq %rdx, 0x10(%rsp)
movq -0x8(%rax,%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x108(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x108(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
movq 0xc8(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x80(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x88(%rsp)
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
cmpq $0x0, 0xe0(%rsp)
je 0x1d4d
leaq 0xa0(%rsp), %rdi
movl $0x20, %esi
callq 0x17e0
movq $0x0, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0xe0(%rsp), %rax
ja 0x1c66
leaq 0xa0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x1810
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1900
movq 0xe8(%rsp), %rsi
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x80(%rsp), %rdx
movq %rsp, %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %rsp, %rsi
callq 0x1930
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
jmp 0x1bd0
movq 0xe0(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x1d4b
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0x60(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0xe0(%rsp), %rdx
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
movq 0xe0(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x60(%rsp,%rax)
leaq 0xa0(%rsp), %rdi
movl $0x60, %esi
callq 0x17e0
leaq 0xa0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x1810
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1900
leaq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %rsp, %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %rsp, %rsi
callq 0x1930
jmp 0x1d4d
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0xa0(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x1980
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x17e0
movq $0x0, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0x108(%rsp), %rax
ja 0x1e79
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1900
leaq 0xa0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x1810
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1900
movq 0x100(%rsp), %rsi
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x80(%rsp), %rdx
movq 0xd8(%rsp), %rcx
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x1060
leaq 0x50(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x1930
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
jmp 0x1da2
movq 0x108(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jne 0x1f45
leaq 0xa0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x1810
leaq 0xa0(%rsp), %rdi
movl $0x10, %esi
callq 0x17e0
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1900
leaq 0x50(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1930
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x10, %edx
callq 0x21b0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x1f40
movq 0xd8(%rsp), %rdi
movq 0x108(%rsp), %rdx
xorl %esi, %esi
callq 0x1050
movl $0xffffffff, 0xf4(%rsp) # imm = 0xFFFFFFFF
jmp 0x219b
jmp 0x217d
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0xa0(%rsp), %rdi
movl $0x40, %esi
callq 0x17e0
leaq 0xa0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x1810
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1900
leaq 0x30(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0x60(%rsp), %rdi
movq 0x100(%rsp), %rsi
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0x108(%rsp), %rdx
movq 0xb8(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
leaq 0x20(%rsp), %rdi
addq 0x108(%rsp), %rdi
movq 0xb8(%rsp), %rcx
shlq $0x4, %rcx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rdi
movq 0x108(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movl $0x10, %edx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1050
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x1930
movq 0x108(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x60(%rsp,%rax)
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq 0x108(%rsp), %rcx
movq 0xb8(%rsp), %rdx
shlq $0x4, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x20da
movq 0xb0(%rsp), %rax
movb 0x60(%rsp,%rax), %dl
movq 0xd8(%rsp), %rax
movq 0xb8(%rsp), %rcx
shlq $0x4, %rcx
addq 0xb0(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x2075
leaq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1930
leaq 0xa0(%rsp), %rdi
movl $0x50, %esi
callq 0x17e0
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1900
leaq 0x50(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1930
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x10, %edx
callq 0x21b0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x217b
movq 0xd8(%rsp), %rdi
movq 0x108(%rsp), %rdx
xorl %esi, %esi
callq 0x1050
movl $0xffffffff, 0xf4(%rsp) # imm = 0xFFFFFFFF
jmp 0x219b
jmp 0x217d
movq 0x108(%rsp), %rcx
movq 0xd0(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addq $0xf8, %rsp
retq
nopw (%rax,%rax)