// File: toxaart[P]hierarchical_block_sparse_lib/source/HierarchicalBlockSparseMatrix.h
// Function: hbsm::HierarchicalBlockSparseMatrix<double>::write_to_buffer(char*, unsigned long) const
void HierarchicalBlockSparseMatrix<Treal>::write_to_buffer(char * dataBuffer, size_t const bufferSize) const  {
			if(bufferSize < get_size())
				throw std::runtime_error("Error in HierarchicalBlockSparseMatrix<Treal>::write_to_buffer(): buffer too small.");

			char* p = dataBuffer;

			if(empty()){

				int n_bytes_written = 0;

				memcpy(p, &nRows, sizeof(int));
				p += sizeof(int);
				n_bytes_written += sizeof(int);

				memcpy(p, &nCols, sizeof(int));
				p += sizeof(int);
				n_bytes_written += sizeof(int);

				memcpy(p, &nRows_orig, sizeof(int));
				p += sizeof(int);
				n_bytes_written += sizeof(int);

				memcpy(p, &nCols_orig, sizeof(int));
				p += sizeof(int);
				n_bytes_written += sizeof(int);

				memcpy(p, &blocksize, sizeof(int));
				p += sizeof(int);
				n_bytes_written += sizeof(int);

				memcpy(p, &frob_norm_squared_internal, sizeof(Treal));
				p += sizeof(Treal);
				n_bytes_written += sizeof(Treal);

				memcpy(p, &n_block_multiplies, sizeof(size_t));
				p += sizeof(size_t);
				n_bytes_written += sizeof(size_t);

				//no children - write 4 zeros!
				size_t zero_size = 0;
				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);
				n_bytes_written += sizeof(size_t);

				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);
				n_bytes_written += sizeof(size_t);

				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);
				n_bytes_written += sizeof(size_t);

				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);
				n_bytes_written += sizeof(size_t);

				return;
			}


			if(lowest_level()){

				memcpy(p, &nRows, sizeof(int));
				p += sizeof(int);

				memcpy(p, &nCols, sizeof(int));
				p += sizeof(int);

				memcpy(p, &nRows_orig, sizeof(int));
				p += sizeof(int);

				memcpy(p, &nCols_orig, sizeof(int));
				p += sizeof(int);

				memcpy(p, &blocksize, sizeof(int));
				p += sizeof(int);

				memcpy(p, &frob_norm_squared_internal, sizeof(Treal));
				p += sizeof(Treal);

				memcpy(p, &n_block_multiplies, sizeof(size_t));
				p += sizeof(size_t);

				//no children - write 4 zeros!
				size_t zero_size = 0;
				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);

				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);

				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);

				memcpy(p, &zero_size, sizeof(size_t));
				p += sizeof(size_t);

				memcpy(p, &submatrix[0], submatrix.size() * sizeof(Treal));
				p += submatrix.size() * sizeof(Treal);

				return;
			}

			size_t size_child0 = 0, size_child1 = 0, size_child2 = 0, size_child3 = 0;
			std::vector<char> buf_child0, buf_child1, buf_child2, buf_child3;

			if(children[0] != NULL){ // if child do not exist, its size will be zero!
				size_child0 = children[0]->get_size();
				buf_child0.resize(size_child0);
				children[0]->write_to_buffer(&buf_child0[0],size_child0);
			}

			if(children[1] != NULL){
				size_child1 = children[1]->get_size();
				buf_child1.resize(size_child1);
				children[1]->write_to_buffer(&buf_child1[0],size_child1);
			}

			if(children[2] != NULL){
				size_child2 = children[2]->get_size();
				buf_child2.resize(size_child2);
				children[2]->write_to_buffer(&buf_child2[0],size_child2);
			}

			if(children[3] != NULL){
				size_child3 = children[3]->get_size();
				buf_child3.resize(size_child3);
				children[3]->write_to_buffer(&buf_child3[0],size_child3);
			}


			memcpy(p, &nRows, sizeof(int));
			p += sizeof(int);

			memcpy(p, &nCols, sizeof(int));
			p += sizeof(int);

			memcpy(p, &nRows_orig, sizeof(int));
			p += sizeof(int);

			memcpy(p, &nCols_orig, sizeof(int));
			p += sizeof(int);

			memcpy(p, &blocksize, sizeof(int));
			p += sizeof(int);

			memcpy(p, &frob_norm_squared_internal, sizeof(Treal));
			p += sizeof(Treal);

			memcpy(p, &n_block_multiplies, sizeof(size_t));
			p += sizeof(size_t);

			// write sizes of children even if they are zeros!
			memcpy(p, &size_child0, sizeof(size_t));
			p += sizeof(size_t);

			memcpy(p, &size_child1, sizeof(size_t));
			p += sizeof(size_t);

			memcpy(p, &size_child2, sizeof(size_t));
			p += sizeof(size_t);

			memcpy(p, &size_child3, sizeof(size_t));
			p += sizeof(size_t);


	    if(size_child0 > 0){ // if child size is > 0, write size and the child itself
				memcpy(p, &buf_child0[0],  size_child0);
				p += size_child0;
			}

			if(size_child1 > 0){
				memcpy(p, &buf_child1[0],  size_child1);
				p += size_child1;
			}

			if(size_child2 > 0){
				memcpy(p, &buf_child2[0],  size_child2);
				p += size_child2;
			}

			if(size_child3 > 0){
				memcpy(p, &buf_child3[0],  size_child3);
				p += size_child3;
			}

		}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xd0(%rbp)
