// File: /workspace/github_cpp_makefile_star2_O3/toxaart[P]hierarchical_block_sparse_lib/source/HierarchicalBlockSparseMatrix.h
// Function: hbsm::HierarchicalBlockSparseMatrix<double>::add(hbsm::HierarchicalBlockSparseMatrix<double> const&, hbsm::HierarchicalBlockSparseMatrix<double> const&, hbsm::HierarchicalBlockSparseMatrix<double>&, unsigned long*)
void HierarchicalBlockSparseMatrix<Treal>::add(HierarchicalBlockSparseMatrix<Treal> const & A,
				       HierarchicalBlockSparseMatrix<Treal> const & B,
				       HierarchicalBlockSparseMatrix<Treal> & C,
					   size_t* no_of_resizes){

      if(!C.empty()) C.clear();

			if(A.empty() && B.empty()){
				return;
			}


			if(A.nRows_orig != B.nRows_orig || A.nCols_orig != B.nCols_orig ){
				throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrices to add have different sizes!");
			}

      C.set_params(A.get_params());

      C.resize(A.nRows_orig,A.nCols_orig, no_of_resizes);

			if(A.lowest_level()){


				int blocksize = A.blocksize;

				int noOfElements = blocksize*blocksize;
				Treal const ONEreal = 1.0;
				int  const ONEint  = 1;

				//copy A to C
				memcpy(&C.submatrix[0], &A.submatrix[0], sizeof(Treal) * noOfElements);

				// Add 1.0 * B to C.
				axpy(&noOfElements, &ONEreal, &B.submatrix[0], &ONEint, &C.submatrix[0], &ONEint);

				return;
			}

			for(int i = 0; i < 4; ++i){

				if(A.children[i] != NULL && B.children[i] != NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");


					C.children[i] = std::make_shared<HierarchicalBlockSparseMatrix<Treal> >();
					C.children[i]->parent = &C;

					add(*A.children[i], *B.children[i], *C.children[i], no_of_resizes);

				}

				if(A.children[i] == NULL && B.children[i] != NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");
					C.children[i] = B.children[i]; // smart pointer assignment. Now C.children[i] has parent in B, but it does not matter since they are of the  same size!

				}

				if(A.children[i] != NULL && B.children[i] == NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");
					C.children[i] = A.children[i]; // smart pointer assignment. Now C.children[i] has parent in A, but it does not matter since they are of the  same size!

				}

				if(A.children[i] == NULL && B.children[i] == NULL){

					if(C.children[i] != NULL) throw std::runtime_error("Error in HierarchicalBlockSparseMatrix::add(): matrix C has non-null child!");

					//nothing to do here, leave it NULL
				}

			}

			C.n_block_multiplies = A.get_n_block_multiplications() + B.get_n_block_multiplications();

			return;
		}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rdx, %rdi
callq 0x1b2de
testb %al, %al
jne 0xa1b0
movq %rbx, %rdi
callq 0x1b326
movq %r15, %rdi
callq 0x1b2de
testb %al, %al
je 0xa1cc
movq %r14, %rdi
callq 0x1b2de
testb %al, %al
jne 0xa3db
movl 0x8(%r15), %eax
cmpl 0x8(%r14), %eax
jne 0xa462
movl 0xc(%r15), %eax
cmpl 0xc(%r14), %eax
jne 0xa462
movl 0x10(%r15), %eax
leaq 0x8(%rsp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
callq 0x9920
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
movq %rbx, %rdi
movq %r13, %rcx
callq 0x9986
movq %r15, %rdi
callq 0x1b448
testb %al, %al
je 0xa278
movl 0x10(%r15), %edx
imull %edx, %edx
leaq 0x14(%rsp), %r12
movl %edx, (%r12)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
leaq 0x8(%rsp), %r13
movq %rax, (%r13)
leaq 0x10(%rsp), %rbp
movl $0x1, (%rbp)
movq 0x70(%rbx), %rdi
movq 0x70(%r15), %rsi
shlq $0x3, %rdx
callq 0x2180
movq 0x70(%r14), %rdx
movq 0x70(%rbx), %r8
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rcx
movq %rbp, %r9
callq 0x21e0
jmp 0xa3db
leaq 0x30(%rbx), %rbp
xorl %r12d, %r12d
movq %rbp, (%rsp)
movq 0x28(%r15,%r12), %rax
testq %rax, %rax
je 0xa33a
cmpq $0x0, 0x28(%r14,%r12)
je 0xa370
cmpq $0x0, -0x8(%rbp,%r12)
jne 0xa3ea
movl $0x98, %edi
callq 0x21c0
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x21a50(%rip), %rcx     # 0x2bd18
movq %rcx, (%rax)
leaq 0x10(%rax), %rdx
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movups %xmm0, 0x48(%rax)
movups %xmm0, 0x58(%rax)
movups %xmm0, 0x68(%rax)
movups %xmm0, 0x78(%rax)
movups %xmm0, 0x88(%rax)
movq %rdx, -0x8(%rbp,%r12)
movq (%rbp,%r12), %rdi
movq %rax, (%rbp,%r12)
testq %rdi, %rdi
je 0xa31a
callq 0x1b482
movq -0x8(%rbp,%r12), %rdx
movq %rbx, 0x68(%rdx)
movq 0x28(%r15,%r12), %rdi
movq 0x28(%r14,%r12), %rsi
movq %r13, %rcx
callq 0xa182
movq 0x28(%r15,%r12), %rax
testq %rax, %rax
jne 0xa370
movq 0x28(%r14,%r12), %rax
testq %rax, %rax
je 0xa3ad
cmpq $0x0, -0x8(%rbp,%r12)
jne 0xa408
leaq (%r12,%rbp), %rdi
movq %rax, -0x8(%rbp,%r12)
leaq (%r14,%r12), %rsi
addq $0x30, %rsi
callq 0x1c8e8
movq 0x28(%r15,%r12), %rax
testq %rax, %rax
je 0xa3ad
cmpq $0x0, 0x28(%r14,%r12)
jne 0xa3c1
cmpq $0x0, -0x8(%rbp,%r12)
jne 0xa426
leaq (%r12,%rbp), %rdi
movq %rax, -0x8(%rbp,%r12)
movq %r15, %rbp
addq %r12, %r15
addq $0x30, %r15
movq %r15, %rsi
callq 0x1c8e8
cmpq $0x0, -0x8(%r15)
movq %rbp, %r15
movq (%rsp), %rbp
jne 0xa3c1
cmpq $0x0, 0x28(%r14,%r12)
jne 0xa3c1
cmpq $0x0, -0x8(%rbp,%r12)
jne 0xa444
addq $0x10, %r12
cmpq $0x40, %r12
jne 0xa283
movq 0x20(%r14), %rax
addq 0x20(%r15), %rax
movq %rax, 0x20(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20c0
movq %rax, %r14
leaq 0x1a87b(%rip), %rsi     # 0x24c79
movq %rax, %rdi
callq 0x20a0
jmp 0xa47e
movl $0x10, %edi
callq 0x20c0
movq %rax, %r14
leaq 0x1a85d(%rip), %rsi     # 0x24c79
movq %rax, %rdi
callq 0x20a0
jmp 0xa47e
movl $0x10, %edi
callq 0x20c0
movq %rax, %r14
leaq 0x1a83f(%rip), %rsi     # 0x24c79
movq %rax, %rdi
callq 0x20a0
jmp 0xa47e
movl $0x10, %edi
callq 0x20c0
movq %rax, %r14
leaq 0x1a821(%rip), %rsi     # 0x24c79
movq %rax, %rdi
callq 0x20a0
jmp 0xa47e
movl $0x10, %edi
callq 0x20c0
movq %rax, %r14
leaq 0x1a7ae(%rip), %rsi     # 0x24c24
movq %rax, %rdi
callq 0x20a0
movq 0x21b6b(%rip), %rsi     # 0x2bff0
movq 0x21b34(%rip), %rdx     # 0x2bfc0
movq %r14, %rdi
callq 0x2290
jmp 0xa49c
jmp 0xa49c
jmp 0xa49c
jmp 0xa49c
movq %rax, %rbx
movq %r14, %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x22b0
nop