// File: /workspace/github_c_makefile_star2_O2/Leeheng86[P]word2vec/word2vec.c
// Function: RandomWalk()
void RandomWalk() {
  printf("Starting random walk using edge file %s\n", train_file);
  srand((unsigned)time(NULL));
  //std::unordered_map<std::string, std::vector<std::string> > umap;
  FILE *fin;
  fin = fopen(train_file, "rb");
  if (fin == NULL) {
    printf("ERROR: training data file not found!\n");
    exit(1);
  }
  // Create Graph
  // Single thread since std::unordered_map is not thread safe on writing
  while (1) {
    if (feof(fin)) break;
    char ca[MAX_STRING], cb[MAX_STRING];
    fscanf(fin, "%s %s\n", ca, cb);
    std::string a(ca);
    std::string b(cb);

    // remember the original position
    if (discard == 1) {
      left_node.insert(a);
    }
    if (discard == 2) {
      right_node.insert(b);
    }

    addLink(umap, a, b);
    if (undirected) addLink(umap, b, a);
  }
  fclose(fin);
  if (discard == 1) {
    printf("# of left node: %lu\n", left_node.size());
  }
  if (discard == 2) {
    printf("# of right node: %lu\n", right_node.size());
  }

  // Random Walk
  std::string new_train_file = std::string(train_file).append("_walks");

  pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
  for (int a = 0; a < num_threads; a++) pthread_create(&pt[a], NULL, RandomWalkThread, (void *)(long long)a);
  for (int a = 0; a < num_threads; a++) pthread_join(pt[a], NULL);

  printf("Loaded and preprocessed threads produced edge file, save all walks to file %s\n", new_train_file.c_str());
  std::ofstream of_walks(new_train_file, std::ios_base::binary);
  for (int a = 0; a < num_threads; a++) {
    std::ifstream if_a(std::string(train_file).append("_walks").append(std::to_string((long long)a)), std::ios_base::binary);
    of_walks << if_a.rdbuf();
  }

  printf("\n");
  strcpy(train_file, new_train_file.c_str());
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
leaq 0x298a(%rip), %rdi      # 0x718f
leaq 0x6ab4(%rip), %rbx      # 0xb2c0
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21e0
movl %eax, %edi
callq 0x2200
leaq 0x284a(%rip), %rsi      # 0x7075
movq %rbx, %rdi
callq 0x2280
testq %rax, %rax
je 0x4b9a
movq %rax, %rbx
leaq 0x300(%rsp), %r14
leaq 0x10(%rsp), %r13
leaq 0xd0(%rsp), %r12
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x2370
movq %rbx, %rdi
testl %eax, %eax
jne 0x498f
movq %r12, %r15
leaq 0x2942(%rip), %rsi      # 0x71b8
leaq 0xf0(%rsp), %r12
movq %r12, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2440
movq %r13, %rdi
leaq 0x30(%rsp), %rbp
movq %r12, %rsi
movq %rbp, %rdx
callq 0x5448
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
callq 0x5448
movl 0x6c48(%rip), %eax      # 0xb4fc
cmpl $0x1, %eax
movq %r15, %r12
leaq 0xb0(%rsp), %r15
jne 0x48d9
leaq 0x6c6d(%rip), %rdi      # 0xb538
movq %r13, %rsi
callq 0x6130
movl 0x6c23(%rip), %eax      # 0xb4fc
cmpl $0x2, %eax
jne 0x48ed
leaq 0x6c83(%rip), %rdi      # 0xb568
movq %rbp, %rsi
callq 0x6130
movq %r12, %rdi
movq %r13, %rsi
callq 0x2300
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2300
leaq 0x6bf6(%rip), %rdi      # 0xb500
movq %r12, %rsi
movq %r15, %rdx
callq 0x4380
movq %r15, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x2110
cmpl $0x0, 0x6970(%rip)      # 0xb29c
je 0x4975
leaq 0x70(%rsp), %rdi
movq %rbp, %rsi
callq 0x2300
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2300
leaq 0x6bab(%rip), %rdi      # 0xb500
leaq 0x70(%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4380
movq %rbx, %rdi
callq 0x2110
movq %r15, %rdi
callq 0x2110
movq %rbp, %rdi
callq 0x2110
movq %r13, %rdi
callq 0x2110
movq 0x8(%rsp), %rbx
jmp 0x4859
callq 0x2210
movl 0x6b62(%rip), %eax      # 0xb4fc
cmpl $0x1, %eax
jne 0x49ba
movq 0x6bba(%rip), %rsi      # 0xb560
leaq 0x2812(%rip), %rdi      # 0x71bf
xorl %eax, %eax
callq 0x2050
movl 0x6b42(%rip), %eax      # 0xb4fc
cmpl $0x2, %eax
jne 0x49d4
movq 0x6bca(%rip), %rsi      # 0xb590
leaq 0x2807(%rip), %rdi      # 0x71d4
xorl %eax, %eax
callq 0x2050
leaq 0x68e5(%rip), %rsi      # 0xb2c0
leaq 0xf0(%rsp), %rdi
leaq 0x300(%rsp), %rdx
callq 0x5448
leaq 0x2757(%rip), %rsi      # 0x714e
leaq 0xf0(%rsp), %rdi
callq 0x2460
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x2300
leaq 0xf0(%rsp), %rdi
callq 0x2110
movslq 0x684b(%rip), %r13      # 0xb270
movq %r13, %rdi
shlq $0x3, %rdi
callq 0x22f0
movq %rax, %rbx
xorl %r14d, %r14d
leaq -0x534(%rip), %r15      # 0x450a
movq %rax, %r12
movslq %r13d, %rax
cmpq %rax, %r14
jge 0x4a69
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
callq 0x2430
incq %r14
movl 0x680d(%rip), %r13d     # 0xb270
addq $0x8, %r12
jmp 0x4a41
xorl %r14d, %r14d
movslq %r13d, %rax
cmpq %rax, %r14
jge 0x4a8b
movq (%rbx,%r14,8), %rdi
xorl %esi, %esi
callq 0x21d0
incq %r14
movl 0x67e7(%rip), %r13d     # 0xb270
jmp 0x4a6c
leaq 0x10(%rsp), %rbx
movq (%rbx), %rsi
leaq 0x2750(%rip), %rdi      # 0x71ea
xorl %eax, %eax
callq 0x2050
leaq 0x300(%rsp), %rdi
pushq $0x4
popq %rdx
movq %rbx, %rsi
callq 0x2470
xorl %r14d, %r14d
leaq 0x30(%rsp), %r12
leaq 0x50(%rsp), %rbx
leaq 0xf0(%rsp), %r15
leaq 0x300(%rsp), %rbp
movslq 0x6798(%rip), %rax      # 0xb270
cmpq %rax, %r14
jge 0x4b57
movq %r12, %rdi
leaq 0x67d9(%rip), %rsi      # 0xb2c0
leaq 0x7(%rsp), %rdx
callq 0x5448
movq %r12, %rdi
leaq 0x2653(%rip), %rsi      # 0x714e
callq 0x2460
movq %rax, %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5483
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2490
movq %r15, %rdi
movq %rax, %rsi
pushq $0x4
popq %rdx
callq 0x23a0
movq %rbx, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x2110
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
callq 0x2090
movq %r15, %rdi
callq 0x20a0
incq %r14
jmp 0x4ad1
pushq $0xa
popq %rdi
callq 0x2310
leaq 0x10(%rsp), %rbx
movq (%rbx), %rsi
leaq 0x6752(%rip), %rdi      # 0xb2c0
callq 0x21f0
leaq 0x300(%rsp), %rdi
callq 0x2350
movq %rbx, %rdi
callq 0x2110
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27d3(%rip), %rdi      # 0x7374
callq 0x2360
pushq $0x1
popq %rdi
callq 0x22a0
jmp 0x4c15
jmp 0x4bfe
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x4c64
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x2110
jmp 0x4bd7
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x4c25
movq %rax, %rbx
jmp 0x4c43
jmp 0x4bf7
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x20a0
jmp 0x4c43
movq %rax, %rbx
jmp 0x4c39
jmp 0x4c15
movq %rax, %rbx
jmp 0x4c69
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2110
jmp 0x4c1d
movq %rax, %rbx
jmp 0x4c5f
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x2110
jmp 0x4c55
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2110
leaq 0x30(%rsp), %rdi
callq 0x2110
leaq 0x300(%rsp), %rdi
callq 0x2350
jmp 0x4c5f
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2110
leaq 0x10(%rsp), %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x23f0