callq 0x6090
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x631d
movl $0x10, %edi
callq 0x20a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
leaq 0xbb40(%rip), %rsi      # 0x11e1f
callq 0x2070
jmp 0x62e6
movq -0xe0(%rbp), %rdi
movq 0x11cfc(%rip), %rsi     # 0x17ff0
movq 0x11cc5(%rip), %rdx     # 0x17fc0
callq 0x22c0
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x2130
jmp 0x6b5c
movq -0xd8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
callq 0x4970
testb $0x1, %al
jne 0x633a
jmp 0x64c0
movq -0xd8(%rbp), %rcx
movl $0x0, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl (%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl 0xc(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl 0x10(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x4, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rcx), %rdx
movq %rdx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rcx), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movslq -0x34(%rbp), %rax
addq $0x8, %rax
movl %eax, -0x34(%rbp)
jmp 0x6b53
movq -0xd8(%rbp), %rdi
callq 0x97b0
testb $0x1, %al
jne 0x64d5
jmp 0x6640
movq -0xd8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl (%rdi), %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rdi), %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rdi), %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0xc(%rdi), %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x10(%rdi), %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rdi), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rdi), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xf0(%rbp)
addq $0x70, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9c20
movq -0xd8(%rbp), %rdi
movq %rax, -0xe8(%rbp)
addq $0x70, %rdi
callq 0x8cf0
movq -0xf0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq %rax, %rdx
shlq $0x3, %rdx
callq 0x2160
movq -0xd8(%rbp), %rdi
addq $0x70, %rdi
callq 0x8cf0
shlq $0x3, %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x6b53
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xdd40
leaq -0x98(%rbp), %rdi
callq 0xdd40
leaq -0xb0(%rbp), %rdi
callq 0xdd40
leaq -0xc8(%rbp), %rdi
callq 0xdd40
movq -0xd8(%rbp), %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9800
testb $0x1, %al
jne 0x66aa
jmp 0x675e
movq -0xd8(%rbp), %rdi
addq $0x28, %rdi
callq 0x9830
movq %rax, %rdi
callq 0x6090
movq %rax, -0xf8(%rbp)
jmp 0x66cb
movq -0xf8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0xdd60
jmp 0x66e5
movq -0xd8(%rbp), %rdi
addq $0x28, %rdi
callq 0x9830
movq %rax, -0x100(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x80(%rbp), %rdi
callq 0x6b70
movq -0x100(%rbp), %rdi
movq %rax, %rsi
movq -0x50(%rbp), %rdx
callq 0x6280
jmp 0x671e
jmp 0x675e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x7e10
leaq -0xb0(%rbp), %rdi
callq 0x7e10
leaq -0x98(%rbp), %rdi
callq 0x7e10
leaq -0x80(%rbp), %rdi
callq 0x7e10
jmp 0x6b5c
movq -0xd8(%rbp), %rdi
addq $0x28, %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9800
testb $0x1, %al
jne 0x677c
jmp 0x67f8
movq -0xd8(%rbp), %rdi
addq $0x38, %rdi
callq 0x9830
movq %rax, %rdi
callq 0x6090
movq %rax, -0x108(%rbp)
jmp 0x679d
movq -0x108(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0xdd60
jmp 0x67ba
movq -0xd8(%rbp), %rdi
addq $0x38, %rdi
callq 0x9830
movq %rax, -0x110(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x98(%rbp), %rdi
callq 0x6b70
movq -0x110(%rbp), %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rdx
callq 0x6280
jmp 0x67f6
jmp 0x67f8
movq -0xd8(%rbp), %rdi
addq $0x28, %rdi
addq $0x20, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9800
testb $0x1, %al
jne 0x6816
jmp 0x6892
movq -0xd8(%rbp), %rdi
addq $0x48, %rdi
callq 0x9830
movq %rax, %rdi
callq 0x6090
movq %rax, -0x118(%rbp)
jmp 0x6837
movq -0x118(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0xdd60
jmp 0x6854
movq -0xd8(%rbp), %rdi
addq $0x48, %rdi
callq 0x9830
movq %rax, -0x120(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0xb0(%rbp), %rdi
callq 0x6b70
movq -0x120(%rbp), %rdi
movq %rax, %rsi
movq -0x60(%rbp), %rdx
callq 0x6280
jmp 0x6890
jmp 0x6892
movq -0xd8(%rbp), %rdi
addq $0x28, %rdi
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9800
testb $0x1, %al
jne 0x68b0
jmp 0x692c
movq -0xd8(%rbp), %rdi
addq $0x58, %rdi
callq 0x9830
movq %rax, %rdi
callq 0x6090
movq %rax, -0x128(%rbp)
jmp 0x68d1
movq -0x128(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0xdd60
jmp 0x68ee
movq -0xd8(%rbp), %rdi
addq $0x58, %rdi
callq 0x9830
movq %rax, -0x130(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0xc8(%rbp), %rdi
callq 0x6b70
movq -0x130(%rbp), %rdi
movq %rax, %rsi
movq -0x68(%rbp), %rdx
callq 0x6280
jmp 0x692a
jmp 0x692c
movq -0xd8(%rbp), %rcx
movq -0x30(%rbp), %rax
movl (%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0xc(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x10(%rcx), %edx
movl %edx, (%rax)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x18(%rcx), %rdx
movq %rdx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rcx), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x50(%rbp)
jbe 0x6a63
movq -0x30(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0x80(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6b70
movq -0x138(%rbp), %rdi
movq %rax, %rsi
movq -0x50(%rbp), %rdx
callq 0x2160
movq -0x50(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x58(%rbp)
jbe 0x6aa4
movq -0x30(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0x98(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6b70
movq -0x140(%rbp), %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rdx
callq 0x2160
movq -0x58(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x60(%rbp)
jbe 0x6ae5
movq -0x30(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6b70
movq -0x148(%rbp), %rdi
movq %rax, %rsi
movq -0x60(%rbp), %rdx
callq 0x2160
movq -0x60(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x68(%rbp)
jbe 0x6b26
movq -0x30(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0xc8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6b70
movq -0x150(%rbp), %rdi
movq %rax, %rsi
movq -0x68(%rbp), %rdx
callq 0x2160
movq -0x68(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x7e10
leaq -0xb0(%rbp), %rdi
callq 0x7e10
leaq -0x98(%rbp), %rdi
callq 0x7e10
leaq -0x80(%rbp), %rdi
callq 0x7e10
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x22e0
nopw %cs:(%rax,%rax)
nop