// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x128, %rsp            # imm = 0x128
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movq %rdx, 0x108(%rsp)
movq %rcx, 0x100(%rsp)
movq %r8, 0xf8(%rsp)
movq %r9, 0xf0(%rsp)
movq 0x130(%rsp), %rax
movq 0x138(%rsp), %rcx
movq -0x10(%rax,%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq -0x8(%rax,%rcx), %rax
movq %rax, 0x28(%rsp)
movq 0x138(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x138(%rsp)
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
movq 0xf8(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x90(%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x98(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0xa0(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0xa8(%rsp)
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
leaq 0xc0(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a50
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a50
leaq 0xc0(%rsp), %rdi
movl $0x30, %esi
callq 0x1b30
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
movq 0xf0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x1b90
leaq 0xc0(%rsp), %rdi
movl $0x70, %esi
callq 0x1b30
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
movq 0xf0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x1b90
cmpq $0x0, 0x110(%rsp)
je 0x1f48
leaq 0xc0(%rsp), %rdi
movl $0x20, %esi
callq 0x1b30
movq $0x0, 0xe8(%rsp)
movq 0xe8(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0x110(%rsp), %rax
ja 0x1e5a
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
movq 0xe8(%rsp), %rsi
callq 0x1a50
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
movq 0x118(%rsp), %rsi
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x90(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x1b90
movq 0xe8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xe8(%rsp)
jmp 0x1db9
movq 0x110(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x1f46
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
leaq 0x70(%rsp), %rdi
movq 0x118(%rsp), %rsi
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0x110(%rsp), %rdx
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
movq 0x110(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x70(%rsp,%rax)
leaq 0xc0(%rsp), %rdi
movl $0x60, %esi
callq 0x1b30
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
movq 0xe8(%rsp), %rsi
callq 0x1a50
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
leaq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x1b90
jmp 0x1f48
movq 0x20(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0xc8(%rsp)
movzbl 0xc0(%rsp), %eax
andl $0x7f, %eax
xorl $0x80, %eax
movb %al, 0xc0(%rsp)
movq $0x0, 0xe8(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x1030
movq %rax, 0xd8(%rsp)
leaq 0x20(%rsp), %rdi
addq $0x8, %rdi
callq 0x1030
movq %rax, 0xd0(%rsp)
movq 0xe8(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0x138(%rsp), %rax
ja 0x20d5
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1a50
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
movq 0xf0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq 0x108(%rsp), %rcx
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
movq 0x108(%rsp), %rdi
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x130(%rsp), %rsi
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x1b90
movq 0xe8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xe8(%rsp)
movq 0xd0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd0(%rsp)
cmpq $0x0, 0xd0(%rsp)
jne 0x20d0
movq 0xd8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd8(%rsp)
leaq 0xc0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x1a50
movzbl 0xc0(%rsp), %eax
andl $0x7f, %eax
xorl $0x80, %eax
movb %al, 0xc0(%rsp)
jmp 0x1fad
movq 0x138(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x21d7
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
movq 0xd0(%rsp), %rsi
callq 0x1a50
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
movq 0xf0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
movq $0x0, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
movq 0x138(%rsp), %rcx
movq 0xe8(%rsp), %rdx
shlq $0x4, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x21d5
movq 0x130(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x4, %rcx
addq 0xe0(%rsp), %rcx
movzbl (%rax,%rcx), %eax
movq 0xe0(%rsp), %rcx
movzbl (%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0x108(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x4, %rcx
addq 0xe0(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0xe0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xe0(%rsp)
jmp 0x2149
jmp 0x21d7
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0x1b30
movq $0x0, 0xe8(%rsp)
movq 0xe8(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0x138(%rsp), %rax
ja 0x22a7
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
movq 0xe8(%rsp), %rsi
callq 0x1a50
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
movq 0x108(%rsp), %rsi
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x90(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x1b90
movq 0xe8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xe8(%rsp)
jmp 0x2206
movq 0x138(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x2393
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1060
leaq 0x70(%rsp), %rdi
movq 0x108(%rsp), %rsi
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0x138(%rsp), %rdx
movq 0xe8(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
movq 0x138(%rsp), %rax
movq 0xe8(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x70(%rsp,%rax)
leaq 0xc0(%rsp), %rdi
movl $0x40, %esi
callq 0x1b30
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
movq 0xe8(%rsp), %rsi
callq 0x1a50
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
leaq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdx
movq %rsp, %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x1b90
leaq 0xc0(%rsp), %rdi
movl $0x10, %esi
callq 0x1b30
leaq 0xc0(%rsp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1a50
leaq 0x90(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1b60
leaq 0x80(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
callq 0x10e0
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x10, %edx
callq 0x2460
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x242e
movq 0x108(%rsp), %rdi
movq 0x138(%rsp), %rdx
xorl %esi, %esi
callq 0x1060
movl $0xffffffff, 0x124(%rsp) # imm = 0xFFFFFFFF
jmp 0x244c
movq 0x138(%rsp), %rcx
movq 0x100(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0x124(%rsp)
movl 0x124(%rsp), %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopl (%rax,%rax)