// File: /workspace/github_c_makefile_star2_O0/olcaytaner[P]Isell/matrix.c
// Function: pseudoinverse
matrix pseudoinverse(matrix a, double range, int* newdim)
{
	/*!Last Changed 02.02.2005*/
	int i;
	matrix u, v, s, partv, partu, transu, result, tmpresult;
	double* w, sum, sumnow;
 u = matrix_copy(a);
	w = safemalloc(u.col * sizeof(double), "pseuodoinverse", 5);
	v = svdcmp(u, u.row, u.col, w);   
	sum = 0;
	for (i = 0; i < u.col; i++)
		 sum += w[i];
	*newdim = u.col;
	sumnow = 0;
	for (i = 0; i < u.col; i++)
	 {
		 sumnow += w[i];
			if (sumnow / sum > range)
			 {
				 *newdim = i + 1;
				 break;
			 }
	 }
	partv = matrix_partial(v, 0, a.row - 1, 0, *newdim - 1);
	matrix_free(v);
	partu = matrix_partial(u, 0, a.row - 1, 0, *newdim - 1);
	matrix_free(u);
	transu = matrix_transpose(partu);
	matrix_free(partu);
	s = identity(*newdim);
	for (i = 0; i < *newdim; i++)
		 s.values[i][i] = 1 / w[i];
 tmpresult = matrix_multiply(partv, s);
	matrix_free(partv);
	matrix_free(s);
	safe_free(w);
	result = matrix_multiply(tmpresult, transu);
	matrix_free(tmpresult);
	matrix_free(transu);
	return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x20(%rbp)
movq %rsi, -0x18(%rbp)
movsd %xmm0, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x58550
movq %rax, -0xd8(%rbp)
movq %rdx, -0xd0(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x40(%rbp)
movslq -0x44(%rbp), %rdi
shlq $0x3, %rdi
leaq 0x8e83c(%rip), %rsi     # 0xe6a48
movl $0x5, %edx
callq 0x52d70
movq %rax, -0xb8(%rbp)
movl -0x48(%rbp), %edx
movl -0x44(%rbp), %ecx
movq -0xb8(%rbp), %r8
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x585c0
movq %rax, -0xe8(%rbp)
movq %rdx, -0xe0(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc0(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x582a0
movq -0xb8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5826d
movl -0x44(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc8(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x58315
movq -0xb8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
divsd -0xc0(%rbp), %xmm0
ucomisd -0x28(%rbp), %xmm0
jbe 0x58308
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
jmp 0x58315
jmp 0x5830a
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x582bb
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movq -0x30(%rbp), %rax
movl (%rax), %r9d
subl $0x1, %r9d
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
xorl %r8d, %r8d
movl %r8d, %edx
callq 0x59de0
movq %rax, -0xf8(%rbp)
movq %rdx, -0xf0(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x59ea0
movl -0x20(%rbp), %ecx
subl $0x1, %ecx
movq -0x30(%rbp), %rax
movl (%rax), %r9d
subl $0x1, %r9d
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %r8d, %r8d
movl %r8d, %edx
callq 0x59de0
movq %rax, -0x108(%rbp)
movq %rdx, -0x100(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x59ea0
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x59ed0
movq %rax, -0x118(%rbp)
movq %rdx, -0x110(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x59ea0
movq -0x30(%rbp), %rax
movl (%rax), %edi
callq 0x59f70
movq %rax, -0x128(%rbp)
movq %rdx, -0x120(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x58485
movq -0xb8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsd 0x89bb8(%rip), %xmm0    # 0xe2018
divsd (%rax,%rcx,8), %xmm0
movq -0x60(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x34(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x58442
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x59fe0
movq %rax, -0x138(%rbp)
movq %rdx, -0x130(%rbp)
movups -0x138(%rbp), %xmm0
movaps %xmm0, -0xb0(%rbp)
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x59ea0
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x59ea0
movq -0xb8(%rbp), %rdi
callq 0x52e80
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0x90(%rbp), %rcx
callq 0x59fe0
movq %rax, -0x148(%rbp)
movq %rdx, -0x140(%rbp)
movups -0x148(%rbp), %xmm0
movaps %xmm0, -0x10(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x59ea0
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x59ea0
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopl (%rax)