// File: /workspace/github_c_makefile_star2_O1/Leeheng86[P]word2vec/word2vec.c
// Function: CreateBinaryTree()
void CreateBinaryTree() {
  long long a, b, i, min1i, min2i, pos1, pos2, point[MAX_CODE_LENGTH];
  char code[MAX_CODE_LENGTH];
  long long *count = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));
  long long *binary = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));
  long long *parent_node = (long long *)calloc(vocab_size * 2 + 1, sizeof(long long));
  for (a = 0; a < vocab_size; a++) count[a] = vocab[a].cn;
  for (a = vocab_size; a < vocab_size * 2; a++) count[a] = 1e15;
  pos1 = vocab_size - 1;
  pos2 = vocab_size;
  // Following algorithm constructs the Huffman tree by adding one node at a time
  for (a = 0; a < vocab_size - 1; a++) {
    // First, find two smallest nodes 'min1, min2'
    if (pos1 >= 0) {
      if (count[pos1] < count[pos2]) {
        min1i = pos1;
        pos1--;
      } else {
        min1i = pos2;
        pos2++;
      }
    } else {
      min1i = pos2;
      pos2++;
    }
    if (pos1 >= 0) {
      if (count[pos1] < count[pos2]) {
        min2i = pos1;
        pos1--;
      } else {
        min2i = pos2;
        pos2++;
      }
    } else {
      min2i = pos2;
      pos2++;
    }
    count[vocab_size + a] = count[min1i] + count[min2i];
    parent_node[min1i] = vocab_size + a;
    parent_node[min2i] = vocab_size + a;
    binary[min2i] = 1;
  }
  // Now assign binary code to each vocabulary word
  for (a = 0; a < vocab_size; a++) {
    b = a;
    i = 0;
    while (1) {
      code[i] = binary[b];
      point[i] = b;
      i++;
      b = parent_node[b];
      if (b == vocab_size * 2 - 2) break;
    }
    vocab[a].codelen = i;
    vocab[a].point[0] = vocab_size - 2;
    for (b = 0; b < i; b++) {
      vocab[a].code[i - b - 1] = code[b];
      vocab[a].point[i - b] = point[b] - vocab_size;
    }
  }
  free(count);
  free(binary);
  free(parent_node);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x170, %rsp            # imm = 0x170
movq 0x8688(%rip), %r12      # 0xb490
leaq (%r12,%r12), %r13
leaq 0x1(,%r12,2), %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x2160
movq %rax, %rbx
movl $0x8, %esi
movq %r15, %rdi
callq 0x2160
movq %rax, %r14
movl $0x8, %esi
movq %r15, %rdi
callq 0x2160
movq %rax, %r15
testq %r12, %r12
jle 0x2e65
movq 0x8628(%rip), %rax      # 0xb478
xorl %ecx, %ecx
movq (%rax), %rdx
movq %rdx, (%rbx,%rcx,8)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %r12
jne 0x2e52
cmpq %r13, %r12
jge 0x2e86
leaq (%rbx,%r12,8), %rax
xorl %ecx, %ecx
movabsq $0x38d7ea4c68000, %rdx  # imm = 0x38D7EA4C68000
movq %rdx, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x2e7a
cmpq $0x2, %r12
jl 0x2f01
leaq -0x1(%r12), %rdx
leaq (%rbx,%r12,8), %rax
leaq -0x1(%r12), %rcx
xorl %esi, %esi
movq %r12, %rdi
testq %rdx, %rdx
js 0x2eb6
movq (%rbx,%rdx,8), %r8
cmpq (%rbx,%rdi,8), %r8
jge 0x2eb6
movq %rdx, %r8
decq %rdx
jmp 0x2ebc
movq %rdi, %r8
incq %rdi
testq %rdx, %rdx
js 0x2ed3
movq (%rbx,%rdx,8), %r9
cmpq (%rbx,%rdi,8), %r9
jge 0x2ed3
movq %rdx, %r9
decq %rdx
jmp 0x2ed9
movq %rdi, %r9
incq %rdi
movq (%rbx,%r9,8), %r10
addq (%rbx,%r8,8), %r10
movq %r10, (%rax,%rsi,8)
leaq (%r12,%rsi), %r10
movq %r10, (%r15,%r8,8)
movq %r10, (%r15,%r9,8)
movq $0x1, (%r14,%r9,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x2e9f
movq 0x8588(%rip), %rsi      # 0xb490
testq %rsi, %rsi
jle 0x2fc2
xorl %eax, %eax
leaq -0x2(,%rsi,2), %rdi
movl $0x1, %r10d
xorl %r8d, %r8d
movq %rax, %r9
movq %r8, %rcx
movq %r10, %rdx
movb (%r14,%r9,8), %r8b
movb %r8b, (%rsp,%rcx)
movq %r9, 0x30(%rsp,%rcx,8)
leaq 0x1(%rcx), %r8
movq (%r15,%r9,8), %r9
incq %r10
cmpq %rdi, %r9
jne 0x2f27
movq 0x8527(%rip), %rdi      # 0xb478
leaq (%rax,%rax,4), %r9
movb %r8b, 0x20(%rdi,%r9,8)
addl $-0x2, %esi
movq 0x8(%rdi,%r9,8), %rdi
movl %esi, (%rdi)
leaq (,%rax,8), %rsi
leaq (%rsi,%rsi,4), %rsi
xorl %edi, %edi
movb (%rsp,%rdi), %r8b
movq 0x84fb(%rip), %r9       # 0xb478
movq 0x18(%r9,%rsi), %r9
movb %r8b, -0x1(%r9,%rdx)
movl 0x30(%rsp,%rdi,8), %r8d
subl 0x84fd(%rip), %r8d      # 0xb490
movq 0x84de(%rip), %r9       # 0xb478
movq 0x8(%r9,%rsi), %r9
movl %r8d, (%r9,%rdx,4)
incq %rdi
decq %rdx
addq $-0x1, %rcx
jb 0x2f72
incq %rax
movq 0x84d7(%rip), %rsi      # 0xb490
cmpq %rsi, %rax
jl 0x2f13
movq %rbx, %rdi
callq 0x22b0
movq %r14, %rdi
callq 0x22b0
movq %r15, %rdi
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x22b0