// File: /workspace/github_cpp_makefile_star2_O2/epfml[P]Bi-Sent2Vec/src/productquantizer.cc
// Function: fasttext::ProductQuantizer::MStep(float const*, float*, unsigned char const*, int, int)
void ProductQuantizer::MStep(const real* x0, real* centroids,
                             const uint8_t* codes,
                             int32_t d, int32_t n) {
  std::vector<int32_t> nelts(ksub_, 0);
  memset(centroids, 0, sizeof(real) * d * ksub_);
  const real* x = x0;
  for (auto i = 0; i < n; i++) {
    auto k = codes[i];
    real* c = centroids + k * d;
    for (auto j = 0; j < d; j++) {
      c[j] += x[j];
    }
    nelts[k]++;
    x += d;
  }

  real* c = centroids;
  for (auto k = 0; k < ksub_; k++) {
    real z = (real) nelts[k];
    if (z != 0) {
      for (auto j = 0; j < d; j++) {
        c[j] /= z;
      }
    }
    c += d;
  }

  std::uniform_real_distribution<> runiform(0,1);
  for (auto k = 0; k < ksub_; k++) {
    if (nelts[k] == 0) {
      int32_t m = 0;
      while (runiform(rng) * (n - ksub_) >= nelts[m] - 1) {
        m = (m + 1) % ksub_;
      }
      memcpy(centroids + k * d, centroids + m * d, sizeof(real) * d);
      for (auto j = 0; j < d; j++) {
        int32_t sign = (j % 2) * 2 - 1;
        centroids[k * d + j] += sign * eps_;
        centroids[m * d + j] -= sign * eps_;
      }
      nelts[k] = nelts[m] / 2;
      nelts[m] -= nelts[k];
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, 0xc(%rsp)
movl %r8d, %ebx
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r15
movslq 0x4(%rdi), %rsi
leaq 0x50(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x28(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0x8676
movl %ebx, 0x8(%rsp)
movslq %ebx, %r14
leaq (,%r14,4), %rax
movq %rax, 0x18(%rsp)
movslq 0x4(%r15), %rdx
imulq %rax, %rdx
xorl %ebx, %ebx
movq %r12, 0x10(%rsp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x41e0
leaq 0x28(%rsp), %rax
movq (%rax), %rax
testl %r14d, %r14d
movl $0x0, %r12d
movq %r14, 0x40(%rsp)
cmovgl %r14d, %r12d
movq 0x18(%rsp), %r9
movl 0xc(%rsp), %edx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
cmpq %rcx, %rbx
je 0xb224
movzbl (%r13,%rbx), %edx
movl %edx, %esi
imull 0x8(%rsp), %esi
movslq %esi, %rsi
movq 0x10(%rsp), %rdi
leaq (%rdi,%rsi,4), %rsi
xorl %edi, %edi
cmpq %rdi, %r12
je 0xb219
movss (%rbp,%rdi,4), %xmm0
addss (%rsi,%rdi,4), %xmm0
movss %xmm0, (%rsi,%rdi,4)
incq %rdi
jmp 0xb1ff
incl (%rax,%rdx,4)
incq %rbx
addq %r9, %rbp
jmp 0xb1df
movl 0x4(%r15), %eax
movq 0x28(%rsp), %rcx
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
movq 0x10(%rsp), %rdi
cmpq %rsi, %rdx
je 0xb276
movl (%rcx,%rdx,4), %r8d
testl %r8d, %r8d
je 0xb26e
cvtsi2ss %r8d, %xmm0
xorl %r8d, %r8d
cmpq %r8, %r12
je 0xb26e
movss (%rdi,%r8,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rdi,%r8,4)
incq %r8
jmp 0xb254
incq %rdx
addq %r9, %rdi
jmp 0xb23e
xorps %xmm0, %xmm0
movhps 0xf668(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
leaq 0x50(%rsp), %rcx
movaps %xmm0, (%rcx)
leaq 0x48(%r15), %rcx
movq %rcx, 0x48(%rsp)
addq %r12, %r12
xorl %edx, %edx
movq 0x10(%rsp), %r13
movslq %eax, %rcx
cmpq %rcx, %rdx
jge 0xb3a4
movq 0x28(%rsp), %rcx
cmpl $0x0, (%rcx,%rdx,4)
jne 0xb399
movq %rdx, 0x20(%rsp)
xorl %r14d, %r14d
leaq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x89a8
movl 0x4(%r15), %ecx
movl 0xc(%rsp), %eax
subl %ecx, %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movslq %r14d, %rbx
movq 0x28(%rsp), %rbp
movl (%rbp,%rbx,4), %eax
decl %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jb 0xb305
incl %r14d
movl %r14d, %eax
cltd
idivl %ecx
movl %edx, %r14d
jmp 0xb2be
movq 0x20(%rsp), %rax
imulq 0x40(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,4), %rdi
imull 0x8(%rsp), %r14d
movslq %r14d, %rax
leaq (%rcx,%rax,4), %r14
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0x42b0
xorl %eax, %eax
cmpq %rax, %r12
je 0xb379
movl %eax, %ecx
andl $0x2, %ecx
decl %ecx
cvtsi2ss %ecx, %xmm0
movss 0x18(%r15), %xmm1
mulss %xmm0, %xmm1
addss (%r13,%rax,2), %xmm1
movss %xmm1, (%r13,%rax,2)
mulss 0x18(%r15), %xmm0
movss (%r14,%rax,2), %xmm1
subss %xmm0, %xmm1
movss %xmm1, (%r14,%rax,2)
addq $0x2, %rax
jmp 0xb335
movl (%rbp,%rbx,4), %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movq 0x20(%rsp), %rdx
movl %eax, (%rbp,%rdx,4)
subl %eax, (%rbp,%rbx,4)
movl 0x4(%r15), %eax
movq 0x18(%rsp), %r9
incq %rdx
addq %r9, %r13
jmp 0xb29b
leaq 0x28(%rsp), %rdi
callq 0x8c0e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x8c0e
movq %rbx, %rdi
callq 0x45d0