// File: /workspace/github_c_makefile_star2_O2/opencoff[P]crypto-rand/aes.c
// Function: AES_CTR_xcrypt_buffer
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
{
  uint8_t buffer[AES_BLOCKLEN];
  
  size_t i;
  int bi;
  for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi)
  {
    if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */
    {
      
      memcpy(buffer, ctx->Iv, AES_BLOCKLEN);
      Cipher((state_t*)buffer,ctx->RoundKey);

      /* Increment Iv and handle overflow */
      for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
      {
	/* inc will overflow */
        if (ctx->Iv[bi] == 255)
	{
          ctx->Iv[bi] = 0;
          continue;
        } 
        ctx->Iv[bi] += 1;
        break;   
      }
      bi = 0;
    }

    buf[i] = (buf[i] ^ buffer[bi]);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
leaq 0xf0(%rdi), %r13
pushq $0x10
popq %rax
xorl %ebp, %ebp
movq %rsp, %r12
leaq 0x1316(%rip), %r14      # 0x3120
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
cmpq %rdx, %rbp
je 0x1faf
cmpl $0x10, %eax
jne 0x1f9d
movdqu (%r13), %xmm0
movdqa %xmm0, (%rsp)
xorl %edi, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1fbe
movb $0x1, %bl
movq %r12, %rax
xorl %ecx, %ecx
movdqa 0x12c3(%rip), %xmm4     # 0x3110
cmpq $0x4, %rcx
je 0x1e74
xorl %edx, %edx
cmpq $0x4, %rdx
je 0x1e6c
movzbl (%rax,%rdx,4), %esi
movb (%rsi,%r14), %sil
movb %sil, (%rax,%rdx,4)
incq %rdx
jmp 0x1e55
incq %rcx
incq %rax
jmp 0x1e4d
movb 0x5(%rsp), %al
movb 0x1(%rsp), %cl
movb 0x2(%rsp), %dl
movb %al, 0x1(%rsp)
movb 0x9(%rsp), %al
movb %al, 0x5(%rsp)
movb 0xd(%rsp), %al
movb %al, 0x9(%rsp)
movb %cl, 0xd(%rsp)
movb 0xa(%rsp), %al
movb %al, 0x2(%rsp)
movb %dl, 0xa(%rsp)
movb 0x6(%rsp), %al
movb 0xe(%rsp), %cl
movb %cl, 0x6(%rsp)
movb %al, 0xe(%rsp)
movb 0x3(%rsp), %al
movb 0xf(%rsp), %cl
movb %cl, 0x3(%rsp)
movb 0xb(%rsp), %cl
movb %cl, 0xf(%rsp)
movb 0x7(%rsp), %cl
movb %cl, 0xb(%rsp)
movb %al, 0x7(%rsp)
cmpb $0xe, %bl
je 0x1f5d
xorl %eax, %eax
cmpq $0x4, %rax
je 0x1f48
movd (%rsp,%rax,4), %xmm0
movdqa %xmm0, %xmm1
psrld $0x8, %xmm1
movdqa %xmm0, %xmm2
pslld $0x18, %xmm2
por %xmm1, %xmm2
pxor %xmm0, %xmm2
movdqa %xmm2, 0x20(%rsp)
movb 0x22(%rsp), %cl
xorb 0x20(%rsp), %cl
pxor %xmm1, %xmm1
pcmpgtb %xmm2, %xmm1
paddb %xmm2, %xmm2
pand %xmm4, %xmm1
movzbl %cl, %ecx
movd %ecx, %xmm3
punpcklbw %xmm3, %xmm3    # xmm3 = xmm3[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
pshuflw $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0,4,5,6,7]
pxor %xmm2, %xmm3
pxor %xmm0, %xmm3
pxor %xmm1, %xmm3
movd %xmm3, (%rsp,%rax,4)
incq %rax
jmp 0x1edf
movzbl %bl, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1fbe
incb %bl
jmp 0x1e40
pushq $0xe
popq %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1fbe
pushq $0xf
popq %rcx
xorl %eax, %eax
testl %ecx, %ecx
js 0x1f93
movl %ecx, %edx
movb (%r13,%rdx), %sil
cmpb $-0x1, %sil
jne 0x1f8b
movb $0x0, (%r13,%rdx)
decl %ecx
jmp 0x1f6e
incb %sil
movb %sil, (%r13,%rdx)
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rsi
movl %eax, %ecx
movb (%rsp,%rcx), %cl
xorb %cl, (%rsi,%rbp)
incq %rbp
incl %eax
jmp 0x1e14
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq