// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rdi, %r14
vxorps %xmm0, %xmm0, %xmm0
movq %rsp, %r13
vmovaps %xmm0, (%r13)
movq (%rcx), %rax
movq %rax, (%r13)
leaq 0x20(%rsp), %rbp
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2210
xorl %ebx, %ebx
movl %ebx, %r12d
movb 0xf(%rsp), %al
incb %al
movb %al, 0xf(%rsp)
cmpq $0x11, %r15
jb 0x3d44
testb %al, %al
jne 0x3cf8
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x3cf0
movq %r14, %rdi
movq %r13, %rsi
cmpl $0x100, %r12d           # imm = 0x100
jne 0x3d14
movq %rbp, %rdx
callq 0x2210
xorl %r12d, %r12d
jmp 0x3cd7
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
callq 0x2210
movq %rbp, %rdi
movq %rbx, %rsi
pushq $0x10
popq %rdx
callq 0x2250
addq $0x10, %rbx
movq %rbx, 0x18(%rsp)
addq $-0x10, %r15
incl %r12d
movl %r12d, %ebx
jmp 0x3cd7
testb %al, %al
jne 0x3d55
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x3d4d
cmpl $0x100, %ebx            # imm = 0x100
jne 0x3d85
movq %rsp, %r13
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2210
xorl %ebx, %ebx
incb 0xf(%r13)
jne 0x3d85
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x3d7d
movq %rsp, %rbp
leaq 0x40(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2210
leaq 0x20(%rsp), %rdi
pushq $0x10
popq %rdx
movq %r13, %rsi
callq 0x2250
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x2280
incb 0xf(%rbp)
jne 0x3dcd
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x3dc5
cmpl $0xff, %ebx
jne 0x3dfa
movq %rsp, %rbx
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2210
incb 0xf(%rbx)
jne 0x3dfa
leaq 0xe(%rsp), %rax
incb (%rax)
leaq -0x1(%rax), %rax
je 0x3df2
movq %rsp, %rsi
movq %r14, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rdx
callq 0x2210
leaq 0x20(%rsp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x2250
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq