// File: /workspace/github_cpp_makefile_star2_O1/ivanoleinik[P]archiver/src/huffman.cpp
// Function: HuffTree::HuffTree(std::vector<unsigned long, std::allocator<unsigned long>>&)
HuffTree::HuffTree(std::vector<std::size_t> &frequencies) : _frequencies(frequencies) {
    for (size_t c = 0; c < 256; c++)
        _tree.push(std::make_shared<TreeNode>(c, _frequencies[static_cast<unsigned char>(c)]));

    std::shared_ptr<TreeNode> left, right, top;

    while (_tree.size() > 1) {
        left = _tree.top();
        _tree.pop();
        right = _tree.top();
        _tree.pop();

        top = std::make_shared<TreeNode>(left->freq + right->freq);
        top->left = left;
        top->right = right;
        _tree.push(top);
    }

    _codes.assign(256, {});
    _fill_codes(_tree.top(), {});
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
callq 0x3766
leaq 0x18(%rbx), %r13
xorl %ebp, %ebp
movq %rbp, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbx)
leaq 0x38(%rbx), %rax
movq %rax, 0xa0(%rsp)
movups %xmm0, 0x38(%rbx)
movq %rbp, 0x48(%rbx)
leaq 0x20(%rsp), %r14
movq %r13, 0x8(%rsp)
movq (%rbx), %r12
movq $0x0, 0x20(%rsp)
movl $0x40, %edi
callq 0x21b0
movq %rax, %r13
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r13)
leaq 0x631c(%rip), %rax      # 0x8c78
movq %rax, (%r13)
movq %r13, %r15
addq $0x10, %r15
movq (%r12,%rbp,8), %rdx
movzbl %bpl, %esi
movq %r15, %rdi
callq 0x24fc
movq %r13, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x37e6
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x29a0
callq 0x3bc4
incq %rbp
cmpq $0x100, %rbp            # imm = 0x100
jne 0x292e
xorps %xmm0, %xmm0
leaq 0x98(%rsp), %r15
movaps %xmm0, -0x8(%r15)
leaq 0x88(%rsp), %r12
movaps %xmm0, -0x8(%r12)
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rax
subq %rsi, %rax
cmpq $0x11, %rax
jb 0x2af8
movq (%rsi), %rax
movq %rax, 0x90(%rsp)
addq $0x8, %rsi
movq %r15, %rdi
callq 0x3b72
movq %r13, %rdi
callq 0x3852
movq (%r13), %rsi
movq (%rsi), %rax
movq %rax, 0x80(%rsp)
addq $0x8, %rsi
movq %r12, %rdi
callq 0x3b72
movq %r13, %rdi
callq 0x3852
movq %rbx, %r14
movq %r12, %rbx
movq %r15, %r13
movq 0x90(%rsp), %rax
movq 0x8(%rax), %r12
movq 0x80(%rsp), %rax
movq 0x8(%rax), %r15
movl $0x40, %edi
callq 0x21b0
movq %rax, %rbp
addq %r12, %r15
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%rbp)
leaq 0x6209(%rip), %rax      # 0x8c78
movq %rax, (%rbp)
movq %rbp, %r12
addq $0x10, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x254e
movq %r12, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movq %rbp, 0x18(%rsp)
testq %rdi, %rdi
je 0x2a9e
callq 0x3bc4
movq 0x10(%rsp), %rdi
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rdi)
addq $0x18, %rdi
movq %r13, %r15
movq %r13, %rsi
callq 0x3b72
movq 0x10(%rsp), %rdi
movq 0x80(%rsp), %rax
movq %rax, 0x20(%rdi)
addq $0x28, %rdi
movq %rbx, %r12
movq %rbx, %rsi
callq 0x3b72
movq 0x8(%rsp), %r13
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rbx
callq 0x3912
jmp 0x29d2
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
movq $0x0, 0x20(%rdx)
movl $0x100, %esi            # imm = 0x100
movq 0xa0(%rsp), %rdi
callq 0x480a
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2b51
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq (%r13), %rax
movq (%rax), %rcx
movq %rcx, 0xa8(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0xb0(%rsp)
testq %rax, %rax
je 0x2b86
movq 0x6458(%rip), %rcx      # 0x8fd0
cmpb $0x0, (%rcx)
je 0x2b82
incl 0x8(%rax)
jmp 0x2b86
lock
incl 0x8(%rax)
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
movq $0x0, 0x20(%rdx)
leaq 0xa8(%rsp), %rsi
movq %rbx, %rdi
callq 0x25ba
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2bdd
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movl %eax, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movl %eax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x2bef
callq 0x3bc4
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2bfe
callq 0x3bc4
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x2c10
callq 0x3bc4
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x2c22
callq 0x3bc4
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %r14
movq %rax, %r12
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2c6a
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movl %eax, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movl %eax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x2cd3
callq 0x3bc4
jmp 0x2cd3
movq %rbx, %r14
movq %rax, %r12
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2cd3
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
xorl %eax, %eax
movq %rax, 0x20(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
jmp 0x2cd3
jmp 0x2cd0
movq %rbx, %r14
movq %rax, %r12
jmp 0x2d06
movq %rbx, %r14
movq %rax, %r12
movq 0x28(%rsp), %rdi
jmp 0x2cfc
movq %rbx, %r14
movq %rax, %r12
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2ce2
callq 0x3bc4
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x2cf4
callq 0x3bc4
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x2d06
callq 0x3bc4
movq 0x8(%rsp), %rbx
movq 0xa0(%rsp), %rdi
callq 0x397e
movq %rbx, %rdi
callq 0x403c
movq (%r14), %rdi
testq %rdi, %rdi
je 0x2d34
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %r12, %rdi
callq 0x22d0