// File: /workspace/github_cpp_makefile_star2_O2/yukimasano[P]cpp_matrix/Matrix.cpp
// Function: QRsolve(Matrix&, Vector&)
Vector QRsolve(Matrix& A, Vector& v){
	// QR via Householder
	assert(nrows(A) == ncols(A));
	int m = ncols(A);
	Matrix R = A;
	Vector temp=ones(m);
	Matrix Q = diag(temp);
    for (int j = 0; j<m; j++){
    	Matrix x(m-j,1);
    	double len=0.0;
    	for (int l=j; l<m; l++){
    		x.set_val(l+1-j,1 ,R(l+1,j+1));
    		len+=R(l+1,j+1) * R(l+1,j+1);
    	}

    	len=sqrt(len);

    	int sign = (x(1,1) >= 0) - (x(1,1) < 0);
    	Matrix e(m-j,1);
    	e.set_val(1,1,1.0);

    	Matrix u = (sign*len*e + x);

    	// normalise u
    	len=0.0;
    	for (int i = 1; i<=m-j; i++){
    		len+=u(i,1)*u(i,1);
    	}
      //std::cout<<j<<u<<std::endl<<std::flush;
      if (len!=0){
    	   u=u* (1./sqrt(len));
      }
      //   	std::cout<<"here comes  u " << std::endl<< j<<"  "<< u << std::endl<<std::flush;

    	// SUBMULTIPLICATION    START /////////////////////////////
    	Matrix sub(m-j, m-j);
    	Matrix subQ(m,m-j);
    	for (int i = j; i<m; i++){
    	//	std::cout<<i<<std::flush<<std::endl;
    		for (int h = j; h<m; h++){
    			sub.set_val(i+1 - j,h+1 - j, R(i+1,h+1));
    		}
    	}

    	for (int i = 0; i<m; i++){
			for (int h = j; h<m; h++){
				subQ.set_val(i+1,h+1-j,Q(i+1,h+1));
			}
		}

    	Matrix w=(u.T());
    	Matrix z= w*sub;
      //  	std::cout<<"here is sub pre   "<<std::endl<< u*z<<std::flush<< std::endl;
    	sub = sub -2.0*(u*z);

    	Vector temp2= ones(m-j);
    	Matrix subeye= diag(temp2);
    	Matrix temp3= subeye -2.0*(u*w);
    	subQ= subQ*(temp3);

      //    	std::cout<<"here is sub post   "<<std::endl<< sub<<std::flush<< std::endl;
    	for (int i = j; i<m; i++){
    		for (int h = j; h<m; h++){
    			R.set_val(i+1,h+1, sub(i+1- j,h+1- j));
    		}
    	}

    	for (int i = 0; i<m; i++){
			for (int h = j; h<m; h++){
				Q.set_val(i+1, h+1, subQ(i+1,h+1-j));
			}
      //std::cout<<j<<"end of for"<<std::endl<<std::flush;
		}

    	// SUBMULTIPLICATION  END  ////////////////////////////

    }
    //    std::cout<< "here is R "<< std::endl<< R<<std::endl<<std::flush;
    //    std::cout<< "here is Q "<< std::endl<< Q<<std::endl<<std::flush;
    //    std::cout<< "here is QR "<< std::endl<< Q*R<<std::endl<<std::flush;

    // Invert Q multiply b with it.
    Matrix QT=Q.T();
    Vector v2=QT* v;
    //BACKWARD SOLVE FOR X
	Vector x(m);
	for (int k=m-1; k>=0; k--){
		x.set_val(k+1, v2(k+1));
		for (int j = k+1; j<m; j++){
			x.set_val(k+1, x(k+1)- R(k+1,j+1)*x(j+1));
			}
		x.set_val(k+1, x(k+1)/R(k+1,k+1));
		}
    //	std::cout<< A*x -v <<std::endl;
	return x;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdx, 0xf8(%rsp)
movl 0x8(%rsi), %ebx
cmpl 0xc(%rsi), %ebx
jne 0x537a
movq %rdi, %r12
leaq 0xa0(%rsp), %rdi
callq 0x3b80
leaq 0x138(%rsp), %rdi
movq %rbx, 0x18(%rsp)
movl %ebx, %esi
callq 0x8995
leaq 0xd8(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x3b09
xorl %ebx, %ebx
movq 0x18(%rsp), %rax
testl %eax, %eax
movl $0x0, %ebp
cmovgl %eax, %ebp
movq %rax, %rcx
negq %rcx
movq %rcx, 0x40(%rsp)
pushq $0x1
popq %r15
movl %eax, %ecx
movq %r12, 0x100(%rsp)
movq %rbp, 0xb0(%rsp)
movq 0x18(%rsp), %rax
cmpl %ebx, %eax
movl %ebx, %edx
cmovgl %eax, %edx
movq %rdx, 0x28(%rsp)
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movq %rdx, 0x70(%rsp)
cmpq %rbp, %rbx
je 0x5212
movl %ecx, 0x9c(%rsp)
movl %eax, %esi
subl %ebx, %esi
leaq 0x30(%rsp), %rdi
movl %esi, 0x14(%rsp)
movl %r15d, %edx
callq 0x2d58
movq %rbx, %r13
subq 0x28(%rsp), %r13
movq 0x70(%rsp), %rax
negl %eax
movq %rax, 0x70(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x1(%rbx), %rbp
movq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
xorpd %xmm0, %xmm0
pushq $0x1
popq %r12
leaq 0xa0(%rsp), %r14
movq 0x40(%rsp), %rax
addq %r12, %rax
cmpq $0x1, %rax
je 0x4cd5
movsd %xmm0, (%rsp)
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rbx
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x2e20
movsd (%rax), %xmm0
movq 0x68(%rsp), %rax
movq -0x8(%rax,%r12,8), %rax
movsd %xmm0, (%rax)
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x2e20
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x2e20
movsd 0x20(%rsp), %xmm1
mulsd (%rax), %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
incq %r12
jmp 0x4c61
ucomisd 0x43a3(%rip), %xmm0     # 0x9080
movq %rbp, 0x108(%rsp)
jb 0x4ced
sqrtsd %xmm0, %xmm0
jmp 0x4cf2
callq 0x20f0
movsd %xmm0, (%rsp)
movq 0x40(%rsp), %r12
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movl %r15d, %esi
movl %r15d, %edx
callq 0x2e20
movq 0xb0(%rsp), %rbp
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
movl %r15d, %edx
callq 0x2e20
movsd (%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movl 0x14(%rsp), %esi
movl %r15d, %edx
callq 0x2d58
xorl %eax, %eax
xorpd %xmm0, %xmm0
ucomisd 0x68(%rsp), %xmm0
seta %al
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
adcl $-0x1, %eax
negl %eax
movq 0x88(%rsp), %rcx
movq (%rcx), %rcx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rcx)
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x48(%rsp)
leaq 0x58(%rsp), %r14
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x38f1
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
callq 0x30c0
movq %r14, %rdi
callq 0x2dea
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movl %r15d, %ebx
movq 0x70(%rsp), %rax
addl %ebx, %eax
cmpl $0x1, %eax
je 0x4e25
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x2e20
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl %r15d, %edx
callq 0x2e20
movsd 0x20(%rsp), %xmm1
mulsd (%rax), %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
incl %ebx
jmp 0x4dd4
movsd (%rsp), %xmm0
ucomisd 0x424e(%rip), %xmm0     # 0x9080
jne 0x4e36
jnp 0x4e88
jb 0x4e3f
sqrtsd (%rsp), %xmm0
jmp 0x4e49
movsd (%rsp), %xmm0
callq 0x20f0
movsd 0x41c7(%rip), %xmm1     # 0x9018
divsd %xmm0, %xmm1
movsd %xmm1, 0x48(%rsp)
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
leaq 0x78(%rsp), %r14
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
callq 0x380b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x33ce
movq %rbx, %rdi
callq 0x2dea
leaq 0x58(%rsp), %rdi
movl 0x14(%rsp), %ebx
movl %ebx, %esi
movl %ebx, %edx
callq 0x2d58
leaq 0x48(%rsp), %rdi
movq 0x18(%rsp), %rsi
movl %ebx, %edx
callq 0x2d58
movl 0x28(%rsp), %eax
movq %rax, (%rsp)
movq 0x8(%rsp), %rbx
cmpq (%rsp), %rbx
je 0x4f09
incq %rbx
movq %rbx, %r14
subq 0x8(%rsp), %r14
pushq $0x1
popq %r15
leaq (%r12,%r15), %rax
cmpq $0x1, %rax
je 0x4eb8
movq 0x8(%rsp), %rax
leal (%rax,%r15), %edx
leaq 0xa0(%rsp), %rdi
movl %ebx, %esi
callq 0x2e20
movsd (%rax), %xmm0
movq 0x58(%rsp), %rax
movq -0x8(%rax,%r14,8), %rax
movsd %xmm0, -0x8(%rax,%r15,8)
incq %r15
jmp 0x4ecd
xorl %ebx, %ebx
leaq 0x58(%rsp), %r15
leaq 0x48(%rsp), %r12
cmpq %rbp, %rbx
je 0x4f5d
incq %rbx
pushq $0x1
popq %r14
leaq (%r14,%r13), %rax
cmpq $0x1, %rax
je 0x4f15
movq 0x8(%rsp), %rax
leal (%rax,%r14), %edx
leaq 0xd8(%rsp), %rdi
movl %ebx, %esi
callq 0x2e20
movsd (%rax), %xmm0
movq 0x48(%rsp), %rax
movq -0x8(%rax,%rbx,8), %rax
movsd %xmm0, -0x8(%rax,%r14,8)
incq %r14
jmp 0x4f21
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
callq 0x2f86
leaq 0x158(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x35d4
movl 0x14(%rsp), %ebp
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0xe8(%rsp)
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
movq %r14, %rdx
callq 0x35d4
leaq 0xc8(%rsp), %r14
movq %r14, %rdi
leaq 0xe8(%rsp), %rsi
movq %rbx, %rdx
callq 0x38f1
leaq 0x118(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x32b3
movq %r15, %rdi
movq %rbx, %rsi
callq 0x33ce
movq %rbx, %r14
movq %rbx, %rdi
callq 0x2dea
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x2dea
leaq 0xb8(%rsp), %rdi
callq 0x2dea
movq %r14, %rdi
movl %ebp, %esi
callq 0x8995
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3b09
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0x110(%rsp)
leaq 0x148(%rsp), %rbx
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
leaq 0x128(%rsp), %rdx
callq 0x35d4
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
leaq 0x110(%rsp), %rsi
movq %rbx, %rdx
callq 0x38f1
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
leaq 0xc8(%rsp), %rsi
movq %r14, %rdx
callq 0x32b3
movq %r14, %rdi
callq 0x2dea
leaq 0x148(%rsp), %rdi
callq 0x2dea
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x35d4
movq %r12, %rdi
movq %r14, %rsi
callq 0x33ce
movq %r14, %rdi
callq 0x2dea
movq 0xa0(%rsp), %r12
movq 0x8(%rsp), %r15
cmpq (%rsp), %r15
je 0x511f
leaq 0x1(%r15), %r14
movl %r14d, %ebx
movq 0x8(%rsp), %rbp
subl %ebp, %ebx
pushq $0x1
popq %r13
cmpq %rbp, 0x18(%rsp)
je 0x511a
leaq 0x58(%rsp), %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x2e20
movsd (%rax), %xmm0
movq (%r12,%r15,8), %rax
movsd %xmm0, (%rax,%rbp,8)
incl %r13d
incq %rbp
jmp 0x50ef
movq %r14, %r15
jmp 0x50d7
movq 0xd8(%rsp), %r12
xorl %r15d, %r15d
movq 0xb0(%rsp), %rbp
cmpq %rbp, %r15
je 0x5175
leaq 0x1(%r15), %rbx
pushq $0x1
popq %r13
movq 0x8(%rsp), %r14
cmpq %r14, 0x28(%rsp)
je 0x5170
leaq 0x48(%rsp), %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x2e20
movsd (%rax), %xmm0
movq (%r12,%r15,8), %rax
movsd %xmm0, (%rax,%r14,8)
incl %r13d
incq %r14
jmp 0x5144
movq %rbx, %r15
jmp 0x5132
leaq 0xb8(%rsp), %rdi
callq 0x2dea
leaq 0xc8(%rsp), %rdi
callq 0x2dea
leaq 0x118(%rsp), %rdi
callq 0x8104
leaq 0x158(%rsp), %rdi
callq 0x2dea
leaq 0x128(%rsp), %rdi
callq 0x2dea
leaq 0x48(%rsp), %rdi
callq 0x2dea
leaq 0x58(%rsp), %rdi
callq 0x2dea
leaq 0x78(%rsp), %rdi
callq 0x2dea
leaq 0x88(%rsp), %rdi
callq 0x2dea
leaq 0x30(%rsp), %rdi
callq 0x2dea
movl 0x9c(%rsp), %ecx
decl %ecx
incq 0x40(%rsp)
movq 0x108(%rsp), %rbx
movq 0x100(%rsp), %r12
pushq $0x1
popq %r15
jmp 0x4be5
leaq 0x30(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x2f86
leaq 0x88(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movq 0xf8(%rsp), %rdx
callq 0x372f
movq %r12, %rdi
movq 0x18(%rsp), %r14
movl %r14d, %esi
callq 0x8068
leaq 0x88(%rsp), %rbx
leaq 0xa0(%rsp), %r15
testl %r14d, %r14d
jle 0x5327
movq %rbx, %rdi
movl %r14d, %esi
callq 0x86de
movsd (%rax), %xmm0
movq %r12, %rdi
movl %r14d, %esi
callq 0x89d4
leal -0x1(%r14), %r13d
movl %r14d, %ebp
cmpl 0x18(%rsp), %ebp
jge 0x52e7
movq %r12, %rdi
movl %r14d, %esi
callq 0x86de
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
incl %ebp
movq %r15, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x2e20
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x86de
movsd 0x28(%rsp), %xmm1
mulsd (%rax), %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movq %r12, %rdi
movl %r14d, %esi
callq 0x89d4
jmp 0x5288
movq %r12, %rdi
movl %r14d, %esi
callq 0x86de
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movl %r14d, %esi
movl %r14d, %edx
callq 0x2e20
movsd 0x8(%rsp), %xmm0
divsd (%rax), %xmm0
movq %r12, %rdi
movl %r14d, %esi
callq 0x89d4
movl %r13d, %r14d
jmp 0x525e
leaq 0x88(%rsp), %rdi
callq 0x8104
leaq 0x30(%rsp), %rdi
callq 0x2dea
leaq 0xd8(%rsp), %rdi
callq 0x2dea
leaq 0x138(%rsp), %rdi
callq 0x8104
leaq 0xa0(%rsp), %rdi
callq 0x2dea
movq %r12, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f99(%rip), %rdi      # 0x931a
leaq 0x3dc9(%rip), %rsi      # 0x9151
leaq 0x3fdb(%rip), %rcx      # 0x936a
movl $0x1e9, %edx            # imm = 0x1E9
callq 0x20b0
movq %rax, %rbx
jmp 0x54a3
jmp 0x5490
jmp 0x547c
movq %rax, %rbx
jmp 0x5541
movq %rax, %rbx
jmp 0x554e
jmp 0x5488
jmp 0x545d
jmp 0x5434
movq %rax, %rbx
jmp 0x5422
jmp 0x54fc
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x2dea
jmp 0x5451
movq %rax, %rbx
jmp 0x54d1
movq %rax, %rbx
jmp 0x5415
movq %rax, %rbx
jmp 0x54eb
jmp 0x54b4
movq %rax, %rbx
jmp 0x54c4
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x2dea
leaq 0xc8(%rsp), %rdi
callq 0x2dea
leaq 0xb8(%rsp), %rdi
callq 0x2dea
jmp 0x54de
movq %rax, %rbx
jmp 0x54de
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x2dea
jmp 0x54b7
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x54bf
jmp 0x5488
movq %rax, %rbx
jmp 0x5509
movq %rax, %rbx
jmp 0x551d
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0x5518
jmp 0x5490
movq %rax, %rbx
jmp 0x5534
jmp 0x5490
jmp 0x5498
movq %rax, %rbx
jmp 0x5513
movq %rax, %rbx
jmp 0x552a
movq %rax, %rbx
movq %r12, %rdi
callq 0x8104
leaq 0x88(%rsp), %rdi
callq 0x8104
jmp 0x552a
jmp 0x54b4
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x2dea
leaq 0xc8(%rsp), %rdi
callq 0x2dea
leaq 0x118(%rsp), %rdi
callq 0x8104
leaq 0x158(%rsp), %rdi
callq 0x2dea
leaq 0x128(%rsp), %rdi
callq 0x2dea
jmp 0x54ff
jmp 0x54fc
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2dea
leaq 0x58(%rsp), %rdi
callq 0x2dea
leaq 0x78(%rsp), %rdi
callq 0x2dea
leaq 0x88(%rsp), %rdi
callq 0x2dea
leaq 0x30(%rsp), %rdi
callq 0x2dea
leaq 0xd8(%rsp), %rdi
callq 0x2dea
leaq 0x138(%rsp), %rdi
callq 0x8104
leaq 0xa0(%rsp), %rdi
callq 0x2dea
movq %rbx, %rdi
callq 0x21d0