// File: /workspace/github_c_makefile_star2_O3/Leeheng86[P]word2vec/distance.c
// Function: main
int main(int argc, char **argv) {
  FILE *f;
  char st1[max_size];
  char *bestw[N];
  char file_name[max_size], st[100][max_size];
  float dist, len, bestd[N], vec[max_size];
  long long words, size, a, b, c, d, cn, bi[100];
  char ch;
  float *M;
  char *vocab;
  if (argc < 2) {
    printf("Usage: ./distance <FILE>\nwhere FILE contains word projections in the BINARY FORMAT\n");
    return 0;
  }
  strcpy(file_name, argv[1]);
  f = fopen(file_name, "rb");
  if (f == NULL) {
    printf("Input file not found\n");
    return -1;
  }
  fscanf(f, "%lld", &words);
  fscanf(f, "%lld", &size);
  vocab = (char *)malloc((long long)words * max_w * sizeof(char));
  for (a = 0; a < N; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
  M = (float *)malloc((long long)words * (long long)size * sizeof(float));
  if (M == NULL) {
    printf("Cannot allocate memory: %lld MB    %lld  %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
    return -1;
  }
  for (b = 0; b < words; b++) {
    a = 0;
    while (1) {
      vocab[b * max_w + a] = fgetc(f);
      if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
      if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
    }
    vocab[b * max_w + a] = 0;
    for (a = 0; a < size; a++) fread(&M[a + b * size], sizeof(float), 1, f);
    len = 0;
    for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
    len = sqrt(len);
    for (a = 0; a < size; a++) M[a + b * size] /= len;
  }
  fclose(f);
  while (1) {
    for (a = 0; a < N; a++) bestd[a] = 0;
    for (a = 0; a < N; a++) bestw[a][0] = 0;
    printf("Enter word or sentence (EXIT to break): ");
    a = 0;
    while (1) {
      st1[a] = fgetc(stdin);
      if ((st1[a] == '\n') || (a >= max_size - 1)) {
        st1[a] = 0;
        break;
      }
      a++;
    }
    if (!strcmp(st1, "EXIT")) break;
    cn = 0;
    b = 0;
    c = 0;
    while (1) {
      st[cn][b] = st1[c];
      b++;
      c++;
      st[cn][b] = 0;
      if (st1[c] == 0) break;
      if (st1[c] == ' ') {
        cn++;
        b = 0;
        c++;
      }
    }
    cn++;
    for (a = 0; a < cn; a++) {
      for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
      if (b == words) b = -1;
      bi[a] = b;
      printf("\nWord: %s  Position in vocabulary: %lld\n", st[a], bi[a]);
      if (b == -1) {
        printf("Out of dictionary word!\n");
        break;
      }
    }
    if (b == -1) continue;
    printf("\n                                              Word       Cosine distance\n------------------------------------------------------------------------\n");
    for (a = 0; a < size; a++) vec[a] = 0;
    for (b = 0; b < cn; b++) {
      if (bi[b] == -1) continue;
      for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
    }
    len = 0;
    for (a = 0; a < size; a++) len += vec[a] * vec[a];
    len = sqrt(len);
    for (a = 0; a < size; a++) vec[a] /= len;
    for (a = 0; a < N; a++) bestd[a] = -1;
    for (a = 0; a < N; a++) bestw[a][0] = 0;
    for (c = 0; c < words; c++) {
      a = 0;
      for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
      if (a == 1) continue;
      dist = 0;
      for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
      for (a = 0; a < N; a++) {
        if (dist > bestd[a]) {
          for (d = N - 1; d > a; d--) {
            bestd[d] = bestd[d - 1];
            strcpy(bestw[d], bestw[d - 1]);
          }
          bestd[a] = dist;
          strcpy(bestw[a], &vocab[c * max_w]);
          break;
        }
      }
    }
    for (a = 0; a < N; a++) printf("%50s\t\t%f\n", bestw[a], bestd[a]);
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x34158, %rsp          # imm = 0x34158
cmpl $0x1, %edi
jg 0x1225
leaq 0xf63(%rip), %rdi       # 0x217c
callq 0x10b0
xorl %eax, %eax
jmp 0x1846
movq 0x8(%rsi), %rsi
leaq 0x2c40(%rsp), %rbx
movq %rbx, %rdi
callq 0x1050
leaq 0xde4(%rip), %rsi       # 0x2024
movq %rbx, %rdi
callq 0x1070
testq %rax, %rax
je 0x180e
movq %rax, %r15
leaq 0xdcc(%rip), %rbx       # 0x2027
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rbx
imulq $0x32, %rbx, %rdi
callq 0x1080
movq %rax, 0x18(%rsp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x1080
movq %rax, 0xd0(%rsp,%r12,8)
incq %r12
cmpq $0x28, %r12
jne 0x1294
movq (%rsp), %r13
movq %r13, %r14
imulq %rbx, %r14
leaq (,%r14,4), %rdi
callq 0x1080
testq %rax, %rax
je 0x181c
testq %rbx, %rbx
movq %rax, 0x8(%rsp)
jle 0x141e
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x10d0
movl %eax, %ebp
imulq $0x32, %r14, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r12
movb %bpl, (%rcx,%rax)
movq %r15, %rdi
callq 0x10c0
cmpb $0x20, %bpl
je 0x1355
movl $0x0, %r13d
testl %eax, %eax
jne 0x1358
xorl %r13d, %r13d
movq %r13, %rax
cmpq $0x32, %r13
setb %cl
cmpb $0xa, %bpl
setne %dl
andb %cl, %dl
movzbl %dl, %r13d
addq %rax, %r13
movq %r15, %rdi
callq 0x10d0
movl %eax, %ebp
movb %bpl, (%r12,%r13)
movq %r15, %rdi
callq 0x10c0
cmpb $0x20, %bpl
je 0x1358
testl %eax, %eax
je 0x1319
jmp 0x1358
xorl %r13d, %r13d
movb $0x0, (%r12,%r13)
movq (%rsp), %rdi
xorps %xmm0, %xmm0
testq %rdi, %rdi
jle 0x13c8
movq 0x8(%rsp), %rbp
movq %rbp, %r12
xorl %r13d, %r13d
imulq %rbx, %rdi
addq %r12, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x10a0
incq %r13
movq (%rsp), %rdi
addq $0x4, %r12
cmpq %rdi, %r13
jl 0x1374
testq %rdi, %rdi
xorps %xmm0, %xmm0
jle 0x13cd
movq %rbx, %rax
imulq %rdi, %rax
addq %rbp, %rax
xorl %ecx, %ecx
movss (%rax,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rdi
jne 0x13b1
jmp 0x13cd
movq 0x8(%rsp), %rbp
ucomiss 0xc4c(%rip), %xmm0      # 0x2020
jb 0x13dc
sqrtss %xmm0, %xmm0
jmp 0x13e1
callq 0x10e0
movq (%rsp), %rax
testq %rax, %rax
jle 0x140c
movq %rax, %rcx
imulq %rbx, %rcx
addq %rbp, %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x13f6
incq %r14
addq $0x4, %rbx
cmpq 0x10(%rsp), %r14
jl 0x12e3
movq %r15, %rdi
callq 0x1060
movq 0x208(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0xa0, %edx
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x1040
xorl %eax, %eax
movq 0xd0(%rsp,%rax,8), %rcx
movb $0x0, (%rcx)
incq %rax
cmpq $0x28, %rax
jne 0x1446
leaq 0xbfa(%rip), %rdi       # 0x205b
xorl %eax, %eax
callq 0x1030
xorl %ebx, %ebx
movq 0x2b6f(%rip), %r14      # 0x3fe0
movq (%r14), %rdi
callq 0x10d0
movb %al, 0x530(%rsp,%rbx)
leaq 0x1(%rbx), %rcx
cmpb $0xa, %al
je 0x1494
cmpq $0x7cf, %rbx            # imm = 0x7CF
movq %rcx, %rbx
jb 0x1471
movb $0x0, 0x52f(%rsp,%rcx)
movl 0x530(%rsp), %eax
movl $0x54495845, %ecx       # imm = 0x54495845
xorl %ecx, %eax
movzbl 0x534(%rsp), %ecx
orl %eax, %ecx
je 0x121e
leaq 0x3411(%rsp), %rax
xorl %ecx, %ecx
xorl %r13d, %r13d
incq %rcx
movq %rax, %rdx
movb 0x52f(%rsp,%rcx), %sil
movb %sil, -0x1(%rdx)
movb $0x0, (%rdx)
movzbl 0x530(%rsp,%rcx), %esi
testl %esi, %esi
je 0x14fe
incq %rdx
incq %rcx
cmpl $0x20, %esi
jne 0x14cd
incq %r13
addq $0x7d0, %rax            # imm = 0x7D0
jmp 0x14c7
xorl %r12d, %r12d
movq 0x10(%rsp), %r14
testq %r14, %r14
jle 0x1544
imulq $0x7d0, %r12, %rax      # imm = 0x7D0
leaq (%rsp,%rax), %r15
addq $0x3410, %r15           # imm = 0x3410
movq 0x18(%rsp), %rbx
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x1546
incq %rbp
addq $0x32, %rbx
cmpq %rbp, %r14
jne 0x1524
movq %r14, %rbp
jmp 0x1546
xorl %ebp, %ebp
cmpq %r14, %rbp
movq %rbp, %rdx
movq $-0x1, %rax
cmoveq %rax, %rdx
movq %rdx, 0x210(%rsp,%r12,8)
imulq $0x7d0, %r12, %rax      # imm = 0x7D0
leaq (%rsp,%rax), %rsi
addq $0x3410, %rsi           # imm = 0x3410
leaq 0xb11(%rip), %rdi       # 0x2089
xorl %eax, %eax
callq 0x1030
cmpq %r14, %rbp
je 0x165f
leaq 0x1(%r12), %rax
cmpq %r13, %r12
movq %rax, %r12
jne 0x1501
cmpq $-0x1, %rbp
movq 0x8(%rsp), %rbp
je 0x1433
leaq 0xb25(%rip), %rdi       # 0x20d4
callq 0x10b0
movq (%rsp), %rbx
testq %rbx, %rbx
jle 0x15d4
leaq (,%rbx,4), %rdx
leaq 0xd00(%rsp), %rdi
xorl %esi, %esi
callq 0x1040
xorl %eax, %eax
testq %rbx, %rbx
setle %dl
movq 0x210(%rsp,%rax,8), %rcx
cmpq $-0x1, %rcx
sete %sil
orb %dl, %sil
jne 0x1621
imulq %rbx, %rcx
leaq (,%rcx,4), %rcx
addq %rbp, %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm0
addss 0xd00(%rsp,%rdx,4), %xmm0
movss %xmm0, 0xd00(%rsp,%rdx,4)
incq %rdx
cmpq %rdx, %rbx
jne 0x1602
leaq 0x1(%rax), %rcx
cmpq %r13, %rax
movq %rcx, %rax
jne 0x15d6
xorps %xmm0, %xmm0
testq %rbx, %rbx
jle 0x1650
xorl %eax, %eax
movss 0xd00(%rsp,%rax,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rax
cmpq %rax, %rbx
jne 0x1637
ucomiss 0x9c9(%rip), %xmm0      # 0x2020
jb 0x1670
sqrtss %xmm0, %xmm0
jmp 0x1675
leaq 0xa56(%rip), %rdi       # 0x20bc
callq 0x10b0
jmp 0x1433
callq 0x10e0
movaps 0x994(%rip), %xmm2      # 0x2010
movl $0x1, %edi
movq (%rsp), %rax
testq %rax, %rax
jle 0x16aa
xorl %ecx, %ecx
movss 0xd00(%rsp,%rcx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0xd00(%rsp,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x168c
xorl %eax, %eax
movaps %xmm2, 0x30(%rsp,%rax,4)
addq $0x4, %rax
cmpq $0x28, %rax
jne 0x16ac
xorl %eax, %eax
movq 0xd0(%rsp,%rax,8), %rcx
movb $0x0, (%rcx)
incq %rax
cmpq $0x28, %rax
jne 0x16bd
movq 0x10(%rsp), %rax
testq %rax, %rax
jle 0x17dc
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $-0x1, %rcx
xorl %edx, %edx
cmpq %r12, 0x218(%rsp,%rcx,8)
cmoveq %rdi, %rdx
incq %rcx
cmpq %rcx, %r13
jne 0x16ee
cmpq $0x1, %rdx
je 0x17cc
movq (%rsp), %rcx
testq %rcx, %rcx
jle 0x1740
movq %rcx, %rdx
imulq %r15, %rdx
addq %rbp, %rdx
xorps %xmm1, %xmm1
xorl %esi, %esi
movss 0xd00(%rsp,%rsi,4), %xmm0
mulss (%rdx,%rsi,4), %xmm0
addss %xmm0, %xmm1
incq %rsi
cmpq %rsi, %rcx
jne 0x1724
jmp 0x1743
xorps %xmm1, %xmm1
xorl %r14d, %r14d
ucomiss 0x30(%rsp,%r14,4), %xmm1
ja 0x1759
incq %r14
cmpq $0x28, %r14
jne 0x1746
jmp 0x17cc
movss %xmm1, 0x24(%rsp)
cmpq $0x26, %r14
ja 0x179a
movl $0x27, %eax
movq 0x28(%rsp), %rdi
leaq -0x1(%rax), %rbp
movss 0x2c(%rsp,%rax,4), %xmm0
movss %xmm0, 0x30(%rsp,%rax,4)
movq 0xc8(%rsp,%rax,8), %rbx
movq %rbx, %rsi
callq 0x1050
movq %rbx, %rdi
movq %rbp, %rax
cmpq %r14, %rbp
ja 0x176f
movss 0x24(%rsp), %xmm0
movss %xmm0, 0x30(%rsp,%r14,4)
movq 0xd0(%rsp,%r14,8), %rdi
imulq $0x32, %r12, %rsi
addq 0x18(%rsp), %rsi
callq 0x1050
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rbp
movl $0x1, %edi
incq %r12
addq $0x4, %r15
cmpq %rax, %r12
jl 0x16e5
xorl %ebx, %ebx
leaq 0x8cd(%rip), %r14       # 0x20b2
movq 0xd0(%rsp,%rbx,8), %rsi
xorps %xmm0, %xmm0
cvtss2sd 0x30(%rsp,%rbx,4), %xmm0
movq %r14, %rdi
movb $0x1, %al
callq 0x1030
incq %rbx
cmpq $0x28, %rbx
jne 0x17e5
jmp 0x1433
leaq 0x952(%rip), %rdi       # 0x2167
callq 0x10b0
jmp 0x1841
shrq $0x12, %r14
movabsq $0xfffffffffff, %rsi    # imm = 0xFFFFFFFFFFF
andq %r14, %rsi
leaq 0x7f8(%rip), %rdi       # 0x202c
movq %rbx, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x34158, %rsp          # imm = 0x34158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq