// File: /workspace/github_cpp_makefile_star2_O2/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 0x303c
leaq 0x18(%rbx), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbx)
andq $0x0, 0x28(%rbx)
leaq 0x38(%rbx), %rbp
movups %xmm0, 0x38(%rbx)
andq $0x0, 0x48(%rbx)
leaq 0x68(%rsp), %r12
xorl %edx, %edx
leaq 0x60(%rsp), %r15
movq %rsp, %r13
movq %rdx, (%rsp)
cmpq $0x100, %rdx            # imm = 0x100
jae 0x2730
shlq $0x3, %rdx
addq (%rbx), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x30a0
movq %r14, %rdi
movq %r15, %rsi
callq 0x308a
movq %r12, %rdi
callq 0x3398
movq (%rsp), %rdx
incq %rdx
jmp 0x26f5
movq %rbp, 0x18(%rsp)
xorps %xmm0, %xmm0
movq %rsp, %r12
movaps %xmm0, (%r12)
leaq 0x30(%rsp), %r13
movaps %xmm0, (%r13)
leaq 0x20(%rsp), %rbp
movaps %xmm0, (%rbp)
leaq 0x60(%rsp), %r15
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rax
subq %rsi, %rax
cmpq $0x11, %rax
jb 0x27fa
movq %r12, %rdi
callq 0x32ba
movq %r14, %rdi
callq 0x30c2
movq (%r14), %rsi
movq %r13, %rdi
callq 0x32ba
movq %r14, %rdi
callq 0x30c2
movq (%rsp), %rax
movq 0x30(%rsp), %rcx
movq 0x8(%rcx), %rcx
addq 0x8(%rax), %rcx
movq %rcx, 0x48(%rsp)
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0x30db
movq %rbp, %rdi
movq %r15, %rsi
callq 0x395a
leaq 0x68(%rsp), %rdi
callq 0x3398
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
movq %r12, %rsi
callq 0x32ba
movq 0x20(%rsp), %rdi
addq $0x20, %rdi
movq %r13, %rsi
callq 0x32ba
movq %r14, %rdi
movq %rbp, %rsi
callq 0x30fa
jmp 0x2758
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
andq $0x0, 0x20(%rdx)
movl $0x100, %esi            # imm = 0x100
movq 0x18(%rsp), %rdi
callq 0x43b6
leaq 0x60(%rsp), %rdi
callq 0x302c
movq 0x18(%rbx), %rsi
leaq 0x50(%rsp), %r15
movq %r15, %rdi
callq 0x33a6
leaq 0x90(%rsp), %rdx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
andq $0x0, 0x20(%rdx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x24f0
leaq 0x90(%rsp), %rdi
callq 0x302c
leaq 0x58(%rsp), %rdi
callq 0x3398
leaq 0x28(%rsp), %rdi
callq 0x3398
leaq 0x38(%rsp), %rdi
callq 0x3398
leaq 0x8(%rsp), %rdi
callq 0x3398
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x302c
leaq 0x58(%rsp), %rdi
callq 0x3398
jmp 0x28e3
movq %rax, %r15
leaq 0x60(%rsp), %rdi
callq 0x302c
jmp 0x28e3
jmp 0x28e0
movq %rax, %r15
movq %r12, %rdi
callq 0x3398
jmp 0x2906
movq %rax, %r15
jmp 0x2906
movq %rax, %r15
leaq 0x28(%rsp), %rdi
callq 0x3398
leaq 0x38(%rsp), %rdi
callq 0x3398
leaq 0x8(%rsp), %rdi
callq 0x3398
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
callq 0x3110
movq %r14, %rdi
callq 0x38de
movq %rbx, %rdi
callq 0x39fc
movq %r15, %rdi
callq 0x22b0