// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/d_diagonalize.c
// Function: d_Diagonalize
void d_Diagonalize(int N, double **MATRIX, float *eigen_values,
		   float **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);

  // Change sign of eigenvectors
  for(j=1; j<=N; j++){
    double sum=0;
    for(i=1; i<=N; i++)sum+=eigen_vt[i][j];
    if(sum<0)for(i=1; i<=N; i++)eigen_vt[i][j]=-eigen_vt[i][j];
  }

  // 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, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %r15
movl %edi, %r13d
movl $0x0, 0x4(%rsp)
movslq %edi, %r14
leaq (,%r14,4), %rdi
callq 0x21d0
movq %rax, 0x20(%rsp)
movl $0x1, %edi
movq %r14, %rsi
callq 0x4aeb9
movq %rax, 0x18(%rsp)
movl $0x1, %edi
movq %r14, %rsi
callq 0x4aeb9
movq %rax, %rbp
movl $0x1, %edi
movl $0x1, %edx
movq %r14, %rsi
movq %r14, %rcx
callq 0x4afc2
movq %rax, %rbx
movl %r13d, %r12d
movq %r14, 0x10(%rsp)
testl %r14d, %r14d
jle 0x4a580
xorl %eax, %eax
movq 0x8(%rbx,%rax,8), %rcx
movq (%r15,%rax,8), %rdx
incq %rax
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x4a568
cmpq %r12, %rax
jne 0x4a55a
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movq 0x18(%rsp), %r14
movq %r14, %rcx
callq 0x4b5f0
movq %rbp, %rdi
movq %r14, %rsi
movl %r13d, %edx
movq %rbx, %rcx
callq 0x4c0d8
testl %eax, %eax
movq 0x20(%rsp), %r14
je 0x4a5be
movq $0x0, 0x8(%rsp)
movq %rbx, %r15
jmp 0x4a620
testl %r13d, %r13d
jle 0x4a5eb
xorl %eax, %eax
movq 0x8(%rbx,%rax,8), %rcx
movq (%r15,%rax,8), %rdx
incq %rax
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x4a5d3
cmpq %r12, %rax
jne 0x4a5c5
movl $0x1, %edi
movl $0x1, %edx
movq 0x10(%rsp), %rcx
movq %rcx, %rsi
callq 0x4afc2
movq %rax, %r15
leaq 0x4(%rsp), %r8
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x4b9a8
movq %r15, 0x8(%rsp)
movl (%rsp), %edx
testl %edx, %edx
js 0x4a63b
movq %rbp, %rdi
addq $0x8, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0x4a415
jmp 0x4a6b5
testl %r13d, %r13d
jle 0x4a669
leal 0x1(%r13), %eax
movl $0x1, %ecx
movapd 0x6a3f(%rip), %xmm0     # 0x51090
movsd (%rbp,%rcx,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4a651
testl %edx, %edx
setns (%rsp)
movq %rbp, %rdi
addq $0x8, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0x4a415
testl %r13d, %r13d
setle %al
orb (%rsp), %al
jne 0x4a6b5
leal 0x1(%r13), %eax
movl $0x1, %ecx
movapd 0x69f3(%rip), %xmm0     # 0x51090
movsd (%rbp,%rcx,8), %xmm1
xorpd %xmm0, %xmm1
movlpd %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4a69d
testl %r13d, %r13d
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r10
jle 0x4a720
leal 0x1(%r13), %eax
movl $0x1, %ecx
xorpd %xmm0, %xmm0
movapd 0x69b7(%rip), %xmm1     # 0x51090
movl $0x1, %edx
xorpd %xmm2, %xmm2
movq (%r15,%rdx,8), %rsi
addsd (%rsi,%rcx,8), %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x4a6e2
ucomisd %xmm2, %xmm0
jbe 0x4a718
movl $0x1, %edx
movq (%r15,%rdx,8), %rsi
movsd (%rsi,%rcx,8), %xmm2
xorpd %xmm1, %xmm2
movlpd %xmm2, (%rsi,%rcx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x4a6fe
incq %rcx
cmpq %rax, %rcx
jne 0x4a6d9
testl %r13d, %r13d
jle 0x4a7a2
decl %r13d
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movl %ecx, %esi
movl %r13d, %edi
movslq (%r14,%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
cvtsd2ss %xmm1, %xmm1
movslq %edi, %rsi
movss %xmm1, (%r10,%rsi,4)
movq (%r9,%rsi,8), %rsi
xorl %edi, %edi
movq 0x8(%r15,%rdi,8), %r8
xorps %xmm1, %xmm1
cvtsd2ss 0x8(%r8,%rdx,8), %xmm1
movss %xmm1, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %r12
jne 0x4a77e
incq %rax
cmpq %r12, %rax
jne 0x4a730
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 0x4a800
movl $0x1, %esi
movl $0x1, %ecx
movq %r14, %rdx
movq %r14, %r8
callq 0x4b4b1
movq 0x20(%rsp), %rdi
callq 0x2050
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq