// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/shellaes128v1d5n64/ref/shellaes.c
// Function: CENC
void CENC(const u32 rkprime[], u8 S[], u8 F[], const u8 nonce[], unsigned long long int ptlen){

    u8 ctr[16];
    u8 G[16];
    u8 ct[16];
    int i, j=0;

    /*initialize ctr*/
    memset(ctr, 0, 16);
    memcpy(ctr, nonce, byte_length_nonce);

    /*produce the output S except the last block*/
    aesEncrypt(rkprime, ctr, G);

    while(ptlen>16){
        /*increase ctr by adding 1*/
        if(++ctr[15]==0){
            i=14;
            while(++ctr[i]==0){
                --i;
            }
        }
        /*update the value of G*/
        if(j==w){
            aesEncrypt(rkprime, ctr, G);
            j=0;
            continue;
        }

        aesEncrypt(rkprime, ctr, S);

        xor_byte_string(G, S, 16);

        S+=16;

        ptlen-=16;
        ++j;
    }

    /*produce the last block of S*/

    if(++ctr[15]==0){
          i=14;
          while(++ctr[i]==0){
               --i;
          }
    }

    if(j==w){
         aesEncrypt(rkprime, ctr, G);
         j=0;
         if(++ctr[15]==0){
            i=14;
            while(++ctr[i]==0){
                    --i;
            }
        }
    }

    aesEncrypt(rkprime, ctr, ct);
    xor_byte_string(G, ct, 16);

    memcpy(S, ct, ptlen);

    ++j;

    /*produce the value of F*/

    if(++ctr[15]==0){
          i=14;
          while(++ctr[i]==0){
               --i;
          }
    }

    if(j==w){
         aesEncrypt(rkprime, ctr, G);
         j=0;
         if(++ctr[15]==0){
            i=14;
            while(++ctr[i]==0){
                    --i;
            }
        }
    }

    aesEncrypt(rkprime, ctr, F);
    xor_byte_string(G, F, 16);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r15
movq %rdx, 0x38(%rsp)
movq %rsi, %r12
movq %rdi, %r14
vxorps %xmm0, %xmm0, %xmm0
movq %rsp, %rsi
vmovaps %xmm0, (%rsi)
movq (%rcx), %rax
movq %rax, (%rsi)
leaq 0x20(%rsp), %rdx
callq 0x21e0
xorl %ebx, %ebx
movq %r15, 0x18(%rsp)
cmpq $0x11, %r15
setae %r15b
jb 0x421c
xorl %ebx, %ebx
movq %rsp, %r13
leaq 0x20(%rsp), %rbp
incb 0xf(%rsp)
jne 0x41c4
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x41bc
movq %r14, %rdi
movq %r13, %rsi
cmpl $0x100, %ebx            # imm = 0x100
jne 0x41e4
movq %rbp, %rdx
callq 0x21e0
xorl %ebx, %ebx
testb $0x1, %r15b
jne 0x41b1
jmp 0x421c
movq %r12, %rdx
callq 0x21e0
vmovups (%r12), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovups %xmm0, (%r12)
addq $0x10, %r12
movq 0x18(%rsp), %rax
addq $-0x10, %rax
incl %ebx
movq %rax, 0x18(%rsp)
cmpq $0x11, %rax
setae %r15b
jae 0x41b1
incb 0xf(%rsp)
jne 0x422f
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x4227
cmpl $0x100, %ebx            # imm = 0x100
jne 0x425f
movq %rsp, %r13
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x21e0
xorl %ebx, %ebx
incb 0xf(%r13)
jne 0x425f
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x4257
movq %rsp, %r13
leaq 0x40(%rsp), %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x21e0
vmovaps (%rbp), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovaps %xmm0, (%rbp)
movq %r12, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
callq 0x2240
incb 0xf(%r13)
jne 0x42a8
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x42a0
cmpl $0xff, %ebx
jne 0x42d6
movq %rsp, %r15
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x21e0
incb 0xf(%r15)
jne 0x42d6
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x42ce
movq %rsp, %rsi
movq %r14, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rdx
callq 0x21e0
vmovups (%rbx), %xmm0
vxorps 0x20(%rsp), %xmm0, %xmm0
vmovups %xmm0, (%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq