// File: /workspace/github_c_makefile_star2_O3/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 $0xb8, %rsp
movq %rcx, 0xa0(%rsp)
movq %rdx, 0x48(%rsp)
movq 0xf0(%rsp), %rax
movq 0xf8(%rsp), %r12
movq -0x8(%rax,%r12), %rax
movq %rax, 0x78(%rsp)
addq $-0x8, %r12
movl 0x8(%r8), %eax
movl %eax, 0x18(%rsp)
movq (%r8), %rax
movq %rax, 0x10(%rsp)
testq %rsi, %rsi
movq %r12, 0x40(%rsp)
je 0x1878
movq %rsi, %rbp
movq %rdi, %r15
movq %r9, 0x60(%rsp)
vxorps %xmm0, %xmm0, %xmm0
cmpq $0x8, %rsi
jae 0x1887
xorl %ebx, %ebx
jmp 0x1928
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0x30(%rsp)
jmp 0x19d7
movl $0x8, %r12d
leaq 0x1d(%rsp), %r13
xorl %ebx, %ebx
leaq 0x80(%rsp), %r14
vmovaps %xmm0, 0x30(%rsp)
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x1746(%rip), %ymm0, %ymm0 # 0x3000
movb $0x20, 0x1c(%rsp)
movb $0x0, 0x4(%r13)
movl $0x0, (%r13)
movb %al, 0x22(%rsp)
vpmovqb %ymm0, 0x23(%rsp)
movb %bl, 0x27(%rsp)
leaq -0x8(%r15,%r12), %rsi
movl $0xc0, %edi
leaq 0x10(%rsp), %rdx
movq %r14, %rcx
vzeroupper
callq 0x10c0
vmovsd 0x80(%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 0x189c
leaq (,%rbx,8), %rax
subq %rax, %rbp
vmovaps %xmm0, 0x30(%rsp)
jbe 0x19cd
leaq 0x28(%rsp), %r14
movq $0x0, (%r14)
addq %rax, %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x1110
movb $-0x80, 0x28(%rsp,%rbp)
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x1687(%rip), %ymm0, %ymm0 # 0x3000
leaq 0x10(%rsp), %rdx
movb $0x60, 0xc(%rdx)
movl $0x0, 0xd(%rdx)
movb $0x0, 0x11(%rdx)
movb %al, 0x12(%rdx)
vpmovqb %ymm0, 0x13(%rdx)
movb %bl, 0x17(%rdx)
leaq 0x80(%rsp), %r15
movl $0xc0, %edi
movq %r14, %rsi
movq %r15, %rcx
vzeroupper
callq 0x10c0
vmovsd (%r15), %xmm0
vmovaps 0x30(%rsp), %xmm1
vxorps %xmm1, %xmm0, %xmm1
vmovaps %xmm1, 0x30(%rsp)
movq 0x40(%rsp), %r12
movq 0x60(%rsp), %r9
movl (%r9), %edi
vmovd %edi, %xmm0
shrb $0x4, %dil
vmovd 0x1(%r9), %xmm1
movb 0x4(%r9), %r8b
vpsrlw $0x4, %xmm1, %xmm1
vpsllw $0x4, %xmm0, %xmm2
vpternlogd $0xe4, 0x171f(%rip){1to4}, %xmm1, %xmm2 # 0x3120
movb 0x5(%r9), %r13b
movl %r13d, %eax
shrb $0x4, %al
shlb $0x4, %r8b
orb %al, %r8b
shlb $0x4, %r13b
movq $0x0, 0x50(%rsp)
cmpq $0x8, %r12
vmovdqa %xmm2, 0x60(%rsp)
jae 0x1a32
xorl %ebx, %ebx
jmp 0x1add
movl $0x1, %eax
xorl %ecx, %ecx
movq %r12, %rbp
movq %rcx, %rdx
shrq $0x28, %rdx
andb $0xf, %dl
orb %r13b, %dl
vpbroadcastq %rcx, %ymm0
vpsrlvq 0x15a8(%rip), %ymm0, %ymm0 # 0x3000
movb %dil, 0x1c(%rsp)
vmovd %xmm2, 0x1d(%rsp)
movb %r8b, 0x21(%rsp)
movb %dl, 0x22(%rsp)
vpmovqb %ymm0, 0x23(%rsp)
movq %rax, %rbx
movb %cl, 0x27(%rsp)
movq 0xf0(%rsp), %rax
leaq (%rax,%rcx,8), %rsi
movq 0x48(%rsp), %rax
leaq (%rax,%rcx,8), %r15
movq %rdi, %r14
movl $0xc0, %edi
leaq 0x10(%rsp), %rdx
movq %r15, %rcx
movl %r8d, %r12d
vzeroupper
callq 0x1050
vmovdqa 0x60(%rsp), %xmm2
movl %r12d, %r8d
movq %r14, %rdi
movq (%r15), %rax
xorq %rax, 0x50(%rsp)
leaq 0x1(%rbx), %rax
leaq 0x8(,%rbx,8), %rdx
movq %rbx, %rcx
cmpq %rbp, %rdx
movq %rbp, %r12
jbe 0x1a39
leaq (,%rbx,8), %rbp
movq %r12, %r15
subq %rbp, %r15
jne 0x1b81
orb $0x10, %dil
movq %rbx, %rax
shrq $0x28, %rax
andb $0xf, %al
vpbroadcastq %rbx, %ymm0
vpsrlvq 0x14f3(%rip), %ymm0, %ymm0 # 0x3000
orb %r13b, %al
leaq 0x10(%rsp), %rdx
movb %dil, 0xc(%rdx)
vmovd %xmm2, 0xd(%rdx)
movb %r8b, 0x11(%rdx)
movb %al, 0x12(%rdx)
vpmovqb %ymm0, 0x13(%rdx)
movb %bl, 0x17(%rdx)
leaq 0x50(%rsp), %rsi
leaq 0xa8(%rsp), %r14
movl $0xc0, %edi
movq %r14, %rcx
vzeroupper
callq 0x10c0
vmovq (%r14), %xmm0
vpxor 0x30(%rsp), %xmm0, %xmm0
vmovq %xmm0, (%r14)
vmovq 0x78(%rsp), %xmm1
vpxor %xmm0, %xmm1, %xmm0
vmovq %xmm0, %rax
testq %rax, %rax
movq 0x48(%rsp), %rdi
jne 0x1d36
jmp 0x1d47
xorl %eax, %eax
leaq 0xb0(%rsp), %rsi
movq %rax, (%rsi)
leal 0x40(%rdi), %eax
movq %rbx, %rcx
shrq $0x28, %rcx
andb $0xf, %cl
orb %r13b, %cl
movq %rcx, %rdx
movq %rcx, 0x98(%rsp)
movq %rbx, %rcx
shrq $0x20, %rcx
movq %rcx, 0x90(%rsp)
movl %ebx, %r9d
shrl $0x18, %r9d
movl %r9d, 0x5c(%rsp)
movl %ebx, %r10d
shrl $0x10, %r10d
movl %r10d, 0x58(%rsp)
movl %ebx, %r12d
shrl $0x8, %r12d
leaq 0x10(%rsp), %r14
movb %al, 0xc(%r14)
vmovd %xmm2, 0xd(%r14)
movb %r8b, 0x11(%r14)
movb %dl, 0x12(%r14)
movb %cl, 0x13(%r14)
movb %r9b, 0x14(%r14)
movb %r10b, 0x15(%r14)
movb %r12b, 0x16(%r14)
movb %bl, 0x17(%r14)
leaq 0x88(%rsp), %rcx
movq %rdi, %r13
movl $0xc0, %edi
movq %r14, %rdx
movb %r8b, 0xf(%rsp)
callq 0x10c0
leaq 0x28(%rsp), %rdi
xorl %eax, %eax
movq %rax, (%rdi)
movq 0xf0(%rsp), %rsi
addq %rbp, %rsi
movq %r15, %rdx
callq 0x1110
movq 0x40(%rsp), %rax
leaq (%rsp,%rax), %rdi
addq $0x88, %rdi
subq %rbp, %rdi
movl $0x8, %edx
subq %r15, %rdx
xorl %esi, %esi
callq 0x1040
leaq 0x88(%rsp), %rax
movq (%rax), %rax
leaq 0x28(%rsp), %rsi
xorq %rax, (%rsi)
movb $-0x80, 0x28(%rsp,%r15)
movq %rbp, %rdi
movq 0x48(%rsp), %rbp
addq %rbp, %rdi
cmpq $0x1, %r15
movl $0x0, %eax
adcq %rax, %r15
movq %r15, %rdx
callq 0x1110
leaq 0x28(%rsp), %rax
movq (%rax), %rax
leaq 0x50(%rsp), %rsi
xorq %rax, (%rsi)
orb $0x50, %r13b
movb %r13b, 0xc(%r14)
vmovaps 0x60(%rsp), %xmm0
vmovss %xmm0, 0xd(%r14)
movb 0xf(%rsp), %al
movb %al, 0x11(%r14)
movq 0x98(%rsp), %rax
movb %al, 0x12(%r14)
movq 0x90(%rsp), %rax
movb %al, 0x13(%r14)
movl 0x5c(%rsp), %eax
movb %al, 0x14(%r14)
movl 0x58(%rsp), %eax
movb %al, 0x15(%r14)
movb %r12b, 0x16(%r14)
movq 0x40(%rsp), %r12
movb %bl, 0x17(%r14)
leaq 0xa8(%rsp), %r15
movl $0xc0, %edi
movq %r14, %rdx
movq %r15, %rcx
callq 0x10c0
vmovq (%r15), %xmm0
vpxor 0x30(%rsp), %xmm0, %xmm0
vmovq %xmm0, (%r15)
vmovq 0x78(%rsp), %xmm1
vpxor %xmm0, %xmm1, %xmm0
vmovq %xmm0, %rax
testq %rax, %rax
je 0x1d47
movq %rbp, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1d54
movq 0xa0(%rsp), %rax
movq %r12, (%rax)
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop