// File: /workspace/github_c_makefile_star2_O3/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 0x1a43
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 0x1a47
movq %rax, %r14
leaq 0xd6a(%rip), %rsi       # 0x200f
leaq 0x20(%rsp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x1110
testq %rbx, %rbx
je 0x12c5
cmpq %rbx, 0x20(%rsp)
jle 0x12c5
movq %rbx, 0x20(%rsp)
leaq 0xd43(%rip), %rsi       # 0x200f
leaq 0x48(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1110
movq 0x20(%rsp), %r15
imulq $0x32, %r15, %rdi
callq 0x1090
movq %rax, 0x8(%rsp)
movq (%rbx), %rbx
imulq %r15, %rbx
leaq (,%rbx,4), %rdi
callq 0x1090
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x1a55
testq %r15, %r15
jle 0x1488
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x10f0
movl %eax, %ebp
movq %r12, 0x40(%rsp)
imulq $0x32, %r12, %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax), %r13
movb %bpl, (%rcx,%rax)
movq %r14, %rdi
callq 0x10d0
cmpb $0x20, %bpl
je 0x139e
movl $0x0, %r12d
testl %eax, %eax
jne 0x13a1
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 0x13a1
testl %eax, %eax
je 0x1361
jmp 0x13a1
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 0x13aa
movq 0x48(%rsp), %rdi
xorps %xmm0, %xmm0
testq %rdi, %rdi
jle 0x142b
movq 0x18(%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 0x48(%rsp), %rdi
addq $0x4, %r12
cmpq %rdi, %r13
jl 0x13d6
testq %rdi, %rdi
xorps %xmm0, %xmm0
jle 0x142b
movq %rbx, %rax
imulq %rdi, %rax
addq 0x18(%rsp), %rax
xorl %ecx, %ecx
movss (%rax,%rcx,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rdi
jne 0x1416
ucomiss 0xbd6(%rip), %xmm0      # 0x2008
movq 0x40(%rsp), %r12
jb 0x143f
sqrtss %xmm0, %xmm0
jmp 0x1444
callq 0x1100
movq 0x48(%rsp), %rax
testq %rax, %rax
jle 0x1472
movq %rax, %rcx
imulq %rbx, %rcx
addq 0x18(%rsp), %rcx
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x145c
incq %r12
addq $0x32, %r15
addq $0x4, %rbx
cmpq 0x20(%rsp), %r12
jl 0x1326
movq %r14, %rdi
callq 0x1060
movss 0xb6c(%rip), %xmm3      # 0x2004
leaq 0xb96(%rip), %r12       # 0x2035
movl $0x0, (%rsp)
movl $0x0, 0x14(%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, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movl %edx, 0x10(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
movss %xmm1, 0x50(%rsp)
divss %xmm1, %xmm0
mulss %xmm3, %xmm0
movl %r9d, 0x30(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movl %edi, 0x38(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %edi, %xmm0
divss %xmm0, %xmm1
movl %r10d, 0x2c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %r10d, %xmm0
mulss %xmm3, %xmm1
movl %r8d, 0x34(%rsp)
xorps %xmm2, %xmm2
cvtsi2ss %r8d, %xmm2
cvtss2sd %xmm1, %xmm1
movsd %xmm1, 0x60(%rsp)
divss %xmm2, %xmm0
mulss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movb $0x0, 0x1fb0(%rsp)
movq %r12, %rdi
leaq 0x70(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x70(%rsp)
je 0x1592
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 0x1574
cmpw $0x3a, 0x70(%rsp)
leaq 0x17e0(%rsp), %r14
je 0x17ce
movl 0x70(%rsp), %eax
movl $0x54495845, %ecx       # imm = 0x54495845
xorl %ecx, %eax
movzbl 0x74(%rsp), %ecx
orl %eax, %ecx
je 0x17ce
movq 0x2a1b(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10d0
testl %eax, %eax
jne 0x17ce
movl 0x70(%rsp), %eax
movl $0x54495845, %ecx       # imm = 0x54495845
xorl %ecx, %eax
movzbl 0x74(%rsp), %ecx
orl %eax, %ecx
je 0x1a0d
movq %r12, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x17e0(%rsp)
je 0x162a
xorl %ebx, %ebx
movsbl 0x17e0(%rsp,%rbx), %edi
callq 0x1120
movb %al, 0x17e0(%rsp,%rbx)
incq %rbx
movq %r14, %rdi
callq 0x1040
cmpq %rax, %rbx
jb 0x1606
movq %r12, %rdi
leaq 0x1010(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x1010(%rsp)
je 0x166f
xorl %ebx, %ebx
movsbl 0x1010(%rsp,%rbx), %edi
callq 0x1120
movb %al, 0x1010(%rsp,%rbx)
incq %rbx
movq %r14, %rdi
callq 0x1040
cmpq %rax, %rbx
jb 0x164b
movq %r12, %rdi
leaq 0x840(%rsp), %r14
movq %r14, %rsi
xorl %eax, %eax
callq 0x1070
cmpb $0x0, 0x840(%rsp)
je 0x16b4
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 0x1690
movq 0x20(%rsp), %r13
testq %r13, %r13
jle 0x1749
movq 0x8(%rsp), %rbx
xorl %r14d, %r14d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10a0
testl %eax, %eax
je 0x16e8
incq %r14
addq $0x32, %rbx
cmpq %r14, %r13
jne 0x16ca
movq %r13, %r14
testq %r13, %r13
jle 0x1750
movq 0x8(%rsp), %rbx
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x17e0(%rsp), %rsi
callq 0x10a0
testl %eax, %eax
je 0x1717
incq %rbp
addq $0x32, %rbx
cmpq %rbp, %r13
jne 0x16f4
movq %r13, %rbp
testq %r13, %r13
jle 0x1752
movq 0x8(%rsp), %rbx
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x1010(%rsp), %rsi
callq 0x10a0
testl %eax, %eax
je 0x1755
incq %r15
addq $0x32, %rbx
cmpq %r15, %r13
jne 0x1724
movq %r13, %r15
jmp 0x1755
xorl %ebp, %ebp
xorl %r14d, %r14d
jmp 0x1752
xorl %ebp, %ebp
xorl %r15d, %r15d
movb $0x0, 0x1fb0(%rsp)
incl 0x40(%rsp)
cmpq %r13, %r14
sete %al
cmpq %r13, %rbp
sete %cl
orb %al, %cl
cmpq %r13, %r15
sete %al
orb %cl, %al
jne 0x1551
testq %r13, %r13
jle 0x17b6
movq 0x8(%rsp), %rbx
xorl %r12d, %r12d
movq %rbx, %rdi
leaq 0x840(%rsp), %rsi
callq 0x10a0
testl %eax, %eax
je 0x17b9
incq %r12
addq $0x32, %rbx
cmpq %r12, %r13
jne 0x178a
leaq 0x884(%rip), %r12       # 0x2035
jmp 0x1551
xorl %r12d, %r12d
cmpq %r13, %r12
leaq 0x872(%rip), %r12       # 0x2035
je 0x1551
jmp 0x187b
cmpl $0x0, 0x4(%rsp)
je 0x182e
movl (%rsp), %edx
cmpl $0x1, %edx
adcl $0x0, %edx
movl 0x14(%rsp), %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
divss %xmm1, %xmm0
mulss 0x808(%rip), %xmm0      # 0x2004
cvtss2sd %xmm0, %xmm0
leaq 0x836(%rip), %rdi       # 0x203d
movb $0x1, %al
callq 0x1030
leaq 0x84b(%rip), %rdi       # 0x2060
movsd 0x68(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movb $0x3, %al
callq 0x1030
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1070
movq 0x279e(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x10d0
testl %eax, %eax
jne 0x1a0d
incl 0x4(%rsp)
movl $0x0, (%rsp)
leaq 0x851(%rip), %rdi       # 0x20b5
movq %r15, %rsi
xorl %eax, %eax
callq 0x1030
movl $0x0, 0x14(%rsp)
jmp 0x1551
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
jle 0x18cf
leaq (,%rdi,4), %rax
movq %rax, %rcx
imulq %r15, %rcx
movq 0x18(%rsp), %rsi
addq %rsi, %rcx
movq %rax, %rdx
imulq %r14, %rdx
addq %rsi, %rdx
imulq %rbp, %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 0x18af
testq %r13, %r13
jle 0x196d
leaq (,%rdi,4), %rcx
xorps %xmm0, %xmm0
movq 0x18(%rsp), %rbx
xorl %edx, %edx
movq %rdi, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
cmpq %r15, %rdx
je 0x1962
cmpq %r14, %rdx
je 0x1962
cmpq %rbp, %rdx
je 0x1962
xorps %xmm2, %xmm2
testq %rdi, %rdi
jle 0x1927
xorl %eax, %eax
movss 0x2f50(%rsp,%rax,4), %xmm1
mulss (%rbx,%rax,4), %xmm1
addss %xmm1, %xmm2
incq %rax
cmpq %rax, %rdi
jne 0x190d
ucomiss %xmm0, %xmm2
jbe 0x1962
imulq $0x32, %rdx, %rsi
addq 0x8(%rsp), %rsi
leaq 0x1fb0(%rsp), %rdi
movq %rdx, 0x68(%rsp)
movss %xmm2, 0x60(%rsp)
callq 0x1050
movq 0x68(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdi
movss 0x60(%rsp), %xmm0
incq %rdx
addq %rcx, %rbx
cmpq %r13, %rdx
jne 0x18f4
leaq 0x840(%rsp), %rdi
leaq 0x1fb0(%rsp), %rsi
callq 0x10a0
testl %eax, %eax
je 0x19af
movss 0x676(%rip), %xmm3      # 0x2004
movl 0x10(%rsp), %edx
movl 0x3c(%rsp), %esi
movl 0x38(%rsp), %edi
movl 0x34(%rsp), %r8d
movl 0x30(%rsp), %r9d
movl 0x2c(%rsp), %r10d
movl (%rsp), %r11d
jmp 0x19eb
incl 0x14(%rsp)
movl 0x3c(%rsp), %esi
incl %esi
cmpl $0x5, 0x4(%rsp)
movss 0x63e(%rip), %xmm3      # 0x2004
movl 0x10(%rsp), %edx
movl 0x38(%rsp), %edi
movl 0x34(%rsp), %r8d
movl 0x30(%rsp), %r9d
movl 0x2c(%rsp), %r10d
movl (%rsp), %r11d
jg 0x19e8
incl %r9d
jmp 0x19eb
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 0x14cd
movl 0x40(%rsp), %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movss 0x50(%rsp), %xmm1
divss %xmm0, %xmm1
mulss 0x5da(%rip), %xmm1      # 0x2004
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
leaq 0x682(%rip), %rdi       # 0x20ba
movl 0x10(%rsp), %esi
movb $0x1, %al
callq 0x1030
xorl %eax, %eax
jmp 0x1a79
leaq 0x696(%rip), %rdi       # 0x20e4
callq 0x10c0
jmp 0x1a74
shrq $0x12, %rbx
movabsq $0xfffffffffff, %rsi    # imm = 0xFFFFFFFFFFF
andq %rbx, %rsi
leaq 0x5a7(%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