// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %r14
movq %rcx, %rbp
movq %rcx, 0x10(%rsp)
movl %esi, %r13d
movl %edi, %ebx
cmpl %esi, %edi
movl %esi, %r12d
cmovll %edi, %r12d
movq %rdx, 0x8(%rsp)
movl %esi, %edi
imull %ebx, %edi
shlq $0x3, %rdi
callq 0xe600
movq %rax, %r15
movq 0x8(%rsp), %rdi
movl %ebx, %esi
movl %r13d, %edx
movq %rbp, %rcx
movq %rax, %r8
movq 0x18(%rsp), %rbp
movq %rbp, %r9
callq 0x672a5
movq %r15, %rdi
movl %ebx, %esi
movl %r13d, %edx
movq 0x10(%rsp), %rcx
movq %rbp, %r8
callq 0x67559
movq %r15, %rdi
movq %r13, 0x20(%rsp)
leal 0x1(%r13), %eax
xorl %ecx, %ecx
testl %r12d, %r12d
movl $0x0, %edx
cmovgl %r12d, %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x679a0
movslq %ecx, %rcx
movsd (%rdi,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rsi,8)
incq %rsi
addl %eax, %ecx
jmp 0x67986
movslq %r12d, %r10
xorl %ecx, %ecx
testl %ebx, %ebx
cmovlel %ecx, %ebx
movq 0x20(%rsp), %rax
testl %eax, %eax
cmovgl %eax, %ecx
movslq %eax, %r11
movq 0x10(%rsp), %rax
leaq 0x8(%rax), %rdx
leaq (,%r10,8), %rbp
movq 0x18(%rsp), %rax
leaq (%rax,%r11,8), %r12
shlq $0x3, %r11
movq %r11, %r9
negq %r9
pushq $0x1
popq %r15
movq %rax, 0x8(%rsp)
movq %r10, 0x30(%rsp)
movq %r11, 0x28(%rsp)
cmpq %r10, %r15
jge 0x67b25
movsd (%r14,%r15,8), %xmm0
ucomisd -0x8(%r14,%r15,8), %xmm0
jbe 0x67b11
movq %rdx, %r8
movq %rbx, %r10
movq %rdi, %rsi
subq $0x1, %r10
jb 0x67a2b
movsd (%r8), %xmm1
movsd %xmm1, (%rsi)
addq $0x8, %rsi
addq %rbp, %r8
jmp 0x67a13
xorl %eax, %eax
cmpq %rax, %rcx
je 0x67a42
movsd (%r12,%rax,8), %xmm1
movsd %xmm1, (%rsi,%rax,8)
incq %rax
jmp 0x67a2d
movq 0x8(%rsp), %r11
movq %r12, %r8
movq %rdx, %rax
movq %r15, %r10
testq %r10, %r10
jle 0x67aa9
movsd -0x8(%r14,%r10,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x67aac
movsd %xmm1, (%r14,%r10,8)
decq %r10
movq %rax, %rsi
movq %rbx, %r13
subq $0x1, %r13
jb 0x67a85
movsd -0x8(%rsi), %xmm1
movsd %xmm1, (%rsi)
addq %rbp, %rsi
jmp 0x67a71
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x67a9d
movsd (%r11,%rsi,8), %xmm1
movsd %xmm1, (%r8,%rsi,8)
incq %rsi
jmp 0x67a87
addq $-0x8, %rax
addq %r9, %r8
addq %r9, %r11
jmp 0x67a50
xorl %r10d, %r10d
movslq %r10d, %rax
movsd %xmm0, (%r14,%rax,8)
movq 0x10(%rsp), %rsi
leaq (%rsi,%rax,8), %r8
movq %rbx, %r11
movq %rdi, %rsi
subq $0x1, %r11
jb 0x67adc
movsd (%rsi), %xmm0
addq $0x8, %rsi
movsd %xmm0, (%r8)
addq %rbp, %r8
jmp 0x67ac4
imull 0x20(%rsp), %r10d
movslq %r10d, %rax
movq 0x18(%rsp), %r8
leaq (%r8,%rax,8), %rax
xorl %r8d, %r8d
movq 0x30(%rsp), %r10
movq 0x28(%rsp), %r11
cmpq %r8, %rcx
je 0x67b11
movsd (%rsi,%r8,8), %xmm0
movsd %xmm0, (%rax,%r8,8)
incq %r8
jmp 0x67afb
incq %r15
addq $0x8, %rdx
addq %r11, %r12
addq %r11, 0x8(%rsp)
jmp 0x679ee
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe560