// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, -0x30(%rsp)
testq %rdx, %rdx
je 0x220f
movq -0x30(%rsp), %rcx
leaq 0xf0(%rcx), %rax
leaq 0xe0(%rcx), %rdi
movq %rdi, -0x20(%rsp)
addq $0x10, %rcx
movq %rcx, -0x28(%rsp)
movl $0x10, %ecx
xorl %r8d, %r8d
leaq -0x48(%rsp), %r10
leaq 0x114f(%rip), %r11      # 0x3110
movq %rdx, -0x10(%rsp)
movq %rsi, -0x18(%rsp)
cmpl $0x10, %ecx
jne 0x21f6
movq %r8, -0x8(%rsp)
movups (%rax), %xmm0
movaps %xmm0, -0x48(%rsp)
movq -0x30(%rsp), %rcx
movq %r10, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movb (%rcx,%r8), %r9b
xorb %r9b, (%rsi,%r8)
incq %r8
cmpq $0x4, %r8
jne 0x1fee
incq %rdi
addq $0x4, %rsi
addq $0x4, %rcx
cmpq $0x4, %rdi
jne 0x1feb
movl $0x1, %ebx
movq -0x28(%rsp), %r14
movq %r10, %rcx
xorl %esi, %esi
xorl %edi, %edi
movzbl (%rcx,%rdi,4), %r8d
movb (%r8,%r11), %r8b
movb %r8b, (%rcx,%rdi,4)
incq %rdi
cmpq $0x4, %rdi
jne 0x2021
incq %rsi
incq %rcx
cmpq $0x4, %rsi
jne 0x201f
movb -0x43(%rsp), %cl
movb -0x47(%rsp), %sil
movb -0x46(%rsp), %dil
movb %cl, -0x47(%rsp)
movb -0x3f(%rsp), %cl
movb %cl, -0x43(%rsp)
movb -0x3b(%rsp), %cl
movb %cl, -0x3f(%rsp)
movb %sil, -0x3b(%rsp)
movb -0x3e(%rsp), %cl
movb %cl, -0x46(%rsp)
movb %dil, -0x3e(%rsp)
movb -0x42(%rsp), %cl
movb -0x3a(%rsp), %sil
movb %sil, -0x42(%rsp)
movb %cl, -0x3a(%rsp)
movb -0x45(%rsp), %cl
movb -0x39(%rsp), %sil
movb %sil, -0x45(%rsp)
movb -0x3d(%rsp), %sil
movb %sil, -0x39(%rsp)
movb -0x41(%rsp), %sil
movb %sil, -0x3d(%rsp)
movb %cl, -0x41(%rsp)
cmpq $0xe, %rbx
je 0x2193
xorl %r15d, %r15d
movb -0x48(%rsp,%r15,4), %r12b
movb -0x47(%rsp,%r15,4), %r8b
movb -0x46(%rsp,%r15,4), %bpl
movl %ebp, %r13d
xorb %r8b, %r13b
leal (,%r13,2), %ecx
xorb %r8b, %cl
xorb %r12b, %r8b
movb -0x45(%rsp,%r15,4), %dil
leal (%r8,%r8), %r9d
xorb %r12b, %r9b
xorb %dil, %r12b
leal (%r12,%r12), %esi
xorb %dil, %sil
xorb %bpl, %dil
movl %edi, %edx
xorb %r8b, %dl
sarb $0x7, %r8b
andb $0x1b, %r8b
xorb %r8b, %r9b
xorb %dl, %r9b
movb %r9b, -0x48(%rsp,%r15,4)
sarb $0x7, %r13b
andb $0x1b, %r13b
xorb %r13b, %cl
xorb %dl, %cl
movb %cl, -0x47(%rsp,%r15,4)
leal (%rdi,%rdi), %ecx
sarb $0x7, %dil
andb $0x1b, %dil
xorb %bpl, %cl
xorb %dil, %cl
xorb %dl, %cl
movb %cl, -0x46(%rsp,%r15,4)
sarb $0x7, %r12b
andb $0x1b, %r12b
xorb %r12b, %sil
xorb %dl, %sil
movb %sil, -0x45(%rsp,%r15,4)
incq %r15
cmpq $0x4, %r15
jne 0x20bc
movq %r14, %rcx
movq %r10, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movb (%rcx,%r8), %dl
xorb %dl, (%rsi,%r8)
incq %r8
cmpq $0x4, %r8
jne 0x2165
incq %rdi
addq $0x4, %rsi
addq $0x4, %rcx
cmpq $0x4, %rdi
jne 0x2162
incq %rbx
addq $0x10, %r14
jmp 0x201a
movq -0x20(%rsp), %rcx
movq %r10, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movb (%rcx,%r8), %dl
xorb %dl, (%rsi,%r8)
incq %r8
cmpq $0x4, %r8
jne 0x21a0
incq %rdi
addq $0x4, %rsi
addq $0x4, %rcx
cmpq $0x4, %rdi
jne 0x219d
movl $0xf, %ecx
movq -0x10(%rsp), %rdx
movq -0x18(%rsp), %rsi
movq -0x8(%rsp), %r8
movl %ecx, %ecx
movb (%rax,%rcx), %dil
cmpb $-0x1, %dil
jne 0x21ed
movb $0x0, (%rax,%rcx)
addl $-0x1, %ecx
jb 0x21d6
jmp 0x21f4
incb %dil
movb %dil, (%rax,%rcx)
xorl %ecx, %ecx
movl %ecx, %edi
movb -0x48(%rsp,%rdi), %dil
xorb %dil, (%rsi,%r8)
incq %r8
incl %ecx
cmpq %rdx, %r8
jne 0x1fcb
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop