// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/shellaes128v1d5n64/ref/shellaes.c
// Function: shellaesEnc_short
void shellaesEnc_short(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], F[16], keyprime[16], L[16], Lprime[16], S[16], I[16];

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

         u8 pt[16], pt1[16], pt2[16], ct1[16], ct2[16];
         u8 mask[16];
         u8 temp[16];
         int i;

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

         /*PXMAC: process the associated data and save output to V[]*/
         memset(V, 0, 16);
         PXMAC(mk, sk, L, V, rk, ad, adlen);

         /*CENC: process nonce and save the output to S[] and F[]*/
         CENC(rkprime, S, F, nonce, 16);


         /*pad p[] to a full block and save it to pt[]*/
         memset(pt, 0, 16);
         memcpy(pt, p, ptlen);
         pt[ptlen]=0x80;

         /*mask pt[] by xoring the output S[] of CENC, and save it to I[] */
         memcpy(I, S, 16);
         xor_byte_string(pt, I, 16);

         /*compute 3^3*Lprime and save it to mask[]*/
         memcpy(mask, Lprime, 16);
         for(i=0; i<3; ++i){
            memcpy(temp, mask, 16);
            double_mask(mask);
            xor_byte_string(temp, mask, 16);
         }

         /*compute XEX cipher with tweak of 3^3*Lprime and input I[], and save the output to ct1[]*/

         memcpy(pt1, I, 16);
         xor_byte_string(mask, pt1, 16);
         aesEncrypt(rk, pt1, ct1);
         xor_byte_string(mask, ct1, 16);

         /*compute XEX cipher with tweak of 2*3^3*Lprime and input I[], and save the output to ct2[]*/
         double_mask(mask);
         memcpy(pt2, I, 16);
         xor_byte_string(mask, pt2, 16);
         aesEncrypt(rk, pt2, ct2);
         xor_byte_string(mask, ct2, 16);

         /*compute XEX cipher with tweak of 2^2*3^3*Lprime and input of xoring ct1 and the output V of PX-MAC,
           and save the output to ct1[]*/
         xor_byte_string(ct1, V, 16);
         memcpy(pt1, V, 16);
         double_mask(mask);
         xor_byte_string(mask, pt1, 16);
         aesEncrypt(rk, pt1, ct1);
         xor_byte_string(mask, ct1, 16);

        /*compute XEX cipher with tweak of 2^3*3^3*Lprime and input of xoring ct2 and the updated V,
          and save the output to ct2[]*/
         xor_byte_string(ct2, V, 16);
         memcpy(pt2, V, 16);
         double_mask(mask);
         xor_byte_string(mask, pt2, 16);
         aesEncrypt(rk, pt2, ct2);
         xor_byte_string(mask, ct2, 16);

         /*xor ct2 with the output F of CENC*/
         xor_byte_string(F, ct2, 16);

         /*tag splitting:
         produce the ciphertext and tag from ct1 and ct2*/
         memcpy(c, ct1, ptlen);
         for(i=ptlen; i<16; ++i){
            tag[i-ptlen]=ct1[i];
         }
         for(i=0; i<ptlen; ++i){
            tag[16-ptlen+i]=ct2[i];
         }

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

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

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3b8, %rsp            # imm = 0x3B8
movq %r9, %rbx
movq %r8, 0x38(%rsp)
movq %rcx, %r13
movq %rdx, 0x28(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, %r14
movq 0x3f8(%rsp), %rbp
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2180
movq %r14, (%rsp)
leaq 0x1c0(%rsp), %rdi
leaq 0x210(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
leaq 0x100(%rsp), %r14
movq %r14, %r8
movq %r15, %r9
callq 0x2240
leaq 0x300(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2180
vxorps %xmm0, %xmm0, %xmm0
leaq 0xe0(%rsp), %rcx
vmovaps %xmm0, (%rcx)
movq %r13, (%rsp)
leaq 0x1c0(%rsp), %rdi
leaq 0x210(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
movq %r15, %r8
movq 0x28(%rsp), %r9
callq 0x21b0
leaq 0xf0(%rsp), %r14
leaq 0xd0(%rsp), %rdx
pushq $0x10
popq %r13
movq %r12, %rdi
movq %r14, %rsi
movq 0x30(%rsp), %rcx
movq %r13, %r8
callq 0x2190
leaq 0xa0(%rsp), %r15
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%r15)
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movq %rbx, %rdx
callq 0x2280
movb $-0x80, 0xa0(%rsp,%rbx)
vmovaps (%r14), %xmm0
leaq 0x60(%rsp), %rsi
vmovaps %xmm0, (%rsi)
movq %r15, %rdi
movq %r13, %rdx
callq 0x2250
leaq 0xb0(%rsp), %rax
vmovaps (%rax), %xmm0
leaq 0x40(%rsp), %r14
vmovaps %xmm0, (%r14)
pushq $0x3
popq %r12
leaq 0x70(%rsp), %r15
subl $0x1, %r12d
jb 0x4bad
vmovaps 0x40(%rsp), %xmm0
vmovaps %xmm0, 0x70(%rsp)
movq %r14, %rdi
callq 0x21f0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2250
jmp 0x4b83
vmovaps 0x60(%rsp), %xmm0
leaq 0x90(%rsp), %r12
vmovaps %xmm0, (%r12)
leaq 0x40(%rsp), %r13
pushq $0x10
popq %r15
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2250
leaq 0x110(%rsp), %rdi
leaq 0x10(%rsp), %r14
movq %r12, %rsi
movq %r14, %rdx
callq 0x2210
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2250
movq %r13, %rdi
callq 0x21f0
vmovaps 0x60(%rsp), %xmm0
leaq 0x80(%rsp), %r12
vmovaps %xmm0, (%r12)
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2250
leaq 0x50(%rsp), %r14
leaq 0x110(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2210
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2250
leaq 0xe0(%rsp), %r14
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2250
vmovaps (%r14), %xmm0
leaq 0x90(%rsp), %r12
vmovaps %xmm0, (%r12)
movq %r13, %rdi
callq 0x21f0
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2250
leaq 0x110(%rsp), %rdi
movq %r12, %rsi
leaq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x2210
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2250
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2250
vmovaps (%r14), %xmm0
leaq 0x80(%rsp), %r14
vmovaps %xmm0, (%r14)
movq %r13, %rdi
callq 0x21f0
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2250
leaq 0x110(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2210
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2250
leaq 0xd0(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2250
movq 0x3f0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x2280
movslq %ebx, %rax
subq %rbx, %rbp
cmpq $0x10, %rax
jge 0x4d52
movb 0x10(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
jmp 0x4d3f
addq $0x10, %rbp
xorl %eax, %eax
cmpq %rax, %rbx
je 0x4d6a
movb 0x50(%rsp,%rax), %cl
movb %cl, (%rbp,%rax)
incq %rax
jmp 0x4d58
addq $0x3b8, %rsp            # imm = 0x3B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq