// File: /workspace/github_c_makefile_star2_O3/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 0x865f(%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 0x2e8e
movq 0x85ff(%rip), %rax      # 0xb478
xorl %ecx, %ecx
movq (%rax), %rdx
movq %rdx, (%rbx,%rcx,8)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %r12
jne 0x2e7b
cmpq %r13, %r12
jge 0x2f38
movabsq $0x38d7ea4c68000, %rax  # imm = 0x38D7EA4C68000
leaq 0x1(%r12), %rcx
andq $-0x2, %rcx
leaq -0x1(%r12), %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%rbx,%r12,8), %rdx
addq $0x8, %rdx
xorl %esi, %esi
movdqa 0x4165(%rip), %xmm1     # 0x7030
movdqa 0x416d(%rip), %xmm2     # 0x7040
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movq %rsi, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm1, %xmm4
pxor %xmm2, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm0, %xmm5
pcmpeqd %xmm0, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %edi
notl %edi
testb $0x1, %dil
je 0x2f1c
movq %rax, -0x8(%rdx,%rsi,8)
pxor %xmm3, %xmm4
pextrw $0x4, %xmm4, %edi
testb $0x1, %dil
je 0x2f2f
movq %rax, (%rdx,%rsi,8)
addq $0x2, %rsi
cmpq %rsi, %rcx
jne 0x2edb
cmpq $0x2, %r12
jl 0x2fe9
leaq -0x1(%r12), %rdi
leaq (%rbx,%r12,8), %rax
leaq -0x1(%r12), %rcx
xorl %edx, %edx
movq %r12, %rsi
testq %rdi, %rdi
js 0x2f7d
movq (%rbx,%rdi,8), %r11
movq (%rbx,%rsi,8), %r10
cmpq %r10, %r11
jge 0x2f83
testq %rdi, %rdi
je 0x2fa4
leaq -0x1(%rdi), %r9
movq -0x8(%rbx,%rdi,8), %r11
movq %rsi, %r8
movq %rdi, %rsi
jmp 0x2f8f
leaq 0x1(%rsi), %r10
jmp 0x2fb0
leaq 0x1(%rsi), %r8
movq 0x8(%rbx,%rsi,8), %r10
movq %rdi, %r9
cmpq %r10, %r11
jge 0x2f9c
movq %r9, %r10
decq %r9
jmp 0x2fb7
movq %r8, %r10
incq %r8
jmp 0x2fb7
movq $-0x1, %rdi
movq %rsi, %r10
xorl %esi, %esi
leaq 0x1(%r10), %r8
movq %rdi, %r9
movq (%rbx,%r10,8), %rdi
addq (%rbx,%rsi,8), %rdi
movq %rdi, (%rax,%rdx,8)
leaq (%r12,%rdx), %rdi
movq %rdi, (%r15,%rsi,8)
movq %rdi, (%r15,%r10,8)
movq $0x1, (%r14,%r10,8)
incq %rdx
movq %r8, %rsi
movq %r9, %rdi
cmpq %rdx, %rcx
jne 0x2f55
testq %r12, %r12
jle 0x309a
xorl %eax, %eax
leaq -0x2(,%r12,2), %rsi
movl $0x1, %r9d
xorl %edi, %edi
movq %rax, %r8
movq %rdi, %rcx
movq %r9, %rdx
movb (%r14,%r8,8), %dil
movb %dil, (%rsp,%rcx)
movq %r8, 0x30(%rsp,%rcx,8)
leaq 0x1(%rcx), %rdi
movq (%r15,%r8,8), %r8
incq %r9
cmpq %rsi, %r8
jne 0x3007
movq 0x8447(%rip), %rsi      # 0xb478
leaq (%rax,%rax,4), %r8
movb %dil, 0x20(%rsi,%r8,8)
addl $-0x2, %r12d
movq 0x8(%rsi,%r8,8), %rdi
movl %r12d, (%rdi)
leaq (,%rax,8), %rdi
leaq (%rdi,%rdi,4), %rdi
xorl %r8d, %r8d
movb (%rsp,%r8), %r9b
movq 0x18(%rsi,%rdi), %rsi
movb %r9b, -0x1(%rsi,%rdx)
movl 0x30(%rsp,%r8,8), %r9d
movq 0x8421(%rip), %r12      # 0xb490
subl %r12d, %r9d
movq 0x83ff(%rip), %rsi      # 0xb478
movq 0x8(%rsi,%rdi), %r10
movl %r9d, (%r10,%rdx,4)
incq %r8
decq %rdx
addq $-0x1, %rcx
jb 0x3055
incq %rax
cmpq %r12, %rax
jl 0x2ff4
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