// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rax
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq -0x8(%rax,%rcx), %rax
movq %rax, 0x8(%rsp)
movq 0xc8(%rsp), %rax
subq $0x8, %rax
movq %rax, 0xc8(%rsp)
leaq 0x64(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x1040
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x40(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x48(%rsp)
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
cmpq $0x0, 0xa0(%rsp)
je 0x1cb6
leaq 0x64(%rsp), %rdi
movl $0x20, %esi
callq 0x1820
movq $0x0, 0x78(%rsp)
movq 0x78(%rsp), %rax
addq $0x1, %rax
shlq $0x3, %rax
cmpq 0xa0(%rsp), %rax
ja 0x1bed
leaq 0x64(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x1910
movq 0xa8(%rsp), %rsi
movq 0x78(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
leaq 0x40(%rsp), %rdx
movq %rsp, %rcx
movl $0xc0, %edi
callq 0x10d0
leaq 0x38(%rsp), %rdi
movq %rsp, %rsi
callq 0x1940
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x1b75
movq 0xa0(%rsp), %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jbe 0x1cb4
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x30(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x78(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq 0xa0(%rsp), %rdx
movq 0x78(%rsp), %rax
shlq $0x3, %rax
subq %rax, %rdx
callq 0x1120
movq 0xa0(%rsp), %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
subq %rcx, %rax
movb $-0x80, 0x30(%rsp,%rax)
leaq 0x64(%rsp), %rdi
movl $0x60, %esi
callq 0x1820
leaq 0x64(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x1910
leaq 0x30(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq %rsp, %rcx
movl $0xc0, %edi
callq 0x10d0
leaq 0x38(%rsp), %rdi
movq %rsp, %rsi
callq 0x1940
jmp 0x1cb6
leaq 0x64(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x1990
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x64(%rsp), %rdi
xorl %esi, %esi
callq 0x1820
movq $0x0, 0x78(%rsp)
movq 0x78(%rsp), %rax
addq $0x1, %rax
shlq $0x3, %rax
cmpq 0xc8(%rsp), %rax
ja 0x1d9e
leaq 0x40(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x1910
leaq 0x64(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x1910
movq 0xc0(%rsp), %rsi
movq 0x78(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
leaq 0x40(%rsp), %rdx
movq 0x98(%rsp), %rcx
movq 0x78(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
movl $0xc0, %edi
callq 0x1050
leaq 0x28(%rsp), %rdi
movq 0x98(%rsp), %rsi
movq 0x78(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x1940
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x1cee
movq 0xc8(%rsp), %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jne 0x1e55
leaq 0x64(%rsp), %rdi
movl $0x10, %esi
callq 0x1820
leaq 0x64(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x1910
leaq 0x28(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
leaq 0x20(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x1940
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x8, %edx
callq 0x2080
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x1e50
movq 0x98(%rsp), %rdi
movq 0xc8(%rsp), %rdx
xorl %esi, %esi
callq 0x1040
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x2066
jmp 0x2048
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x64(%rsp), %rdi
movl $0x40, %esi
callq 0x1820
leaq 0x64(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x1910
leaq 0x18(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x30(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movq 0x78(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq 0xc8(%rsp), %rdx
movq 0x78(%rsp), %rax
shlq $0x3, %rax
subq %rax, %rdx
callq 0x1120
leaq 0x10(%rsp), %rdi
addq 0xc8(%rsp), %rdi
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rdi
movq 0xc8(%rsp), %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
subq %rcx, %rax
movl $0x8, %edx
subq %rax, %rdx
xorl %esi, %esi
callq 0x1040
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1940
movq 0xc8(%rsp), %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
subq %rcx, %rax
movb $-0x80, 0x30(%rsp,%rax)
movq $0x0, 0x70(%rsp)
movq 0x70(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq 0x78(%rsp), %rdx
shlq $0x3, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x1fb1
movq 0x70(%rsp), %rax
movb 0x30(%rsp,%rax), %dl
movq 0x98(%rsp), %rax
movq 0x78(%rsp), %rcx
shlq $0x3, %rcx
addq 0x70(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x1f61
leaq 0x28(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1940
leaq 0x64(%rsp), %rdi
movl $0x50, %esi
callq 0x1820
leaq 0x40(%rsp), %rdi
leaq 0x64(%rsp), %rsi
callq 0x1910
leaq 0x28(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
leaq 0x20(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x1940
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x8, %edx
callq 0x2080
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2046
movq 0x98(%rsp), %rdi
movq 0xc8(%rsp), %rdx
xorl %esi, %esi
callq 0x1040
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x2066
jmp 0x2048
movq 0xc8(%rsp), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)