// File: /workspace/github_cpp_makefile_star2_O3/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 0x3718
leaq 0x18(%rbx), %rbp
xorl %r13d, %r13d
movq %r13, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbx)
leaq 0x38(%rbx), %rax
movq %rax, 0x78(%rsp)
movups %xmm0, 0x38(%rbx)
movq %r13, 0x48(%rbx)
leaq 0x90(%rsp), %r14
movq %rbp, 0x8(%rsp)
movq (%rbx), %rbp
movq $0x0, 0x90(%rsp)
movl $0x40, %edi
callq 0x21b0
movq %rax, %r15
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r15)
leaq 0x639a(%rip), %rax      # 0x8cb0
movq %rax, (%r15)
leaq 0x10(%r15), %r12
movq (%rbp,%r13,8), %rax
movb %r13b, 0x10(%r15)
movq %rax, 0x18(%r15)
leaq 0x38(%r15), %rsi
movq %r15, %rdi
addq $0x28, %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
callq 0x3bda
movq %r15, 0x98(%rsp)
movq %r12, 0x90(%rsp)
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3798
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x2979
callq 0x3c2e
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x28e5
xorps %xmm0, %xmm0
leaq 0x68(%rsp), %r13
movaps %xmm0, -0x8(%r13)
leaq 0x58(%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 0x2acc
movq (%rsi), %rax
movq %rax, 0x60(%rsp)
addq $0x8, %rsi
movq %r13, %rdi
callq 0x3bda
movq %rbp, %rdi
callq 0x3804
movq (%rbp), %rsi
movq (%rsi), %rax
movq %rax, 0x50(%rsp)
addq $0x8, %rsi
movq %r12, %rdi
callq 0x3bda
movq %rbp, %rdi
callq 0x3804
movq %rbx, %r14
movq %r12, %rbx
movq 0x60(%rsp), %rax
movq 0x8(%rax), %r12
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rbp
movl $0x40, %edi
callq 0x21b0
movq %rax, %r15
addq %r12, %rbp
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x8(%r15)
leaq 0x6279(%rip), %rax      # 0x8cb0
movq %rax, (%r15)
movq %rbp, 0x18(%r15)
leaq 0x38(%r15), %rsi
leaq 0x28(%r15), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
callq 0x3bda
movq %r15, %rdi
addq $0x10, %rdi
movq %rdi, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq %r15, 0x18(%rsp)
testq %rax, %rax
je 0x2a80
movq %rax, %rdi
callq 0x3c2e
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rdi)
addq $0x18, %rdi
movq %r13, %rsi
callq 0x3bda
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rdi)
addq $0x28, %rdi
movq %rbx, %r12
movq %rbx, %rsi
callq 0x3bda
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rbx
callq 0x38be
jmp 0x29a9
leaq 0xb0(%rsp), %r13
xorps %xmm0, %xmm0
movaps %xmm0, -0x10(%r13)
movaps %xmm0, -0x20(%r13)
movq $0x0, (%r13)
leaq 0x90(%rsp), %rdx
movl $0x100, %esi            # imm = 0x100
movq 0x78(%rsp), %rdi
callq 0x485a
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2b1d
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq (%rbp), %rax
movq (%rax), %rcx
movq %rcx, 0x80(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x88(%rsp)
testq %rax, %rax
je 0x2b52
movq 0x648c(%rip), %rcx      # 0x8fd0
cmpb $0x0, (%rcx)
je 0x2b4e
incl 0x8(%rax)
jmp 0x2b52
lock
incl 0x8(%rax)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
movq $0x0, 0x20(%rdx)
leaq 0x80(%rsp), %rsi
movq %rbx, %rdi
callq 0x256c
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2ba9
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 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x2bbb
callq 0x3c2e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2bca
callq 0x3c2e
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x2bd9
callq 0x3c2e
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x2be8
callq 0x3c2e
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %r14
movq %rax, %r12
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2c30
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 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x2c81
callq 0x3c2e
jmp 0x2c81
movq %rbx, %r14
movq %rax, %r12
movq 0x90(%rsp), %rsi
testq %rsi, %rsi
je 0x2c81
movq %r13, %rdi
callq 0x237c
jmp 0x2c81
jmp 0x2c7e
movq %rbx, %r14
movq %rax, %r12
jmp 0x2cae
movq %rbx, %r14
movq %rax, %r12
movq 0x98(%rsp), %rdi
jmp 0x2ca4
movq %rbx, %r14
movq %rax, %r12
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x2c90
callq 0x3c2e
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x2c9f
callq 0x3c2e
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x2cae
callq 0x3c2e
movq 0x8(%rsp), %rbx
movq 0x78(%rsp), %rdi
callq 0x392a
movq %rbx, %rdi
callq 0x40a6
movq (%r14), %rdi
testq %rdi, %rdi
je 0x2cd9
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %r12, %rdi
callq 0x22d0
nop