// File: /workspace/github_cpp_makefile_star2_O1/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, %rbp
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), %r15
movq %r15, 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 0x5124
movq 0x50(%rsp), %r13
movq 0x58(%rsp), %rbx
cmpq %rbx, %r13
je 0x3256
movl $0x8, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x21f0
addq $0x8, %r13
jmp 0x323b
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x50(%rsp), %rcx
callq 0x4e80
movaps 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x10(%rsp)
movq 0x48(%rbp), %rdi
movups %xmm0, 0x40(%rbp)
testq %rdi, %rdi
je 0x3296
callq 0x3bc4
leaq 0x40(%rbp), %r13
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x32a9
callq 0x3bc4
movq (%r13), %rsi
addq $0x38, %rsi
movq 0x38(%rsp), %rdi
callq 0x3a2c
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2210
movq %r12, %rdi
callq 0x22c0
movq %rbp, %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 (%rbp), %rsi
movq 0x8(%rbp), %rdx
addq %rsi, %rdx
movq $0x0, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
movups 0x10(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x4a90
movq (%r13), %rax
movq 0x18(%rax), %rax
movq (%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x3362
movq 0x5c7c(%rip), %rcx      # 0x8fd0
cmpb $0x0, (%rcx)
je 0x335e
incl 0x8(%rax)
jmp 0x3362
lock
incl 0x8(%rax)
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x3390
movq 0x5c4e(%rip), %rcx      # 0x8fd0
cmpb $0x0, (%rcx)
je 0x338c
incl 0x8(%rax)
jmp 0x3390
lock
incl 0x8(%rax)
movq %r15, 0x68(%rsp)
movq (%r13), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
movq %r14, 0x70(%rsp)
je 0x33bb
xorl %r15d, %r15d
addq (%rax), %r15
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x33ad
jmp 0x33be
xorl %r15d, %r15d
movq %rbp, 0x30(%rsp)
movq 0x8(%rbp), %rax
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x347e
leaq 0x28(%rsp), %r13
movq 0x30(%rsp), %rax
movq (%rax), %rbx
addq %rbx, 0x40(%rsp)
testq %r15, %r15
je 0x3466
movzbl (%rbx), %r14d
xorl %r12d, %r12d
movq %r15, %rbp
xorl %eax, %eax
btl %r12d, %r14d
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 %r13, %rdi
callq 0x3b72
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rcx
orq 0x10(%rax), %rcx
jne 0x3456
movsbl (%rax), %esi
movq 0x48(%rsp), %rdi
callq 0x2170
decq %rbp
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3b72
cmpq $0x6, %r12
ja 0x3468
incq %r12
testq %rbp, %rbp
jne 0x33f6
jmp 0x3468
xorl %ebp, %ebp
testq %r15, %r15
je 0x347e
incq %rbx
movq %rbp, %r15
cmpq 0x40(%rsp), %rbx
jne 0x33e7
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x348d
callq 0x3bc4
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x349c
callq 0x3bc4
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x34b3
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 0x34cf
jmp 0x34cf
movq %rax, %r12
jmp 0x351b
jmp 0x34cf
jmp 0x34cf
movq %rax, %r12
jmp 0x3504
movq %rax, %r12
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x34e6
callq 0x3bc4
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
movq 0x70(%rsp), %r14
movq 0x30(%rsp), %rbp
movq 0x68(%rsp), %r15
je 0x3504
callq 0x3bc4
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x351b
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x38(%rsp), %rdi
callq 0x397e
movq 0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x3533
callq 0x3bc4
movq 0x48(%rsp), %rax
movq (%rax), %rdi
cmpq %r15, %rdi
je 0x354b
movq (%r15), %rsi
incq %rsi
callq 0x21c0
movq (%rbp), %rdi
cmpq %r14, %rdi
je 0x355f
movq (%r14), %rsi
incq %rsi
callq 0x21c0
movq %r12, %rdi
callq 0x22d0
nop