// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq 0x8898(%rip), %r13      # 0xb490
leaq (%r13,%r13), %rbp
leaq 0x1(%r13,%r13), %r15
pushq $0x8
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2160
movq %rax, %rbx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2160
movq %rax, %r14
movq %r15, %rdi
movq %r12, %rsi
callq 0x2160
movq %rax, %r15
movq 0x8841(%rip), %rax      # 0xb478
xorl %ecx, %ecx
testq %r13, %r13
movl $0x0, %edx
cmovgq %r13, %rdx
movq %rax, %rsi
cmpq %rcx, %rdx
je 0x2c5d
movq (%rsi), %rdi
movq %rdi, (%rbx,%rcx,8)
incq %rcx
addq $0x28, %rsi
jmp 0x2c48
movabsq $0x38d7ea4c68000, %rcx  # imm = 0x38D7EA4C68000
movq %r13, %rdx
cmpq %rbp, %rdx
jge 0x2c78
movq %rcx, (%rbx,%rdx,8)
incq %rdx
jmp 0x2c6a
leaq -0x1(%r13), %r8
xorl %ecx, %ecx
testq %r8, %r8
cmovgq %r8, %rcx
pushq $-0x1
popq %rdx
movq %r13, %rsi
movq %r13, %rdi
subq $0x1, %rcx
jb 0x2d20
testq %r8, %r8
js 0x2cc0
movq (%rbx,%r8,8), %r12
movq (%rbx,%rdi,8), %r11
cmpq %r11, %r12
jge 0x2cc6
testq %r8, %r8
je 0x2ce7
leaq -0x1(%r8), %r10
movq -0x8(%rbx,%r8,8), %r12
movq %rdi, %r9
movq %r8, %rdi
jmp 0x2cd2
leaq 0x1(%rdi), %r11
jmp 0x2cef
leaq 0x1(%rdi), %r9
movq 0x8(%rbx,%rdi,8), %r11
movq %r8, %r10
cmpq %r11, %r12
jge 0x2cdf
movq %r10, %r11
decq %r10
jmp 0x2cf6
movq %r9, %r11
incq %r9
jmp 0x2cf6
movq %rdi, %r11
movq %rdx, %r8
xorl %edi, %edi
leaq 0x1(%r11), %r9
movq %r8, %r10
movq (%rbx,%r11,8), %r8
addq (%rbx,%rdi,8), %r8
movq %r8, (%rbx,%rsi,8)
movq %rsi, (%r15,%rdi,8)
movq %rsi, (%r15,%r11,8)
movq $0x1, (%r14,%r11,8)
incq %rsi
movq %r10, %r8
movq %r9, %rdi
jmp 0x2c8e
xorl %ecx, %ecx
pushq $0x1
popq %rdx
cmpq %r13, %rcx
jge 0x2dc0
leaq -0x2(,%r13,2), %r8
movq %rdx, %r11
xorl %r10d, %r10d
movq %rcx, %r9
movq %r10, %rsi
movq %r11, %rdi
movb (%r14,%r9,8), %r10b
movb %r10b, (%rsp,%rsi)
movq %r9, 0x30(%rsp,%rsi,8)
leaq 0x1(%rsi), %r10
movq (%r15,%r9,8), %r9
incq %r11
cmpq %r8, %r9
jne 0x2d3f
imulq $0x28, %rcx, %r8
movb %r10b, 0x20(%rax,%r8)
leal -0x2(%r13), %r9d
movq 0x8(%rax,%r8), %r10
movl %r9d, (%r10)
xorl %r9d, %r9d
cmpq $-0x1, %rsi
je 0x2db8
movb (%rsp,%r9), %r10b
movq 0x18(%rax,%r8), %rax
movb %r10b, -0x1(%rax,%rdi)
movl 0x30(%rsp,%r9,8), %r10d
movq 0x86f6(%rip), %r13      # 0xb490
subl %r13d, %r10d
movq 0x86d4(%rip), %rax      # 0xb478
movq 0x8(%rax,%r8), %r11
movl %r10d, (%r11,%rdi,4)
incq %r9
decq %rdi
decq %rsi
jmp 0x2d7a
incq %rcx
jmp 0x2d25
movq %rbx, %rdi
callq 0x2290
movq %r14, %rdi
callq 0x2290
movq %r15, %rdi
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2290