// File: /workspace/github_c_makefile_star2_O2/Leeheng86[P]word2vec/word-analogy.c
// Function: main
int main(int argc, char **argv) {
  FILE *f;
  char st1[max_size];
  char bestw[N][max_size];
  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: ./word-analogy <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));
  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 three words (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++;
    if (cn < 3) {
      printf("Only %lld words were entered.. three words are needed at the input to perform the calculation\n", cn);
      continue;
    }
    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 = 0;
      bi[a] = b;
      printf("\nWord: %s  Position in vocabulary: %lld\n", st[a], bi[a]);
      if (b == 0) {
        printf("Out of dictionary word!\n");
        break;
      }
    }
    if (b == 0) continue;
    printf("\n                                              Word              Distance\n------------------------------------------------------------------------\n");
    for (a = 0; a < size; a++) vec[a] = M[a + bi[1] * size] - M[a + bi[0] * size] + M[a + bi[2] * 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] = 0;
    for (a = 0; a < N; a++) bestw[a][0] = 0;
    for (c = 0; c < words; c++) {
      if (c == bi[0]) continue;
      if (c == bi[1]) continue;
      if (c == bi[2]) continue;
      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 $0x478c8, %rsp          # imm = 0x478C8
cmpl $0x1, %edi
jg 0x1225
leaq 0xfa1(%rip), %rdi       # 0x21ba
callq 0x10b0
xorl %eax, %eax
jmp 0x1885
movq 0x8(%rsi), %rsi
leaq 0x2b30(%rsp), %rbx
movq %rbx, %rdi
callq 0x1040
leaq 0xdc8(%rip), %rsi       # 0x2008
movq %rbx, %rdi
callq 0x1070
testq %rax, %rax
je 0x184f
movq %rax, %r12
leaq 0xdb0(%rip), %rbx       # 0x200b
leaq 0x20(%rsp), %r14
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
leaq 0x10(%rsp), %r15
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rbp
imulq $0x32, %rbp, %rdi
callq 0x1080
movq %rax, 0x18(%rsp)
movq (%r15), %r15
movq %r15, %rbx
imulq %rbp, %rbx
leaq (,%rbx,4), %rdi
callq 0x1080
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x185d
xorl %r14d, %r14d
pushq $0x4
popq %r13
xorl %esi, %esi
cmpq %rbp, %rsi
jge 0x13d5
movq %rsi, (%rsp)
imulq $0x32, %rsi, %r15
addq 0x18(%rsp), %r15
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x10d0
movl %eax, %ebx
movb %bl, (%r15,%rbp)
movq %r12, %rdi
callq 0x10c0
cmpb $0x20, %bl
je 0x1312
testl %eax, %eax
jne 0x1312
cmpq $0x32, %rbp
setb %al
cmpb $0xa, %bl
setne %cl
andb %al, %cl
movzbl %cl, %eax
addq %rax, %rbp
jmp 0x12dc
movb $0x0, (%r15,%rbp)
movq 0x8(%rsp), %rbx
xorl %r15d, %r15d
movq 0x10(%rsp), %rdi
cmpq %rdi, %r15
jge 0x1347
imulq %r14, %rdi
addq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x10a0
incq %r15
addq $0x4, %rbx
jmp 0x131f
testq %rdi, %rdi
movl $0x0, %eax
cmovgq %rdi, %rax
imulq %r14, %rdi
addq 0x8(%rsp), %rdi
xorps %xmm0, %xmm0
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1378
movss (%rdi,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
jmp 0x1361
ucomiss 0xc85(%rip), %xmm0      # 0x2004
jb 0x1387
sqrtss %xmm0, %xmm0
jmp 0x138c
callq 0x10e0
movq (%rsp), %rsi
movq 0x10(%rsp), %rax
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
imulq %r14, %rax
addq 0x8(%rsp), %rax
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x13c4
movss (%rax,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rax,%rdx,4)
incq %rdx
jmp 0x13ac
incq %rsi
movq 0x20(%rsp), %rbp
addq $0x4, %r14
jmp 0x12c4
movq %r12, %rdi
callq 0x1050
movq 0x2bfc(%rip), %r15      # 0x3fe0
xorl %eax, %eax
cmpq $0x28, %rax
jne 0x1406
xorl %eax, %eax
cmpq $0x13880, %rax          # imm = 0x13880
je 0x1410
movb $0x0, 0x3300(%rsp,%rax)
addq $0x7d0, %rax            # imm = 0x7D0
jmp 0x13ee
andl $0x0, 0x60(%rsp,%rax,4)
incq %rax
jmp 0x13e6
leaq 0xc28(%rip), %rdi       # 0x203f
xorl %eax, %eax
callq 0x1030
xorl %ebx, %ebx
movq (%r15), %rdi
callq 0x10d0
movb %al, 0x420(%rsp,%rbx)
leaq 0x1(%rbx), %rcx
cmpb $0xa, %al
je 0x1443
cmpq $0x7cf, %rbx            # imm = 0x7CF
movq %rcx, %rbx
jb 0x1420
movb $0x0, 0x41f(%rsp,%rcx)
leaq 0x420(%rsp), %rdi
leaq 0xc09(%rip), %rsi       # 0x2063
pushq $0x5
popq %rdx
callq 0x1060
testl %eax, %eax
je 0x121e
leaq 0x16b81(%rsp), %rax
xorl %ecx, %ecx
xorl %edi, %edi
incq %rcx
movq %rax, %rdx
movb 0x41f(%rsp,%rcx), %sil
movb %sil, -0x1(%rdx)
movb $0x0, (%rdx)
movzbl 0x420(%rsp,%rcx), %esi
testl %esi, %esi
je 0x14ad
incq %rdx
incq %rcx
cmpl $0x20, %esi
jne 0x147c
incq %rdi
addq $0x7d0, %rax            # imm = 0x7D0
jmp 0x1476
leaq 0x1(%rdi), %rsi
cmpq $0x2, %rdi
jb 0x1602
movq %rdi, 0x58(%rsp)
xorl %r15d, %r15d
pushq $0x1
popq %r12
movq %rsi, (%rsp)
cmpq %rsi, %r15
je 0x1571
movq 0x20(%rsp), %r14
imulq $0x7d0, %r15, %rax      # imm = 0x7D0
leaq (%rsp,%rax), %r13
addq $0x16b80, %r13          # imm = 0x16B80
testq %r14, %r14
movl $0x0, %r12d
cmovgq %r14, %r12
movq 0x18(%rsp), %rbx
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x151f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1090
testl %eax, %eax
je 0x151c
incq %rbp
addq $0x32, %rbx
jmp 0x14ff
movq %rbp, %r12
cmpq %r14, %r12
movl $0x0, %ebx
cmovneq %r12, %rbx
movq %rbx, 0x100(%rsp,%r15,8)
leaq 0xb8d(%rip), %rdi       # 0x20c7
movq %r13, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1030
incq %r15
testq %rbx, %rbx
movq (%rsp), %rsi
jne 0x14cb
leaq 0xb9c(%rip), %rdi       # 0x20fa
callq 0x10b0
xorl %eax, %eax
movq 0x2a74(%rip), %r15      # 0x3fe0
jmp 0x13e6
movl $0x0, %eax
testq %r12, %r12
movq 0x2a60(%rip), %r15      # 0x3fe0
je 0x13e6
leaq 0xb85(%rip), %rdi       # 0x2112
callq 0x10b0
movq 0x10(%rsp), %rsi
movq 0x100(%rsp), %rbx
movq 0x108(%rsp), %r13
movq 0x110(%rsp), %r8
testq %rsi, %rsi
movl $0x0, %eax
cmovgq %rsi, %rax
movq %r8, %rcx
imulq %rsi, %rcx
movq 0x8(%rsp), %rdi
leaq (%rdi,%rcx,4), %rcx
movq %rbx, %rdx
imulq %rsi, %rdx
leaq (%rdi,%rdx,4), %rdx
imulq %r13, %rsi
leaq (%rdi,%rsi,4), %rsi
xorl %edi, %edi
cmpq %rdi, %rax
je 0x1615
movss (%rsi,%rdi,4), %xmm0
subss (%rdx,%rdi,4), %xmm0
addss (%rcx,%rdi,4), %xmm0
movss %xmm0, 0xbf0(%rsp,%rdi,4)
incq %rdi
jmp 0x15e0
leaq 0xa5f(%rip), %rdi       # 0x2068
xorl %eax, %eax
callq 0x1030
jmp 0x13e4
xorps %xmm0, %xmm0
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1635
movss 0xbf0(%rsp,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
jmp 0x161a
ucomiss 0x9c8(%rip), %xmm0      # 0x2004
jb 0x1644
sqrtss %xmm0, %xmm0
jmp 0x164f
movq %r8, %r14
callq 0x10e0
movq %r14, %r8
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
movl $0x0, %r9d
cmovgq %rdi, %r9
xorl %eax, %eax
cmpq %rax, %r9
je 0x1686
movss 0xbf0(%rsp,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0xbf0(%rsp,%rax,4)
incq %rax
jmp 0x1666
xorl %eax, %eax
cmpq $0x28, %rax
je 0x1698
andl $0x0, 0x60(%rsp,%rax,4)
incq %rax
jmp 0x1688
xorl %eax, %eax
cmpq $0x13880, %rax          # imm = 0x13880
je 0x16b2
movb $0x0, 0x3300(%rsp,%rax)
addq $0x7d0, %rax            # imm = 0x7D0
jmp 0x169a
movq 0x20(%rsp), %r10
testq %r10, %r10
movl $0x0, %eax
cmovleq %rax, %r10
shlq $0x2, %rdi
movq 0x8(%rsp), %rbp
xorl %r15d, %r15d
movq %rbx, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
movq %r9, 0x30(%rsp)
movq %r10, 0x28(%rsp)
cmpq %r10, %r15
je 0x180f
cmpq %rbx, %r15
sete %al
cmpq %r13, %r15
sete %cl
orb %al, %cl
cmpq %r8, %r15
sete %al
orb %cl, %al
jne 0x1804
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x58(%rsp), %rsi
cmpq %rsi, %rcx
ja 0x1731
cmpq %r15, 0x100(%rsp,%rcx,8)
cmoveq %rdx, %rax
incq %rcx
jmp 0x171b
cmpq $0x1, %rax
je 0x1804
xorps %xmm1, %xmm1
xorl %eax, %eax
cmpq %rax, %r9
je 0x175d
movss 0xbf0(%rsp,%rax,4), %xmm0
mulss (%rbp,%rax,4), %xmm0
addss %xmm0, %xmm1
incq %rax
jmp 0x1740
leaq 0x3300(%rsp), %r12
xorl %r14d, %r14d
cmpq $0x28, %r14
je 0x1804
ucomiss 0x60(%rsp,%r14,4), %xmm1
ja 0x1786
incq %r14
addq $0x7d0, %r12            # imm = 0x7D0
jmp 0x1768
leaq 0x15be0(%rsp), %rbx
pushq $0x27
popq %rax
movss %xmm1, (%rsp)
cmpq %r14, %rax
jbe 0x17cb
leaq -0x1(%rax), %r13
movss 0x5c(%rsp,%rax,4), %xmm0
movss %xmm0, 0x60(%rsp,%rax,4)
leaq 0x7d0(%rbx), %rdi
movq %rbx, %rsi
callq 0x1040
movss (%rsp), %xmm1
addq $-0x7d0, %rbx           # imm = 0xF830
movq %r13, %rax
jmp 0x1796
movss %xmm1, 0x60(%rsp,%r14,4)
imulq $0x32, %r15, %rsi
addq 0x18(%rsp), %rsi
movq %r12, %rdi
callq 0x1040
pushq $0x1
popq %rdx
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %r8
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r10
incq %r15
addq %rdi, %rbp
jmp 0x16ed
leaq 0x3300(%rsp), %rbx
xorl %r14d, %r14d
movq 0x27bf(%rip), %r15      # 0x3fe0
cmpq $0x28, %r14
je 0x13e4
cvtss2sd 0x60(%rsp,%r14,4), %xmm0
leaq 0x8b7(%rip), %rdi       # 0x20f0
movq %rbx, %rsi
movb $0x1, %al
callq 0x1030
incq %r14
addq $0x7d0, %rbx            # imm = 0x7D0
jmp 0x1821
leaq 0x94f(%rip), %rdi       # 0x21a5
callq 0x10b0
jmp 0x1882
shrq $0x12, %rbx
movabsq $0xfffffffffff, %rsi    # imm = 0xFFFFFFFFFFF
andq %rbx, %rsi
leaq 0x79b(%rip), %rdi       # 0x2010
movq %rbp, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rax
addq $0x478c8, %rsp          # imm = 0x478C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq