// File: /workspace/github_c_makefile_star2_O3/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, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movl %edi, %ebx
movl $0x0, 0xc(%rsp)
movslq %edi, %r12
leaq (,%r12,4), %rdi
callq 0x21e0
movq %rax, (%rsp)
movl $0x1, %edi
movq %r12, %rsi
callq 0x4af55
movq %rax, 0x30(%rsp)
movl $0x1, %edi
movq %r12, %rsi
callq 0x4af55
movq %rax, %rbp
movl $0x1, %edi
movl $0x1, %edx
movq %r12, %rsi
movq %r12, %rcx
callq 0x4b04c
movq %rax, %r13
movl %ebx, %r15d
testl %r12d, %r12d
jle 0x4a95c
xorl %eax, %eax
movq 0x8(%r13,%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 0x4a944
cmpq %r15, %rax
jne 0x4a936
movq %r12, 0x28(%rsp)
movq %r13, %rdi
movl %ebx, %esi
movq %rbp, %rdx
movq 0x30(%rsp), %r12
movq %r12, %rcx
callq 0x4b658
movq %rbp, %rdi
movq %r12, %rsi
movl %ebx, %edx
movq %r13, %rcx
callq 0x4c0cc
testl %eax, %eax
je 0x4a998
movq $0x0, 0x20(%rsp)
movq %r13, %r14
jmp 0x4a9fb
testl %ebx, %ebx
movq 0x28(%rsp), %r12
jle 0x4a9c9
xorl %eax, %eax
movq 0x8(%r13,%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 0x4a9b1
cmpq %r15, %rax
jne 0x4a9a3
movl $0x1, %edi
movl $0x1, %edx
movq %r12, %rsi
movq %r12, %rcx
callq 0x4b04c
movq %rax, %r14
leaq 0xc(%rsp), %r8
movq %r13, %rdi
movl %ebx, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x4ba1c
movq %r14, 0x20(%rsp)
cmpl $0x0, 0x8(%rsp)
movq (%rsp), %rdx
js 0x4aaa6
movq %rbp, %rdi
addq $0x8, %rdi
movl %ebx, %esi
callq 0x4a499
movq (%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
testl %ebx, %ebx
jle 0x4ab27
decl %ebx
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movl %ecx, %esi
movl %ebx, %edi
movslq (%r9,%rax,4), %rdx
movsd 0x8(%rbp,%rdx,8), %xmm1
xorpd %xmm2, %xmm2
cmpeqsd %xmm1, %xmm2
movq %xmm2, %rbx
xorpd %xmm2, %xmm2
cmpneqsd %xmm1, %xmm2
movq %xmm2, %r8
subl %r8d, %ecx
addl %edi, %ebx
ucomisd %xmm1, %xmm0
cmovnel %esi, %edi
cmovpl %esi, %edi
movslq %edi, %rsi
movsd %xmm1, (%r11,%rsi,8)
movq (%r10,%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 0x4aa80
incq %rax
cmpq %r15, %rax
jne 0x4aa38
jmp 0x4ab27
testl %ebx, %ebx
jle 0x4ab19
leal 0x1(%rbx), %r12d
movl $0x1, %eax
movsd (%rbp,%rax,8), %xmm0
xorpd 0x65cf(%rip), %xmm0     # 0x51090
movlpd %xmm0, (%rbp,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x4aab3
movq %rbp, %rdi
addq $0x8, %rdi
movl %ebx, %esi
callq 0x4a499
movq (%rsp), %r9
testl %ebx, %ebx
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r11
jle 0x4ab27
movl $0x1, %eax
movapd 0x6594(%rip), %xmm1     # 0x51090
movsd (%rbp,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, (%rbp,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x4aafc
jmp 0x4aa26
movq %rbp, %rdi
addq $0x8, %rdi
movl %ebx, %esi
callq 0x4a499
movl $0x1, %esi
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rdx
callq 0x4b50b
movl $0x1, %esi
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x4b50b
movl $0x1, %esi
movl $0x1, %ecx
movq %r13, %rdi
movq %rbx, %rdx
movq %rbx, %r8
callq 0x4b53b
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4ab85
movl $0x1, %esi
movl $0x1, %ecx
movq %rbx, %rdx
movq %rbx, %r8
callq 0x4b53b
movq (%rsp), %rdi
callq 0x2050
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq