// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/joltikneq9696v13/ref/joltik.c
// Function: joltik_aead_decrypt
int joltik_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[12];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[8];
    uint8_t last_block[8];
    uint8_t Checksum[8];
    uint8_t Final[8];
    uint8_t zero_block[8];
    uint8_t Pad[8];
    uint8_t Tag[8];
    uint8_t temp[8];

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

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

    memset(tweak, 0, sizeof(tweak));

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

    /* Associated data */
    memset(Auth, 0, 8);
    if(ass_data_len) {
            set_stage_in_tweak(tweak, MSB_AD);
            i=0;
            while (8*(i+1) <= ass_data_len) {
                set_block_number_in_tweak(tweak, i);
                set_tweak_in_tweakey(tweakey, tweak);
                aesTweakEncrypt(TWEAKEY_STATE_SIZE, ass_data+8*i, tweakey, temp);
                xor_values(Auth, temp);
                i++;
            }

            /* Last block if incomplete */
            if ( ass_data_len > 8*i ) {
                memset(last_block, 0, 8);
                memcpy(last_block, ass_data+8*i, ass_data_len-8*i);
                last_block[ass_data_len-8*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 */
    set_nonce_in_tweak(tweak, nonce);

    memset(Checksum, 0, 8);
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while(8*(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+8*i, tweakey, message+8*i);

        xor_values(Checksum, message+8*i);
        i++;
    }

    /* Last block */
    /* If the block is full, i.e. M_last=epsilon */
    if (c_len == 8*i) {
        set_stage_in_tweak(tweak, MSB_CHKSUM_FULL);
        set_block_number_in_tweak(tweak, i);
        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 { /* If the block is partial and has been padded */

        /* Prepare the full-zero block */
        memset(zero_block, 0, 8);

        /* 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, 8);
        memcpy(last_block, ciphertext+8*i, c_len-8*i);
        memset(Pad+c_len-8*i, 0, 8-(c_len-8*i));
        xor_values(last_block, Pad);
        last_block[c_len-8*i]=0x80;

        for (j=0; j<c_len-8*i; j++) {
            message[8*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 $0xa8, %rsp
movq %r9, %r12
movq %rcx, 0x68(%rsp)
movq %rdx, 0x20(%rsp)
movq 0xe8(%rsp), %rax
movq 0xe0(%rsp), %rcx
movq -0x8(%rcx,%rax), %rax
andq $0x0, (%rsp)
movq %rax, 0x58(%rsp)
andl $0x0, 0x8(%rsp)
movq (%r8), %rax
movq %rax, 0x30(%rsp)
movl 0x8(%r8), %eax
movl %eax, 0x38(%rsp)
andq $0x0, 0x28(%rsp)
testq %rsi, %rsi
je 0x1898
movq %rsi, %r15
movq %rdi, %r14
movq %r12, 0x10(%rsp)
movb $0x20, (%rsp)
xorl %esi, %esi
leaq 0x70(%rsp), %rbp
xorl %r13d, %r13d
xorl %ebx, %ebx
leaq 0x8(%rsi), %r12
vpbroadcastq %rbx, %ymm0
cmpq %r15, %r12
ja 0x17e7
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
movb %al, 0x6(%rsp)
vpsrlvq 0x186c(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb %bl, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x3c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
addq %r14, %rsi
movl $0xc0, %edi
leaq 0x30(%rsp), %rdx
movq %rbp, %rcx
vzeroupper
callq 0x10d0
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x1674
addq $-0x8, %r13
incq %rbx
movq %r12, %rsi
jmp 0x176f
movq %r13, %rax
negq %rax
cmpq %r15, %rax
movq 0x10(%rsp), %r12
jae 0x1898
leaq 0x18(%rsp), %rbp
andq $0x0, (%rbp)
leaq (%r14,%rbx,8), %rsi
addq %r13, %r15
movq %rbp, %rdi
movq %r15, %rdx
vmovdqu %ymm0, 0x80(%rsp)
vzeroupper
callq 0x1120
movb $0x60, (%rsp)
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
movb %al, 0x6(%rsp)
vmovdqu 0x80(%rsp), %ymm0
vpsrlvq 0x17ba(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb $-0x80, 0x18(%rsp,%r15)
movb %bl, 0xb(%rsp)
movq (%rsp), %rax
leaq 0x3c(%rsp), %rcx
movq %rax, (%rcx)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rcx)
leaq 0x30(%rsp), %rdx
leaq 0x70(%rsp), %r14
movl $0xc0, %edi
movq %rbp, %rsi
movq %r14, %rcx
vzeroupper
callq 0x10d0
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x1674
movq 0xe8(%rsp), %rax
addq $-0x8, %rax
movq %rax, 0x10(%rsp)
movq %rsp, %rbx
movq %rbx, %rdi
movq %r12, %rsi
vzeroupper
callq 0x1688
leaq 0x50(%rsp), %rax
andq $0x0, (%rax)
movb (%rbx), %al
movb 0x6(%rbx), %r14b
andb $0xf, %al
movq %rax, 0x80(%rsp)
movb %al, (%rbx)
andb $-0x10, %r14b
leaq 0x60(%rsp), %r13
leaq 0x18(%rsp), %r12
xorl %ebx, %ebx
xorl %r15d, %r15d
leaq 0x8(%rbx), %rbp
vpbroadcastq %r15, %ymm0
movq 0x10(%rsp), %rax
cmpq %rax, %rbp
ja 0x1982
movq %r15, %rax
shrq $0x28, %rax
andb $0xf, %al
orb %r14b, %al
movb %al, 0x6(%rsp)
vpsrlvq 0x16e7(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb %r15b, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x3c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
movq 0xe0(%rsp), %rax
leaq (%rax,%rbx), %rsi
addq 0x20(%rsp), %rbx
movl $0xc0, %edi
leaq 0x30(%rsp), %rdx
movq %rbx, %rcx
vzeroupper
callq 0x1050
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x1674
addq $-0x8, %r13
addq $-0x8, %r12
incq %r15
movq %rbp, %rbx
jmp 0x18e8
movq %rax, %rbp
subq %rbx, %rbp
jne 0x1a11
movq 0x80(%rsp), %rax
orb $0x10, %al
movb %al, (%rsp)
movq %r15, %rax
shrq $0x28, %rax
andb $0xf, %al
orb %al, %r14b
movb %r14b, 0x6(%rsp)
vpsrlvq 0x164b(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb %r15b, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x3c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x78(%rsp), %rbx
movl $0xc0, %edi
movq %rbx, %rcx
vzeroupper
callq 0x10d0
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x1674
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
jmp 0x1b5b
leaq 0xa0(%rsp), %rsi
andq $0x0, (%rsi)
movq 0x80(%rsp), %rax
addl $0x40, %eax
movb %al, (%rsp)
movq %r15, %rax
shrq $0x28, %rax
andb $0xf, %al
orb %al, %r14b
movb %r14b, 0x6(%rsp)
vpsrlvq 0x15bb(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movq 0x20(%rsp), %rax
leaq (%rax,%rbx), %r14
movb %r15b, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x3c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movl $0xc0, %edi
vzeroupper
callq 0x10d0
leaq 0x18(%rsp), %rdi
andq $0x0, (%rdi)
movq 0xe0(%rsp), %rsi
addq %rbx, %rsi
movq %rbp, %rdx
callq 0x1120
addq 0x10(%rsp), %r13
movq 0xe8(%rsp), %rax
subq %rax, %rbx
addq $0x10, %rbx
xorl %r15d, %r15d
movq %r13, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x1040
leaq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1674
movq 0xe8(%rsp), %rax
movb $-0x80, -0x8(%r12,%rax)
cmpq %r15, %rbp
je 0x1afa
movb 0x18(%rsp,%r15), %al
movb %al, (%r14)
incq %r15
incq %r14
jmp 0x1ae5
leaq 0x50(%rsp), %rbx
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x1674
movq 0x80(%rsp), %rax
orb $0x50, %al
movb %al, (%rsp)
movl 0x8(%rsp), %eax
leaq 0x3c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
leaq 0x30(%rsp), %rdx
leaq 0x78(%rsp), %r15
movl $0xc0, %edi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x10d0
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x1674
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
callq 0x1b9b
testl %eax, %eax
je 0x1b7a
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x1040
pushq $-0x1
popq %rax
jmp 0x1b89
movq 0x68(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq