// File: IlanPrice[P]Linear-Algebra-Library-in-Cplusplus/Matrix.cpp
// Function: backsubst(Matrix const&, Vector const&, int)
Vector backsubst(const Matrix& A, const Vector& b, int bwidth)
{
	int n = length(b);
	int count = 0;
	Vector x(n);
	if (bwidth==0)
	{
		for (int i=n;i>0;i--)
		{
			if (A(i,i)==0)
			{
				throw Exception("Singular Matrix",
						  "no solution to equation as coefficient matrix is singular");
			}
			x(i) = b(i);
			for (int j = (i+1); j<=n; j++)
			{
				x(i) = x(i) - A(i,j)*x(j);
				count++;
			}
			x(i) = x(i)/A(i,i);
		}
	}
	else
	{
		for (int i=n;i>0;i--)
		{
			if (A(i,i)==0)
			{
				throw Exception("Singular Matrix",
										  "no solution to equation as coefficient matrix is singular");
			}
			x(i) = b(i);
			for (int j = (i+1); j<=(i+bwidth - 1); j++)
				{
					if(j<=n)
					{
						x(i) = x(i) - A(i,j)*x(j);
						count++;
					}
				}
			x(i) = x(i)/A(i,i);
		}
	}
	std::cout<<"operation count: "<<count<<"\n";
	return x;
}

// Assembly:
subq $0x238, %rsp            # imm = 0x238
movq %rdi, 0x148(%rsp)
movq %rdi, %rax
movq %rax, 0x140(%rsp)
movq %rdi, 0x230(%rsp)
movq %rsi, 0x228(%rsp)
movq %rdx, 0x220(%rsp)
movl %ecx, 0x21c(%rsp)
movq 0x220(%rsp), %rdi
callq 0x4090
movq 0x148(%rsp), %rdi
movl %eax, 0x218(%rsp)
movl $0x0, 0x214(%rsp)
movb $0x0, 0x213(%rsp)
movl 0x218(%rsp), %esi
callq 0x2d70
cmpl $0x0, 0x21c(%rsp)
jne 0x9271
movl 0x218(%rsp), %eax
movl %eax, 0x20c(%rsp)
cmpl $0x0, 0x20c(%rsp)
jle 0x926c
movq 0x228(%rsp), %rdi
movl 0x20c(%rsp), %edx
movl %edx, %esi
callq 0x7250
movq %rax, 0x138(%rsp)
jmp 0x8e7e
movq 0x138(%rsp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x900f
jp 0x900f
movb $0x1, 0x1ae(%rsp)
movl $0x40, %edi
callq 0x2070
movq %rax, 0x128(%rsp)
leaq 0x1d7(%rsp), %rdi
movq %rdi, 0x130(%rsp)
callq 0x21c0
movq 0x130(%rsp), %rdx
leaq 0x9651(%rip), %rsi      # 0x1252c
leaq 0x1d8(%rsp), %rdi
callq 0x2110
jmp 0x8eea
leaq 0x1af(%rsp), %rdi
movq %rdi, 0x120(%rsp)
callq 0x21c0
movq 0x120(%rsp), %rdx
leaq 0x962e(%rip), %rsi      # 0x1253c
leaq 0x1b0(%rsp), %rdi
callq 0x2110
jmp 0x8f1d
movq 0x128(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
leaq 0x1b0(%rsp), %rdx
callq 0x4d60
jmp 0x8f3c
movq 0x128(%rsp), %rdi
movb $0x0, 0x1ae(%rsp)
leaq 0xce4d(%rip), %rsi      # 0x15da0
leaq -0x422a(%rip), %rdx     # 0x4d30
callq 0x2180
jmp 0x96de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
jmp 0x96c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
jmp 0x8fe4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
jmp 0x8fca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x2080
leaq 0x1af(%rsp), %rdi
callq 0x20f0
leaq 0x1d8(%rsp), %rdi
callq 0x2080
leaq 0x1d7(%rsp), %rdi
callq 0x20f0
testb $0x1, 0x1ae(%rsp)
jne 0x8ffd
jmp 0x900a
movq 0x128(%rsp), %rdi
callq 0x20a0
jmp 0x96c4
movq 0x220(%rsp), %rdi
movl 0x20c(%rsp), %esi
callq 0x3c10
movq %rax, 0x118(%rsp)
jmp 0x902d
movq 0x148(%rsp), %rdi
movq 0x118(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x108(%rsp)
movl 0x20c(%rsp), %esi
callq 0x3ee0
movq %rax, 0x110(%rsp)
jmp 0x9060
movq 0x110(%rsp), %rax
movsd 0x108(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x20c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a8(%rsp)
movl 0x1a8(%rsp), %eax
cmpl 0x218(%rsp), %eax
jg 0x91ae
movq 0x148(%rsp), %rdi
movl 0x20c(%rsp), %esi
callq 0x3ee0
movq %rax, 0x100(%rsp)
jmp 0x90b8
movq 0x100(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xf0(%rsp)
movq 0x228(%rsp), %rdi
movl 0x20c(%rsp), %esi
movl 0x1a8(%rsp), %edx
callq 0x7250
movq %rax, 0xf8(%rsp)
jmp 0x90f2
movq 0x148(%rsp), %rdi
movq 0xf8(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xe0(%rsp)
movl 0x1a8(%rsp), %esi
callq 0x3ee0
movq %rax, 0xe8(%rsp)
jmp 0x9125
movq 0x148(%rsp), %rdi
movsd 0xf0(%rsp), %xmm0
movsd 0xe0(%rsp), %xmm1
movq 0xe8(%rsp), %rax
movsd (%rax), %xmm2
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movl 0x20c(%rsp), %esi
callq 0x3ee0
movq %rax, 0xd8(%rsp)
jmp 0x9172
movq 0xd8(%rsp), %rax
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x214(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x214(%rsp)
movl 0x1a8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a8(%rsp)
jmp 0x9086
movq 0x148(%rsp), %rdi
movl 0x20c(%rsp), %esi
callq 0x3ee0
movq %rax, 0xc8(%rsp)
jmp 0x91cc
movq 0xc8(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x228(%rsp), %rdi
movl 0x20c(%rsp), %edx
movl %edx, %esi
callq 0x7250
movq %rax, 0xc0(%rsp)
jmp 0x9201
movq 0x148(%rsp), %rdi
movsd 0xb8(%rsp), %xmm0
movq 0xc0(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movl 0x20c(%rsp), %esi
callq 0x3ee0
movq %rax, 0xb0(%rsp)
jmp 0x9241
movq 0xb0(%rsp), %rax
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x20c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x20c(%rsp)
jmp 0x8e50
jmp 0x9652
movl 0x218(%rsp), %eax
movl %eax, 0x1a4(%rsp)
cmpl $0x0, 0x1a4(%rsp)
jle 0x9650
movq 0x228(%rsp), %rdi
movl 0x1a4(%rsp), %edx
movl %edx, %esi
callq 0x7250
movq %rax, 0xa0(%rsp)
jmp 0x92ad
movq 0xa0(%rsp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x9425
jp 0x9425
movb $0x1, 0x156(%rsp)
movl $0x40, %edi
callq 0x2070
movq %rax, 0x90(%rsp)
leaq 0x17f(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0x21c0
movq 0x98(%rsp), %rdx
leaq 0x9222(%rip), %rsi      # 0x1252c
leaq 0x180(%rsp), %rdi
callq 0x2110
jmp 0x9319
leaq 0x157(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x21c0
movq 0x88(%rsp), %rdx
leaq 0x91ff(%rip), %rsi      # 0x1253c
leaq 0x158(%rsp), %rdi
callq 0x2110
jmp 0x934c
movq 0x90(%rsp), %rdi
leaq 0x180(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0x4d60
jmp 0x936b
movq 0x90(%rsp), %rdi
movb $0x0, 0x156(%rsp)
leaq 0xca1e(%rip), %rsi      # 0x15da0
leaq -0x4659(%rip), %rdx     # 0x4d30
callq 0x2180
jmp 0x96de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
jmp 0x93fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
jmp 0x93e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x200(%rsp)
movl %eax, 0x1fc(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x2080
leaq 0x157(%rsp), %rdi
callq 0x20f0
leaq 0x180(%rsp), %rdi
callq 0x2080
leaq 0x17f(%rsp), %rdi
callq 0x20f0
testb $0x1, 0x156(%rsp)
jne 0x9413
jmp 0x9420
movq 0x90(%rsp), %rdi
callq 0x20a0
jmp 0x96c4
movq 0x220(%rsp), %rdi
movl 0x1a4(%rsp), %esi
callq 0x3c10
movq %rax, 0x80(%rsp)
jmp 0x9443
movq 0x148(%rsp), %rdi
movq 0x80(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x1a4(%rsp), %esi
callq 0x3ee0
movq %rax, 0x78(%rsp)
jmp 0x9470
movq 0x78(%rsp), %rax
movsd 0x70(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x1a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x150(%rsp)
movl 0x150(%rsp), %eax
movl 0x1a4(%rsp), %ecx
addl 0x21c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jg 0x95b0
movl 0x150(%rsp), %eax
cmpl 0x218(%rsp), %eax
jg 0x9598
movq 0x148(%rsp), %rdi
movl 0x1a4(%rsp), %esi
callq 0x3ee0
movq %rax, 0x68(%rsp)
jmp 0x94df
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x228(%rsp), %rdi
movl 0x1a4(%rsp), %esi
movl 0x150(%rsp), %edx
callq 0x7250
movq %rax, 0x60(%rsp)
jmp 0x9510
movq 0x148(%rsp), %rdi
movq 0x60(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0x150(%rsp), %esi
callq 0x3ee0
movq %rax, 0x50(%rsp)
jmp 0x953a
movq 0x148(%rsp), %rdi
movsd 0x58(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movq 0x50(%rsp), %rax
movsd (%rax), %xmm2
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x1a4(%rsp), %esi
callq 0x3ee0
movq %rax, 0x40(%rsp)
jmp 0x9578
movq 0x40(%rsp), %rax
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x214(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x214(%rsp)
jmp 0x959a
movl 0x150(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x150(%rsp)
jmp 0x9490
movq 0x148(%rsp), %rdi
movl 0x1a4(%rsp), %esi
callq 0x3ee0
movq %rax, 0x30(%rsp)
jmp 0x95cb
movq 0x30(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x228(%rsp), %rdi
movl 0x1a4(%rsp), %edx
movl %edx, %esi
callq 0x7250
movq %rax, 0x28(%rsp)
jmp 0x95f7
movq 0x148(%rsp), %rdi
movsd 0x20(%rsp), %xmm0
movq 0x28(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x1a4(%rsp), %esi
callq 0x3ee0
movq %rax, 0x18(%rsp)
jmp 0x962b
movq 0x18(%rsp), %rax
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x1a4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1a4(%rsp)
jmp 0x927f
jmp 0x9652
movq 0xc977(%rip), %rdi      # 0x15fd0
leaq 0x8f16(%rip), %rsi      # 0x12576
callq 0x20e0
movq %rax, 0x8(%rsp)
jmp 0x966c
movq 0x8(%rsp), %rdi
movl 0x214(%rsp), %esi
callq 0x2190
movq %rax, (%rsp)
jmp 0x9683
movq (%rsp), %rdi
leaq 0x8c1c(%rip), %rsi      # 0x122aa
callq 0x20e0
jmp 0x9695
movb $0x1, 0x213(%rsp)
testb $0x1, 0x213(%rsp)
jne 0x96b4
movq 0x148(%rsp), %rdi
callq 0x2ea0
movq 0x140(%rsp), %rax
addq $0x238, %rsp            # imm = 0x238
retq
movq 0x148(%rsp), %rdi
callq 0x2ea0
movq 0x200(%rsp), %rdi
callq 0x21b0
nop