// File: /workspace/github_c_makefile_star2_O1/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 0x1253
leaq 0xeb0(%rip), %rdi       # 0x20f9
callq 0x10c0
jmp 0x1a22
movq %rsi, %rbx
movl %edi, %ebp
movq 0x8(%rsi), %rsi
leaq 0x2780(%rsp), %rdi
callq 0x1050
cmpl $0x2, %ebp
jne 0x1272
xorl %ebx, %ebx
jmp 0x127e
movq 0x10(%rbx), %rdi
callq 0x10e0
movslq %eax, %rbx
leaq 0xd87(%rip), %rsi       # 0x200c
leaq 0x2780(%rsp), %rdi
callq 0x1080
testq %rax, %rax
je 0x1a26
movq %rax, %r14
leaq 0xd6a(%rip), %rsi       # 0x200f
leaq 0x18(%rsp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x1110
testq %rbx, %rbx
je 0x12c5
cmpq %rbx, 0x18(%rsp)
jle 0x12c5
movq %rbx, 0x18(%rsp)
leaq 0xd43(%rip), %rsi       # 0x200f
leaq 0x40(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1110
movq 0x18(%rsp), %r15
imulq $0x32, %r15, %rdi
callq 0x1090
movq %rax, %r12
imulq (%rbx), %r15
leaq (,%r15,4), %rdi
callq 0x1090
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x1a34
cmpq $0x0, 0x18(%rsp)
movq %r12, 0x10(%rsp)
jle 0x1495
xorl %ebx, %ebx
movq %r12, %r15
xorl %r13d, %r13d
movq %r14, %rdi
callq 0x10f0
movl %eax, %ebp
movq %r13, 0x28(%rsp)
imulq $0x32, %r13, %rax
leaq (%r12,%rax), %r13
movb %bpl, (%r12,%rax)
movq %r14, %rdi
callq 0x10d0
cmpb $0x20, %bpl
je 0x139a
movl $0x0, %r12d
testl %eax, %eax
jne 0x139d
xorl %r12d, %r12d
movq %r12, %rax
cmpq $0x32, %r12
setb %cl
cmpb $0xa, %bpl
setne %dl
andb %cl, %dl
movzbl %dl, %r12d
addq %rax, %r12
movq %r14, %rdi
callq 0x10f0
movl %eax, %ebp
movb %bpl, (%r13,%r12)
movq %r14, %rdi
callq 0x10d0
cmpb $0x20, %bpl
je 0x139d
testl %eax, %eax
je 0x135d
jmp 0x139d
xorl %r12d, %r12d
movb $0x0, (%r13,%r12)
xorl %r12d, %r12d
movsbl (%r15,%r12), %edi
callq 0x1120
movb %al, (%r15,%r12)
incq %r12
cmpq $0x32, %r12
jne 0x13a6
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
jle 0x13f9
movq 0x20(%rsp), %r12
xorl %r13d, %r13d
imulq %rbx, %rdi
addq %r12, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x10b0
incq %r13
movq 0x40(%rsp), %rdi
addq $0x4, %r12
cmpq %rdi, %r13
jl 0x13cf
testq %rdi, %rdi
jle 0x1430
movq %rdi, %rax
imulq %rbx, %rax
addq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x10(%rsp), %r12
movq 0x28(%rsp), %r13
movss (%rax,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rdi
jne 0x1419
jmp 0x143d
xorps %xmm0, %xmm0
movq 0x10(%rsp), %r12
movq 0x28(%rsp), %r13
ucomiss 0xbc4(%rip), %xmm0      # 0x2008
jb 0x144c
sqrtss %xmm0, %xmm0
jmp 0x1451
callq 0x1100
movq 0x40(%rsp), %rax
testq %rax, %rax
jle 0x147f
movq %rax, %rcx
imulq %rbx, %rcx
addq 0x20(%rsp), %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x1469
incq %r13
addq $0x32, %r15
addq $0x4, %rbx
cmpq 0x18(%rsp), %r13
jl 0x1327
movq %r14, %rdi
callq 0x1060
movss 0xb5f(%rip), %xmm3      # 0x2004
movl $0x0, 0xc(%rsp)
movl $0x0, 0x8(%rsp)
xorl %edx, %edx
xorl %esi, %esi
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r8d, %r8d
xorl %r9d, %r9d
movl $0x0, (%rsp)
movl $0x0, 0x28(%rsp)
movl %esi, 0x3c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movl %edx, 0x4(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
movss %xmm1, 0x6c(%rsp)
divss %xmm1, %xmm0
mulss %xmm3, %xmm0
movl %r8d, 0x38(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %r8d, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movl %ecx, 0x68(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
divss %xmm0, %xmm1
movl %r9d, 0x34(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %r9d, %xmm0
mulss %xmm3, %xmm1
movl %eax, 0x64(%rsp)
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
cvtss2sd %xmm1, %xmm1
movsd %xmm1, 0x50(%rsp)
divss %xmm2, %xmm0
mulss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
movb $0x0, 0x17e0(%rsp)
leaq 0xad2(%rip), %r14       # 0x2035
movq %r14, %rdi
leaq 0x70(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x70(%rsp)
je 0x159c
xorl %ebx, %ebx
movsbl 0x70(%rsp,%rbx), %edi
callq 0x1120
movb %al, 0x70(%rsp,%rbx)
incq %rbx
movq %r15, %rdi
callq 0x1040
cmpq %rax, %rbx
jb 0x157e
cmpw $0x3a, 0x70(%rsp)
leaq 0x1fb0(%rsp), %r13
leaq 0x1010(%rsp), %rbp
je 0x17c0
movl 0x70(%rsp), %eax
movl $0x54495845, %ecx       # imm = 0x54495845
xorl %ecx, %eax
movzbl 0x74(%rsp), %ecx
orl %eax, %ecx
je 0x17c0
movq 0x2a09(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10d0
testl %eax, %eax
jne 0x17c0
movl 0x70(%rsp), %eax
movl $0x54495845, %ecx       # imm = 0x54495845
xorl %ecx, %eax
movzbl 0x74(%rsp), %ecx
orl %eax, %ecx
je 0x19ec
movq %r14, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x1fb0(%rsp)
je 0x163c
xorl %ebx, %ebx
movsbl 0x1fb0(%rsp,%rbx), %edi
callq 0x1120
movb %al, 0x1fb0(%rsp,%rbx)
incq %rbx
movq %r13, %rdi
callq 0x1040
cmpq %rax, %rbx
jb 0x1618
movq %r14, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x1010(%rsp)
je 0x1679
xorl %ebx, %ebx
movsbl 0x1010(%rsp,%rbx), %edi
callq 0x1120
movb %al, 0x1010(%rsp,%rbx)
incq %rbx
movq %rbp, %rdi
callq 0x1040
cmpq %rax, %rbx
jb 0x1655
movq %r14, %rdi
leaq 0x840(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x840(%rsp)
je 0x16be
xorl %ebx, %ebx
movsbl 0x840(%rsp,%rbx), %edi
callq 0x1120
movb %al, 0x840(%rsp,%rbx)
incq %rbx
movq %r14, %rdi
callq 0x1040
cmpq %rax, %rbx
jb 0x169a
movq 0x18(%rsp), %rbp
testq %rbp, %rbp
jle 0x16ee
movq %r12, %rbx
xorl %r14d, %r14d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10a0
testl %eax, %eax
je 0x16f1
incq %r14
addq $0x32, %rbx
cmpq %r14, %rbp
jne 0x16ce
movq %rbp, %r14
jmp 0x16f1
xorl %r14d, %r14d
testq %rbp, %rbp
jle 0x171c
movq %r12, %rbx
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r13, %rsi
callq 0x10a0
testl %eax, %eax
je 0x171f
incq %r15
addq $0x32, %rbx
cmpq %r15, %rbp
jne 0x16fc
movq %rbp, %r15
jmp 0x171f
xorl %r15d, %r15d
testq %rbp, %rbp
jle 0x174f
movq %r12, %rbx
xorl %r13d, %r13d
movq %rbx, %rdi
leaq 0x1010(%rsp), %rsi
callq 0x10a0
testl %eax, %eax
je 0x1752
incq %r13
addq $0x32, %rbx
cmpq %r13, %rbp
jne 0x172a
movq %rbp, %r13
jmp 0x1752
xorl %r13d, %r13d
movb $0x0, 0x17e0(%rsp)
incl 0x28(%rsp)
cmpq %rbp, %r14
sete %al
cmpq %rbp, %r15
sete %cl
orb %al, %cl
cmpq %rbp, %r13
sete %al
orb %cl, %al
jne 0x1554
testq %rbp, %rbp
jle 0x17aa
movq %r12, %rbx
xorl %r12d, %r12d
movq %rbx, %rdi
leaq 0x840(%rsp), %rsi
callq 0x10a0
testl %eax, %eax
je 0x17ad
incq %r12
addq $0x32, %rbx
cmpq %r12, %rbp
jne 0x1785
movq %rbp, %r12
jmp 0x17ad
xorl %r12d, %r12d
cmpq %rbp, %r12
movq 0x10(%rsp), %r12
je 0x1554
jmp 0x186d
cmpl $0x0, (%rsp)
je 0x1820
movl 0xc(%rsp), %edx
cmpl $0x1, %edx
adcl $0x0, %edx
movl 0x8(%rsp), %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
divss %xmm1, %xmm0
mulss 0x816(%rip), %xmm0      # 0x2004
cvtss2sd %xmm0, %xmm0
leaq 0x844(%rip), %rdi       # 0x203d
movb $0x1, %al
callq 0x1030
leaq 0x859(%rip), %rdi       # 0x2060
movsd 0x58(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
movb $0x3, %al
callq 0x1030
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1070
movq 0x27ac(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10d0
testl %eax, %eax
jne 0x19ec
incl (%rsp)
movl $0x0, 0xc(%rsp)
leaq 0x85f(%rip), %rdi       # 0x20b5
movq %r15, %rsi
xorl %eax, %eax
callq 0x1030
movl $0x0, 0x8(%rsp)
jmp 0x1554
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
jle 0x18c1
leaq (,%rdi,4), %rax
movq %rax, %rcx
imulq %r13, %rcx
movq 0x20(%rsp), %rsi
addq %rsi, %rcx
movq %rax, %rdx
imulq %r14, %rdx
addq %rsi, %rdx
imulq %r15, %rax
addq %rsi, %rax
xorl %esi, %esi
movss (%rax,%rsi,4), %xmm0
subss (%rdx,%rsi,4), %xmm0
addss (%rcx,%rsi,4), %xmm0
movss %xmm0, 0x2f50(%rsp,%rsi,4)
incq %rsi
cmpq %rsi, %rdi
jne 0x18a1
testq %rbp, %rbp
jle 0x1956
leaq (,%rdi,4), %rcx
xorps %xmm0, %xmm0
movq 0x20(%rsp), %rbx
xorl %r12d, %r12d
movq %rdi, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
cmpq %r13, %r12
je 0x194b
cmpq %r14, %r12
je 0x194b
cmpq %r15, %r12
je 0x194b
xorps %xmm2, %xmm2
testq %rdi, %rdi
jle 0x191a
xorl %eax, %eax
movss 0x2f50(%rsp,%rax,4), %xmm1
mulss (%rbx,%rax,4), %xmm1
addss %xmm1, %xmm2
incq %rax
cmpq %rax, %rdi
jne 0x1900
ucomiss %xmm0, %xmm2
jbe 0x194b
imulq $0x32, %r12, %rsi
addq 0x10(%rsp), %rsi
leaq 0x17e0(%rsp), %rdi
movss %xmm2, 0x58(%rsp)
callq 0x1050
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %rdi
movss 0x58(%rsp), %xmm0
incq %r12
addq %rcx, %rbx
cmpq %rbp, %r12
jne 0x18e7
leaq 0x840(%rsp), %rdi
leaq 0x17e0(%rsp), %rsi
callq 0x10a0
testl %eax, %eax
je 0x1990
movq 0x10(%rsp), %r12
movss 0x688(%rip), %xmm3      # 0x2004
movl 0x4(%rsp), %edx
movl 0x3c(%rsp), %esi
movl 0x38(%rsp), %r8d
movl 0x34(%rsp), %r9d
jmp 0x19c3
incl 0x8(%rsp)
movl 0x3c(%rsp), %esi
incl %esi
cmpl $0x5, (%rsp)
movq 0x10(%rsp), %r12
movss 0x659(%rip), %xmm3      # 0x2004
movl 0x4(%rsp), %edx
movl 0x38(%rsp), %r8d
movl 0x34(%rsp), %r9d
jg 0x19c0
incl %r8d
jmp 0x19c3
incl %r9d
incl %edx
xorl %eax, %eax
xorl %ecx, %ecx
cmpl $0x6, (%rsp)
setge %al
setl %cl
movl 0x68(%rsp), %edi
addl %ecx, %edi
movl %edi, %ecx
movl 0x64(%rsp), %edi
addl %eax, %edi
movl %edi, %eax
incl 0xc(%rsp)
jmp 0x14d2
movl 0x28(%rsp), %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movss 0x6c(%rsp), %xmm1
divss %xmm0, %xmm1
mulss 0x5fb(%rip), %xmm1      # 0x2004
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
leaq 0x6a3(%rip), %rdi       # 0x20ba
movl 0x4(%rsp), %esi
movb $0x1, %al
callq 0x1030
xorl %eax, %eax
jmp 0x1a58
leaq 0x6b7(%rip), %rdi       # 0x20e4
callq 0x10c0
jmp 0x1a53
shrq $0x12, %r15
movabsq $0xfffffffffff, %rsi    # imm = 0xFFFFFFFFFFF
andq %r15, %rsi
leaq 0x5c8(%rip), %rdi       # 0x2014
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x4e98, %rsp           # imm = 0x4E98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq