// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/shellaes128v1d5n64/ref/shellaes.c
// Function: shellaesDec_nfb
int shellaesDec_nfb(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], U[16], checksum[16], F[16], keyprime[16], L[16], Lprime[16];
    u8* S;
    u8* I;
    u8* Z;
    u8 tempTag[16], tempTag2[16];
    int i, flag;

    unsigned long long int ct_fblen;
    unsigned int ct_nfblen;

    ct_nfblen=ctlen%16;
    ct_fblen=ctlen-ct_nfblen;

    S= (u8 *) malloc(ctlen);
    I= (u8 *) malloc(ctlen);
    Z= (u8 *) malloc(ct_fblen);

    memset(V, 0, 16);
    memset(U, 0, 16);
    memset(checksum, 0, 16);
    memset(keyprime, 0, 16);
    memset(F, 0, 16);
    memset(L, 0, 16);
    memset(Lprime, 0, 16);

    u32 mk[4*d], sk[12*d], isk[12*d], rk[44], irk[44], rkprime[44];

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

    /*decrypt the last non-full block*/
    memcpy(tempTag, tag, 16);
    XLSInv(irk, Lprime, c+ct_fblen, ct_fblen, ct_nfblen, p+ct_fblen, tempTag);

    /*process associated data*/
    PXMAC(mk, sk, L, V, rk, ad, adlen);

    /*decryption of XEX layer*/
    XEXLayerDec(irk, Lprime, c, Z, ct_fblen);

    /*decryption of PXENC layer*/
    PXDEC(mk, isk, L, V, Z, rk, I, ct_fblen);
    memcpy(U, V, 16);

    /*process nonce*/
    CENC(rkprime, S, F, nonce, ctlen);

    /*produce plaintext*/
    memcpy(p, I, ct_fblen);
    xor_byte_string(S, p, ct_fblen);

    /*verify the validness of tag*/
    checksum_state_string(I, checksum, ct_fblen);
    tagGen(rk, checksum, Lprime, U, F, tempTag2, ct_fblen);

    flag=1;

    for(i=0; i<16; ++i){
        if(tempTag[i]!=tempTag2[i]){
            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 $0x528, %rsp            # imm = 0x528
movq %r9, %r15
movq %r8, 0x28(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %r14
movl %r15d, %r12d
andl $0xf, %r12d
movq %r9, %rbx
andq $-0x10, %rbx
movq %r9, %rdi
callq 0x20e0
movq %rax, 0x50(%rsp)
movq %r15, %rdi
callq 0x20e0
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
callq 0x20e0
movq %rax, 0x30(%rsp)
vxorps %xmm0, %xmm0, %xmm0
leaq 0xd0(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0xb0(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x90(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0xa0(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x80(%rsp), %r13
vmovaps %xmm0, (%r13)
leaq 0x10(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x1e0(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2180
leaq 0x130(%rsp), %rdi
movq %r14, %rsi
callq 0x2260
movq %r14, 0x8(%rsp)
movq %rbp, (%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x430(%rsp), %rsi
leaq 0x340(%rsp), %rdx
movq %r13, %rcx
leaq 0x10(%rsp), %r8
movq %r8, %r14
leaq 0x90(%rsp), %rbp
movq %rbp, %r9
callq 0x2060
leaq 0x290(%rsp), %rdi
movq %rbp, %rsi
callq 0x2180
movq 0x568(%rsp), %rax
vmovups (%rax), %xmm0
leaq 0x70(%rsp), %rax
vmovaps %xmm0, (%rax)
movq 0x28(%rsp), %rbp
leaq (%rbp,%rbx), %rdx
movq 0x560(%rsp), %rcx
leaq (%rcx,%rbx), %r9
movq %rax, (%rsp)
leaq 0x130(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rcx
movl %r12d, %r8d
callq 0x2040
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x430(%rsp), %rsi
movq %r13, %rdx
leaq 0xd0(%rsp), %r14
movq %r14, %rcx
leaq 0x1e0(%rsp), %r13
movq %r13, %r8
movq 0x48(%rsp), %r9
callq 0x21b0
leaq 0x130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq 0x30(%rsp), %r12
movq %r12, %rcx
movq %rbx, %r8
callq 0x2150
movq %rbx, 0x8(%rsp)
movq 0x38(%rsp), %rbp
movq %rbp, (%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x340(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %r14, %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x20a0
vmovaps (%r14), %xmm0
leaq 0xc0(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x290(%rsp), %rdi
movq 0x50(%rsp), %r12
movq %r12, %rsi
leaq 0xa0(%rsp), %r14
movq %r14, %rdx
movq 0x58(%rsp), %rcx
movq %r15, %r8
callq 0x2190
movq 0x560(%rsp), %rdi
movq %rdi, %r13
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2280
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2250
movq %rbp, %rdi
leaq 0xb0(%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdx
callq 0x21c0
movq %rbx, (%rsp)
leaq 0x60(%rsp), %r9
leaq 0x1e0(%rsp), %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
movq %r14, %r8
callq 0x2080
pushq $0x1
popq %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq $0x10, %rdx
je 0x5c3b
movb 0x70(%rsp,%rdx), %sil
cmpb 0x60(%rsp,%rdx), %sil
cmovnel %ecx, %eax
incq %rdx
jmp 0x5c23
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq