// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movq %rcx, 0xb0(%rsp)
movq %rdx, 0x40(%rsp)
movq 0x100(%rsp), %rax
movq 0x108(%rsp), %r10
movq -0x8(%rax,%r10), %rax
movq %rax, 0x68(%rsp)
addq $-0x8, %r10
movl 0x8(%r8), %eax
movl %eax, 0x18(%rsp)
movq (%r8), %rax
movq %rax, 0x10(%rsp)
testq %rsi, %rsi
movq %r10, 0x28(%rsp)
je 0x1873
movq %rsi, %rbp
movq %rdi, %r15
cmpq $0x8, %rsi
jae 0x1882
vxorps %xmm0, %xmm0, %xmm0
xorl %ebx, %ebx
jmp 0x1941
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
jmp 0x19fa
movq %r9, 0x80(%rsp)
vxorps %xmm0, %xmm0, %xmm0
movl $0x8, %r12d
leaq 0x1d(%rsp), %r13
xorl %ebx, %ebx
leaq 0x70(%rsp), %r14
vmovaps %xmm0, 0x30(%rsp)
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
movq %rbx, %rcx
shrq $0x20, %rcx
movl %ebx, %edx
shrl $0x18, %edx
movl %ebx, %esi
shrl $0x10, %esi
movl %ebx, %edi
shrl $0x8, %edi
movb $0x20, 0x1c(%rsp)
movb $0x0, 0x4(%r13)
movl $0x0, (%r13)
movb %al, 0x22(%rsp)
movb %cl, 0x23(%rsp)
movb %dl, 0x24(%rsp)
movb %sil, 0x25(%rsp)
movb %dil, 0x26(%rsp)
movb %bl, 0x27(%rsp)
leaq -0x8(%r15,%r12), %rsi
movl $0xc0, %edi
leaq 0x10(%rsp), %rdx
movq %r14, %rcx
callq 0x10c0
vmovsd 0x70(%rsp), %xmm0
vmovaps 0x30(%rsp), %xmm1
vxorps %xmm0, %xmm1, %xmm1
vmovaps %xmm1, 0x30(%rsp)
vmovaps 0x30(%rsp), %xmm0
incq %rbx
addq $0x8, %r12
cmpq %rbp, %r12
jbe 0x18a0
movq 0x28(%rsp), %r10
movq 0x80(%rsp), %r9
leaq (,%rbx,8), %rax
subq %rax, %rbp
vmovaps %xmm0, 0x30(%rsp)
jbe 0x19fa
leaq 0x50(%rsp), %r14
movq $0x0, (%r14)
addq %rax, %r15
movq %r14, %rdi
movq %r15, %rsi
movq %r10, %r12
movq %rbp, %rdx
movq %r9, %r13
callq 0x1110
movb $-0x80, 0x50(%rsp,%rbp)
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
movq %rbx, %rcx
shrq $0x20, %rcx
movl %ebx, %esi
shrl $0x18, %esi
movl %ebx, %edi
shrl $0x10, %edi
movl %ebx, %r8d
shrl $0x8, %r8d
leaq 0x10(%rsp), %rdx
movb $0x60, 0xc(%rdx)
movl $0x0, 0xd(%rdx)
movb $0x0, 0x11(%rdx)
movb %al, 0x12(%rdx)
movb %cl, 0x13(%rdx)
movb %sil, 0x14(%rdx)
movb %dil, 0x15(%rdx)
movb %r8b, 0x16(%rdx)
movb %bl, 0x17(%rdx)
leaq 0x70(%rsp), %r15
movl $0xc0, %edi
movq %r14, %rsi
movq %r15, %rcx
callq 0x10c0
movq %r13, %r9
movq %r12, %r10
vmovsd (%r15), %xmm0
vmovaps 0x30(%rsp), %xmm1
vxorps %xmm1, %xmm0, %xmm1
vmovaps %xmm1, 0x30(%rsp)
movl (%r9), %ebx
vmovd %ebx, %xmm0
shrb $0x4, %bl
vmovd 0x1(%r9), %xmm1
movb 0x4(%r9), %r13b
vpsrlw $0x4, %xmm1, %xmm1
vpsllw $0x4, %xmm0, %xmm2
vpternlogd $0xe4, 0x16fd(%rip){1to4}, %xmm1, %xmm2 # 0x3120
movb 0x5(%r9), %r12b
movl %r12d, %eax
shrb $0x4, %al
shlb $0x4, %r13b
orb %al, %r13b
shlb $0x4, %r12b
movq $0x0, 0x48(%rsp)
cmpq $0x8, %r10
vmovdqa %xmm2, 0x80(%rsp)
jae 0x1a57
xorl %ebp, %ebp
jmp 0x1b1f
movl $0x1, %ecx
xorl %eax, %eax
movl %r12d, %r15d
movb %r13b, 0xf(%rsp)
movb %bl, 0x1c(%rsp)
vmovd %xmm2, 0x1d(%rsp)
movq %rcx, %rbp
movb %r13b, 0x21(%rsp)
movl %r15d, %r12d
andb $-0x10, %r12b
movq %rax, %r15
shrq $0x28, %r15
andb $0xf, %r15b
orb %r12b, %r15b
movq %rax, %rcx
shrq $0x20, %rcx
movq %rbx, %r13
movq %r10, %rbx
movl %eax, %edx
shrl $0x18, %edx
movl %eax, %esi
shrl $0x10, %esi
movl %eax, %edi
shrl $0x8, %edi
movb %r15b, 0x22(%rsp)
movb %cl, 0x23(%rsp)
movb %dl, 0x24(%rsp)
movb %sil, 0x25(%rsp)
movb %dil, 0x26(%rsp)
movb %al, 0x27(%rsp)
movq 0x100(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
movl $0xc0, %edi
leaq 0x10(%rsp), %rdx
movq %r14, %rcx
callq 0x1050
vmovdqa 0x80(%rsp), %xmm2
movq (%r14), %rax
xorq %rax, 0x48(%rsp)
leaq 0x1(%rbp), %rcx
leaq 0x8(,%rbp,8), %rdx
movq %rbp, %rax
cmpq %rbx, %rdx
movq %rbx, %r10
movq %r13, %rbx
movb 0xf(%rsp), %r13b
jbe 0x1a66
leaq (,%rbp,8), %r11
movq %r10, %r15
subq %r11, %r15
jne 0x1bc2
orb $0x10, %bl
movq %rbp, %rax
shrq $0x28, %rax
andb $0xf, %al
vpbroadcastq %rbp, %ymm0
vpsrlvq 0x14b2(%rip), %ymm0, %ymm0 # 0x3000
orb %r12b, %al
leaq 0x10(%rsp), %rdx
movb %bl, 0xc(%rdx)
vmovd %xmm2, 0xd(%rdx)
movb %r13b, 0x11(%rdx)
movb %al, 0x12(%rdx)
vpmovqb %ymm0, 0x13(%rdx)
movb %bpl, 0x17(%rdx)
leaq 0x48(%rsp), %rsi
leaq 0xb8(%rsp), %r14
movl $0xc0, %edi
movq %r14, %rcx
vzeroupper
callq 0x10c0
vmovq (%r14), %xmm0
vpxor 0x30(%rsp), %xmm0, %xmm0
vmovq %xmm0, (%r14)
vmovq 0x68(%rsp), %xmm1
vpxor %xmm0, %xmm1, %xmm0
vmovq %xmm0, %rax
testq %rax, %rax
movq 0x40(%rsp), %rdi
jne 0x1d77
jmp 0x1d8a
xorl %eax, %eax
leaq 0xc0(%rsp), %rsi
movq %rax, (%rsi)
leal 0x40(%rbx), %eax
movq %rbp, %rdx
shrq $0x28, %rdx
andb $0xf, %dl
orb %r12b, %dl
movq %rdx, %rdi
movq %rdx, 0xa8(%rsp)
movq %rbp, %rdx
shrq $0x20, %rdx
movq %rdx, 0xa0(%rsp)
movl %ebp, %r8d
shrl $0x18, %r8d
movl %r8d, 0x64(%rsp)
movl %ebp, %r9d
shrl $0x10, %r9d
movl %r9d, 0x60(%rsp)
movl %ebp, %r10d
shrl $0x8, %r10d
movl %r10d, 0x5c(%rsp)
leaq 0x10(%rsp), %r14
movb %al, 0xc(%r14)
vmovd %xmm2, 0xd(%r14)
movb %r13b, 0x11(%r14)
movb %dil, 0x12(%r14)
movb %dl, 0x13(%r14)
movb %r8b, 0x14(%r14)
movb %r9b, 0x15(%r14)
movb %r10b, 0x16(%r14)
movb %bpl, 0x17(%r14)
leaq 0x78(%rsp), %rcx
movq %rbp, 0x98(%rsp)
movq %r11, %r12
movl $0xc0, %edi
movq %r14, %rdx
callq 0x10c0
leaq 0x50(%rsp), %rbp
xorl %eax, %eax
movq %rax, (%rbp)
movq 0x100(%rsp), %rsi
addq %r12, %rsi
movq %rbp, %rdi
movq %r15, %rdx
callq 0x1110
movq 0x28(%rsp), %rax
leaq (%rsp,%rax), %rdi
addq $0x78, %rdi
subq %r12, %rdi
movl $0x8, %edx
subq %r15, %rdx
xorl %esi, %esi
callq 0x1040
leaq 0x78(%rsp), %rax
movq (%rax), %rax
xorq %rax, (%rbp)
movb $-0x80, 0x50(%rsp,%r15)
addq 0x40(%rsp), %r12
cmpq $0x1, %r15
movl $0x0, %eax
adcq %rax, %r15
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1110
movq (%rbp), %rax
leaq 0x48(%rsp), %rsi
xorq %rax, (%rsi)
orb $0x50, %bl
movb %bl, 0xc(%r14)
vmovaps 0x80(%rsp), %xmm0
vmovss %xmm0, 0xd(%r14)
movb %r13b, 0x11(%r14)
movq 0xa8(%rsp), %rax
movb %al, 0x12(%r14)
movq 0xa0(%rsp), %rax
movb %al, 0x13(%r14)
movl 0x64(%rsp), %eax
movb %al, 0x14(%r14)
movl 0x60(%rsp), %eax
movb %al, 0x15(%r14)
movl 0x5c(%rsp), %eax
movb %al, 0x16(%r14)
movq 0x98(%rsp), %rax
movb %al, 0x17(%r14)
leaq 0xb8(%rsp), %rbx
movl $0xc0, %edi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x10c0
vmovq (%rbx), %xmm0
vpxor 0x30(%rsp), %xmm0, %xmm0
vmovq %xmm0, (%rbx)
vmovq 0x68(%rsp), %xmm1
vpxor %xmm0, %xmm1, %xmm0
vmovq %xmm0, %rax
testq %rax, %rax
je 0x1d8a
movq 0x40(%rsp), %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
callq 0x1040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1d9c
movq 0xb0(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop