// File: /workspace/github_c_makefile_star2_O3/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 $0x128, %rsp            # imm = 0x128
movq %rcx, 0xb0(%rsp)
movq %rdx, 0x40(%rsp)
movq 0x160(%rsp), %rdx
movq 0x168(%rsp), %rcx
vmovups -0x10(%rdx,%rcx), %xmm0
vmovaps %xmm0, 0xc0(%rsp)
vmovups (%r8), %xmm0
addq $-0x10, %rcx
vmovaps %xmm0, 0x20(%rsp)
vxorps %xmm3, %xmm3, %xmm3
vxorps %xmm0, %xmm0, %xmm0
testq %rsi, %rsi
movq %rcx, 0x8(%rsp)
je 0x1a60
movq %rsi, %rbp
movq %rdi, %r15
movq %r9, 0x18(%rsp)
xorl %r12d, %r12d
cmpq $0x10, %rsi
jae 0x1907
xorl %ebx, %ebx
jmp 0x199f
movl $0x10, %r13d
leaq 0x31(%rsp), %r14
xorl %ebx, %ebx
vmovaps %xmm0, 0x60(%rsp)
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x36ce(%rip), %ymm0, %ymm0 # 0x5000
movb $0x20, 0x30(%rsp)
movl %r12d, 0x3(%r14)
movl %r12d, (%r14)
movb %al, 0x38(%rsp)
vpmovqb %ymm0, 0x39(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x3d(%rsp)
movb %bh, 0x3e(%rsp)
movb %bl, 0x3f(%rsp)
leaq (%r15,%r13), %rsi
addq $-0x10, %rsi
movl $0x100, %edi            # imm = 0x100
leaq 0x20(%rsp), %rdx
leaq 0xe0(%rsp), %rcx
vzeroupper
callq 0x10d0
vmovaps 0x60(%rsp), %xmm0
vxorps 0xe0(%rsp), %xmm0, %xmm0
incq %rbx
addq $0x10, %r13
cmpq %rbp, %r13
jbe 0x1914
movq %rbx, %rax
shlq $0x4, %rax
subq %rax, %rbp
jbe 0x1a4a
vmovaps %xmm0, 0x60(%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 0x3611(%rip), %ymm0, %ymm0 # 0x5000
leaq 0x20(%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, 0x3e(%rsp)
movb %bl, 0x1f(%rdx)
leaq 0xe0(%rsp), %r15
movl $0x100, %edi            # imm = 0x100
movq %r14, %rsi
movq %r15, %rcx
vzeroupper
callq 0x10d0
vmovaps 0x60(%rsp), %xmm0
vxorps (%r15), %xmm0, %xmm0
movq 0x8(%rsp), %rcx
movq 0x160(%rsp), %rdx
movq 0x18(%rsp), %r9
vxorps %xmm3, %xmm3, %xmm3
vmovaps %xmm0, 0x60(%rsp)
movl (%r9), %ebp
vmovd %ebp, %xmm0
shrb $0x4, %bpl
movb 0x4(%r9), %r8b
vmovd 0x1(%r9), %xmm1
vpsrlw $0x4, %xmm1, %xmm1
vpsllw $0x4, %xmm0, %xmm2
vpternlogd $0xe4, 0x3690(%rip){1to4}, %xmm1, %xmm2 # 0x5120
movb 0x5(%r9), %r11b
movl %r11d, %eax
shrb $0x4, %al
shlb $0x4, %r8b
orb %al, %r8b
movb 0x6(%r9), %r10b
movl %r10d, %eax
shrb $0x4, %al
shlb $0x4, %r11b
orb %al, %r11b
movb 0x7(%r9), %dil
movl %edi, %eax
shrb $0x4, %al
shlb $0x4, %r10b
orb %al, %r10b
shlb $0x4, %dil
vmovaps %xmm3, 0x70(%rsp)
cmpq $0x10, %rcx
vmovdqa %xmm2, 0xd0(%rsp)
jae 0x1ae2
xorl %ebx, %ebx
jmp 0x1bcd
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %rbp, 0xb8(%rsp)
movb %dil, 0x48(%rsp)
movq 0x40(%rsp), %r13
movq %rbx, 0x18(%rsp)
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
orb %dil, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x34e7(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %ecx
movb %bpl, 0x30(%rsp)
vmovd %xmm2, 0x31(%rsp)
movb %r8b, 0x35(%rsp)
movb %r11b, 0x36(%rsp)
movb %r10b, 0x37(%rsp)
movb %al, 0x38(%rsp)
vpmovqb %ymm0, 0x39(%rsp)
shrl $0x10, %ecx
movb %cl, 0x3d(%rsp)
movb %bh, 0x3e(%rsp)
movb %bl, 0x3f(%rsp)
movq %rdx, %r15
leaq (%rdx,%r14), %rsi
leaq (%r13,%r14), %rcx
movl $0x100, %edi            # imm = 0x100
leaq 0x20(%rsp), %rdx
movl %r8d, %r12d
movl %r11d, %ebp
movl %r10d, %ebx
vzeroupper
callq 0x1060
movb 0x48(%rsp), %dil
movl %ebx, %r10d
movq 0x18(%rsp), %rbx
vmovdqa 0xd0(%rsp), %xmm2
movl %ebp, %r11d
movl %r12d, %r8d
movq 0xb8(%rsp), %rbp
movq %r15, %rdx
vmovdqa 0x70(%rsp), %xmm0
vpxor (%r13,%r14), %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp)
incq %rbx
leaq 0x10(%r14), %rax
addq $0x20, %r14
cmpq 0x8(%rsp), %r14
movq %rax, %r14
jbe 0x1af9
movq %rbx, %r12
shlq $0x4, %r12
movq 0x8(%rsp), %r14
subq %r12, %r14
jne 0x1c80
movq %rbx, %rax
shrq $0x38, %rax
andb $0xf, %al
orb %dil, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3403(%rip), %ymm0, %ymm0 # 0x5000
orb $0x10, %bpl
leaq 0x20(%rsp), %rdx
movb %bpl, 0x10(%rdx)
vmovd %xmm2, 0x11(%rdx)
movb %r8b, 0x15(%rdx)
movb %r11b, 0x16(%rdx)
movb %r10b, 0x17(%rdx)
movb %al, 0x18(%rdx)
vpmovqb %ymm0, 0x19(%rdx)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x1d(%rdx)
movb %bh, 0x3e(%rsp)
movb %bl, 0x1f(%rdx)
leaq 0x70(%rsp), %rsi
leaq 0x110(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rcx
vzeroupper
callq 0x10d0
vmovdqa 0x60(%rsp), %xmm0
vpxor (%rbx), %xmm0, %xmm0
vmovdqa %xmm0, (%rbx)
vpxor 0xc0(%rsp), %xmm0, %xmm0
vptest %xmm0, %xmm0
movq 0x40(%rsp), %rdi
jne 0x1eb4
jmp 0x1ec7
vpxor %xmm0, %xmm0, %xmm0
leaq 0x100(%rsp), %rax
vmovdqa %xmm0, (%rax)
movq %r12, 0x98(%rsp)
leal 0x40(%rbp), %eax
movl %eax, 0x14(%rsp)
movq %rbx, %r12
shrq $0x38, %r12
andb $0xf, %r12b
orb %dil, %r12b
movq %r12, 0x18(%rsp)
movq %rbx, %rsi
shrq $0x30, %rsi
movq %rsi, 0x48(%rsp)
movq %rbx, %rdi
shrq $0x28, %rdi
movq %rdi, 0xa8(%rsp)
movq %rbx, %r9
shrq $0x20, %r9
movq %r9, 0xa0(%rsp)
movl %ebx, %ecx
shrl $0x18, %ecx
movl %ecx, 0x5c(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movl %eax, 0x58(%rsp)
movl %ebx, %edx
leaq 0x20(%rsp), %r15
movl 0x14(%rsp), %r13d
movb %r13b, 0x10(%r15)
vmovd %xmm2, 0x11(%r15)
shrl $0x8, %edx
movl %edx, 0x54(%rsp)
movb %r8b, 0x15(%r15)
movb %r11b, 0x16(%r15)
movb %r10b, 0x17(%r15)
movb %r12b, 0x18(%r15)
movb %sil, 0x19(%r15)
movb %dil, 0x1a(%r15)
movb %r9b, 0x1b(%r15)
movb %cl, 0x1c(%r15)
movb %al, 0x1d(%r15)
movb %dl, 0x1e(%r15)
movb %bl, 0x1f(%r15)
leaq 0xf0(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
leaq 0x100(%rsp), %rsi
movq %r15, %rdx
movb %r8b, 0x6(%rsp)
movb %r11b, 0x7(%rsp)
movb %r10b, 0x14(%rsp)
callq 0x10d0
leaq 0x80(%rsp), %r13
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, (%r13)
movq 0x160(%rsp), %rsi
movq 0x98(%rsp), %r12
addq %r12, %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x1100
movq 0x8(%rsp), %rax
leaq (%rsp,%rax), %rdi
addq $0xf0, %rdi
subq %r12, %rdi
movl $0x10, %edx
subq %r14, %rdx
xorl %esi, %esi
callq 0x1050
vmovaps (%r13), %xmm0
leaq 0xf0(%rsp), %rcx
vxorps (%rcx), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
movb $-0x80, 0x80(%rsp,%r14)
addq 0x40(%rsp), %r12
cmpq $0x1, %r14
adcq $0x0, %r14
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1100
leaq 0x70(%rsp), %rsi
vmovaps (%rsi), %xmm0
vxorps (%r13), %xmm0, %xmm0
vmovaps %xmm0, (%rsi)
orb $0x50, %bpl
movb %bpl, 0x10(%r15)
vmovaps 0xd0(%rsp), %xmm0
vmovss %xmm0, 0x11(%r15)
movb 0x6(%rsp), %al
movb %al, 0x15(%r15)
movb 0x7(%rsp), %al
movb %al, 0x16(%r15)
movb 0x14(%rsp), %al
movb %al, 0x17(%r15)
movq 0x18(%rsp), %rax
movb %al, 0x18(%r15)
movq 0x48(%rsp), %rax
movb %al, 0x19(%r15)
movq 0xa8(%rsp), %rax
movb %al, 0x1a(%r15)
movq 0xa0(%rsp), %rax
movb %al, 0x1b(%r15)
movl 0x5c(%rsp), %eax
movb %al, 0x1c(%r15)
movl 0x58(%rsp), %eax
movb %al, 0x1d(%r15)
movl 0x54(%rsp), %eax
movb %al, 0x1e(%r15)
movb %bl, 0x1f(%r15)
leaq 0x110(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %r15, %rdx
movq %rbx, %rcx
callq 0x10d0
vmovdqa 0x60(%rsp), %xmm0
vpxor (%rbx), %xmm0, %xmm0
vmovdqa %xmm0, (%rbx)
vpxor 0xc0(%rsp), %xmm0, %xmm0
vptest %xmm0, %xmm0
je 0x1ec7
movq 0x40(%rsp), %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x1050
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1ed9
movq 0xb0(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop