// File: /workspace/github_c_makefile_star2_O2/Leeheng86[P]word2vec/compute-accuracy.c
// Function: main
int main(int argc, char **argv)
{
  FILE *f;
  char st1[max_size], st2[max_size], st3[max_size], st4[max_size], bestw[N][max_size], file_name[max_size], ch;
  float dist, len, bestd[N], vec[max_size];
  long long words, size, a, b, c, d, b1, b2, b3, threshold = 0;
  float *M;
  char *vocab;
  int TCN, CCN = 0, TACN = 0, CACN = 0, SECN = 0, SYCN = 0, SEAC = 0, SYAC = 0, QID = 0, TQ = 0, TQS = 0;
  if (argc < 2) {
    printf("Usage: ./compute-accuracy <FILE> <threshold>\nwhere FILE contains word projections, and threshold is used to reduce vocabulary of the model for fast approximate evaluation (0 = off, otherwise typical value is 30000)\n");
    return 0;
  }
  strcpy(file_name, argv[1]);
  if (argc > 2) threshold = atoi(argv[2]);
  f = fopen(file_name, "rb");
  if (f == NULL) {
    printf("Input file not found\n");
    return -1;
  }
  fscanf(f, "%lld", &words);
  if (threshold) if (words > threshold) words = threshold;
  fscanf(f, "%lld", &size);
  vocab = (char *)malloc(words * max_w * sizeof(char));
  M = (float *)malloc(words * size * sizeof(float));
  if (M == NULL) {
    printf("Cannot allocate memory: %lld MB\n", words * size * sizeof(float) / 1048576);
    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 < max_w; a++) vocab[b * max_w + a] = toupper(vocab[b * max_w + a]);
    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);
  TCN = 0;
  while (1) {
    for (a = 0; a < N; a++) bestd[a] = 0;
    for (a = 0; a < N; a++) bestw[a][0] = 0;
    scanf("%s", st1);
    for (a = 0; a < strlen(st1); a++) st1[a] = toupper(st1[a]);
    if ((!strcmp(st1, ":")) || (!strcmp(st1, "EXIT")) || feof(stdin)) {
      if (TCN == 0) TCN = 1;
      if (QID != 0) {
        printf("ACCURACY TOP1: %.2f %%  (%d / %d)\n", CCN / (float)TCN * 100, CCN, TCN);
        printf("Total accuracy: %.2f %%   Semantic accuracy: %.2f %%   Syntactic accuracy: %.2f %% \n", CACN / (float)TACN * 100, SEAC / (float)SECN * 100, SYAC / (float)SYCN * 100);
      }
      QID++;
      scanf("%s", st1);
      if (feof(stdin)) break;
      printf("%s:\n", st1);
      TCN = 0;
      CCN = 0;
      continue;
    }
    if (!strcmp(st1, "EXIT")) break;
    scanf("%s", st2);
    for (a = 0; a < strlen(st2); a++) st2[a] = toupper(st2[a]);
    scanf("%s", st3);
    for (a = 0; a<strlen(st3); a++) st3[a] = toupper(st3[a]);
    scanf("%s", st4);
    for (a = 0; a < strlen(st4); a++) st4[a] = toupper(st4[a]);
    for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st1)) break;
    b1 = b;
    for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st2)) break;
    b2 = b;
    for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st3)) break;
    b3 = b;
    for (a = 0; a < N; a++) bestd[a] = 0;
    for (a = 0; a < N; a++) bestw[a][0] = 0;
    TQ++;
    if (b1 == words) continue;
    if (b2 == words) continue;
    if (b3 == words) continue;
    for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st4)) break;
    if (b == words) continue;
    for (a = 0; a < size; a++) vec[a] = (M[a + b2 * size] - M[a + b1 * size]) + M[a + b3 * size];
    TQS++;
    for (c = 0; c < words; c++) {
      if (c == b1) continue;
      if (c == b2) continue;
      if (c == b3) 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;
        }
      }
    }
    if (!strcmp(st4, bestw[0])) {
      CCN++;
      CACN++;
      if (QID <= 5) SEAC++; else SYAC++;
    }
    if (QID <= 5) SECN++; else SYCN++;
    TCN++;
    TACN++;
  }
  printf("Questions seen / total: %d %d   %.2f %% \n", TQS, TQ, TQS/(float)TQ*100);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e98, %rsp           # imm = 0x4E98
