// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rdx, %rdi
callq 0x1ad92
testb %al, %al
jne 0xa214
movq %rbx, %rdi
callq 0x1adc6
movq %r15, %rdi
callq 0x1ad92
testb %al, %al
je 0xa230
movq %r14, %rdi
callq 0x1ad92
testb %al, %al
jne 0xa42f
movl 0x8(%r15), %eax
cmpl 0x8(%r14), %eax
jne 0xa4b6
movl 0xc(%r15), %eax
cmpl 0xc(%r14), %eax
jne 0xa4b6
movl 0x10(%r15), %eax
leaq 0x8(%rsp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
callq 0x9974
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x99da
movq %r15, %rdi
callq 0x1aed4
testb %al, %al
je 0xa2dc
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 0xa42f
leaq 0x30(%rbx), %r13
xorl %ebp, %ebp
cmpq $0x0, 0x28(%r15,%rbp)
je 0xa38d
cmpq $0x0, 0x28(%r14,%rbp)
je 0xa38d
cmpq $0x0, -0x8(%r13,%rbp)
jne 0xa43e
movl $0x98, %edi
callq 0x21c0
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x209bb(%rip), %rcx     # 0x2ace0
movq %rcx, (%rax)
leaq 0x10(%rax), %rcx
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 %rcx, -0x8(%r13,%rbp)
movq (%r13,%rbp), %rdi
movq %rax, (%r13,%rbp)
testq %rdi, %rdi
je 0xa372
callq 0x1af0e
movq -0x8(%r13,%rbp), %rdx
movq %rbx, 0x68(%rdx)
movq 0x28(%r15,%rbp), %rdi
movq 0x28(%r14,%rbp), %rsi
movq %r12, %rcx
callq 0xa1e6
cmpq $0x0, 0x28(%r15,%rbp)
jne 0xa3c3
movq 0x28(%r14,%rbp), %rax
testq %rax, %rax
je 0xa3c3
cmpq $0x0, -0x8(%r13,%rbp)
jne 0xa45c
movq %r13, %rdi
addq %rbp, %rdi
movq %rax, -0x8(%r13,%rbp)
leaq (%r14,%rbp), %rsi
addq $0x30, %rsi
callq 0x1c38c
movq 0x28(%r15,%rbp), %rax
testq %rax, %rax
je 0xa3f9
cmpq $0x0, 0x28(%r14,%rbp)
jne 0xa3f9
cmpq $0x0, -0x8(%r13,%rbp)
jne 0xa47a
movq %r13, %rdi
addq %rbp, %rdi
movq %rax, -0x8(%r13,%rbp)
leaq (%r15,%rbp), %rsi
addq $0x30, %rsi
callq 0x1c38c
cmpq $0x0, 0x28(%r15,%rbp)
jne 0xa415
cmpq $0x0, 0x28(%r14,%rbp)
jne 0xa415
cmpq $0x0, -0x8(%r13,%rbp)
jne 0xa498
addq $0x10, %rbp
cmpq $0x40, %rbp
jne 0xa2e2
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, %rbx
leaq 0x197c7(%rip), %rsi     # 0x23c19
movq %rax, %rdi
callq 0x20a0
jmp 0xa4d2
movl $0x10, %edi
callq 0x20c0
movq %rax, %rbx
leaq 0x197a9(%rip), %rsi     # 0x23c19
movq %rax, %rdi
callq 0x20a0
jmp 0xa4d2
movl $0x10, %edi
callq 0x20c0
movq %rax, %rbx
leaq 0x1978b(%rip), %rsi     # 0x23c19
movq %rax, %rdi
callq 0x20a0
jmp 0xa4d2
movl $0x10, %edi
callq 0x20c0
movq %rax, %rbx
leaq 0x1976d(%rip), %rsi     # 0x23c19
movq %rax, %rdi
callq 0x20a0
jmp 0xa4d2
movl $0x10, %edi
callq 0x20c0
movq %rax, %rbx
leaq 0x196fa(%rip), %rsi     # 0x23bc4
movq %rax, %rdi
callq 0x20a0
movq 0x20b17(%rip), %rsi     # 0x2aff0
movq 0x20ae0(%rip), %rdx     # 0x2afc0
movq %rbx, %rdi
callq 0x2290
jmp 0xa4f0
jmp 0xa4f0
jmp 0xa4f0
jmp 0xa4f0
movq %rax, %r14
movq %rbx, %rdi
callq 0x2140
movq %r14, %rdi
callq 0x22b0
nop