// File: hey-yahei[P]ConvNet/src/helper.cpp
// Function: LoadFromHuffmanFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<unsigned char, std::allocator<unsigned char>>&, TarFile*)
unsigned long LoadFromHuffmanFile(string prefix, vector<unsigned char> &dst, TarFile* tarfile) {
	string data = prefix + ".hf.dat";
	string codebook = prefix + ".hfcb.dat";
	int cnt = 0;

	Tree* huffman_tree = DecodeHuffmanCodebook(codebook.c_str(), tarfile);
	Tree* ptr = huffman_tree;
	Bit accessor(data.c_str(), tarfile);
	int bit;
	while ((bit = accessor.Next()) != -1) {
		if (0 == bit) {
			ptr = ptr->left;
		} else if (1 == bit) {
			ptr = ptr->right;
		} else {
			cerr << "LoadFromHuffmanFile: unknown bit" << endl;
			exit(1);
		}

		if (-1 != ptr->value) {
			dst.push_back(ptr->value);
			cnt++;
			ptr = huffman_tree;
		}
	}

	delete huffman_tree;
	return cnt;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rsi, %rax
movq %rdi, %rsi
movq %rsi, -0xa8(%rbp)
movq %rsi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq 0x4782(%rip), %rdx      # 0xc3ad
leaq -0x38(%rbp), %rdi
callq 0x8430
movq -0xa8(%rbp), %rsi
leaq 0x4773(%rip), %rdx      # 0xc3b5
leaq -0x58(%rbp), %rdi
callq 0x8430
jmp 0x7c4d
movl $0x0, -0x68(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x7920
movq %rax, -0xb0(%rbp)
jmp 0x7c72
movq -0xb0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x20b0
movq %rax, %rsi
movq -0x18(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0x8310
jmp 0x7ca3
jmp 0x7ca5
leaq -0x98(%rbp), %rdi
callq 0x8220
movl %eax, -0xb4(%rbp)
jmp 0x7cb9
movl -0xb4(%rbp), %eax
movl %eax, -0x9c(%rbp)
cmpl $-0x1, %eax
je 0x7dc2
xorl %eax, %eax
cmpl -0x9c(%rbp), %eax
jne 0x7d27
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x7d7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x7e32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x7e29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x83d0
jmp 0x7e29
movl $0x1, %eax
cmpl -0x9c(%rbp), %eax
jne 0x7d42
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x7d7d
movq 0xa2a7(%rip), %rdi      # 0x11ff0
leaq 0x466f(%rip), %rsi      # 0xc3bf
callq 0x21c0
movq %rax, -0xc0(%rbp)
jmp 0x7d5e
movq -0xc0(%rbp), %rdi
movq 0xa254(%rip), %rsi      # 0x11fc0
callq 0x2200
jmp 0x7d73
movl $0x1, %edi
callq 0x2290
jmp 0x7d7f
movq -0x78(%rbp), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x10(%rcx), %eax
je 0x7dbd
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rax
movb 0x10(%rax), %al
movb %al, -0x9d(%rbp)
leaq -0x9d(%rbp), %rsi
callq 0x84b0
jmp 0x7dac
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x7ca5
movq -0x70(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x7df0
movq -0xc8(%rbp), %rdi
callq 0x84e0
movq -0xc8(%rbp), %rdi
movl $0x18, %esi
callq 0x21f0
movslq -0x68(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x83d0
leaq -0x58(%rbp), %rdi
callq 0x2130
leaq -0x38(%rbp), %rdi
callq 0x2130
movq -0xd0(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x2130
leaq -0x38(%rbp), %rdi
callq 0x2130
movq -0x60(%rbp), %rdi
callq 0x2370
nopw %cs:(%rax,%rax)