cmpl $0x1, %edi
jg 0x1263
leaq 0xea0(%rip), %rdi       # 0x20f9
callq 0x10d0
jmp 0x19c3
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8(%rsi), %rsi
leaq 0x2780(%rsp), %rdi
callq 0x1050
cmpl $0x2, %ebp
jne 0x1282
xorl %ebx, %ebx
jmp 0x128e
movq 0x10(%rbx), %rdi
callq 0x10f0
movslq %eax, %rbx
leaq 0xd77(%rip), %rsi       # 0x200c
leaq 0x2780(%rsp), %rdi
callq 0x1090
testq %rax, %rax
je 0x19c7
movq %rax, %r15
leaq 0xd5a(%rip), %rsi       # 0x200f
leaq 0x20(%rsp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x1120
testq %rbx, %rbx
je 0x12d5
cmpq %rbx, 0x20(%rsp)
jle 0x12d5
movq %rbx, 0x20(%rsp)
leaq 0xd33(%rip), %rsi       # 0x200f
leaq 0x58(%rsp), %r14
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1120
movq 0x20(%rsp), %rbx
imulq $0x32, %rbx, %rdi
callq 0x10a0
movq %rax, %r12
movq (%r14), %r14
imulq %rbx, %r14
leaq (,%r14,4), %rdi
callq 0x10a0
testq %rax, %rax
je 0x19d5
xorl %r14d, %r14d
movq %r12, %rbp
xorl %ecx, %ecx
movq %r12, 0x18(%rsp)
movq %rax, 0x50(%rsp)
cmpq %rbx, %rcx
jge 0x1466
movq %r12, %rax
movq %rcx, 0x10(%rsp)
imulq $0x32, %rcx, %r12
addq %rax, %r12
xorl %r13d, %r13d
movq %r15, %rdi
callq 0x1100
movl %eax, %ebx
movb %bl, (%r12,%r13)
movq %r15, %rdi
callq 0x10e0
cmpb $0x20, %bl
je 0x137f
testl %eax, %eax
jne 0x137f
cmpq $0x32, %r13
setb %al
cmpb $0xa, %bl
setne %cl
andb %al, %cl
movzbl %cl, %eax
addq %rax, %r13
jmp 0x1349
movb $0x0, (%r12,%r13)
xorl %ebx, %ebx
movq 0x50(%rsp), %r13
cmpq $0x32, %rbx
je 0x13a4
movsbl (%rbp,%rbx), %edi
callq 0x1130
movb %al, (%rbp,%rbx)
incq %rbx
jmp 0x138b
movq %r13, %rbx
xorl %r12d, %r12d
movq 0x58(%rsp), %rdi
cmpq %rdi, %r12
jge 0x13d2
imulq %r14, %rdi
addq %rbx, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x10c0
incq %r12
addq $0x4, %rbx
jmp 0x13aa
testq %rdi, %rdi
movl $0x0, %eax
cmovgq %rdi, %rax
imulq %r14, %rdi
addq %r13, %rdi
xorps %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x18(%rsp), %r12
cmpq %rcx, %rax
je 0x1406
movss (%rdi,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
jmp 0x13ef
ucomiss 0xbfb(%rip), %xmm0      # 0x2008
jb 0x1415
sqrtss %xmm0, %xmm0
jmp 0x141a
callq 0x1110
movq 0x58(%rsp), %rax
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
imulq %r14, %rax
addq %r13, %rax
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x144c
movss (%rax,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rax,%rdx,4)
incq %rdx
jmp 0x1434
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %rbx
addq $0x32, %rbp
addq $0x4, %r14
jmp 0x132e
movq %r15, %rdi
callq 0x1060
movss 0xb8e(%rip), %xmm3      # 0x2004
movl $0x0, (%rsp)
movl $0x0, 0xc(%rsp)
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
movl $0x0, 0x4(%rsp)
movl $0x0, 0x3c(%rsp)
movl %esi, 0x38(%rsp)
cvtsi2ss %esi, %xmm0
movl %edx, 0x8(%rsp)
cvtsi2ss %edx, %xmm1
movss %xmm1, 0x64(%rsp)
divss %xmm1, %xmm0
mulss %xmm3, %xmm0
movl %r9d, 0x2c(%rsp)
cvtsi2ss %r9d, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
movl %edi, 0x34(%rsp)
cvtsi2ss %edi, %xmm0
divss %xmm0, %xmm1
movl %r10d, 0x28(%rsp)
cvtsi2ss %r10d, %xmm0
mulss %xmm3, %xmm1
movl %r8d, 0x30(%rsp)
cvtsi2ss %r8d, %xmm2
cvtss2sd %xmm1, %xmm1
movsd %xmm1, 0x40(%rsp)
divss %xmm2, %xmm0
mulss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movb $0x0, 0x840(%rsp)
leaq 0xb10(%rip), %r14       # 0x2035
movq %r14, %rdi
leaq 0x1fb0(%rsp), %r13
movq %r13, %rsi
xorl %eax, %eax
callq 0x1080
xorl %ebx, %ebx
leaq 0x17e0(%rsp), %r15
movq %r13, %rdi
callq 0x1040
cmpq %rax, %rbx
jae 0x156a
movsbl 0x1fb0(%rsp,%rbx), %edi
callq 0x1130
movb %al, 0x1fb0(%rsp,%rbx)
incq %rbx
jmp 0x1544
cmpw $0x3a, 0x1fb0(%rsp)
je 0x1766
movq %r13, %rdi
leaq 0xab5(%rip), %rsi       # 0x2038
pushq $0x5
popq %rdx
callq 0x1070
testl %eax, %eax
je 0x1766
movq 0x2a46(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10e0
testl %eax, %eax
jne 0x1766
movq %r13, %rdi
leaq 0xa84(%rip), %rsi       # 0x2038
pushq $0x5
popq %rdx
callq 0x1070
testl %eax, %eax
je 0x1993
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1080
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x1040
cmpq %rax, %rbx
jae 0x15f9
movsbl 0x17e0(%rsp,%rbx), %edi
callq 0x1130
movb %al, 0x17e0(%rsp,%rbx)
incq %rbx
jmp 0x15d3
movq %r14, %rdi
leaq 0x1010(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x1080
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x1040
cmpq %rax, %rbx
jae 0x1636
movsbl 0x1010(%rsp,%rbx), %edi
callq 0x1130
movb %al, 0x1010(%rsp,%rbx)
incq %rbx
jmp 0x1610
movq %r14, %rdi
leaq 0x70(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x1080
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x1040
cmpq %rax, %rbx
jae 0x166a
movsbl 0x70(%rsp,%rbx), %edi
callq 0x1130
movb %al, 0x70(%rsp,%rbx)
incq %rbx
jmp 0x164a
movq 0x20(%rsp), %rax
testq %rax, %rax
movl $0x0, %r15d
movq %rax, 0x10(%rsp)
cmovgq %rax, %r15
movq %r12, %rbx
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x16a3
movq %rbx, %rdi
movq %r13, %rsi
callq 0x10b0
testl %eax, %eax
je 0x16a6
incq %rbp
addq $0x32, %rbx
jmp 0x1686
movq %r15, %rbp
movq %r12, %rbx
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x16ce
movq %rbx, %rdi
leaq 0x17e0(%rsp), %rsi
callq 0x10b0
testl %eax, %eax
je 0x16d1
incq %r13
addq $0x32, %rbx
jmp 0x16ac
movq %r15, %r13
movq %r12, %rbx
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x16f9
movq %rbx, %rdi
leaq 0x1010(%rsp), %rsi
callq 0x10b0
testl %eax, %eax
je 0x16fc
incq %r14
addq $0x32, %rbx
jmp 0x16d7
movq %r15, %r14
movb $0x0, 0x840(%rsp)
incl 0x3c(%rsp)
movq 0x10(%rsp), %rdx
cmpq %rdx, %rbp
sete %al
cmpq %rdx, %r13
sete %cl
orb %al, %cl
cmpq %rdx, %r14
sete %al
orb %cl, %al
jne 0x1516
movq %r12, %rbx
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x174e
movq %rbx, %rdi
leaq 0x70(%rsp), %rsi
callq 0x10b0
testl %eax, %eax
je 0x1751
incq %r12
addq $0x32, %rbx
jmp 0x172f
movq %r15, %r12
cmpq 0x10(%rsp), %r12
movq 0x18(%rsp), %r12
je 0x1516
jmp 0x180d
cmpl $0x0, 0x4(%rsp)
je 0x17c0
movl (%rsp), %edx
cmpl $0x1, %edx
adcl $0x0, %edx
movl 0xc(%rsp), %esi
cvtsi2ss %esi, %xmm0
cvtsi2ss %edx, %xmm1
divss %xmm1, %xmm0
mulss 0x876(%rip), %xmm0      # 0x2004
cvtss2sd %xmm0, %xmm0
leaq 0x8a4(%rip), %rdi       # 0x203d
movb $0x1, %al
callq 0x1030
leaq 0x8b9(%rip), %rdi       # 0x2060
movsd 0x48(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movb $0x3, %al
callq 0x1030
movq %r14, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x1080
movq 0x280c(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10e0
testl %eax, %eax
jne 0x1993
incl 0x4(%rsp)
movl $0x0, (%rsp)
leaq 0x8bf(%rip), %rdi       # 0x20b5
movq %r13, %rsi
xorl %eax, %eax
callq 0x1030
movl $0x0, 0xc(%rsp)
jmp 0x1516
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
movl $0x0, %ebx
cmovgq %rdi, %rbx
shlq $0x2, %rdi
movq %rdi, %rax
imulq %r14, %rax
movq 0x50(%rsp), %rsi
addq %rsi, %rax
movq %rdi, %rcx
imulq %rbp, %rcx
addq %rsi, %rcx
movq %rdi, %rdx
imulq %r13, %rdx
addq %rsi, %rdx
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x1869
movss (%rdx,%rsi,4), %xmm0
subss (%rcx,%rsi,4), %xmm0
addss (%rax,%rsi,4), %xmm0
movss %xmm0, 0x2f50(%rsp,%rsi,4)
incq %rsi
jmp 0x1847
xorps %xmm0, %xmm0
movq 0x50(%rsp), %r12
xorl %ecx, %ecx
movq %rdi, 0x40(%rsp)
cmpq %r15, %rcx
je 0x18ec
cmpq %r14, %rcx
je 0x18e4
cmpq %rbp, %rcx
je 0x18e4
cmpq %r13, %rcx
je 0x18e4
xorps %xmm2, %xmm2
xorl %eax, %eax
cmpq %rax, %rbx
je 0x18ae
movss 0x2f50(%rsp,%rax,4), %xmm1
mulss (%r12,%rax,4), %xmm1
addss %xmm1, %xmm2
incq %rax
jmp 0x1891
ucomiss %xmm0, %xmm2
jbe 0x18e4
imulq $0x32, %rcx, %rsi
addq 0x18(%rsp), %rsi
leaq 0x840(%rsp), %rdi
movq %rcx, 0x10(%rsp)
movss %xmm2, 0x48(%rsp)
callq 0x1050
movq 0x10(%rsp), %rcx
movq 0x40(%rsp), %rdi
movss 0x48(%rsp), %xmm0
incq %rcx
addq %rdi, %r12
jmp 0x1878
leaq 0x70(%rsp), %rdi
leaq 0x840(%rsp), %rsi
callq 0x10b0
testl %eax, %eax
je 0x1930
movq 0x18(%rsp), %r12
movss 0x6f5(%rip), %xmm3      # 0x2004
movl 0x8(%rsp), %edx
movl 0x38(%rsp), %esi
movl 0x34(%rsp), %edi
movl 0x30(%rsp), %r8d
movl 0x2c(%rsp), %r9d
movl 0x28(%rsp), %r10d
movl (%rsp), %r11d
jmp 0x1971
incl 0xc(%rsp)
movl 0x38(%rsp), %esi
incl %esi
cmpl $0x5, 0x4(%rsp)
movq 0x18(%rsp), %r12
movss 0x6b8(%rip), %xmm3      # 0x2004
movl 0x8(%rsp), %edx
movl 0x34(%rsp), %edi
movl 0x30(%rsp), %r8d
movl 0x2c(%rsp), %r9d
movl 0x28(%rsp), %r10d
movl (%rsp), %r11d
jg 0x196e
incl %r9d
jmp 0x1971
incl %r10d
incl %edx
xorl %eax, %eax
xorl %ecx, %ecx
cmpl $0x6, 0x4(%rsp)
setge %al
setl %cl
addl %ecx, %edi
addl %eax, %r8d
incl %r11d
movl %r11d, (%rsp)
jmp 0x14a4
movl 0x3c(%rsp), %edx
cvtsi2ss %edx, %xmm0
movss 0x64(%rsp), %xmm1
divss %xmm0, %xmm1
mulss 0x657(%rip), %xmm1      # 0x2004
cvtss2sd %xmm1, %xmm0
leaq 0x702(%rip), %rdi       # 0x20ba
movl 0x8(%rsp), %esi
movb $0x1, %al
callq 0x1030
xorl %eax, %eax
jmp 0x19f7
leaq 0x716(%rip), %rdi       # 0x20e4
callq 0x10d0
jmp 0x19f4
shrq $0x12, %r14
movabsq $0xfffffffffff, %rsi    # imm = 0xFFFFFFFFFFF
andq %r14, %rsi
leaq 0x627(%rip), %rdi       # 0x2014
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rax
addq $0x4e98, %rsp           # imm = 0x4E98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq