// File: /workspace/github_c_makefile_star2_O1/Leeheng86[P]word2vec/word2vec.c
// Function: TrainModel()
void TrainModel() {
  long a, b, c, d;
  FILE *fo;
  pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
  printf("Starting training using file %s\n", train_file);
  starting_alpha = alpha;
  if (read_vocab_file[0] != 0) ReadVocab(); else LearnVocabFromTrainFile();
  if (save_vocab_file[0] != 0) SaveVocab();
  if (output_file[0] == 0) return;
  InitNet();
  if (negative > 0) InitUnigramTable();
  start = clock();
  for (a = 0; a < num_threads; a++) pthread_create(&pt[a], NULL, TrainModelThread, (void *)a);
  for (a = 0; a < num_threads; a++) pthread_join(pt[a], NULL);
  fo = fopen(output_file, "wb");
  if (classes == 0) {
    // Save the word vectors
    fprintf(fo, "%lld %lld\n", vocab_size, layer1_size);
    for (a = 0; a < vocab_size; a++) {
      fprintf(fo, "%s ", vocab[a].word);
      if (binary) for (b = 0; b < layer1_size; b++) fwrite(&syn0[a * layer1_size + b], sizeof(real), 1, fo);
      else for (b = 0; b < layer1_size; b++) fprintf(fo, "%lf ", syn0[a * layer1_size + b]);
      fprintf(fo, "\n");
    }
  } else {
    // Run K-means on the word vectors
    int clcn = classes, iter = 10, closeid;
    int *centcn = (int *)malloc(classes * sizeof(int));
    int *cl = (int *)calloc(vocab_size, sizeof(int));
    real closev, x;
    real *cent = (real *)calloc(classes * layer1_size, sizeof(real));
    for (a = 0; a < vocab_size; a++) cl[a] = a % clcn;
    for (a = 0; a < iter; a++) {
      for (b = 0; b < clcn * layer1_size; b++) cent[b] = 0;
      for (b = 0; b < clcn; b++) centcn[b] = 1;
      for (c = 0; c < vocab_size; c++) {
        for (d = 0; d < layer1_size; d++) cent[layer1_size * cl[c] + d] += syn0[c * layer1_size + d];
        centcn[cl[c]]++;
      }
      for (b = 0; b < clcn; b++) {
        closev = 0;
        for (c = 0; c < layer1_size; c++) {
          cent[layer1_size * b + c] /= centcn[b];
          closev += cent[layer1_size * b + c] * cent[layer1_size * b + c];
        }
        closev = sqrt(closev);
        for (c = 0; c < layer1_size; c++) cent[layer1_size * b + c] /= closev;
      }
      for (c = 0; c < vocab_size; c++) {
        closev = -10;
        closeid = 0;
        for (d = 0; d < clcn; d++) {
          x = 0;
          for (b = 0; b < layer1_size; b++) x += cent[layer1_size * d + b] * syn0[c * layer1_size + b];
          if (x > closev) {
            closev = x;
            closeid = d;
          }
        }
        cl[c] = closeid;
      }
    }
    // Save the K-means classes
    for (a = 0; a < vocab_size; a++) fprintf(fo, "%s %d\n", vocab[a].word, cl[a]);
    free(centcn);
    free(cent);
    free(cl);
  }
  fclose(fo);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movslq 0x70fe(%rip), %rdi      # 0xb268
shlq $0x3, %rdi
callq 0x2300
movq %rax, %rbx
leaq 0x2f8c(%rip), %rdi      # 0x7109
leaq 0x713c(%rip), %rsi      # 0xb2c0
xorl %eax, %eax
callq 0x2050
movss 0x70f5(%rip), %xmm0     # 0xb288
movss %xmm0, 0x731d(%rip)     # 0xb4b8
cmpb $0x0, 0x726e(%rip)      # 0xb410
je 0x41ab
callq 0x3249
jmp 0x41b0
callq 0x2fea
cmpb $0x0, 0x71e9(%rip)      # 0xb3a0
je 0x41be
callq 0x31d1
cmpb $0x0, 0x716b(%rip)      # 0xb330
je 0x4524
callq 0x33a2
cmpl $0x0, 0x70b9(%rip)      # 0xb290
jle 0x41de
callq 0x268c
callq 0x2170
movq %rax, 0x72f6(%rip)      # 0xb4e0
cmpl $0x0, 0x7077(%rip)      # 0xb268
jle 0x4223
xorl %r14d, %r14d
leaq -0xc81(%rip), %r15      # 0x357c
movq %rbx, %r12
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
callq 0x2430
incq %r14
movslq 0x704e(%rip), %rax      # 0xb268
addq $0x8, %r12
cmpq %rax, %r14
jl 0x4200
cmpl $0x0, 0x703e(%rip)      # 0xb268
jle 0x4249
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
xorl %esi, %esi
callq 0x21e0
incq %r14
movslq 0x7024(%rip), %rax      # 0xb268
cmpq %rax, %r14
jl 0x422f
leaq 0x70e0(%rip), %rdi      # 0xb330
leaq 0x2e66(%rip), %rsi      # 0x70bd
callq 0x22a0
movq %rax, %r14
movq 0x724a(%rip), %rbx      # 0xb4b0
testq %rbx, %rbx
je 0x4533
movq %r14, (%rsp)
leaq (,%rbx,4), %rdi
callq 0x2300
movq %rax, %r14
movq 0x7206(%rip), %r13      # 0xb490
movl $0x4, %esi
movq %r13, %rdi
callq 0x2160
movq %rax, %r15
movq 0x6fd7(%rip), %rdi      # 0xb278
imulq %rbx, %rdi
movl $0x4, %esi
callq 0x2160
movq %rax, %r12
testq %r13, %r13
jle 0x42d0
movslq %ebx, %rsi
xorl %ecx, %ecx
movq %rcx, %rax
cqto
idivq %rsi
movl %edx, (%r15,%rcx,4)
incq %rcx
cmpq %rcx, %r13
jne 0x42bc
movslq %ebx, %r13
leaq (,%r13,4), %rax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq 0x6f8a(%rip), %rbx      # 0xb278
movq %rbx, %rax
imulq %r13, %rax
testq %rax, %rax
jle 0x430d
movq 0x8(%rsp), %rdx
imulq %rbx, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x2150
testq %r13, %r13
jle 0x4324
xorl %eax, %eax
movl $0x1, (%r14,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x4314
movq 0x7165(%rip), %rax      # 0xb490
testq %rax, %rax
jle 0x4382
movq 0x7189(%rip), %rcx      # 0xb4c0
leaq (,%rbx,4), %rdx
xorl %esi, %esi
testq %rbx, %rbx
jle 0x436f
movslq (%r15,%rsi,4), %rdi
imulq %rbx, %rdi
leaq (%r12,%rdi,4), %rdi
xorl %r8d, %r8d
movss (%rcx,%r8,4), %xmm0
addss (%rdi,%r8,4), %xmm0
movss %xmm0, (%rdi,%r8,4)
incq %r8
cmpq %r8, %rbx
jne 0x4355
movslq (%r15,%rsi,4), %rdi
incl (%r14,%rdi,4)
incq %rsi
addq %rdx, %rcx
cmpq %rax, %rsi
jne 0x4341
testq %r13, %r13
jle 0x4428
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x6ee2(%rip), %rax      # 0xb278
testq %rax, %rax
jle 0x43d3
xorps %xmm1, %xmm1
cvtsi2ssl (%r14,%rbp,4), %xmm1
movq %rax, %rcx
imulq %rbx, %rcx
addq %r12, %rcx
xorps %xmm0, %xmm0
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm2
divss %xmm1, %xmm2
movss %xmm2, (%rcx,%rdx,4)
mulss %xmm2, %xmm2
addss %xmm2, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x43b3
jmp 0x43d6
xorps %xmm0, %xmm0
ucomiss 0x2c6f(%rip), %xmm0     # 0x704c
jb 0x43e5
sqrtss %xmm0, %xmm0
jmp 0x43ea
callq 0x2420
movq 0x6e87(%rip), %rax      # 0xb278
testq %rax, %rax
jle 0x4418
movq %rax, %rcx
imulq %rbx, %rcx
addq %r12, %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x4402
incq %rbp
addq $0x4, %rbx
cmpq %r13, %rbp
jne 0x438f
movq 0x7061(%rip), %rax      # 0xb490
testq %rax, %rax
jle 0x44ad
movq 0x6e3d(%rip), %rcx      # 0xb278
movq 0x707e(%rip), %rdx      # 0xb4c0
leaq (,%rcx,4), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
testq %r13, %r13
jle 0x449e
movq %r12, %r9
movss 0x2be9(%rip), %xmm0     # 0x7048
xorl %r10d, %r10d
xorps %xmm1, %xmm1
testq %rcx, %rcx
jle 0x4485
xorl %r11d, %r11d
movss (%r9,%r11,4), %xmm2
mulss (%rdx,%r11,4), %xmm2
addss %xmm2, %xmm1
incq %r11
cmpq %r11, %rcx
jne 0x446d
ucomiss %xmm0, %xmm1
cmoval %r10d, %r8d
maxss %xmm0, %xmm1
incq %r10
addq %rsi, %r9
movaps %xmm1, %xmm0
cmpq %r13, %r10
jne 0x4462
movl %r8d, (%r15,%rdi,4)
incq %rdi
addq %rsi, %rdx
cmpq %rax, %rdi
jne 0x444c
movq 0x10(%rsp), %rax
incq %rax
cmpq $0xa, %rax
jne 0x42e2
cmpq $0x0, 0x6fc9(%rip)      # 0xb490
movq (%rsp), %r13
jle 0x4504
movl $0x4, %ebx
xorl %ebp, %ebp
movq 0x6f9d(%rip), %rax      # 0xb478
movq (%rax,%rbx,4), %rdx
movl (%r15,%rbp,4), %ecx
movq %r13, %rdi
leaq 0x2c51(%rip), %rsi      # 0x713e
xorl %eax, %eax
callq 0x2330
incq %rbp
addq $0xa, %rbx
cmpq 0x6f8e(%rip), %rbp      # 0xb490
jl 0x44d4
movq %r14, %rdi
callq 0x22b0
movq %r12, %rdi
callq 0x22b0
movq %r15, %rdi
callq 0x22b0
movq %r13, %rdi
jmp 0x4640
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6f56(%rip), %rdx      # 0xb490
movq 0x6d37(%rip), %rcx      # 0xb278
leaq 0x2be2(%rip), %rsi      # 0x712a
movq %r14, %rdi
xorl %eax, %eax
callq 0x2330
cmpq $0x0, 0x6f36(%rip)      # 0xb490
movq %r14, %rdi
jle 0x4640
leaq 0x2bcf(%rip), %r15      # 0x7139
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x6f01(%rip), %rax      # 0xb478
leaq (,%r13,4), %rcx
addq %r13, %rcx
movq 0x10(%rax,%rcx,8), %rdx
movq %rdi, %r14
leaq 0x2ba4(%rip), %rsi      # 0x7135
xorl %eax, %eax
callq 0x2330
cmpl $0x0, 0x6ee1(%rip)      # 0xb480
movq 0x6cd2(%rip), %rdi      # 0xb278
je 0x45e6
testq %rdi, %rdi
jle 0x461c
xorl %ebx, %ebx
xorl %ebp, %ebp
imulq %r12, %rdi
addq 0x6f04(%rip), %rdi      # 0xb4c0
addq %rbx, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2480
incq %rbp
movq 0x6c9d(%rip), %rdi      # 0xb278
addq $0x4, %rbx
cmpq %rdi, %rbp
jl 0x45b1
jmp 0x461c
testq %rdi, %rdi
jle 0x461c
xorl %ebx, %ebx
imulq %r12, %rdi
addq 0x6ec8(%rip), %rdi      # 0xb4c0
xorps %xmm0, %xmm0
cvtss2sd (%rdi,%rbx,4), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x2330
incq %rbx
movq 0x6c61(%rip), %rdi      # 0xb278
cmpq %rdi, %rbx
jl 0x45ed
movl $0xa, %edi
movq %r14, %rsi
callq 0x22e0
incq %r13
addq $0x4, %r12
cmpq 0x6e59(%rip), %r13      # 0xb490
movq %r14, %rdi
jl 0x4570
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220