// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RNBasics/RNSvd.cpp
// Function: gaps::bidiagonalize(double const*, int, int, double*, double*, double*)
void bidiagonalize(const RNScalar *a,int m,int n,RNScalar *u,RNScalar *b,RNScalar *v)
{
    int i,j, min=MIN(m,n);
    RNScalar *usave = u, *vsave = v, *h = ALLOC1D(MAX(m,n));

    /* we need square matrices to accumulate the transformations */
    if (min != m) u = ALLOC2D(m,m);
    if (min != n) v = ALLOC2D(n,n);

    /* start off with u and v equal to the identity, and b equal to a */
    CLEAR2D(u,m,m);
    CLEAR2D(v,n,n);
    for (i=0; i<m; i++) REF2D(u,m,m,i,i) = 1;
    for (i=0; i<n; i++) REF2D(v,n,n,i,i) = 1;
    if (b != a) COPY2D(b,m,n,a);

    /* walk down the diagonal */
    for (i = 0; i<min; i++) {
	/* zero the entries below b[i,i] */
	householder_zero_col(b,u,i,i,m,n,h);
	/* zero the entries to the right of b[i,i+1] */
	householder_zero_row(b,v,i,i+1,m,n,h);
    }
    /* when m < n (matrix wider than tall), the above
       leaves a non-0 element in the [m-1,m]th spot.
       This is the bottom superdiagonal element of an (m+1)x(m+1);
       use the clear routine to get rid of it. */
    if (min != n)
	clr_bot_supdiag_elt(b,0,min,v,m,n,n);

    /* For non-square arrays, lop off the parts we don't need */
    if (min!=m) {
	for (i=0; i<m; i++) for (j=0; j<min; j++)
	    REF2D(usave,m,min,i,j)=REF2D(u,m,m,i,j);
	FREE(u);
    }
    if (min!=n) {
	for (i=0; i<n; i++) for (j=0; j<min; j++)
	    REF2D(vsave,min,n,j,i)=REF2D(v,n,n,j,i);
	FREE(v);
    }
    FREE(h);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, %rbp
movl %edx, %r15d
movl %esi, %r14d
movq %rdi, 0x20(%rsp)
cmpl %edx, %esi
movl %edx, %edi
cmovgl %esi, %edi
shlq $0x3, %rdi
callq 0xe600
movq %rax, 0x40(%rsp)
movl %r15d, %eax
movq %rbp, %rbx
cmpl %r15d, %r14d
cmovll %r14d, %eax
movl %eax, 0x14(%rsp)
movq %r14, 0x8(%rsp)
jle 0x6730f
movl %r14d, %edi
imull %edi, %edi
shlq $0x3, %rdi
callq 0xe600
movq 0x8(%rsp), %r14
movq %rax, %rbx
movl %r15d, %r13d
imull %r13d, %r13d
shlq $0x3, %r13
movq 0x30(%rsp), %r12
cmpl %r15d, %r14d
jge 0x67334
movq %r13, %rdi
callq 0xe600
movq 0x8(%rsp), %r14
movq %rax, %r12
movq %r13, 0x18(%rsp)
movq %r14, %r13
movl %r13d, %edx
imull %edx, %edx
shlq $0x3, %rdx
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe270
movq %r12, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0xe270
leal 0x1(%r13), %ecx
testl %r13d, %r13d
movl $0x0, %edx
cmovgl %r13d, %edx
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rdx, 0x50(%rsp)
subq $0x1, %rdx
jb 0x67393
movslq %r14d, %r14
movq %rax, (%rbx,%r14,8)
addl %ecx, %r14d
jmp 0x67381
leal 0x1(%r15), %ecx
xorl %edx, %edx
testl %r15d, %r15d
movl $0x0, %esi
cmovgl %r15d, %esi
movq %rsi, 0x48(%rsp)
movq 0x28(%rsp), %rdi
subq $0x1, %rsi
jb 0x673c0
movslq %edx, %rdx
movq %rax, (%r12,%rdx,8)
addl %ecx, %edx
jmp 0x673af
movq 0x20(%rsp), %rsi
cmpq %rsi, %rdi
je 0x673de
movq 0x8(%rsp), %rax
imull %r15d, %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0xe380
movq %r12, 0x38(%rsp)
movq %r15, 0x18(%rsp)
xorl %r14d, %r14d
movl 0x14(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movl %ecx, 0x20(%rsp)
movq %rbx, 0x60(%rsp)
movq %rbp, 0x58(%rsp)
movq 0x40(%rsp), %r12
movq 0x18(%rsp), %r13
cmpl %r14d, 0x20(%rsp)
je 0x6746d
movq %r12, (%rsp)
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %r14d, %ecx
movq 0x8(%rsp), %rbx
movl %ebx, %r8d
movl %r13d, %r9d
callq 0x666c1
leal 0x1(%r14), %r15d
movq %r12, (%rsp)
movq %rbp, %rdi
movq 0x58(%rsp), %rbp
movq 0x38(%rsp), %rsi
movl %r14d, %edx
movl %r15d, %ecx
movl %ebx, %r8d
movq 0x60(%rsp), %rbx
movl %r13d, %r9d
callq 0x66942
movl %r15d, %r14d
jmp 0x67411
movq 0x18(%rsp), %r15
movq 0x8(%rsp), %rcx
cmpl %r15d, %ecx
movq 0x38(%rsp), %r12
jge 0x6749c
movq 0x28(%rsp), %rdi
xorl %esi, %esi
movl 0x14(%rsp), %edx
movq %r12, %rcx
movl %r15d, %r9d
callq 0x66dcd
movq 0x8(%rsp), %rcx
movl 0x20(%rsp), %r14d
cmpl %r15d, %ecx
jle 0x674f1
movslq 0x14(%rsp), %rax
movslq %ecx, %rcx
shlq $0x3, %rax
shlq $0x3, %rcx
xorl %edx, %edx
movq %rbx, %rsi
cmpq 0x50(%rsp), %rdx
je 0x674e4
xorl %edi, %edi
cmpq %rdi, %r14
je 0x674d9
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rbp,%rdi,8)
incq %rdi
jmp 0x674c4
incq %rdx
addq %rax, %rbp
addq %rcx, %rsi
jmp 0x674bb
movq %rbx, %rdi
callq 0xe560
movq 0x8(%rsp), %rcx
cmpl %r15d, %ecx
jge 0x67541
movslq %r15d, %rax
shlq $0x3, %rax
xorl %ecx, %ecx
movq %r12, %rdx
movq 0x30(%rsp), %r8
movq 0x48(%rsp), %r9
cmpq %r9, %rcx
je 0x67539
xorl %esi, %esi
movq %r14, %rdi
subq $0x1, %rdi
jb 0x6752c
movsd (%rdx,%rsi), %xmm0
movsd %xmm0, (%r8,%rsi)
addq %rax, %rsi
jmp 0x67516
incq %rcx
addq $0x8, %rdx
addq $0x8, %r8
jmp 0x6750c
movq %r12, %rdi
callq 0xe560
movq 0x40(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe560