// File: /workspace/github_c_makefile_star2_O2/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 $0x368, %rsp            # imm = 0x368
movq %r9, %r14
movq %r8, 0x48(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movl %r14d, %eax
andl $0xf, %eax
movl %eax, 0x14(%rsp)
movq %r9, %r13
andq $-0x10, %r13
movq %r9, %rdi
callq 0x20e0
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x20e0
movq %rax, 0x18(%rsp)
movq %r13, %rdi
callq 0x20e0
movq %rax, 0x40(%rsp)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x80(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x60(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x70(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0xa0(%rsp), %r12
vmovaps %xmm0, (%r12)
leaq 0x50(%rsp), %rbp
vmovaps %xmm0, (%rbp)
leaq 0x200(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2180
movq %rbx, (%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
leaq 0x60(%rsp), %r8
movq %r15, %r9
callq 0x2240
leaq 0x2b0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2180
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
movq %r12, %rdx
leaq 0x90(%rsp), %rcx
movq %r15, %r8
movq 0x30(%rsp), %r9
callq 0x21b0
movq %rbp, %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
leaq 0x70(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %r14, %r8
callq 0x2190
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq 0x48(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2280
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2250
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
movq %r13, %rdx
callq 0x21c0
movq %r13, 0x8(%rsp)
movq %r12, (%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x110(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
leaq 0x90(%rsp), %rbx
movq %rbx, %rcx
movq 0x40(%rsp), %r15
movq %r15, %r8
leaq 0x200(%rsp), %r12
movq %r12, %r9
callq 0x2220
vmovaps (%rbx), %xmm0
leaq 0xb0(%rsp), %rax
vmovaps %xmm0, (%rax)
movq %r12, %rdi
leaq 0x50(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq 0x3a0(%rsp), %r14
movq %r14, %rcx
movq %r13, %r8
callq 0x2130
movq %r13, (%rsp)
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
movq %rbx, %rdx
leaq 0xb0(%rsp), %rcx
leaq 0x70(%rsp), %r8
movq 0x3a8(%rsp), %rbx
movq %rbx, %r9
callq 0x2080
addq %r13, %rbp
addq %r13, %r14
movq %rbx, (%rsp)
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
movl 0x14(%rsp), %r8d
movq %r14, %r9
callq 0x2200
movq 0x18(%rsp), %rdi
callq 0x2160
movq %r15, %rdi
callq 0x2160
movq 0x20(%rsp), %rdi
callq 0x2160
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq