// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/d_diagonalize.c
// Function: f_Diagonalize
void f_Diagonalize(int N, float **MATRIX, float *eigen_values,
		   float **eigen_vector, int SIGN)
{
  double **eigen_vt, *a; float *m;
  int jacobi_rotations=0, i, j, k, ik, 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 with tridimensional reduction
  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!)
    printf("tqli routine has failed, trying jacobi\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 with jacobi routine
    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:
  d_sort(eigen_va+1, N, i_rank);
  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
  ik=0; // int ilow=N-1;
  for(i=0; i<N; i++){
    //ir=i+1; k=ik; ik++;
    ir=i_rank[i]+1;
    /*if(select && (select[ir]==0)){
      k=ilow; ilow--; if(selected)selected[k]=0;
      }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 $0x48, %rsp
movl %r8d, %ebx
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, %r14
movl %edi, %r12d
movl $0x0, 0x1c(%rsp)
movslq %edi, %r13
leaq (,%r13,4), %rdi
callq 0x21e0
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movq %r13, %rsi
callq 0x4af55
movq %rax, 0x30(%rsp)
movl $0x1, %edi
movq %r13, %rsi
callq 0x4af55
movq %rax, %rbp
movl $0x1, %edi
movl $0x1, %edx
movq %r13, %rsi
movq %r13, %rcx
callq 0x4b04c
movq %rax, %r15
movq %r12, 0x8(%rsp)
movl %r12d, %r12d
movq %r13, 0x28(%rsp)
testl %r13d, %r13d
jle 0x4a2a4
xorl %eax, %eax
movaps 0x6dd5(%rip), %xmm0     # 0x51030
movq 0x8(%r15,%rax,8), %rcx
movq (%r14,%rax,8), %rdx
xorl %esi, %esi
testl %ebx, %ebx
js 0x4a282
xorps %xmm1, %xmm1
cvtss2sd (%rdx,%rsi,4), %xmm1
movsd %xmm1, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x4a26a
jmp 0x4a29c
movss (%rdx,%rsi,4), %xmm1
xorps %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd %xmm1, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x4a282
incq %rax
cmpq %r12, %rax
jne 0x4a25b
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
movq 0x30(%rsp), %r13
movq %r13, %rcx
callq 0x4b658
movq %rbp, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %r13
movl %r13d, %edx
movq %r15, %rcx
callq 0x4c0cc
testl %eax, %eax
je 0x4a2ea
movq $0x0, 0x20(%rsp)
movq %r13, %r14
movq %r15, %r13
jmp 0x4a387
leaq 0x13d30(%rip), %rdi     # 0x5e021
callq 0x2090
testl %r13d, %r13d
jle 0x4a34d
xorl %eax, %eax
movaps 0x6d2c(%rip), %xmm0     # 0x51030
movq 0x8(%r15,%rax,8), %rcx
movq (%r14,%rax,8), %rdx
xorl %esi, %esi
testl %ebx, %ebx
js 0x4a32b
xorps %xmm1, %xmm1
cvtss2sd (%rdx,%rsi,4), %xmm1
movsd %xmm1, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x4a313
jmp 0x4a345
movss (%rdx,%rsi,4), %xmm1
xorps %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
movsd %xmm1, 0x8(%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x4a32b
incq %rax
cmpq %r12, %rax
jne 0x4a304
movl $0x1, %edi
movl $0x1, %edx
movq 0x28(%rsp), %rcx
movq %rcx, %rsi
callq 0x4b04c
movq %rax, %r13
leaq 0x1c(%rsp), %r8
movq %r15, %rdi
movq 0x8(%rsp), %r14
movl %r14d, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x4ba1c
movq %r13, 0x20(%rsp)
movq %rbp, %rdi
addq $0x8, %rdi
movl %r14d, %esi
movq 0x10(%rsp), %rdx
callq 0x4a499
testl %ebx, %ebx
setns %al
testl %r14d, %r14d
setle %cl
orb %al, %cl
jne 0x4a3d0
leal 0x1(%r14), %eax
movl $0x1, %ecx
movaps 0x6cd6(%rip), %xmm0     # 0x51090
movsd (%rbp,%rcx,8), %xmm1
xorps %xmm0, %xmm1
movlps %xmm1, (%rbp,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x4a3ba
testl %r14d, %r14d
movq 0x10(%rsp), %r8
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %r10
jle 0x4a422
xorl %eax, %eax
movslq (%r8,%rax,4), %rcx
xorps %xmm0, %xmm0
cvtsd2ss 0x8(%rbp,%rcx,8), %xmm0
movss %xmm0, (%r10,%rax,4)
movq (%r9,%rax,8), %rdx
incq %rax
xorl %esi, %esi
movq 0x8(%r13,%rsi,8), %rdi
xorps %xmm0, %xmm0
cvtsd2ss 0x8(%rdi,%rcx,8), %xmm0
movss %xmm0, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %r12
jne 0x4a402
cmpq %r12, %rax
jne 0x4a3e6
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 %r15, %rdi
movq %rbx, %rdx
movq %rbx, %r8
callq 0x4b53b
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4a480
movl $0x1, %esi
movl $0x1, %ecx
movq %rbx, %rdx
movq %rbx, %r8
callq 0x4b53b
movq 0x10(%rsp), %rdi
callq 0x2050
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq