// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movl %r9d, 0x4(%rsp)
movl %r8d, %r12d
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r15
movslq 0x4(%rdi), %rsi
leaq 0x54(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x28(%rsp), %rdi
leaq 0xb(%rsp), %rcx
callq 0xd97e
movslq %r12d, %rax
movq %rax, 0x40(%rsp)
leaq (,%rax,4), %rbx
movslq 0x4(%r15), %rdx
imulq %rbx, %rdx
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x41b0
movl %r12d, %ebp
cmpl $0x0, 0x4(%rsp)
jle 0xcf62
movq 0x28(%rsp), %rax
movl 0x4(%rsp), %ecx
xorl %edx, %edx
movzbl (%r13,%rdx), %esi
testl %r12d, %r12d
jle 0xcf54
movl %esi, %edi
imull %r12d, %edi
movslq %edi, %rdi
movq 0x10(%rsp), %r8
leaq (%r8,%rdi,4), %rdi
xorl %r8d, %r8d
movss (%r14,%r8,4), %xmm0
addss (%rdi,%r8,4), %xmm0
movss %xmm0, (%rdi,%r8,4)
incq %r8
cmpq %r8, %rbp
jne 0xcf3a
incl (%rax,%rsi,4)
incq %rdx
addq %rbx, %r14
cmpq %rcx, %rdx
jne 0xcf1a
movslq 0x4(%r15), %rax
testq %rax, %rax
jle 0xcfb6
movq 0x28(%rsp), %rcx
xorl %edx, %edx
movq 0x10(%rsp), %rsi
testl %r12d, %r12d
setle %r8b
movl (%rcx,%rdx,4), %edi
testl %edi, %edi
sete %r9b
orb %r8b, %r9b
jne 0xcfab
xorps %xmm0, %xmm0
cvtsi2ss %edi, %xmm0
xorl %edi, %edi
movss (%rsi,%rdi,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %rbp
jne 0xcf95
incq %rdx
addq %rbx, %rsi
cmpq %rax, %rdx
jne 0xcf77
movq %rbx, 0x20(%rsp)
movl %r12d, 0xc(%rsp)
cmpl $0x0, 0x4(%r15)
jle 0xd0f0
leaq 0x48(%r15), %rax
movq %rax, 0x48(%rsp)
addq %rbp, %rbp
xorl %edx, %edx
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %rax
cmpl $0x0, (%rax,%rdx,4)
jne 0xd0db
movq %rdx, 0x18(%rsp)
xorl %r14d, %r14d
movq 0x48(%rsp), %rdi
callq 0xc260
addsd 0x118d9(%rip), %xmm0    # 0x1e8e0
movl 0x4(%r15), %ecx
movl 0x4(%rsp), %eax
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd %xmm0, %xmm1
movslq %r14d, %r12
movq 0x28(%rsp), %r13
movl (%r13,%r12,4), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
ucomisd %xmm0, %xmm1
jb 0xd046
incl %r14d
movl %r14d, %eax
cltd
idivl %ecx
movl %edx, %r14d
jmp 0xcff5
movq 0x18(%rsp), %rax
imulq 0x40(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,4), %rdi
imull 0xc(%rsp), %r14d
movslq %r14d, %rax
leaq (%rcx,%rax,4), %r14
movq %r14, %rsi
movq 0x20(%rsp), %rdx
callq 0x4260
cmpl $0x0, 0xc(%rsp)
jle 0xd0be
xorl %eax, %eax
movl %eax, %ecx
andl $0x2, %ecx
decl %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss 0x18(%r15), %xmm1
mulss %xmm0, %xmm1
addss (%rbx,%rax,2), %xmm1
movss %xmm1, (%rbx,%rax,2)
movss (%r14,%rax,2), %xmm1
mulss 0x18(%r15), %xmm0
subss %xmm0, %xmm1
movss %xmm1, (%r14,%rax,2)
addq $0x2, %rax
cmpq %rax, %rbp
jne 0xd07d
movl (%r13,%r12,4), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movq 0x18(%rsp), %rdx
movl %ecx, (%r13,%rdx,4)
subl %ecx, (%r13,%r12,4)
incq %rdx
movslq 0x4(%r15), %rax
addq 0x20(%rsp), %rbx
cmpq %rax, %rdx
jl 0xcfde
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xd107
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x4300
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xd130
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x4300
movq %rbx, %rdi
callq 0x45b0