// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rsi, %r12
movq %rdi, %r15
leaq 0x10(%rdi), %r14
movq %r14, (%rdi)
xorl %eax, %eax
movq %rax, 0x8(%rdi)
movb %al, 0x10(%rdi)
leaq 0x20(%rdi), %rcx
movq %rcx, 0x48(%rsp)
leaq 0x30(%rdi), %rbp
movq %rbp, 0x20(%rdi)
movq %rax, 0x28(%rdi)
movb %al, 0x30(%rdi)
leaq 0x50(%rdi), %rcx
movq %rcx, 0x38(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x50(%rdi)
movq %rax, 0x60(%rdi)
movq %rsi, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2210
leaq 0x50(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movl $0x100, %esi            # imm = 0x100
callq 0x51ac
movq 0x50(%rsp), %r13
movq 0x58(%rsp), %rbx
cmpq %rbx, %r13
je 0x31e6
movl $0x8, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x21f0
addq $0x8, %r13
jmp 0x31cb
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x4f0e
leaq 0x40(%r15), %r13
movaps 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x10(%rsp)
movq 0x48(%r15), %rdi
movups %xmm0, 0x40(%r15)
testq %rdi, %rdi
je 0x323a
callq 0x3c2e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x323a
callq 0x3c2e
movq (%r13), %rsi
addq $0x38, %rsi
movq 0x38(%rsp), %rdi
callq 0x3a7a
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2210
movq %r12, %rdi
callq 0x22c0
movq %r15, %rdi
movq %rax, %rsi
callq 0x2270
movl $0x800, %esi            # imm = 0x800
movq %r12, %rdi
xorl %edx, %edx
callq 0x2210
movq (%r12), %rax
movq -0x18(%rax), %rax
movq 0xe8(%r12,%rax), %rcx
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
movq $0x0, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
movups 0x10(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x4ade
movq (%r13), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdx
movq %rdx, 0x10(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x28(%rsp), %r12
testq %rax, %rax
je 0x32fd
movq 0x5ce7(%rip), %rcx      # 0x8fd0
cmpb $0x0, (%rcx)
je 0x330d
incl 0x8(%rax)
movq %rdx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
jmp 0x332a
movq %rdx, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
jmp 0x3338
lock
incl 0x8(%rax)
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %rdx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x3338
cmpb $0x0, (%rcx)
je 0x3334
incl 0x8(%rax)
jmp 0x3338
lock
incl 0x8(%rax)
movq %rbp, 0x68(%rsp)
movq (%r13), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
movq %r14, 0x70(%rsp)
je 0x3362
xorl %ebp, %ebp
addq (%rax), %rbp
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x3354
jmp 0x3364
xorl %ebp, %ebp
movq %r15, 0x30(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x3417
leaq 0x18(%rsp), %r13
movq 0x30(%rsp), %rax
movq (%rax), %rbx
addq %rbx, 0x40(%rsp)
testq %rbp, %rbp
je 0x3417
movzbl (%rbx), %r15d
xorl %r14d, %r14d
xorl %eax, %eax
btl %r14d, %r15d
setae %al
movq 0x20(%rsp), %rcx
shll $0x4, %eax
leaq (%rax,%rcx), %rsi
addq $0x18, %rsi
movq -0x8(%rsi), %rax
movq %rax, 0x20(%rsp)
movq %r12, %rdi
callq 0x3bda
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
orq 0x10(%rax), %rcx
jne 0x33fb
movsbl (%rax), %esi
movq 0x48(%rsp), %rdi
callq 0x2170
decq %rbp
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3bda
cmpq $0x6, %r14
ja 0x3409
incq %r14
testq %rbp, %rbp
jne 0x339d
incq %rbx
cmpq 0x40(%rsp), %rbx
jne 0x338d
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x3426
callq 0x3c2e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x3435
callq 0x3c2e
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x344c
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3468
jmp 0x3468
movq %rax, %r12
jmp 0x34b4
jmp 0x3468
jmp 0x3468
movq %rax, %r12
jmp 0x349d
movq %rax, %r12
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x347f
callq 0x3c2e
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
movq 0x70(%rsp), %r14
movq 0x30(%rsp), %r15
movq 0x68(%rsp), %rbp
je 0x349d
callq 0x3c2e
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x34b4
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x38(%rsp), %rdi
callq 0x392a
movq 0x48(%r15), %rdi
testq %rdi, %rdi
je 0x34cc
callq 0x3c2e
movq 0x48(%rsp), %rax
movq (%rax), %rdi
cmpq %rbp, %rdi
je 0x34e5
movq (%rbp), %rsi
incq %rsi
callq 0x21c0
movq (%r15), %rdi
cmpq %r14, %rdi
je 0x34f8
movq (%r14), %rsi
incq %rsi
callq 0x21c0
movq %r12, %rdi
callq 0x22d0