// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/deoxyseq256128v13/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;        
    uint64_t hblock1,hblock2;
    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], 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, 32);

     /* Associated data */
    memset(Auth, 0, 16);
    
    /* The first two blocks with Nonce plaintext */
    set_block_number_in_tweak(tweak + 0 , 0 );
    set_block_number_in_tweak(tweak + 8 , 0 );
    set_stage_in_tweak(tweak, MSB_AD_N1);
    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 (16*(i+1) <= ass_data_len) {
            set_block_number_in_tweak(tweak + 8 , 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 + 8 , i );
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt(TWEAKEY_STATE_SIZE, last_block, tweakey, temp);

            xor_values(Auth, temp);
        }

    }/* if ass_data_len>0 */
    
    

    /* Get the message
     * (from the second pass) 
     */
    memcpy(tweak, Tag, 16 );
    tweak[0] = 0x80 ^ (tweak[0] & 0x7f);   
    
    i = 0;
    hblock1 = get_block_number_from_tag( Tag + 0 );
    hblock2 = get_block_number_from_tag( Tag + 8 );
    
    while (16*(i+1) <= c_len) {
        set_block_number_in_tweak(tweak + 8 , hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, message + 16*i );
	xor_values( message + 16*i  , ciphertext + 16*i);
        i++;
        hblock2++;
        if( hblock2 == 0 ){
            hblock1 ++;
            set_block_number_in_tweak(tweak , hblock1 );
            tweak[0] = 0x80 ^ (tweak[0] & 0x7f);
        }
    }
    

    
    /* Impartial block */
    if (c_len >  16*i) {
        set_block_number_in_tweak(tweak + 8 , hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, temp );
        for( j = 0; j < c_len - 16 * i; j ++)
            message[ 16 * i + j ] = ciphertext[ 16 * i + j] ^ temp[j];
    } 
    

    
    /* Message 
     * first pass
     */
    
    
    memset(tweak, 0, sizeof(tweak));
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (16*(i+1) <= c_len) {
        set_block_number_in_tweak(tweak + 8 , i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message + 16*i, tweakey, temp);
        xor_values(Auth, temp );
        i++;
    }

    
    /* Process incomplete block */
    if (c_len >  16*i) {
      
        /* Prepare the last padded block */
        memset(last_block, 0, 16);
        memcpy(last_block, message+16*i, c_len-16*i);
        last_block[c_len-16*i]=0x80;

        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO );
        set_block_number_in_tweak(tweak + 8 , 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(tweak + 8 , 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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, %rbp
movq %rcx, 0xa8(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %r13
movq %rdi, 0x60(%rsp)
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %r12
vmovups -0x10(%rax,%r12), %xmm0
vmovaps %xmm0, 0xd0(%rsp)
vmovups (%r8), %ymm0
leaq 0x51(%rsp), %rax
vmovups %ymm0, -0x21(%rax)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movb $0x30, -0x1(%rax)
xorl %r15d, %r15d
movq %r15, (%rax)
movq %r15, 0x7(%rax)
leaq 0x30(%rsp), %rbx
leaq 0x70(%rsp), %r14
movl $0x180, %edi            # imm = 0x180
movq %r9, %rsi
movq %rbx, %rdx
movq %r14, %rcx
vzeroupper
callq 0x10c0
vmovaps 0x10(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
movb $0x70, 0x20(%rbx)
movq %r15, 0x21(%rbx)
movq %r15, 0x28(%rbx)
movl $0x180, %edi            # imm = 0x180
movq %rbp, 0x88(%rsp)
movq %rbp, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x10c0
vmovaps 0x10(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
addq $-0x10, %r12
testq %r13, %r13
movq %r12, 0x68(%rsp)
je 0x1b26
xorl %r12d, %r12d
cmpq $0x10, %r13
jae 0x19ed
xorl %ebx, %ebx
jmp 0x1a79
movl $0x10, %r14d
movq 0x60(%rsp), %r15
xorl %ebx, %ebx
leaq 0x51(%rsp), %rbp
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x35f2(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
movb $0x20, 0x50(%rsp)
movl %r12d, 0x3(%rbp)
movl %r12d, (%rbp)
vpmovqb %ymm0, 0x58(%rsp)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x5c(%rsp)
movb %cl, 0x5d(%rsp)
movb %bh, 0x5e(%rsp)
movb %bl, 0x5f(%rsp)
movl $0x180, %edi            # imm = 0x180
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x70(%rsp), %rcx
vzeroupper
callq 0x10c0
vmovaps 0x10(%rsp), %xmm0
vxorps 0x70(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
incq %rbx
addq $0x10, %r15
addq $0x10, %r14
cmpq %r13, %r14
jbe 0x19ff
movq %rbx, %rax
shlq $0x4, %rax
subq %rax, %r13
movq 0x68(%rsp), %r12
jbe 0x1b26
vxorps %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r15
vmovaps %xmm0, (%r15)
movq 0x60(%rsp), %rsi
addq %rax, %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x10f0
movb $-0x80, 0x90(%rsp,%r13)
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3536(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x30(%rsp), %rdx
movb $0x60, 0x20(%rdx)
xorl %ecx, %ecx
leaq 0x51(%rsp), %rsi
movl %ecx, 0x3(%rsi)
movl %ecx, (%rsi)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x5e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x70(%rsp), %r14
movl $0x180, %edi            # imm = 0x180
movq %r15, %rsi
movq %r14, %rcx
vzeroupper
callq 0x10c0
vmovaps 0x10(%rsp), %xmm0
vxorps (%r14), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
vmovdqa 0xd0(%rsp), %xmm0
vmovd %xmm0, %eax
vpextrb $0x1, %xmm0, %esi
vpextrb $0x2, %xmm0, %edi
vpextrb $0x3, %xmm0, %r8d
vpextrb $0x4, %xmm0, %r10d
vpextrb $0x5, %xmm0, %r11d
vpextrb $0x6, %xmm0, %ebp
vpextrb $0x7, %xmm0, %r13d
vpextrb $0x8, %xmm0, %ecx
movl %eax, %r9d
orb $-0x80, %r9b
shlq $0x38, %rcx
vpextrb $0x9, %xmm0, %edx
shlq $0x30, %rdx
orq %rcx, %rdx
vpextrb $0xa, %xmm0, %ecx
shlq $0x28, %rcx
orq %rdx, %rcx
vpextrb $0xb, %xmm0, %edx
shlq $0x20, %rdx
orq %rcx, %rdx
vpextrb $0xc, %xmm0, %ecx
shll $0x18, %ecx
orq %rdx, %rcx
vpextrb $0xd, %xmm0, %edx
shll $0x10, %edx
orq %rcx, %rdx
vpextrb $0xe, %xmm0, %ecx
shll $0x8, %ecx
orq %rdx, %rcx
vpextrb $0xf, %xmm0, %ebx
xorq %rcx, %rbx
cmpq $0x10, %r12
jae 0x1bd7
xorl %edx, %edx
movq 0x130(%rsp), %r15
jmp 0x1dad
movzbl %sil, %ecx
shlq $0x30, %rcx
movzbl %al, %eax
shlq $0x38, %rax
orq %rcx, %rax
movzbl %dil, %ecx
shlq $0x28, %rcx
orq %rax, %rcx
movzbl %r8b, %eax
shlq $0x20, %rax
orq %rcx, %rax
movzbl %r10b, %ecx
shll $0x18, %ecx
orq %rax, %rcx
movzbl %r11b, %eax
shll $0x10, %eax
orq %rcx, %rax
movl %ebp, 0xc(%rsp)
movzbl %bpl, %ecx
shll $0x8, %ecx
orq %rax, %rcx
movl %r13d, 0x8(%rsp)
movzbl %r13b, %r12d
xorq %rcx, %r12
movl $0x1, %eax
xorl %edx, %edx
movq 0x130(%rsp), %r15
movq 0x28(%rsp), %r14
movq %rdx, %rcx
movq %rax, 0x60(%rsp)
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x33a8(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %edx
shrl $0x10, %edx
movq %r9, 0xc0(%rsp)
movb %r9b, 0x50(%rsp)
movq %rsi, 0xc8(%rsp)
movb %sil, 0x51(%rsp)
movq %rdi, 0xb8(%rsp)
movb %dil, 0x52(%rsp)
movq %r8, 0xb0(%rsp)
movb %r8b, 0x53(%rsp)
movl %r10d, %ebp
movb %r10b, 0x54(%rsp)
movl %r11d, %r13d
movb %r11b, 0x55(%rsp)
movl 0xc(%rsp), %esi
movb %sil, 0x56(%rsp)
movl 0x8(%rsp), %esi
movb %sil, 0x57(%rsp)
vpmovqb %ymm0, 0x58(%rsp)
movb %al, 0x5c(%rsp)
movb %dl, 0x5d(%rsp)
movb %bh, 0x5e(%rsp)
movb %bl, 0x5f(%rsp)
shlq $0x4, %rcx
addq 0x28(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
leaq 0x30(%rsp), %rdx
vzeroupper
callq 0x10c0
xorl %eax, %eax
movb (%r15,%rax), %cl
xorb %cl, (%r14,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x1cf5
incq %rbx
jne 0x1d4f
incq %r12
movq %r12, %r9
shrq $0x38, %r9
movq %r12, %rsi
shrq $0x30, %rsi
movq %r12, %rdi
shrq $0x28, %rdi
movq %r12, %r8
shrq $0x20, %r8
movl %r12d, %r10d
shrl $0x18, %r10d
movl %r12d, %r11d
shrl $0x10, %r11d
movl %r12d, %eax
shrl $0x8, %eax
movl %eax, 0xc(%rsp)
orb $-0x80, %r9b
movl %r12d, %eax
movl %eax, 0x8(%rsp)
jmp 0x1d75
movq 0xc8(%rsp), %rsi
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %r8
movq 0xc0(%rsp), %r9
movl %ebp, %r10d
movl %r13d, %r11d
movq 0x60(%rsp), %rdx
leaq 0x1(%rdx), %rax
movq %rax, %rcx
shlq $0x4, %rcx
addq $0x10, %r14
addq $0x10, %r15
cmpq 0x68(%rsp), %rcx
jbe 0x1c41
shlq $0x4, %rdx
movq 0x130(%rsp), %r15
movl 0xc(%rsp), %ebp
movl 0x8(%rsp), %r13d
movq 0x68(%rsp), %r14
subq %rdx, %r14
jbe 0x1e52
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3236(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
movq %rdx, %r12
leaq 0x30(%rsp), %rdx
movb %r9b, 0x20(%rdx)
movb %sil, 0x21(%rdx)
movb %dil, 0x22(%rdx)
movb %r8b, 0x23(%rdx)
movb %r10b, 0x24(%rdx)
movb %r11b, 0x25(%rdx)
movb %bpl, 0x26(%rdx)
movb %r13b, 0x27(%rdx)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x5e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x70(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
vzeroupper
callq 0x10c0
cmpq $0x1, %r14
adcq $0x0, %r14
movq 0x28(%rsp), %rax
addq %r12, %rax
addq %r12, %r15
xorl %ecx, %ecx
movb 0x70(%rsp,%rcx), %dl
xorb (%r15,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpq %rcx, %r14
jne 0x1e3f
movq 0x68(%rsp), %r13
cmpq $0x10, %r13
jae 0x1e64
xorl %ebx, %ebx
jmp 0x1eed
movl $0x10, %r15d
xorl %ebx, %ebx
leaq 0x30(%rsp), %r14
leaq 0x70(%rsp), %r12
movq 0x28(%rsp), %rbp
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x3176(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
movq $0x0, 0x50(%rsp)
vpmovqb %ymm0, 0x58(%rsp)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x5c(%rsp)
movb %cl, 0x5d(%rsp)
movb %bh, 0x5e(%rsp)
movb %bl, 0x5f(%rsp)
movl $0x180, %edi            # imm = 0x180
movq %rbp, %rsi
movq %r14, %rdx
movq %r12, %rcx
vzeroupper
callq 0x10c0
vmovdqa 0x10(%rsp), %xmm0
vpxor 0x70(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
incq %rbx
addq $0x10, %rbp
addq $0x10, %r15
cmpq %r13, %r15
jbe 0x1e7b
movq %rbx, %rsi
shlq $0x4, %rsi
movq %r13, %r14
subq %rsi, %r14
leaq 0x51(%rsp), %r15
jbe 0x1f98
vpxor %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r12
vmovdqa %xmm0, (%r12)
addq 0x28(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x10f0
movb $-0x80, 0x90(%rsp,%r14)
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x30c1(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x30(%rsp), %rdx
movb $0x40, 0x20(%rdx)
xorl %ecx, %ecx
movl %ecx, 0x3(%r15)
movl %ecx, (%r15)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x5e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x70(%rsp), %r14
movl $0x180, %edi            # imm = 0x180
movq %r12, %rsi
movq %r14, %rcx
vzeroupper
callq 0x10c0
vmovdqa 0x10(%rsp), %xmm0
vpxor (%r14), %xmm0, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
leaq 0x30(%rsp), %rdx
movb $0x10, 0x20(%rdx)
xorl %ebx, %ebx
movq %rbx, 0x7(%r15)
movq %rbx, (%r15)
leaq 0x10(%rsp), %rsi
leaq 0xe0(%rsp), %r14
movl $0x180, %edi            # imm = 0x180
movq %r14, %rcx
callq 0x10c0
vmovdqa 0xd0(%rsp), %xmm0
vpxor (%r14), %xmm0, %xmm0
vptest %xmm0, %xmm0
je 0x1fef
movq 0x28(%rsp), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x1050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x1ffa
movq 0xa8(%rsp), %rax
movq %r13, (%rax)
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop