// File: /workspace/github_cpp_makefile_star2_O3/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 0x38(%rsp), %rdi
leaq 0xb(%rsp), %rcx
callq 0xdafc
movslq %r12d, %rax
movq %rax, 0x28(%rsp)
leaq (,%rax,4), %rbx
movslq 0x4(%r15), %rdx
imulq %rbx, %rdx
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x41b0
movq %r13, %r8
leaq 0x38(%rsp), %rax
movq (%rax), %r13
movl %r12d, %ebp
cmpl $0x0, 0x4(%rsp)
jle 0xcedc
movl 0x4(%rsp), %eax
xorl %ecx, %ecx
movzbl (%r8,%rcx), %edx
testl %r12d, %r12d
jle 0xcecc
movl %edx, %esi
imull %r12d, %esi
movslq %esi, %rsi
movq 0x10(%rsp), %rdi
leaq (%rdi,%rsi,4), %rsi
xorl %edi, %edi
movss (%r14,%rdi,4), %xmm0
addss (%rsi,%rdi,4), %xmm0
movss %xmm0, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %rbp
jne 0xceb4
incl (%r13,%rdx,4)
incq %rcx
addq %rbx, %r14
cmpq %rax, %rcx
jne 0xce96
movslq 0x4(%r15), %rax
testq %rax, %rax
jle 0xd06a
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
testl %r12d, %r12d
setle %dil
movl (%r13,%rcx,4), %esi
testl %esi, %esi
sete %r8b
orb %dil, %r8b
jne 0xcf26
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
xorl %esi, %esi
movss (%rdx,%rsi,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rbp
jne 0xcf10
incq %rcx
addq %rbx, %rdx
cmpq %rax, %rcx
jne 0xcef0
testl %eax, %eax
jle 0xd06a
movq %rbx, 0x20(%rsp)
movl %r12d, 0xc(%rsp)
leaq 0x48(%r15), %rcx
movq %rcx, 0x30(%rsp)
addq %rbp, %rbp
xorl %edx, %edx
movq 0x10(%rsp), %rbx
cmpl $0x0, (%r13,%rdx,4)
jne 0xd054
movq %rdx, 0x18(%rsp)
xorl %r12d, %r12d
movq 0x30(%rsp), %rdi
callq 0xc204
addsd 0x1196c(%rip), %xmm0    # 0x1e8e8
movl 0x4(%r15), %ecx
movl 0x4(%rsp), %eax
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd %xmm0, %xmm1
movslq %r12d, %r14
movq 0x38(%rsp), %r13
movl (%r13,%r14,4), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
ucomisd %xmm0, %xmm1
jb 0xcfbb
incl %r12d
movl %r12d, %eax
cltd
idivl %ecx
movl %edx, %r12d
jmp 0xcf6a
movq 0x18(%rsp), %rax
imulq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,4), %rdi
imull 0xc(%rsp), %r12d
movslq %r12d, %rax
leaq (%rcx,%rax,4), %r12
movq %r12, %rsi
movq 0x20(%rsp), %rdx
callq 0x4260
cmpl $0x0, 0xc(%rsp)
jle 0xd033
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 (%r12,%rax,2), %xmm1
mulss 0x18(%r15), %xmm0
subss %xmm0, %xmm1
movss %xmm1, (%r12,%rax,2)
addq $0x2, %rax
cmpq %rax, %rbp
jne 0xcff2
movl (%r13,%r14,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,%r14,4)
movl 0x4(%r15), %eax
incq %rdx
movslq %eax, %rcx
addq 0x20(%rsp), %rbx
cmpq %rcx, %rdx
jl 0xcf56
jmp 0xd06f
testq %r13, %r13
je 0xd07f
movq 0x48(%rsp), %rsi
subq %r13, %rsi
movq %r13, %rdi
callq 0x4300
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xd0a8
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x4300
movq %rbx, %rdi
callq 0x45b0