// File: /workspace/github_c_makefile_star2_O1/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 0x18b7
movq 0x8(%rsi), %rsi
leaq 0x2b30(%rsp), %rbx
movq %rbx, %rdi
callq 0x1050
leaq 0xdc8(%rip), %rsi       # 0x2008
movq %rbx, %rdi
callq 0x1070
testq %rax, %rax
je 0x187f
movq %rax, %r15
leaq 0xdb0(%rip), %rbx       # 0x200b
leaq 0x18(%rsp), %r14
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
leaq 0x8(%rsp), %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %r12
imulq $0x32, %r12, %rdi
callq 0x1080
movq %rax, 0x10(%rsp)
movq (%r13), %r13
movq %r13, %rbx
imulq %r12, %rbx
leaq (,%rbx,4), %rdi
callq 0x1080
movq %rax, (%rsp)
testq %rax, %rax
je 0x188d
cmpq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %r12
jle 0x140f
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x10d0
movl %eax, %ebp
imulq $0x32, %r14, %rax
movq %r12, %rcx
addq %rax, %r12
movb %bpl, (%rcx,%rax)
movq %r15, %rdi
callq 0x10c0
cmpb $0x20, %bpl
je 0x1340
movl $0x0, %r13d
testl %eax, %eax
jne 0x1343
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 0x1343
testl %eax, %eax
je 0x1304
jmp 0x1343
xorl %r13d, %r13d
movb $0x0, (%r12,%r13)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
jle 0x1383
movq (%rsp), %r12
xorl %r13d, %r13d
imulq %rbx, %rdi
addq %r12, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x10a0
incq %r13
movq 0x8(%rsp), %rdi
addq $0x4, %r12
cmpq %rdi, %r13
jl 0x1359
testq %rdi, %rdi
jle 0x13b4
movq %rdi, %rax
imulq %rbx, %rax
addq (%rsp), %rax
xorps %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x10(%rsp), %r12
movss (%rax,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rdi
jne 0x139d
jmp 0x13bc
xorps %xmm0, %xmm0
movq 0x10(%rsp), %r12
ucomiss 0xc41(%rip), %xmm0      # 0x2004
jb 0x13cb
sqrtss %xmm0, %xmm0
jmp 0x13d0
callq 0x10e0
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x13fd
movq %rax, %rcx
imulq %rbx, %rcx
addq (%rsp), %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x13e7
incq %r14
addq $0x4, %rbx
cmpq 0x18(%rsp), %r14
jl 0x12d1
movq %r15, %rdi
callq 0x1060
movq 0x2bc2(%rip), %r15      # 0x3fe0
movl $0xa0, %edx
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x1040
xorl %eax, %eax
movb $0x0, 0x3300(%rsp,%rax)
addq $0x7d0, %rax            # imm = 0x7D0
cmpq $0x13880, %rax          # imm = 0x13880
jne 0x1431
leaq 0xbf1(%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 0x147a
cmpq $0x7cf, %rbx            # imm = 0x7CF
movq %rcx, %rbx
jb 0x1457
movb $0x0, 0x41f(%rsp,%rcx)
movl 0x420(%rsp), %eax
movl $0x54495845, %ecx       # imm = 0x54495845
xorl %ecx, %eax
movzbl 0x424(%rsp), %ecx
orl %eax, %ecx
je 0x121e
leaq 0x16b81(%rsp), %rax
xorl %ecx, %ecx
xorl %ebx, %ebx
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 0x14e3
incq %rdx
incq %rcx
cmpl $0x20, %esi
jne 0x14b2
incq %rbx
addq $0x7d0, %rax            # imm = 0x7D0
jmp 0x14ac
cmpq $0x2, %rbx
jae 0x1502
incq %rbx
leaq 0xb75(%rip), %rdi       # 0x2068
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1030
jmp 0x141e
xorl %r14d, %r14d
movq %rbx, 0x20(%rsp)
movq 0x18(%rsp), %r13
testq %r13, %r13
jle 0x1550
imulq $0x7d0, %r14, %rax      # imm = 0x7D0
leaq (%rsp,%rax), %r15
addq $0x16b80, %r15          # imm = 0x16B80
movq %r12, %rbx
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1090
testl %eax, %eax
je 0x1549
incq %rbp
addq $0x32, %rbx
cmpq %rbp, %r13
jne 0x152b
movq %r13, %rbp
movq 0x20(%rsp), %rbx
jmp 0x1552
xorl %ebp, %ebp
cmpq %r13, %rbp
movl $0x0, %r15d
cmovneq %rbp, %r15
movq %r15, 0x100(%rsp,%r14,8)
imulq $0x7d0, %r14, %rax      # imm = 0x7D0
leaq (%rsp,%rax), %rsi
addq $0x16b80, %rsi          # imm = 0x16B80
leaq 0xb47(%rip), %rdi       # 0x20c7
movq %r15, %rdx
xorl %eax, %eax
callq 0x1030
testq %r15, %r15
je 0x15a1
leaq 0x1(%r14), %rax
cmpq %rbx, %r14
movq %rax, %r14
jne 0x150a
jmp 0x15ad
leaq 0xb52(%rip), %rdi       # 0x20fa
callq 0x10b0
testq %r15, %r15
movq 0x2a29(%rip), %r15      # 0x3fe0
je 0x141e
leaq 0xb4e(%rip), %rdi       # 0x2112
callq 0x10b0
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x1629
movq 0x110(%rsp), %rcx
imulq %rax, %rcx
movq (%rsp), %rdi
leaq (%rdi,%rcx,4), %rcx
movq 0x100(%rsp), %rdx
imulq %rax, %rdx
leaq (%rdi,%rdx,4), %rdx
movq 0x108(%rsp), %rsi
imulq %rax, %rsi
leaq (%rdi,%rsi,4), %rsi
xorl %edi, %edi
movss (%rsi,%rdi,4), %xmm0
subss (%rdx,%rdi,4), %xmm0
addss (%rcx,%rdi,4), %xmm0
movss %xmm0, 0xbf0(%rsp,%rdi,4)
incq %rdi
cmpq %rdi, %rax
jne 0x1609
xorps %xmm0, %xmm0
testq %rax, %rax
jle 0x164c
xorl %ecx, %ecx
movss 0xbf0(%rsp,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x1633
ucomiss 0x9b1(%rip), %xmm0      # 0x2004
jb 0x165b
sqrtss %xmm0, %xmm0
jmp 0x1660
callq 0x10e0
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
jle 0x168a
xorl %eax, %eax
movss 0xbf0(%rsp,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0xbf0(%rsp,%rax,4)
incq %rax
cmpq %rax, %rcx
jne 0x166c
movq %rcx, 0x30(%rsp)
movl $0xa0, %edx
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x1040
xorl %eax, %eax
movb $0x0, 0x3300(%rsp,%rax)
addq $0x7d0, %rax            # imm = 0x7D0
cmpq $0x13880, %rax          # imm = 0x13880
jne 0x16a2
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
movl $0x1, %edx
movq 0x30(%rsp), %rsi
jle 0x183a
movq 0x100(%rsp), %r8
movq 0x108(%rsp), %r9
movq 0x110(%rsp), %r10
leaq (,%rsi,4), %r11
movq (%rsp), %rbp
xorl %r14d, %r14d
movq %rdi, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %r9, 0x48(%rsp)
movq %r10, 0x40(%rsp)
movq %r11, 0x38(%rsp)
cmpq %r8, %r14
je 0x182b
cmpq %r9, %r14
je 0x182b
cmpq %r10, %r14
je 0x182b
movq $-0x1, %rax
xorl %ecx, %ecx
cmpq %r14, 0x108(%rsp,%rax,8)
cmoveq %rdx, %rcx
incq %rax
cmpq %rax, %rbx
jne 0x1734
cmpq $0x1, %rcx
je 0x182b
xorps %xmm1, %xmm1
testq %rsi, %rsi
jle 0x1777
xorl %eax, %eax
movss 0xbf0(%rsp,%rax,4), %xmm0
mulss (%rbp,%rax,4), %xmm0
addss %xmm0, %xmm1
incq %rax
cmpq %rax, %rsi
jne 0x175c
leaq 0x3300(%rsp), %r15
xorl %r13d, %r13d
ucomiss 0x60(%rsp,%r13,4), %xmm1
ja 0x179f
incq %r13
addq $0x7d0, %r15            # imm = 0x7D0
cmpq $0x28, %r13
jne 0x1782
jmp 0x182b
movss %xmm1, 0x2c(%rsp)
cmpq $0x26, %r13
ja 0x17e2
movl $0x27, %eax
leaq 0x163b0(%rsp), %rdi
leaq -0x1(%rax), %r12
movss 0x5c(%rsp,%rax,4), %xmm0
movss %xmm0, 0x60(%rsp,%rax,4)
leaq -0x7d0(%rdi), %rbx
movq %rbx, %rsi
callq 0x1050
movq %rbx, %rdi
movq %r12, %rax
cmpq %r13, %r12
ja 0x17b8
movss 0x2c(%rsp), %xmm0
movss %xmm0, 0x60(%rsp,%r13,4)
imulq $0x32, %r14, %rsi
movq 0x10(%rsp), %r12
addq %r12, %rsi
movq %r15, %rdi
callq 0x1050
movl $0x1, %edx
movq 0x20(%rsp), %rbx
movq 0x30(%rsp), %rsi
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x40(%rsp), %r10
movq 0x38(%rsp), %r11
incq %r14
addq %r11, %rbp
cmpq %rdi, %r14
jne 0x1710
leaq 0x3300(%rsp), %rbx
xorl %r14d, %r14d
movq 0x2794(%rip), %r15      # 0x3fe0
leaq 0x89d(%rip), %r13       # 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x60(%rsp,%r14,4), %xmm0
movq %r13, %rdi
movq %rbx, %rsi
movb $0x1, %al
callq 0x1030
incq %r14
addq $0x7d0, %rbx            # imm = 0x7D0
cmpq $0x28, %r14
jne 0x1853
jmp 0x141e
leaq 0x91f(%rip), %rdi       # 0x21a5
callq 0x10b0
jmp 0x18b2
shrq $0x12, %rbx
movabsq $0xfffffffffff, %rsi    # imm = 0xFFFFFFFFFFF
andq %rbx, %rsi
leaq 0x76b(%rip), %rdi       # 0x2010
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x478c8, %rsp          # imm = 0x478C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq