// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/d_diagonalize.c
// Function: dd_Diagonalize
void dd_Diagonalize(int N, double **MATRIX, double *eigen_values,
		    double **eigen_vector, int SIGN)
{
  double **eigen_vt, *a; double *m;
  int jacobi_rotations=0, i, j, k, ik, ilow, ir;
  int *i_rank=malloc(N*sizeof(int));

  double *e_vector= dvector( (long)1, (long)N);
  double *eigen_va= dvector( (long)1, (long)N);
  double **a_matrix= dmatrix( (long)1, (long)N, (long)1, (long)N);
  double **v_matrix= NULL;

  /* fill matrix a_matrix. Indeces from 1 to N! */
  for(i=0; i<N; i++){
    a=a_matrix[i+1]; m=MATRIX[i];
    for(j=0; j<N; j++)a[j+1]= m[j];
    /* if(SIGN<0){for(j=0; j<N; j++)a[j+1]=-m[j];}
       else{for(j=0; j<N; j++)a[j+1]= m[j];}*/
  }

  /* obtain eigensystem */
  tred2( a_matrix, (int)N, eigen_va, e_vector);
  if( tqli(eigen_va, e_vector, (int)N, a_matrix)){
    eigen_vt=a_matrix;
  }else{
    /* tqli failed */
    /* reconstruct matrix a_matrix (destroyed by tred2/tqli!) */
    for(i=0; i<N; i++){
      a=a_matrix[i+1]; m=MATRIX[i];
      for(j=0; j<N; j++)a[j+1]= m[j];
      /* if(SIGN<0){for(j=0; j<N; j++)a[j+1]=-m[j];}
	 else{for(j=0; j<N; j++)a[j+1]= m[j];}*/
    }

    /* obtain eigensystem */
    v_matrix=dmatrix( (long)1, (long)N, (long)1, (long)N);
    jacobi( a_matrix, N, eigen_va, v_matrix, &jacobi_rotations);
    eigen_vt=v_matrix;
  }

  // Sort:
  if(SIGN<0) for(j=1; j<=N; j++)eigen_va[j]=-eigen_va[j];
  d_sort(eigen_va+1, N, i_rank);
  if(SIGN<0) for(j=1; j<=N; j++)eigen_va[j]=-eigen_va[j];
  // eigsrt(eigen_va, eigen_vt, N);

  // Transpose and shift indices. Omit lambda < E_THR
  float E_THR=0;
  ik=0; ilow=N-1;
  for(i=0; i<N; i++){
    //ir=i+1;  k=ik; ik++;
    ir=i_rank[i]+1;
    if(fabs(eigen_va[ir])<=E_THR){k=ilow; ilow--;}
    else{k=ik; ik++;}
    eigen_values[k]=eigen_va[ir];
    for(j=1; j<=N; j++){
      eigen_vector[k][j-1]=eigen_vt[j][ir];
    }
  }

  free_dvector(e_vector, 1, N);
  free_dvector(eigen_va, 1, N);
  free_dmatrix(a_matrix, 1, N, 1, N);
  if(v_matrix)free_dmatrix(v_matrix, 1, N, 1, N);
  free(i_rank);

  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, (%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r14
movl %edi, %r13d
movl $0x0, 0x4(%rsp)
movslq %edi, %r12
leaq (,%r12,4), %rdi
callq 0x21d0
movq %rax, 0x30(%rsp)
movl $0x1, %edi
movq %r12, %rsi
callq 0x4aeb9
movq %rax, 0x18(%rsp)
movl $0x1, %edi
movq %r12, %rsi
callq 0x4aeb9
movq %rax, %rbp
movl $0x1, %edi
movl $0x1, %edx
movq %r12, %rsi
movq %r12, %rcx
callq 0x4afc2
movq %rax, %rbx
movl %r13d, %r15d
movq %r12, 0x10(%rsp)
testl %r12d, %r12d
jle 0x4a8c7
xorl %eax, %eax
movq 0x8(%rbx,%rax,8), %rcx
movq (%r14,%rax,8), %rdx
incq %rax
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r15
jne 0x4a8af
cmpq %r15, %rax
jne 0x4a8a1
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movq 0x18(%rsp), %r12
movq %r12, %rcx
callq 0x4b5f0
movq %rbp, %rdi
movq %r12, %rsi
movl %r13d, %edx
movq %rbx, %rcx
callq 0x4c0d8
testl %eax, %eax
movq 0x30(%rsp), %r12
je 0x4a905
movq $0x0, 0x8(%rsp)
movq %rbx, %r14
jmp 0x4a967
testl %r13d, %r13d
jle 0x4a932
xorl %eax, %eax
movq 0x8(%rbx,%rax,8), %rcx
movq (%r14,%rax,8), %rdx
incq %rax
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r15
jne 0x4a91a
cmpq %r15, %rax
jne 0x4a90c
movl $0x1, %edi
movl $0x1, %edx
movq 0x10(%rsp), %rcx
movq %rcx, %rsi
callq 0x4afc2
movq %rax, %r14
leaq 0x4(%rsp), %r8
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x4b9a8
movq %r14, 0x8(%rsp)
movl (%rsp), %edx
testl %edx, %edx
js 0x4a982
movq %rbp, %rdi
addq $0x8, %rdi
movl %r13d, %esi
movq %r12, %rdx
callq 0x4a415
jmp 0x4a9fc
testl %r13d, %r13d
jle 0x4a9b0
leal 0x1(%r13), %eax
movl $0x1, %ecx
movapd 0x66f8(%rip), %xmm0     # 0x51090
movsd (%rbp,%rcx,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4a998
testl %edx, %edx
setns (%rsp)
movq %rbp, %rdi
addq $0x8, %rdi
movl %r13d, %esi
movq %r12, %rdx
callq 0x4a415
testl %r13d, %r13d
setle %al
orb (%rsp), %al
jne 0x4a9fc
leal 0x1(%r13), %eax
movl $0x1, %ecx
movapd 0x66ac(%rip), %xmm0     # 0x51090
movsd (%rbp,%rcx,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4a9e4
testl %r13d, %r13d
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %r10
jle 0x4aa81
decl %r13d
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movl %ecx, %esi
movl %r13d, %edi
movslq (%r12,%rax,4), %rdx
movsd 0x8(%rbp,%rdx,8), %xmm1
xorpd %xmm2, %xmm2
cmpeqsd %xmm1, %xmm2
movq %xmm2, %r13
xorpd %xmm2, %xmm2
cmpneqsd %xmm1, %xmm2
movq %xmm2, %r8
subl %r8d, %ecx
addl %edi, %r13d
ucomisd %xmm1, %xmm0
cmovnel %esi, %edi
cmovpl %esi, %edi
movslq %edi, %rsi
movsd %xmm1, (%r10,%rsi,8)
movq (%r9,%rsi,8), %rsi
xorl %edi, %edi
movq 0x8(%r14,%rdi,8), %r8
movsd 0x8(%r8,%rdx,8), %xmm1
movsd %xmm1, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %r15
jne 0x4aa60
incq %rax
cmpq %r15, %rax
jne 0x4aa16
movl $0x1, %esi
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r14
movq %r14, %rdx
callq 0x4b481
movl $0x1, %esi
movq %rbp, %rdi
movq %r14, %rdx
callq 0x4b481
movl $0x1, %esi
movl $0x1, %ecx
movq %rbx, %rdi
movq %r14, %rdx
movq %r14, %r8
callq 0x4b4b1
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x4aadf
movl $0x1, %esi
movl $0x1, %ecx
movq %r14, %rdx
movq %r14, %r8
callq 0x4b4b1
movq %r12, %rdi
callq 0x2050
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq