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

    u8 V[16], U[16], checksum[16], F[16], keyprime[16], L[16], Lprime[16];
    u8* S;
    u8* I;
    u8* Z;

    unsigned long long int pt_fblen;
    unsigned int pt_nfblen;

    pt_nfblen=ptlen%16;
    pt_fblen=ptlen-pt_nfblen;

    S= (u8 *) malloc(ptlen);
    I= (u8 *) malloc(ptlen);
    Z= (u8 *) malloc(pt_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], rk[44], rkprime[44];

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

    /*process the plaintext except the last non-full block*/
    PXMAC(mk, sk, L, V, rk, ad, adlen);
    CENC(rkprime, S, F, nonce, ptlen);
    memcpy(I, p, ptlen);
    xor_byte_string(S, I, ptlen);
    checksum_state_string(I, checksum, pt_fblen);
    PXENC(mk, sk, L, V, Z, rk, I, pt_fblen);
    memcpy(U, V, 16);
    XEXLayerEnc(rk, Lprime, Z, c, pt_fblen);
    tagGen(rk, checksum, Lprime, U, F, tag, pt_fblen);

    /*XLS: process the last non-full block*/
    XLS(rk, Lprime, p+pt_fblen, pt_fblen, pt_nfblen, c+pt_fblen, tag);

    /*
    printf("ciphertext is: \n");
    printf_byte_string(c, ptlen);

    printf("tag value is: \n");
    printf_byte_string(tag, 16);
    */

    free(I);
    free(Z);
    free(S);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x358, %rsp            # imm = 0x358
movq %r9, %r14
movq %r8, 0x18(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r15
movq %r9, %r12
andq $-0x10, %r12
movq %r9, %rdi
callq 0x20e0
movq %rax, %rbx
movq %r14, %rdi
callq 0x20e0
movq %rax, %r13
movq %r12, %rdi
callq 0x20e0
movq %rax, 0x78(%rsp)
vpxor %xmm0, %xmm0, %xmm0
leaq 0x50(%rsp), %rax
vmovdqa %xmm0, (%rax)
vmovdqa %xmm0, 0xa0(%rsp)
vmovdqa %xmm0, 0x30(%rsp)
leaq 0x80(%rsp), %rax
vmovdqa %xmm0, (%rax)
leaq 0x90(%rsp), %rax
vmovdqa %xmm0, (%rax)
leaq 0x40(%rsp), %rax
vmovdqa %xmm0, (%rax)
leaq 0x20(%rsp), %rax
vmovdqa %xmm0, (%rax)
leaq 0x100(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2170
movq %r15, (%rsp)
leaq 0xb0(%rsp), %r15
leaq 0x1b0(%rsp), %rsi
movq %r15, %rdi
leaq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x80(%rsp), %r8
movq %rbp, %r9
callq 0x2210
leaq 0x2a0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x80(%rsp), %rsi
callq 0x2170
movq 0x68(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x1b0(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x100(%rsp), %r8
movq 0x70(%rsp), %r9
callq 0x21a0
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x90(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
callq 0x2180
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x2240
testq %r14, %r14
je 0x687d
leaq 0x1f(%r14), %rax
andq $-0x20, %rax
leaq -0x1(%r14), %rcx
vpbroadcastq %rcx, %ymm0
xorl %ecx, %ecx
vpmovsxbq 0x5a24(%rip), %ymm1 # 0xc1a4
vpmovsxbq 0x5a1f(%rip), %ymm2 # 0xc1a8
vpmovsxbq 0x5a1a(%rip), %ymm3 # 0xc1ac
vpmovsxbq 0x5a15(%rip), %ymm4 # 0xc1b0
vpmovsxbq 0x5a10(%rip), %ymm5 # 0xc1b4
vpmovsxbq 0x5a0b(%rip), %ymm6 # 0xc1b8
vpmovsxbq 0x5a06(%rip), %ymm7 # 0xc1bc
vpmovsxbq 0x5a01(%rip), %ymm8 # 0xc1c0
vpbroadcastq %rcx, %ymm9
vpor %ymm1, %ymm9, %ymm10
vpor %ymm2, %ymm9, %ymm11
vpor %ymm3, %ymm9, %ymm12
vpor %ymm4, %ymm9, %ymm13
vpor %ymm5, %ymm9, %ymm14
vpor %ymm6, %ymm9, %ymm15
vporq %ymm7, %ymm9, %ymm16
vpor %ymm8, %ymm9, %ymm9
vpcmpleuq %ymm0, %ymm9, %k0
vpcmpleuq %ymm0, %ymm16, %k1
kshiftlb $0x4, %k1, %k1
korb %k1, %k0, %k0
vpcmpleuq %ymm0, %ymm15, %k1
vpcmpleuq %ymm0, %ymm14, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
kunpckbw %k0, %k1, %k0
vpcmpleuq %ymm0, %ymm13, %k1
vpcmpleuq %ymm0, %ymm12, %k2
kshiftlb $0x4, %k2, %k2
korb %k2, %k1, %k1
vpcmpleuq %ymm0, %ymm11, %k2
vpcmpleuq %ymm0, %ymm10, %k3
kshiftlb $0x4, %k3, %k3
korb %k3, %k2, %k2
kunpckbw %k1, %k2, %k1
kunpckwd %k0, %k1, %k1
vmovdqu8 (%rbx,%rcx), %ymm9 {%k1} {z}
vmovdqu8 (%r13,%rcx), %ymm10 {%k1} {z}
vpxor %ymm9, %ymm10, %ymm9
vmovdqu8 %ymm9, (%r13,%rcx) {%k1}
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0x67bf
movq 0x390(%rsp), %rbp
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
testq %r12, %r12
je 0x68b2
vmovdqa 0x30(%rsp), %xmm0
xorl %eax, %eax
vpxor (%r13,%rax), %xmm0, %xmm0
addq $0x10, %rax
cmpq %rax, %r12
jne 0x689c
vmovdqa %xmm0, 0x30(%rsp)
andl $0xf, %r14d
movq %r14, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %r13, (%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x100(%rsp), %r14
movq 0x78(%rsp), %r15
movq %r15, %r8
movq %r14, %r9
vzeroupper
callq 0x21f0
leaq 0x50(%rsp), %rax
vmovaps (%rax), %xmm0
leaq 0xa0(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
movq %r15, %rdx
movq %rbp, %rcx
movq %r12, %r8
callq 0x2130
movq %r12, (%rsp)
leaq 0x30(%rsp), %rsi
leaq 0x90(%rsp), %r8
movq %r14, %rdi
leaq 0x20(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
movq 0x398(%rsp), %r14
movq %r14, %r9
callq 0x2080
movq 0x18(%rsp), %rdx
addq %r12, %rdx
addq %r12, %rbp
movq %r14, (%rsp)
leaq 0x100(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %r12, %rcx
movq 0x10(%rsp), %r8
movq %rbp, %r9
callq 0x21d0
movq %r13, %rdi
callq 0x2150
movq %r15, %rdi
callq 0x2150
movq %rbx, %rdi
callq 0x2150
addq $0x358, %rsp            # imm = 0x358
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq