// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %r9, %r12
movq %r8, 0x28(%rsp)
movq %rcx, %rbp
movl %edx, %r13d
movl %esi, %r15d
movq %rdi, 0x20(%rsp)
cmpl %edx, %esi
movl %edx, %edi
cmovgl %esi, %edi
shlq $0x3, %rdi
callq 0xe5e0
movq %rax, 0x30(%rsp)
movl %r13d, %eax
movq %rbp, %rdi
cmpl %r13d, %r15d
cmovll %r15d, %eax
movl %eax, 0x14(%rsp)
jle 0x70b20
movl %r15d, %edi
imull %edi, %edi
shlq $0x3, %rdi
callq 0xe5e0
movq %rax, %rdi
movl %r13d, %ebx
imull %ebx, %ebx
shlq $0x3, %rbx
movq %r12, 0x40(%rsp)
cmpl %r13d, %r15d
movq %rdi, 0x18(%rsp)
jge 0x70b49
movq %rbx, %rdi
callq 0xe5e0
movq 0x18(%rsp), %rdi
movq %rax, %r12
movl %r15d, %edx
imull %edx, %edx
shlq $0x3, %rdx
xorl %r14d, %r14d
xorl %esi, %esi
callq 0xe260
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0xe260
movq 0x18(%rsp), %rsi
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
testl %r15d, %r15d
jle 0x70b94
leal 0x1(%r15), %ecx
movl %r15d, %edx
movslq %r14d, %r14
movq %rax, (%rsi,%r14,8)
addl %ecx, %r14d
decq %rdx
jne 0x70b85
testl %r13d, %r13d
jle 0x70bb0
leal 0x1(%r13), %ecx
movl %r13d, %edx
xorl %esi, %esi
movslq %esi, %rsi
movq %rax, (%r12,%rsi,8)
addl %ecx, %esi
decq %rdx
jne 0x70ba2
movq %rbp, 0x38(%rsp)
movq %r12, %r14
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %rsi
cmpq %rsi, %r12
je 0x70bdc
movl %r15d, %eax
imull %r13d, %eax
shll $0x3, %eax
movslq %eax, %rdx
movq %r12, %rdi
callq 0xe370
movl 0x14(%rsp), %ecx
testl %ecx, %ecx
movq %r15, %rbx
movq %r13, %r9
movq 0x30(%rsp), %rbp
movq %r14, 0x20(%rsp)
jle 0x70c4a
xorl %r15d, %r15d
movq %rbp, (%rsp)
movq %r12, %r13
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movl %r15d, %edx
movl %r15d, %ecx
movl %ebx, %r8d
movq %r9, %r12
callq 0x6fc71
leal 0x1(%r15), %r14d
movq %rbp, (%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movl %r15d, %edx
movl %r14d, %ecx
movl %ebx, %r8d
movl %r12d, %r9d
callq 0x6ffe1
movq %r12, %r9
movq %r13, %r12
movl 0x14(%rsp), %ecx
movl %r14d, %r15d
cmpl %r14d, %ecx
jne 0x70bf7
cmpl %r9d, %ebx
movq %rbx, %r12
movq 0x20(%rsp), %r15
movq %r9, %r13
jge 0x70c74
movq 0x28(%rsp), %rdi
xorl %esi, %esi
movl 0x14(%rsp), %edx
movq %r15, %rcx
movl %r13d, %r9d
callq 0x704be
movl 0x14(%rsp), %ecx
cmpl %r13d, %r12d
movq 0x30(%rsp), %r14
movq 0x40(%rsp), %rbx
movq 0x38(%rsp), %r10
jle 0x70ce3
testl %r12d, %r12d
jle 0x70cd5
movl %r12d, %eax
movl 0x14(%rsp), %ecx
leaq (,%rcx,8), %rdx
leaq (,%rax,8), %rsi
xorl %edi, %edi
movq 0x18(%rsp), %r8
testl %r13d, %r13d
jle 0x70cc7
xorl %r9d, %r9d
movsd (%r8,%r9,8), %xmm0
movsd %xmm0, (%r10,%r9,8)
incq %r9
cmpq %r9, %rcx
jne 0x70cb3
incq %rdi
addq %rdx, %r10
addq %rsi, %r8
cmpq %rax, %rdi
jne 0x70cab
movq 0x18(%rsp), %rdi
callq 0xe540
movl 0x14(%rsp), %ecx
cmpl %r13d, %r12d
jge 0x70d36
testl %r13d, %r13d
jle 0x70d2e
movl %r13d, %eax
movl %ecx, %ecx
leaq (,%rax,8), %rdx
xorl %esi, %esi
movq %r15, %rdi
testl %r12d, %r12d
jle 0x70d1e
movq %rcx, %r8
xorl %r9d, %r9d
movsd (%rdi,%r9), %xmm0
movsd %xmm0, (%rbx,%r9)
addq %rdx, %r9
decq %r8
jne 0x70d0a
incq %rsi
addq $0x8, %rbx
addq $0x8, %rdi
cmpq %rax, %rsi
jne 0x70cff
movq %r15, %rdi
callq 0xe540
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe540