// File: /workspace/github_cpp_makefile_star2_O2/ivanoleinik[P]archiver/src/huffman.cpp
// Function: HuffDecode::HuffDecode(std::istream&)
HuffDecode::HuffDecode(std::istream &in) {
    in.seekg(0, std::ios::beg);

    std::vector<std::size_t> frequencies;
    frequencies.assign(256, 0);
    for (size_t &freq : frequencies)
        in.read(reinterpret_cast<char*>(&freq), sizeof(size_t));
    _tree = std::make_shared<HuffTree>(frequencies);
    _codes = _tree->get_codes();

    in.seekg(0, std::ios::end);
    _encoded_data.reserve(in.tellg());
    in.seekg(256 * sizeof(size_t), std::ios::beg);
    _encoded_data.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());

    std::shared_ptr<TreeNode> root = _tree->get_root();
    std::shared_ptr<TreeNode> curr_node = root;

    size_t freq_sum = 0;
    for (size_t freq : _tree->get_frequencies())
        freq_sum += freq;
    for (unsigned char c : _encoded_data) {
        if (!freq_sum)
            break;
        for (size_t bit = 0; bit < 8 && freq_sum; bit++) {
            if (c & (1 << bit))
                curr_node = curr_node->left;
            else
                curr_node = curr_node->right;

            if (curr_node->is_leaf()) {
                _decoded_data += curr_node->data;
                freq_sum--;
                curr_node = root;
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r12
leaq 0x10(%rdi), %rax
movq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
xorl %eax, %eax
movb %al, 0x10(%rdi)
leaq 0x20(%rdi), %r14
leaq 0x30(%rdi), %rcx
movq %rcx, 0x20(%rdi)
andq $0x0, 0x28(%rdi)
movb %al, 0x30(%rdi)
leaq 0x50(%rdi), %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x50(%rdi)
movq %rdi, (%rsp)
andq $0x0, 0x60(%rdi)
movq %rsi, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21e0
leaq 0x30(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x8(%rsp), %rdx
andq $0x0, (%rdx)
movl $0x100, %esi            # imm = 0x100
callq 0x4cb6
movq (%rsp), %rax
leaq 0x40(%rax), %r13
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rbx
pushq $0x8
popq %r15
cmpq %rbx, %rbp
je 0x2cd1
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x21c0
addq $0x8, %rbp
jmp 0x2cb8
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x31be
leaq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x4a8e
leaq 0x10(%rsp), %rdi
callq 0x3398
movq (%r13), %rsi
addq $0x38, %rsi
movq 0x18(%rsp), %rdi
callq 0x31dc
pushq $0x2
popq %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x21e0
movq %r12, %rdi
callq 0x22a0
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x2250
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdi
xorl %edx, %edx
callq 0x21e0
movq (%r12), %rax
movq -0x18(%rax), %rax
movq 0xe8(%r12,%rax), %rsi
pushq $-0x1
popq %rdx
movq (%rsp), %rdi
xorl %ecx, %ecx
movl %edx, %r8d
callq 0x3132
movq (%r13), %rsi
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x2930
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x33a6
movq (%r13), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
xorl %r15d, %r15d
cmpq %rcx, %rax
je 0x2d94
addq (%rax), %r15
addq $0x8, %rax
jmp 0x2d86
movq (%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
addq %rcx, %rax
movq %rax, 0x48(%rsp)
leaq 0x20(%rsp), %r12
leaq 0x8(%rsp), %r13
cmpq 0x48(%rsp), %rcx
je 0x2e29
testq %r15, %r15
je 0x2e29
movq %rcx, 0x50(%rsp)
movzbl (%rcx), %ebx
xorl %ebp, %ebp
cmpq $0x7, %rbp
ja 0x2e1f
testq %r15, %r15
je 0x2e1f
xorl %eax, %eax
btl %ebp, %ebx
setae %al
movq 0x20(%rsp), %rcx
shll $0x4, %eax
leaq (%rax,%rcx), %rsi
addq $0x10, %rsi
movq %r12, %rdi
callq 0x32ba
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
orq 0x10(%rax), %rcx
jne 0x2e1a
movsbl (%rax), %esi
movq %r14, %rdi
callq 0x2140
decq %r15
movq %r12, %rdi
movq %r13, %rsi
callq 0x32ba
incq %rbp
jmp 0x2dc7
movq 0x50(%rsp), %rcx
incq %rcx
jmp 0x2db1
leaq 0x28(%rsp), %rdi
callq 0x3398
leaq 0x10(%rsp), %rdi
callq 0x3398
leaq 0x30(%rsp), %rdi
callq 0x39fc
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2e63
jmp 0x2e63
movq %rax, %r12
jmp 0x2e89
jmp 0x2e63
jmp 0x2e63
movq %rax, %r12
jmp 0x2e7f
movq %rax, %r12
leaq 0x28(%rsp), %rdi
callq 0x3398
leaq 0x10(%rsp), %rdi
callq 0x3398
leaq 0x30(%rsp), %rdi
callq 0x39fc
movq 0x18(%rsp), %rdi
callq 0x3110
movq (%rsp), %rbx
leaq 0x48(%rbx), %rdi
callq 0x3398
movq %r14, %rdi
callq 0x2090
movq %rbx, %rdi
callq 0x2090
movq %r12, %rdi
callq 0x22b0