// File: /workspace/github_c_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rcx, 0xc0(%rsp)
movq %rdx, 0x40(%rsp)
movq 0x170(%rsp), %r10
movq 0x178(%rsp), %rcx
vmovups -0x10(%r10,%rcx), %xmm0
vmovaps %xmm0, 0xd0(%rsp)
vmovups (%r8), %xmm0
addq $-0x10, %rcx
vmovaps %xmm0, 0x10(%rsp)
vxorps %xmm3, %xmm3, %xmm3
vxorps %xmm0, %xmm0, %xmm0
testq %rsi, %rsi
movq %rcx, 0x38(%rsp)
je 0x1a61
movq %rsi, %rbp
movq %rdi, %r15
movq %r9, 0x30(%rsp)
xorl %r12d, %r12d
cmpq $0x10, %rsi
jae 0x1908
xorl %ebx, %ebx
jmp 0x19a0
movl $0x10, %r13d
leaq 0x21(%rsp), %r14
xorl %ebx, %ebx
vmovaps %xmm0, 0x50(%rsp)
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x36cd(%rip), %ymm0, %ymm0 # 0x5000
movb $0x20, 0x20(%rsp)
movl %r12d, 0x3(%r14)
movl %r12d, (%r14)
movb %al, 0x28(%rsp)
vpmovqb %ymm0, 0x29(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x2d(%rsp)
movb %bh, 0x2e(%rsp)
movb %bl, 0x2f(%rsp)
leaq (%r15,%r13), %rsi
addq $-0x10, %rsi
movl $0x100, %edi            # imm = 0x100
leaq 0x10(%rsp), %rdx
leaq 0xf0(%rsp), %rcx
vzeroupper
callq 0x10d0
vmovaps 0x50(%rsp), %xmm0
vxorps 0xf0(%rsp), %xmm0, %xmm0
incq %rbx
addq $0x10, %r13
cmpq %rbp, %r13
jbe 0x1915
movq %rbx, %rax
shlq $0x4, %rax
subq %rax, %rbp
jbe 0x1a4b
vmovaps %xmm0, 0x50(%rsp)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x80(%rsp), %r14
vmovaps %xmm0, (%r14)
addq %rax, %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x1100
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3610(%rip), %ymm0, %ymm0 # 0x5000
leaq 0x10(%rsp), %rdx
movb $0x60, 0x10(%rdx)
xorl %ecx, %ecx
movl %ecx, 0x11(%rdx)
movl %ecx, 0x14(%rdx)
movb %al, 0x18(%rdx)
vpmovqb %ymm0, 0x19(%rdx)
movb $-0x80, 0x80(%rsp,%rbp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x1d(%rdx)
movb %bh, 0x2e(%rsp)
movb %bl, 0x1f(%rdx)
leaq 0xf0(%rsp), %r15
movl $0x100, %edi            # imm = 0x100
movq %r14, %rsi
movq %r15, %rcx
vzeroupper
callq 0x10d0
vmovaps 0x50(%rsp), %xmm0
vxorps (%r15), %xmm0, %xmm0
movq 0x38(%rsp), %rcx
movq 0x170(%rsp), %r10
movq 0x30(%rsp), %r9
vxorps %xmm3, %xmm3, %xmm3
vmovaps %xmm0, 0x50(%rsp)
movl (%r9), %ebp
vmovd %ebp, %xmm0
shrb $0x4, %bpl
movb 0x4(%r9), %r14b
vmovd 0x1(%r9), %xmm1
vpsrlw $0x4, %xmm1, %xmm1
vpsllw $0x4, %xmm0, %xmm2
vpternlogd $0xe4, 0x368f(%rip){1to4}, %xmm1, %xmm2 # 0x5120
movb 0x5(%r9), %dil
movl %edi, %eax
shrb $0x4, %al
shlb $0x4, %r14b
orb %al, %r14b
movb 0x6(%r9), %r8b
movl %r8d, %eax
shrb $0x4, %al
shlb $0x4, %dil
orb %al, %dil
movb 0x7(%r9), %r13b
movl %r13d, %eax
shrb $0x4, %al
shlb $0x4, %r8b
orb %al, %r8b
shlb $0x4, %r13b
vmovaps %xmm3, 0x70(%rsp)
cmpq $0x10, %rcx
movb %dil, 0x30(%rsp)
vmovdqa %xmm2, 0xe0(%rsp)
movb %r8b, 0xa(%rsp)
jae 0x1aed
xorl %edx, %edx
jmp 0x1bef
xorl %r15d, %r15d
xorl %edx, %edx
movl %r13d, %ebx
movb %r14b, 0xb(%rsp)
movq %rbp, 0xc8(%rsp)
movq 0x40(%rsp), %r12
movb %bpl, 0x20(%rsp)
vmovd %xmm2, 0x21(%rsp)
movb %r14b, 0x25(%rsp)
movb %dil, 0x26(%rsp)
movb %r8b, 0x27(%rsp)
movl %ebx, %r13d
andb $-0x10, %r13b
movq %rdx, %rbx
shrq $0x38, %rbx
andb $0xf, %bl
orb %r13b, %bl
movq %rdx, %rax
shrq $0x30, %rax
movq %rdx, %rcx
shrq $0x28, %rcx
movq %rdx, %r14
shrq $0x20, %rdx
movl %r14d, %esi
shrl $0x18, %esi
movl %r14d, %edi
shrl $0x10, %edi
movl %r14d, %r8d
shrl $0x8, %r8d
movb %bl, 0x28(%rsp)
movb %al, 0x29(%rsp)
movb %cl, 0x2a(%rsp)
movb %dl, 0x2b(%rsp)
movb %sil, 0x2c(%rsp)
movb %dil, 0x2d(%rsp)
movb %r8b, 0x2e(%rsp)
movb %r14b, 0x2f(%rsp)
leaq (%r10,%r15), %rsi
leaq (%r12,%r15), %rcx
movl $0x100, %edi            # imm = 0x100
leaq 0x10(%rsp), %rdx
movq %r10, %rbp
callq 0x1060
movq %r14, %rdx
movb 0xa(%rsp), %r8b
vmovdqa 0xe0(%rsp), %xmm2
movb 0x30(%rsp), %dil
movq %rbp, %r10
movq 0xc8(%rsp), %rbp
movb 0xb(%rsp), %r14b
movq 0x38(%rsp), %rcx
vmovdqa 0x70(%rsp), %xmm0
vpxor (%r12,%r15), %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp)
incq %rdx
leaq 0x10(%r15), %rax
addq $0x20, %r15
cmpq %rcx, %r15
movq %rax, %r15
jbe 0x1b07
movl %r13d, %r9d
movq %rdx, %rsi
shlq $0x4, %rsi
movq %rcx, %r13
subq %rsi, %r13
jne 0x1cac
movq %rcx, %r15
movq %rdx, %rax
shrq $0x38, %rax
andb $0xf, %al
orb %r9b, %al
vpbroadcastq %rdx, %ymm0
vpsrlvq 0x33dd(%rip), %ymm0, %ymm0 # 0x5000
orb $0x10, %bpl
movq %rdx, %rcx
leaq 0x10(%rsp), %rdx
movb %bpl, 0x10(%rdx)
vmovd %xmm2, 0x11(%rdx)
movb %r14b, 0x15(%rdx)
movb %dil, 0x16(%rdx)
movb %r8b, 0x17(%rdx)
movb %al, 0x18(%rdx)
vpmovqb %ymm0, 0x19(%rdx)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x1d(%rdx)
movb %ch, 0x2e(%rsp)
movb %cl, 0x1f(%rdx)
leaq 0x70(%rsp), %rsi
leaq 0x120(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rcx
vzeroupper
callq 0x10d0
vmovdqa 0x50(%rsp), %xmm0
vpxor (%rbx), %xmm0, %xmm0
vmovdqa %xmm0, (%rbx)
vpxor 0xd0(%rsp), %xmm0, %xmm0
vptest %xmm0, %xmm0
movq 0x40(%rsp), %rdi
movq %r15, %rdx
jne 0x1ef0
jmp 0x1efe
vpxor %xmm0, %xmm0, %xmm0
leaq 0x110(%rsp), %rcx
vmovdqa %xmm0, (%rcx)
movq %rsi, 0x98(%rsp)
leal 0x40(%rbp), %eax
movl %eax, 0xc(%rsp)
movq %rdx, %rax
shrq $0x38, %rax
andb $0xf, %al
orb %r9b, %al
movq %rax, 0x68(%rsp)
movq %rdx, %rsi
shrq $0x30, %rsi
movq %rsi, 0xb8(%rsp)
movq %rdx, %r11
shrq $0x28, %r11
movq %r11, 0xb0(%rsp)
movq %rdx, %rbx
shrq $0x20, %rbx
movq %rbx, 0xa8(%rsp)
movl %edx, %r12d
shrl $0x18, %r12d
movl %r12d, 0x4c(%rsp)
movl %edx, %ecx
shrl $0x10, %ecx
movl %ecx, 0x48(%rsp)
movl %edx, %eax
leaq 0x10(%rsp), %r15
movl 0xc(%rsp), %r9d
movb %r9b, 0x10(%r15)
vmovd %xmm2, 0x11(%r15)
shrl $0x8, %eax
movl %eax, 0xc(%rsp)
movb %r14b, 0x15(%r15)
movb %dil, 0x16(%r15)
movb %r8b, 0x17(%r15)
movq 0x68(%rsp), %r9
movb %r9b, 0x18(%r15)
movb %sil, 0x19(%r15)
movb %r11b, 0x1a(%r15)
movb %bl, 0x1b(%r15)
movb %r12b, 0x1c(%r15)
movb %cl, 0x1d(%r15)
movb %al, 0x1e(%r15)
movb %dl, 0x1f(%r15)
leaq 0x100(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
leaq 0x110(%rsp), %rsi
movq %rdx, 0xa0(%rsp)
movq %r15, %rdx
movq %r10, %rbx
callq 0x10d0
leaq 0x80(%rsp), %rdi
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%rdi)
movq 0x98(%rsp), %r12
addq %r12, %rbx
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1100
movq 0x38(%rsp), %rax
leaq (%rsp,%rax), %rdi
addq $0x100, %rdi            # imm = 0x100
movq %r12, %rbx
subq %r12, %rdi
movl $0x10, %edx
subq %r13, %rdx
xorl %esi, %esi
callq 0x1050
leaq 0x80(%rsp), %rax
vmovaps (%rax), %xmm0
leaq 0x100(%rsp), %rcx
vxorps (%rcx), %xmm0, %xmm0
vmovaps %xmm0, (%rax)
movb $-0x80, 0x80(%rsp,%r13)
movq 0x40(%rsp), %r12
addq %r12, %rbx
movq %rbx, %rdi
cmpq $0x1, %r13
adcq $0x0, %r13
movq %rax, %rbx
movq %rax, %rsi
movq %r13, %rdx
callq 0x1100
leaq 0x70(%rsp), %rsi
vmovaps (%rsi), %xmm0
vxorps (%rbx), %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
orb $0x50, %bpl
movb %bpl, 0x10(%r15)
vmovaps 0xe0(%rsp), %xmm0
vmovss %xmm0, 0x11(%r15)
movb %r14b, 0x15(%r15)
movb 0x30(%rsp), %al
movb %al, 0x16(%r15)
movb 0xa(%rsp), %al
movb %al, 0x17(%r15)
movq 0x68(%rsp), %rax
movb %al, 0x18(%r15)
movq 0xb8(%rsp), %rax
movb %al, 0x19(%r15)
movq 0xb0(%rsp), %rax
movb %al, 0x1a(%r15)
movq 0xa8(%rsp), %rax
movb %al, 0x1b(%r15)
movl 0x4c(%rsp), %eax
movb %al, 0x1c(%r15)
movl 0x48(%rsp), %eax
movb %al, 0x1d(%r15)
movl 0xc(%rsp), %eax
movb %al, 0x1e(%r15)
movq 0xa0(%rsp), %rax
movb %al, 0x1f(%r15)
leaq 0x120(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %r15, %rdx
movq %rbx, %rcx
callq 0x10d0
movq 0x38(%rsp), %rdx
vmovdqa 0x50(%rsp), %xmm0
vpxor (%rbx), %xmm0, %xmm0
vmovdqa %xmm0, (%rbx)
vpxor 0xd0(%rsp), %xmm0, %xmm0
vptest %xmm0, %xmm0
je 0x1efe
movq %r12, %rdi
xorl %esi, %esi
callq 0x1050
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1f0b
movq 0xc0(%rsp), %rax
movq %rdx, (%rax)
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)