// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movl %r8d, 0x4(%rsp)
movq %rcx, %r15
movq %rdx, 0x30(%rsp)
andl $0x0, 0x24(%rsp)
movq %rsi, %r12
movl %edi, %r13d
movslq %edi, %rbx
leaq (,%rbx,4), %rdi
callq 0x21d0
movq %rax, 0x18(%rsp)
pushq $0x1
popq %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x44ea8
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x44ea8
movq %rax, %rbp
movq %r14, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x44f9e
xorl %edx, %edx
testl %ebx, %ebx
movl $0x0, %r14d
cmovgl %r13d, %r14d
movq %rbx, 0x28(%rsp)
movl %ebx, %ebx
cmpq %r14, %rdx
je 0x44252
leaq 0x1(%rdx), %rdi
movq 0x8(%rax,%rdx,8), %rcx
movq (%r12,%rdx,8), %rdx
cmpl $0x0, 0x4(%rsp)
js 0x4422b
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x4424d
cvtss2sd (%rdx,%rsi,4), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
jmp 0x44216
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x4424d
movss (%rdx,%rsi,4), %xmm0
xorps 0xc622(%rip), %xmm0     # 0x50860
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
jmp 0x4422d
movq %rdi, %rdx
jmp 0x441fb
movq %rax, %rdi
movq %rax, 0x8(%rsp)
movl %r13d, %esi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
callq 0x45550
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movl %r13d, %edx
movq 0x8(%rsp), %rcx
callq 0x45e80
testl %eax, %eax
je 0x44290
xorl %r12d, %r12d
movq 0x8(%rsp), %rbx
jmp 0x4432f
leaq 0x13cfa(%rip), %rdi     # 0x57f91
callq 0x2090
xorl %edx, %edx
movq 0x8(%rsp), %rdi
movaps 0xc5b6(%rip), %xmm1     # 0x50860
cmpq %r14, %rdx
je 0x442fd
leaq 0x1(%rdx), %rax
movq 0x8(%rdi,%rdx,8), %rcx
movq (%r12,%rdx,8), %rdx
cmpl $0x0, 0x4(%rsp)
js 0x442da
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x442f8
cvtss2sd (%rdx,%rsi,4), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
jmp 0x442c5
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x442f8
movss (%rdx,%rsi,4), %xmm0
xorps %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
jmp 0x442dc
movq %rax, %rdx
jmp 0x442aa
movq %rdi, %r12
pushq $0x1
popq %rdi
movq 0x28(%rsp), %rcx
movq %rcx, %rsi
movq %rdi, %rdx
callq 0x44f9e
movq %rax, %rbx
leaq 0x24(%rsp), %r8
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x458f4
movq %rbx, %r12
movq %rbp, %rdi
addq $0x8, %rdi
movl %r13d, %esi
movq 0x18(%rsp), %rdx
callq 0x4442d
cmpl $0x0, 0x4(%rsp)
movq 0x30(%rsp), %r8
js 0x44408
incl %r13d
decq %r13
xorl %eax, %eax
movq 0x18(%rsp), %rdi
cmpq %r14, %rax
je 0x4439a
movslq (%rdi,%rax,4), %rcx
cvtsd2ss 0x8(%rbp,%rcx,8), %xmm0
movss %xmm0, (%r8,%rax,4)
xorl %edx, %edx
cmpq %rdx, %r13
je 0x44395
movq 0x8(%rbx,%rdx,8), %rsi
cvtsd2ss 0x8(%rsi,%rcx,8), %xmm0
movq (%r15,%rax,8), %rsi
movss %xmm0, (%rsi,%rdx,4)
incq %rdx
jmp 0x44377
incq %rax
jmp 0x44360
pushq $0x1
popq %rbx
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq 0x28(%rsp), %r14
movq %r14, %rdx
callq 0x45405
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x45405
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r14, %r8
callq 0x45435
testq %r12, %r12
je 0x443ef
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %rcx, %rsi
movq %r14, %rdx
movq %r14, %r8
callq 0x45435
movq 0x18(%rsp), %rdi
callq 0x2050
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
movaps 0x6caf(%rip), %xmm0     # 0x4b0c0
cmpq %rax, %r14
je 0x44353
movsd 0x8(%rbp,%rax,8), %xmm1
xorps %xmm0, %xmm1
movlps %xmm1, 0x8(%rbp,%rax,8)
incq %rax
jmp 0x44411