// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/shellaes128v1d5n64/ref/shellaes.c
// Function: shellaesDec_short
int shellaesDec_short(const u8 key[], const u8 nonce[], const u8 ad[], unsigned long long int adlen, const u8 c[], unsigned long long int ctlen, u8 p[], const u8 tag[]){

    u8 V[16], F[16], keyprime[16], L[16], Lprime[16], S[16], I[16];
    u32 mk[4*d], sk[12*d], rk[44], irk[44], rkprime[44];
    u8  pt1[16], pt2[16], ct1[16], ct2[16];
    u8 mask[16], mask1[16];
    u8 temp[16];

    int i, flag;

    /* key setup */
    aesKeySetupEnc(rk, key);
    KeySetupEnc(mk, sk, L, Lprime, keyprime, rk, key);
    aesKeySetupDec(irk, key);
    aesKeySetupEnc(rkprime, keyprime);

    memset(V, 0, 16);

    /*PX-MAC*/
    PXMAC(mk, sk, L, V, rk, ad, adlen);

    /*derive the block of ciphertext and save it to ct1[]*/

    memcpy(ct1, c, ctlen);
    for(i=ctlen; i<16; ++i){
        ct1[i]=tag[i-ctlen];
    }

    /*produce mask 3^3*Lprime*/
    memcpy(mask, Lprime, 16);
    for(i=0; i<3; ++i){
        memcpy(temp, mask, 16);
        double_mask(mask);
        xor_byte_string(temp, mask, 16);
    }

    /*produce mask 2^2*3^3*Lprime*/
    memcpy(mask1, mask, 16);
    double_mask(mask1);
    double_mask(mask1);

    /*decrypt to obtain I[]*/
    xor_byte_string(mask1, ct1, 16);
    aesDecrypt(irk, ct1, pt1);
    xor_byte_string(mask1, pt1, 16);

    xor_byte_string(V, pt1, 16);
    memcpy(ct1, pt1, 16);

    xor_byte_string(ct1, V, 16);

    xor_byte_string(mask, ct1, 16);
    aesDecrypt(irk, ct1, pt1);
    xor_byte_string(mask, pt1, 16);

    memcpy(I, pt1, 16);

    /*decrypt the plaintext*/
    CENC(rkprime, S, F, nonce, 16);
    xor_byte_string(S, pt1, 16);
    memcpy(p, pt1, ctlen);

    /*verify the validness of tag*/
    memcpy(pt2, I, 16);
    double_mask(mask);

    xor_byte_string(mask, pt2, 16);
    aesEncrypt(rk, pt2, ct2);
    xor_byte_string(mask, ct2, 16);

    xor_byte_string(V, ct2, 16);

    memcpy(pt2, ct2, 16);

    double_mask(mask1);

    xor_byte_string(mask1, pt2, 16);
    aesEncrypt(rk, pt2, ct2);
    xor_byte_string(mask1, ct2, 16);

    xor_byte_string(F, ct2, 16);


    flag=1;

    if(pt1[ctlen]!=0x80){
        flag=0;
    }

    for(i=ctlen+1; i<16; ++i){
        if(pt1[i]!=0){
            flag=0;
        }
    }

    for(i=0; i<ctlen; ++i){
        if(ct2[i]!=tag[i+16-ctlen]){
            flag=0;
        }
    }

    /*
     if(flag){
        printf("the plaintext is:\n");
        printf_byte_string(p, ctlen);
    }
    else{
        printf("the tag is invalid!");
    }
    */

    return flag;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %r9, %rbx
movq %r8, 0x30(%rsp)
movq %rcx, %r12
movq %rdx, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r14
movq 0x4a8(%rsp), %rbp
leaq 0x210(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x2180
movq %r14, (%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x370(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
leaq 0x100(%rsp), %r15
movq %r15, %r8
movq %r13, %r9
callq 0x2240
leaq 0x160(%rsp), %rdi
movq %r14, %rsi
callq 0x2260
leaq 0x2c0(%rsp), %rdi
movq %r15, %rsi
callq 0x2180
vxorps %xmm0, %xmm0, %xmm0
leaq 0x60(%rsp), %rcx
vmovaps %xmm0, (%rcx)
movq %r12, (%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x370(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
movq %r13, %r8
movq 0x28(%rsp), %r9
callq 0x21b0
leaq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %rbx, %rdx
callq 0x2280
movslq %ebx, %rax
subq %rbx, %rbp
cmpq $0xf, %rax
jg 0x4e78
movb (%rbp,%rax), %cl
movb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x4e65
vmovaps 0xb0(%rsp), %xmm0
leaq 0x10(%rsp), %r14
vmovaps %xmm0, (%r14)
pushq $0x3
popq %r13
leaq 0x70(%rsp), %r15
pushq $0x10
popq %r12
subl $0x1, %r13d
jb 0x4ec2
vmovaps 0x10(%rsp), %xmm0
vmovaps %xmm0, 0x70(%rsp)
movq %r14, %rdi
callq 0x21f0
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2250
jmp 0x4e98
leaq 0x10(%rsp), %rax
vmovaps (%rax), %xmm0
leaq 0x80(%rsp), %r12
vmovaps %xmm0, (%r12)
movq %r12, %rdi
callq 0x21f0
movq %r12, %rdi
callq 0x21f0
leaq 0x40(%rsp), %r14
pushq $0x10
popq %r13
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0x160(%rsp), %rdi
leaq 0x50(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0x20b0
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2250
vmovaps (%r15), %xmm0
vmovaps %xmm0, (%r14)
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0x160(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x20b0
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2250
vmovaps (%r15), %xmm0
vmovaps %xmm0, 0xa0(%rsp)
leaq 0x2c0(%rsp), %rdi
leaq 0xf0(%rsp), %r14
leaq 0xd0(%rsp), %rdx
movq %r14, %rsi
movq 0x38(%rsp), %rcx
movq %r13, %r8
callq 0x2190
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2250
movq 0x4a0(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2280
vmovaps 0xa0(%rsp), %xmm0
leaq 0xe0(%rsp), %r15
vmovaps %xmm0, (%r15)
movq %r12, %rdi
callq 0x21f0
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0x210(%rsp), %rdi
leaq 0x90(%rsp), %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x2210
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2250
vmovaps (%r14), %xmm0
vmovaps %xmm0, (%r15)
leaq 0x80(%rsp), %r12
movq %r12, %rdi
callq 0x21f0
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0x210(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2210
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2250
leaq 0xd0(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2250
xorl %ecx, %ecx
xorl %eax, %eax
cmpb 0x50(%rsp,%rbx), %cl
seto %al
leal 0x1(%rbx), %edx
cmpl $0x10, %edx
jge 0x50bf
movslq %edx, %rsi
cmpb $0x0, 0x50(%rsp,%rsi)
cmovnel %ecx, %eax
incl %edx
jmp 0x50ab
addq $0x10, %rbp
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rdx, %rbx
je 0x50e1
movb 0x90(%rsp,%rdx), %sil
cmpb (%rbp,%rdx), %sil
cmovnel %ecx, %eax
incq %rdx
jmp 0x50c7
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq