// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x100(%rbp)
movq %rdi, %rax
movq %rax, -0x108(%rbp)
movq -0x10(%rbp), %rsi
callq 0x4f90
movq -0x100(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0xf8(%rbp)
callq 0x5080
jmp 0x3a7a
movq -0x108(%rbp), %rdi
addq $0x38, %rdi
movq %rdi, -0x110(%rbp)
callq 0x50b0
movq $0x0, -0x28(%rbp)
cmpq $0x100, -0x28(%rbp)     # imm = 0x100
jae 0x3b3e
movq -0x108(%rbp), %rdi
movq %rdi, %rax
addq $0x18, %rax
movq %rax, -0x118(%rbp)
movzbl -0x28(%rbp), %eax
movl %eax, %esi
callq 0x51a0
movq %rax, %rdx
leaq -0x38(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x5120
jmp 0x3ad9
movq -0x118(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x50d0
jmp 0x3aeb
leaq -0x38(%rbp), %rdi
callq 0x4bd0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x3a99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x3e5a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x3e42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4bd0
jmp 0x3e42
leaq -0x48(%rbp), %rdi
callq 0x4b30
leaq -0x58(%rbp), %rdi
callq 0x4b30
leaq -0x68(%rbp), %rdi
callq 0x4b30
movq -0x108(%rbp), %rdi
addq $0x18, %rdi
callq 0x51c0
movq %rax, -0x120(%rbp)
jmp 0x3b72
movq -0x120(%rbp), %rax
cmpq $0x1, %rax
jbe 0x3cc8
movq -0x108(%rbp), %rdi
addq $0x18, %rdi
callq 0x51e0
movq %rax, -0x128(%rbp)
jmp 0x3b9c
movq -0x128(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x4ba0
movq -0x108(%rbp), %rdi
addq $0x18, %rdi
callq 0x5200
jmp 0x3bbe
movq -0x108(%rbp), %rdi
addq $0x18, %rdi
callq 0x51e0
movq %rax, -0x130(%rbp)
jmp 0x3bd7
movq -0x130(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x4ba0
movq -0x108(%rbp), %rdi
addq $0x18, %rdi
callq 0x5200
jmp 0x3bf9
leaq -0x48(%rbp), %rdi
callq 0x4c10
movq 0x8(%rax), %rax
movq %rax, -0x138(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x4c10
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq 0x8(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x5250
jmp 0x3c3a
leaq -0x68(%rbp), %rdi
movq %rdi, -0x140(%rbp)
leaq -0x78(%rbp), %rsi
movq %rsi, -0x148(%rbp)
callq 0x4b70
movq -0x148(%rbp), %rdi
callq 0x4bd0
movq -0x140(%rbp), %rdi
callq 0x4c10
movq %rax, %rdi
addq $0x10, %rdi
leaq -0x48(%rbp), %rsi
callq 0x4ba0
movq -0x140(%rbp), %rdi
callq 0x4c10
movq %rax, %rdi
addq $0x20, %rdi
leaq -0x58(%rbp), %rsi
callq 0x4ba0
movq -0x108(%rbp), %rdi
movq -0x140(%rbp), %rsi
addq $0x18, %rdi
callq 0x52c0
jmp 0x3cb2
jmp 0x3b59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x3e27
movq -0x108(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x158(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movq $0x0, -0x90(%rbp)
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x5340
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x5310
jmp 0x3d23
leaq -0xb0(%rbp), %rdi
callq 0x4f70
movq -0x108(%rbp), %rdi
addq $0x18, %rdi
callq 0x51e0
movq %rax, -0x160(%rbp)
jmp 0x3d48
movq -0x160(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x4db0
xorps %xmm0, %xmm0
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movq $0x0, -0xd0(%rbp)
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x5340
movq -0x108(%rbp), %rdi
movq -0x170(%rbp), %rsi
movq -0x168(%rbp), %rdx
callq 0x3750
jmp 0x3dad
leaq -0xf0(%rbp), %rdi
callq 0x4f70
leaq -0xc0(%rbp), %rdi
callq 0x4bd0
leaq -0x68(%rbp), %rdi
callq 0x4bd0
leaq -0x58(%rbp), %rdi
callq 0x4bd0
leaq -0x48(%rbp), %rdi
callq 0x4bd0
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x4f70
jmp 0x3e27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x4f70
leaq -0xc0(%rbp), %rdi
callq 0x4bd0
leaq -0x68(%rbp), %rdi
callq 0x4bd0
leaq -0x58(%rbp), %rdi
callq 0x4bd0
leaq -0x48(%rbp), %rdi
callq 0x4bd0
movq -0x110(%rbp), %rdi
callq 0x5360
movq -0xf8(%rbp), %rdi
callq 0x53c0
movq -0x100(%rbp), %rdi
callq 0x53e0
movq -0x18(%rbp), %rdi
callq 0x33d0
nop