// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/RNBasics/RNSvd.cpp
// Function: gaps::RNSvdDecompose(int, int, double const*, double*, double*, double*)
void RNSvdDecompose(int m, int n, 
    const RNScalar *a,
    RNScalar *u, RNScalar *w, RNScalar *vt)
{
    int i, j, k, min = MIN(m,n);
    RNScalar *g, *p, wi;

    g = ALLOC2D(m,n);
    bidiagonalize(a,m,n,u,g,vt);


    bidiagonal_svd(g,m,n,u,vt);
    for (i=0; i<min; i++) w[i] = REF2D(g,m,n,i,i);

    /* insertion sort singular values.  Note that since
    ** the svd algorithm produces values sorted or nearly sorted,
    ** an insertion sort is efficient.
    */
    for (i=1; i<min; i++) {
	if (w[i] > w[i-1]) {
	    /* save w[i], and col i of u and row i of vt.  use "g" as buffer */
	    wi = w[i];
	    p=g;
	    for (k=0; k<m; k++) *p++ = REF2D(u,m,min,k,i);
	    for (k=0; k<n; k++) *p++ = REF2D(vt,min,n,i,k);
	    /* slide columns over */
	    for (j=i; j>0 && wi > w[j-1]; j--) {
		w[j] = w[j-1];
		for (k=0; k<m; k++) REF2D(u,m,min,k,j)=REF2D(u,m,min,k,j-1);
		for (k=0; k<n; k++) REF2D(vt,min,n,j,k)=REF2D(vt,min,n,j-1,k);
	    }
	    /* put original i stuff where we ended up */
	    w[j] = wi;
	    p=g;
	    for (k=0; k<m; k++) REF2D(u,m,min,k,j) = *p++;
	    for (k=0; k<n; k++) REF2D(vt,min,n,j,k) = *p++;
	}
    }
	    
    FREE(g);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r12
movq %r9, 0x10(%rsp)
movq %r8, %r14
movq %rcx, %rbp
movl %esi, %r13d
movl %edi, %r15d
cmpl %esi, %edi
movl %esi, %eax
cmovll %edi, %eax
movl %eax, 0x8(%rsp)
movq %rdx, (%rsp)
movl %esi, %edi
imull %r15d, %edi
shlq $0x3, %rdi
callq 0xe5e0
movq %rax, %rbx
movq (%rsp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movq %rbp, %rcx
movq %rax, %r8
movq %r12, %r9
callq 0x71b0a
movq %rbx, %rdi
movl %r15d, (%rsp)
movl %r15d, %esi
movq %r13, %r12
movl %r13d, %edx
movq %rbp, 0x20(%rsp)
movq %rbp, %rcx
movq %rbx, %r13
movq 0x10(%rsp), %r8
callq 0x71d9b
movl 0x8(%rsp), %esi
movl %esi, %ebp
testl %esi, %esi
jle 0x7216a
leal 0x1(%r12), %eax
xorl %ecx, %ecx
xorl %edx, %edx
movslq %ecx, %rcx
movsd (%r13,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
addl %eax, %ecx
cmpq %rdx, %rbp
jne 0x72150
cmpl $0x2, %esi
jl 0x72335
movq %r12, %rax
movslq %eax, %r15
movl (%rsp), %edx
movl %eax, %esi
movq 0x20(%rsp), %rax
leaq 0x8(%rax), %rcx
leaq (,%rbp,8), %r8
leaq (,%rsi,8), %rax
movq %rax, 0x38(%rsp)
movq 0x10(%rsp), %rdi
leaq (%rdi,%rsi,8), %r10
leaq (%rdi,%r15,8), %rax
movq %rax, 0x8(%rsp)
shlq $0x3, %r15
movq %r15, 0x40(%rsp)
negq %r15
movl $0x1, %eax
movq %rdi, 0x18(%rsp)
movq %r13, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movsd (%r14,%rax,8), %xmm0
ucomisd -0x8(%r14,%rax,8), %xmm0
jbe 0x72311
movq %r13, %rbp
movq %rdx, %rdi
movq %rcx, %r9
cmpl $0x0, (%rsp)
jle 0x72206
movsd (%r9), %xmm1
movsd %xmm1, (%rbp)
addq $0x8, %rbp
addq %r8, %r9
decq %rdi
jne 0x721f0
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r11
movq %rcx, %r13
movq %rax, %rbx
testl %r12d, %r12d
jle 0x72241
xorl %edi, %edi
movsd (%r10,%rdi,8), %xmm1
movsd %xmm1, (%rbp,%rdi,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x7221d
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r11
movq %rcx, %r13
movq %rax, %rbx
movsd -0x8(%r14,%rbx,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x722a5
movsd %xmm1, (%r14,%rbx,8)
movq %rdx, %rbp
movq %r13, %rdi
cmpl $0x0, (%rsp)
jle 0x72271
movsd -0x8(%rdi), %xmm1
movsd %xmm1, (%rdi)
addq %r8, %rdi
decq %rbp
jne 0x72260
testl %r12d, %r12d
jle 0x7228c
xorl %edi, %edi
movsd (%r9,%rdi,8), %xmm1
movsd %xmm1, (%r11,%rdi,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x72278
leaq -0x1(%rbx), %rdi
addq $-0x8, %r13
addq %r15, %r11
addq %r15, %r9
cmpq $0x2, %rbx
movq %rdi, %rbx
jge 0x72241
xorl %ebx, %ebx
movslq %ebx, %r9
movsd %xmm0, (%r14,%r9,8)
movq 0x30(%rsp), %r13
movq %r13, %rdi
cmpl $0x0, (%rsp)
movq 0x28(%rsp), %rbp
jle 0x722e5
movq 0x20(%rsp), %rdi
leaq (%rdi,%r9,8), %r9
movq %rdx, %r11
movq %r13, %rdi
movsd (%rdi), %xmm0
addq $0x8, %rdi
movsd %xmm0, (%r9)
addq %r8, %r9
decq %r11
jne 0x722d0
testl %r12d, %r12d
jle 0x72311
imull %r12d, %ebx
movslq %ebx, %r9
movq 0x10(%rsp), %r11
leaq (%r11,%r9,8), %r9
xorl %r11d, %r11d
movsd (%rdi,%r11,8), %xmm0
movsd %xmm0, (%r9,%r11,8)
incq %r11
cmpq %r11, %rsi
jne 0x722fd
incq %rax
addq $0x8, %rcx
addq 0x38(%rsp), %r10
movq 0x40(%rsp), %rdi
addq %rdi, 0x8(%rsp)
addq %rdi, 0x18(%rsp)
cmpq %rbp, %rax
jne 0x721ce
movq %r13, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe540