// File: /workspace/github_c_makefile_star2_O3/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:
testq %rdx, %rdx
je 0x2364
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq 0xf0(%rdi), %rax
leaq 0x10(%rdi), %rcx
movl $0x10, %r11d
xorl %r8d, %r8d
leaq -0x18(%rsp), %r9
leaq 0x124e(%rip), %r10      # 0x3160
movdqa 0x11f6(%rip), %xmm0     # 0x3110
movdqa 0x11fe(%rip), %xmm1     # 0x3120
movdqa 0x1206(%rip), %xmm2     # 0x3130
pxor %xmm3, %xmm3
movdqa 0x120a(%rip), %xmm4     # 0x3140
movdqa 0x1212(%rip), %xmm5     # 0x3150
cmpl $0x10, %r11d
jne 0x2340
movdqu (%rax), %xmm6
movdqa %xmm6, -0x18(%rsp)
xorl %r11d, %r11d
movl (%rdi,%r11,4), %ebx
xorl %ebx, -0x18(%rsp,%r11,4)
incq %r11
cmpq $0x4, %r11
jne 0x1f55
movl $0x1, %r11d
movq %rcx, %rbx
movq %r9, %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
movzbl (%r14,%r12,4), %r13d
movb (%r13,%r10), %bpl
movb %bpl, (%r14,%r12,4)
incq %r12
cmpq $0x4, %r12
jne 0x1f79
incq %r15
incq %r14
cmpq $0x4, %r15
jne 0x1f76
movb -0x13(%rsp), %bpl
movb -0x17(%rsp), %r14b
movb -0x16(%rsp), %r15b
movb %bpl, -0x17(%rsp)
movb -0xf(%rsp), %bpl
movb %bpl, -0x13(%rsp)
movb -0xb(%rsp), %bpl
movb %bpl, -0xf(%rsp)
movb %r14b, -0xb(%rsp)
movb -0xe(%rsp), %bpl
movb %bpl, -0x16(%rsp)
movb %r15b, -0xe(%rsp)
movb -0x12(%rsp), %bpl
movb -0xa(%rsp), %r14b
movb %r14b, -0x12(%rsp)
movb %bpl, -0xa(%rsp)
movb -0x15(%rsp), %bpl
movb -0x9(%rsp), %r14b
movb %r14b, -0x15(%rsp)
movb -0xd(%rsp), %r14b
movb %r14b, -0x9(%rsp)
movb -0x11(%rsp), %r14b
movb %r14b, -0xd(%rsp)
movb %bpl, -0x11(%rsp)
cmpq $0xe, %r11
je 0x22ff
movdqa -0x18(%rsp), %xmm8
movdqa %xmm8, %xmm6
pand %xmm0, %xmm6
movd -0x14(%rsp), %xmm7
psllw $0x8, %xmm7
movdqa %xmm0, %xmm9
pandn %xmm7, %xmm9
por %xmm6, %xmm9
pand %xmm1, %xmm9
movd -0x10(%rsp), %xmm6
pslld $0x10, %xmm6
movdqa %xmm1, %xmm7
pandn %xmm6, %xmm7
por %xmm9, %xmm7
pand %xmm2, %xmm7
movd -0xc(%rsp), %xmm9
pslld $0x18, %xmm9
movdqa %xmm2, %xmm6
pandn %xmm9, %xmm6
por %xmm7, %xmm6
movdqa %xmm8, %xmm7
psrlw $0x8, %xmm7
movzbl -0x13(%rsp), %ebp
movd %ebp, %xmm9
psllw $0x8, %xmm9
movdqa %xmm0, %xmm10
pandn %xmm9, %xmm10
por %xmm7, %xmm10
movzbl -0xf(%rsp), %ebp
movd %ebp, %xmm7
pslld $0x10, %xmm7
movdqa %xmm1, %xmm9
pandn %xmm7, %xmm9
pand %xmm1, %xmm10
movzbl -0xb(%rsp), %ebp
movd %ebp, %xmm11
pslld $0x18, %xmm11
movdqa %xmm2, %xmm7
pandn %xmm11, %xmm7
por %xmm9, %xmm7
por %xmm10, %xmm7
movdqa %xmm8, %xmm9
psrld $0x10, %xmm9
pand %xmm0, %xmm9
movzbl -0x12(%rsp), %ebp
movd %ebp, %xmm10
psllw $0x8, %xmm10
movdqa %xmm0, %xmm12
pandn %xmm10, %xmm12
por %xmm9, %xmm12
movzbl -0xe(%rsp), %ebp
movd %ebp, %xmm9
pslld $0x10, %xmm9
movdqa %xmm1, %xmm10
pandn %xmm9, %xmm10
movzbl -0xa(%rsp), %ebp
movd %ebp, %xmm9
pslld $0x18, %xmm9
movdqa %xmm2, %xmm11
pandn %xmm9, %xmm11
por %xmm10, %xmm11
por %xmm12, %xmm11
psrld $0x18, %xmm8
movzbl -0x11(%rsp), %ebp
movd %ebp, %xmm9
psllw $0x8, %xmm9
movdqa %xmm0, %xmm10
pandn %xmm9, %xmm10
por %xmm8, %xmm10
movzbl -0xd(%rsp), %ebp
movd %ebp, %xmm8
pslld $0x10, %xmm8
movdqa %xmm1, %xmm12
pandn %xmm8, %xmm12
movzbl -0x9(%rsp), %ebp
movd %ebp, %xmm8
pslld $0x18, %xmm8
movdqa %xmm2, %xmm9
pandn %xmm8, %xmm9
por %xmm12, %xmm9
por %xmm10, %xmm9
movdqa %xmm9, %xmm8
pxor %xmm11, %xmm8
movdqa %xmm8, %xmm10
pxor %xmm12, %xmm12
pcmpgtb %xmm8, %xmm12
paddb %xmm8, %xmm8
pxor %xmm11, %xmm8
pxor %xmm7, %xmm11
pxor %xmm13, %xmm13
pcmpgtb %xmm11, %xmm13
paddb %xmm11, %xmm11
pxor %xmm7, %xmm11
pxor %xmm6, %xmm7
pxor %xmm7, %xmm10
pxor %xmm14, %xmm14
pcmpgtb %xmm7, %xmm14
paddb %xmm7, %xmm7
pand %xmm4, %xmm14
pxor %xmm6, %xmm7
pxor %xmm14, %xmm7
pxor %xmm10, %xmm7
pand %xmm4, %xmm13
pxor %xmm13, %xmm11
pxor %xmm10, %xmm11
punpckldq %xmm11, %xmm7   # xmm7 = xmm7[0],xmm11[0],xmm7[1],xmm11[1]
pand %xmm4, %xmm12
pxor %xmm12, %xmm8
pxor %xmm10, %xmm8
pxor %xmm9, %xmm6
pxor %xmm11, %xmm11
pcmpgtb %xmm6, %xmm11
paddb %xmm6, %xmm6
pand %xmm4, %xmm11
pxor %xmm9, %xmm6
pxor %xmm11, %xmm6
pxor %xmm10, %xmm6
punpckldq %xmm6, %xmm8    # xmm8 = xmm8[0],xmm6[0],xmm8[1],xmm6[1]
punpcklbw %xmm3, %xmm8    # xmm8 = xmm8[0],xmm3[0],xmm8[1],xmm3[1],xmm8[2],xmm3[2],xmm8[3],xmm3[3],xmm8[4],xmm3[4],xmm8[5],xmm3[5],xmm8[6],xmm3[6],xmm8[7],xmm3[7]
pshufd $0xd7, %xmm8, %xmm6     # xmm6 = xmm8[3,1,1,3]
pshuflw $0x24, %xmm6, %xmm6     # xmm6 = xmm6[0,1,2,0,4,5,6,7]
pshufhw $0xd4, %xmm6, %xmm6     # xmm6 = xmm6[0,1,2,3,4,5,5,7]
pshufd $0x28, %xmm8, %xmm8     # xmm8 = xmm8[0,2,2,0]
pshuflw $0x84, %xmm8, %xmm8     # xmm8 = xmm8[0,1,0,2,4,5,6,7]
pshufhw $0x74, %xmm8, %xmm8     # xmm8 = xmm8[0,1,2,3,4,5,7,5]
packuswb %xmm6, %xmm8
movdqa %xmm5, %xmm6
pandn %xmm8, %xmm6
punpcklbw %xmm3, %xmm7    # xmm7 = xmm7[0],xmm3[0],xmm7[1],xmm3[1],xmm7[2],xmm3[2],xmm7[3],xmm3[3],xmm7[4],xmm3[4],xmm7[5],xmm3[5],xmm7[6],xmm3[6],xmm7[7],xmm3[7]
pshufd $0xd7, %xmm7, %xmm8     # xmm8 = xmm7[3,1,1,3]
pshuflw $0xe2, %xmm8, %xmm8     # xmm8 = xmm8[2,0,2,3,4,5,6,7]
pshufhw $0xed, %xmm8, %xmm8     # xmm8 = xmm8[0,1,2,3,5,7,6,7]
pshufd $0x28, %xmm7, %xmm7     # xmm7 = xmm7[0,2,2,0]
pshuflw $0xe8, %xmm7, %xmm7     # xmm7 = xmm7[0,2,2,3,4,5,6,7]
pshufhw $0xe7, %xmm7, %xmm7     # xmm7 = xmm7[0,1,2,3,7,5,6,7]
packuswb %xmm8, %xmm7
pand %xmm5, %xmm7
por %xmm6, %xmm7
movdqa %xmm7, -0x18(%rsp)
xorl %r14d, %r14d
movl (%rbx,%r14,4), %ebp
xorl %ebp, -0x18(%rsp,%r14,4)
incq %r14
cmpq $0x4, %r14
jne 0x22e1
incq %r11
addq $0x10, %rbx
jmp 0x1f70
xorl %r11d, %r11d
movl 0xe0(%rdi,%r11,4), %ebx
xorl %ebx, -0x18(%rsp,%r11,4)
incq %r11
cmpq $0x4, %r11
jne 0x2302
movl $0xf, %r11d
movl %r11d, %r11d
movb (%rax,%r11), %bl
cmpb $-0x1, %bl
jne 0x2337
movb $0x0, (%rax,%r11)
addl $-0x1, %r11d
jb 0x231e
jmp 0x233d
incb %bl
movb %bl, (%rax,%r11)
xorl %r11d, %r11d
movl %r11d, %ebx
movb -0x18(%rsp,%rbx), %bl
xorb %bl, (%rsi,%r8)
incq %r8
incl %r11d
cmpq %rdx, %r8
jne 0x1f3e
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)