// File: /workspace/github_c_makefile_star2_O1/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 0xf47(%rip), %rdi       # 0x2160
callq 0x10b0
xorl %eax, %eax
jmp 0x1830
movq 0x8(%rsi), %rsi
leaq 0x2c40(%rsp), %rbx
movq %rbx, %rdi
callq 0x1050
leaq 0xdc8(%rip), %rsi       # 0x2008
movq %rbx, %rdi
callq 0x1070
testq %rax, %rax
je 0x17f8
movq %rax, %r15
leaq 0xdb0(%rip), %rbx       # 0x200b
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
leaq 0x8(%rsp), %rdx
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rbx
imulq $0x32, %rbx, %rdi
callq 0x1080
movq %rax, 0x20(%rsp)
movl $0x7d0, %edi            # imm = 0x7D0
callq 0x1080
movq %rax, 0xd0(%rsp,%r12,8)
incq %r12
cmpq $0x28, %r12
jne 0x1296
movq 0x8(%rsp), %r13
movq %r13, %r14
imulq %rbx, %r14
leaq (,%r14,4), %rdi
callq 0x1080
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x1806
cmpq $0x0, 0x10(%rsp)
jle 0x1429
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x10d0
movl %eax, %ebp
imulq $0x32, %r12, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax), %r14
movb %bpl, (%rcx,%rax)
movq %r15, %rdi
callq 0x10c0
cmpb $0x20, %bpl
je 0x135b
movl $0x0, %r13d
testl %eax, %eax
jne 0x135e
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, (%r14,%r13)
movq %r15, %rdi
callq 0x10c0
cmpb $0x20, %bpl
je 0x135e
testl %eax, %eax
je 0x131f
jmp 0x135e
xorl %r13d, %r13d
movb $0x0, (%r14,%r13)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
jle 0x139f
movq 0x18(%rsp), %r14
xorl %r13d, %r13d
imulq %rbx, %rdi
addq %r14, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x10a0
incq %r13
movq 0x8(%rsp), %rdi
addq $0x4, %r14
cmpq %rdi, %r13
jl 0x1375
testq %rdi, %rdi
jle 0x13cf
movq %rdi, %rax
imulq %rbx, %rax
movq 0x18(%rsp), %r14
addq %r14, %rax
xorps %xmm0, %xmm0
xorl %ecx, %ecx
movss (%rax,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rdi
jne 0x13b8
jmp 0x13d7
xorps %xmm0, %xmm0
movq 0x18(%rsp), %r14
ucomiss 0xc26(%rip), %xmm0      # 0x2004
jb 0x13e6
sqrtss %xmm0, %xmm0
jmp 0x13eb
callq 0x10e0
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x1417
movq %rax, %rcx
imulq %rbx, %rcx
addq %r14, %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x1401
incq %r12
addq $0x4, %rbx
cmpq 0x10(%rsp), %r12
jl 0x12e9
movq %r15, %rdi
callq 0x1060
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 0x1444
leaq 0xbe0(%rip), %rdi       # 0x203f
xorl %eax, %eax
callq 0x1030
xorl %ebx, %ebx
movq 0x2b71(%rip), %r14      # 0x3fe0
movq (%r14), %rdi
callq 0x10d0
movb %al, 0x530(%rsp,%rbx)
leaq 0x1(%rbx), %rcx
cmpb $0xa, %al
je 0x1492
cmpq $0x7cf, %rbx            # imm = 0x7CF
movq %rcx, %rbx
jb 0x146f
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 %ebp, %ebp
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 0x14fb
incq %rdx
incq %rcx
cmpl $0x20, %esi
jne 0x14ca
incq %rbp
addq $0x7d0, %rax            # imm = 0x7D0
jmp 0x14c4
xorl %r14d, %r14d
movq 0x10(%rsp), %r13
testq %r13, %r13
jle 0x1542
imulq $0x7d0, %r14, %rax      # imm = 0x7D0
leaq (%rsp,%rax), %r12
addq $0x3410, %r12           # imm = 0x3410
movq 0x20(%rsp), %rbx
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1090
testl %eax, %eax
je 0x1545
incq %r15
addq $0x32, %rbx
cmpq %r15, %r13
jne 0x1522
movq %r13, %r15
jmp 0x1545
xorl %r15d, %r15d
cmpq %r13, %r15
movq %r15, %rbx
movq $-0x1, %rax
cmoveq %rax, %rbx
movq %rbx, 0x210(%rsp,%r14,8)
imulq $0x7d0, %r14, %rax      # imm = 0x7D0
leaq (%rsp,%rax), %rsi
addq $0x3410, %rsi           # imm = 0x3410
leaq 0xaf6(%rip), %rdi       # 0x206d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1030
cmpq %r13, %r15
je 0x1598
leaq 0x1(%r14), %rax
cmpq %rbp, %r14
movq %rax, %r14
jne 0x14fe
jmp 0x15a4
leaq 0xb01(%rip), %rdi       # 0x20a0
callq 0x10b0
cmpq $-0x1, %rbx
movq 0x18(%rsp), %r13
je 0x1431
leaq 0xafe(%rip), %rdi       # 0x20b8
callq 0x10b0
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
jle 0x15e0
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 0x162d
imulq %rbx, %rcx
leaq (,%rcx,4), %rcx
addq %r13, %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 0x160e
leaq 0x1(%rax), %rcx
cmpq %rbp, %rax
movq %rcx, %rax
jne 0x15e2
xorps %xmm0, %xmm0
testq %rbx, %rbx
jle 0x165c
xorl %eax, %eax
movss 0xd00(%rsp,%rax,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rax
cmpq %rax, %rbx
jne 0x1643
ucomiss 0x9a1(%rip), %xmm0      # 0x2004
jb 0x166b
sqrtss %xmm0, %xmm0
jmp 0x1670
callq 0x10e0
movl $0x1, %esi
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x169f
xorl %ecx, %ecx
movss 0xd00(%rsp,%rcx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0xd00(%rsp,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x1681
xorl %eax, %eax
movl $0xbf800000, 0x30(%rsp,%rax,4) # imm = 0xBF800000
incq %rax
cmpq $0x28, %rax
jne 0x16a1
xorl %eax, %eax
movq 0xd0(%rsp,%rax,8), %rcx
movb $0x0, (%rcx)
incq %rax
cmpq $0x28, %rax
jne 0x16b4
cmpq $0x0, 0x10(%rsp)
jle 0x17c6
xorl %ebx, %ebx
xorl %r15d, %r15d
movq $-0x1, %rax
xorl %ecx, %ecx
cmpq %r15, 0x218(%rsp,%rax,8)
cmoveq %rsi, %rcx
incq %rax
cmpq %rax, %rbp
jne 0x16e2
cmpq $0x1, %rcx
je 0x17b4
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x1735
movq %rax, %rcx
imulq %rbx, %rcx
addq %r13, %rcx
xorps %xmm1, %xmm1
xorl %edx, %edx
movss 0xd00(%rsp,%rdx,4), %xmm0
mulss (%rcx,%rdx,4), %xmm0
addss %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rax
jne 0x1719
jmp 0x1738
xorps %xmm1, %xmm1
xorl %r14d, %r14d
ucomiss 0x30(%rsp,%r14,4), %xmm1
ja 0x174e
incq %r14
cmpq $0x28, %r14
jne 0x173b
jmp 0x17b4
movss %xmm1, 0x2c(%rsp)
cmpq $0x26, %r14
ja 0x178c
movl $0x27, %eax
leaq -0x1(%rax), %r12
movss 0x2c(%rsp,%rax,4), %xmm0
movss %xmm0, 0x30(%rsp,%rax,4)
movq 0xc8(%rsp,%rax,8), %rsi
movq 0xd0(%rsp,%rax,8), %rdi
callq 0x1050
movq %r12, %rax
cmpq %r14, %r12
ja 0x175f
movss 0x2c(%rsp), %xmm0
movss %xmm0, 0x30(%rsp,%r14,4)
movq 0xd0(%rsp,%r14,8), %rdi
imulq $0x32, %r15, %rsi
addq 0x20(%rsp), %rsi
callq 0x1050
movl $0x1, %esi
incq %r15
addq $0x4, %rbx
cmpq 0x10(%rsp), %r15
jl 0x16d9
xorl %ebx, %ebx
leaq 0x8c7(%rip), %r14       # 0x2096
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 0x17cf
jmp 0x1431
leaq 0x94c(%rip), %rdi       # 0x214b
callq 0x10b0
jmp 0x182b
shrq $0x12, %r14
movabsq $0xfffffffffff, %rsi    # imm = 0xFFFFFFFFFFF
andq %r14, %rsi
leaq 0x7f2(%rip), %rdi       # 0x2010
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