// File: /workspace/github_c_makefile_star2_O3/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, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %r12
movl %edi, %r15d
movl $0x0, 0x14(%rsp)
movslq %edi, %rbx
leaq (,%rbx,4), %rdi
callq 0x21e0
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movq %rbx, %rsi
callq 0x4af55
movq %rax, %r14
movl $0x1, %edi
movq %rbx, %rsi
callq 0x4af55
movq %rax, %rbp
movl $0x1, %edi
movl $0x1, %edx
movq %rbx, %rsi
movq %rbx, %rcx
callq 0x4b04c
movq %rax, %r13
testl %ebx, %ebx
jle 0x4a600
movl %r15d, %eax
xorl %ecx, %ecx
movq 0x8(%r13,%rcx,8), %rdx
movq (%r12,%rcx,8), %rsi
incq %rcx
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, 0x8(%rdx,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x4a5e8
cmpq %rax, %rcx
jne 0x4a5da
movq %r13, %rdi
movl %r15d, %esi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x4b658
movq %rbp, %rdi
movq %r14, %rsi
movl %r15d, %edx
movq %r13, %rcx
callq 0x4c0cc
testl %eax, %eax
je 0x4a634
movq $0x0, 0x20(%rsp)
movq %r13, %r12
jmp 0x4a697
testl %r15d, %r15d
jle 0x4a664
movl %r15d, %eax
xorl %ecx, %ecx
movq 0x8(%r13,%rcx,8), %rdx
movq (%r12,%rcx,8), %rsi
incq %rcx
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, 0x8(%rdx,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x4a64c
cmpq %rax, %rcx
jne 0x4a63e
movl $0x1, %edi
movl $0x1, %edx
movq %rbx, %rsi
movq %rbx, %rcx
callq 0x4b04c
movq %rax, %r12
leaq 0x14(%rsp), %r8
movq %r13, %rdi
movl %r15d, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x4ba1c
movq %r12, 0x20(%rsp)
cmpl $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movq %r14, 0x28(%rsp)
js 0x4a7c1
movq %rbp, %rdi
addq $0x8, %rdi
movl %r15d, %esi
callq 0x4a499
movq 0x8(%rsp), %r10
movq 0x18(%rsp), %r11
testl %r15d, %r15d
jle 0x4a842
leal 0x1(%r15), %eax
movl $0x1, %ecx
xorpd %xmm0, %xmm0
movapd 0x69ad(%rip), %xmm1     # 0x51090
movl $0x1, %edx
xorpd %xmm2, %xmm2
movq (%r12,%rdx,8), %rsi
addsd (%rsi,%rcx,8), %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x4a6ec
ucomisd %xmm2, %xmm0
jbe 0x4a722
movl $0x1, %edx
movq (%r12,%rdx,8), %rsi
movsd (%rsi,%rcx,8), %xmm2
xorpd %xmm1, %xmm2
movlpd %xmm2, (%rsi,%rcx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x4a708
incq %rcx
cmpq %rax, %rcx
jne 0x4a6e3
testl %r15d, %r15d
jle 0x4a842
movl %r15d, %eax
decl %r15d
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movl %edx, %edi
movl %r15d, %r8d
movslq (%r10,%rcx,4), %rsi
movsd 0x8(%rbp,%rsi,8), %xmm1
xorpd %xmm2, %xmm2
cmpeqsd %xmm1, %xmm2
movq %xmm2, %r15
xorpd %xmm2, %xmm2
cmpneqsd %xmm1, %xmm2
movq %xmm2, %r9
subl %r9d, %edx
addl %r8d, %r15d
ucomisd %xmm1, %xmm0
cmovnel %edi, %r8d
cmovpl %edi, %r8d
cvtsd2ss %xmm1, %xmm1
movslq %r8d, %rdi
movq 0x30(%rsp), %r8
movss %xmm1, (%r8,%rdi,4)
movq (%r11,%rdi,8), %rdi
xorl %r8d, %r8d
movq 0x8(%r12,%r8,8), %r9
xorps %xmm1, %xmm1
cvtsd2ss 0x8(%r9,%rsi,8), %xmm1
movss %xmm1, (%rdi,%r8,4)
incq %r8
cmpq %r8, %rax
jne 0x4a797
incq %rcx
cmpq %rax, %rcx
jne 0x4a741
jmp 0x4a842
testl %r15d, %r15d
jle 0x4a833
leal 0x1(%r15), %r14d
movl $0x1, %eax
movsd (%rbp,%rax,8), %xmm0
xorpd 0x68b3(%rip), %xmm0     # 0x51090
movlpd %xmm0, (%rbp,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x4a7cf
movq %rbp, %rdi
addq $0x8, %rdi
movl %r15d, %esi
callq 0x4a499
movq 0x8(%rsp), %r10
testl %r15d, %r15d
movq 0x18(%rsp), %r11
jle 0x4a842
movl $0x1, %eax
movapd 0x687a(%rip), %xmm1     # 0x51090
movsd (%rbp,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, (%rbp,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x4a816
jmp 0x4a6c5
movq %rbp, %rdi
addq $0x8, %rdi
movl %r15d, %esi
callq 0x4a499
movl $0x1, %esi
movq 0x28(%rsp), %rdi
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 0x4a89b
movl $0x1, %esi
movl $0x1, %ecx
movq %rbx, %rdx
movq %rbx, %r8
callq 0x4b53b
movq 0x8(%rsp), %rdi
callq 0x2050
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq