// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %r8d, 0x20(%rsp)
movq %rcx, %r15
movq %rdx, 0x40(%rsp)
andl $0x0, 0x24(%rsp)
movq %rsi, %r12
movl %edi, %r13d
movslq %edi, %rbx
leaq (,%rbx,4), %rdi
callq 0x21d0
movq %rax, 0x10(%rsp)
pushq $0x1
popq %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x44ea8
movq %rax, 0x18(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x44ea8
movq %rax, %rbp
movq %rax, 0x38(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x44f9e
xorl %esi, %esi
testl %ebx, %ebx
movl $0x0, %r14d
cmovgl %r13d, %r14d
movq %rbx, 0x30(%rsp)
movl %ebx, %ebx
cmpq %r14, %rsi
je 0x448db
leaq 0x1(%rsi), %rcx
movq 0x8(%rax,%rsi,8), %rdx
movq (%r12,%rsi,8), %rdi
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x448d6
movsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rdx,%rsi,8)
incq %rsi
jmp 0x448c1
movq %rcx, %rsi
jmp 0x448ad
movq %rax, %rdi
movq %rax, 0x8(%rsp)
movl %r13d, %esi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
callq 0x45550
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %edx
movq 0x8(%rsp), %rcx
callq 0x45e80
movq 0x8(%rsp), %rdi
testl %eax, %eax
je 0x4491f
movq $0x0, 0x28(%rsp)
movq %rdi, %r12
jmp 0x44985
xorl %edx, %edx
cmpq %r14, %rdx
je 0x4494f
leaq 0x1(%rdx), %rax
movq 0x8(%rdi,%rdx,8), %rcx
movq (%r12,%rdx,8), %rdx
xorl %esi, %esi
cmpq %rsi, %rbx
je 0x4494a
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
jmp 0x44935
movq %rax, %rdx
jmp 0x44921
pushq $0x1
popq %rdi
movq 0x30(%rsp), %rbx
movq %rbx, %rsi
movq %rdi, %rdx
movq %rbx, %rcx
callq 0x44f9e
movq %rax, %r12
leaq 0x24(%rsp), %r8
movq 0x8(%rsp), %rdi
movl %r13d, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x458f4
movq %r12, 0x28(%rsp)
cmpl $0x0, 0x20(%rsp)
movq 0x38(%rsp), %rdi
js 0x44aa6
addq $0x8, %rdi
movl %r13d, %esi
movq 0x10(%rsp), %rdx
callq 0x4442d
leal -0x1(%r13), %eax
incl %r13d
decq %r13
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq 0x30(%rsp), %rbx
cmpq %r14, %rcx
je 0x44a3a
movq 0x10(%rsp), %rsi
movslq (%rsi,%rcx,4), %rsi
movsd 0x8(%rbp,%rsi,8), %xmm1
xorpd %xmm2, %xmm2
cmpneqsd %xmm1, %xmm2
movq %xmm2, %rdi
andl $0x1, %edi
xorpd %xmm2, %xmm2
cmpeqsd %xmm1, %xmm2
ucomisd %xmm1, %xmm0
movl %eax, %r9d
cmovnel %edx, %r9d
cmovpl %edx, %r9d
movq %xmm2, %r8
movslq %r9d, %r9
movq 0x40(%rsp), %r10
movsd %xmm1, (%r10,%r9,8)
xorl %r10d, %r10d
cmpq %r10, %r13
je 0x44a30
movq 0x8(%r12,%r10,8), %r11
movsd 0x8(%r11,%rsi,8), %xmm1
movq (%r15,%r9,8), %r11
movsd %xmm1, (%r11,%r10,8)
incq %r10
jmp 0x44a10
addl %edi, %edx
addl %r8d, %eax
incq %rcx
jmp 0x449bd
pushq $0x1
popq %r14
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x45405
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x45405
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x45435
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x44a8d
pushq $0x1
popq %rcx
movq %rcx, %rsi
movq %rbx, %rdx
movq %rbx, %r8
callq 0x45435
movq 0x10(%rsp), %rdi
callq 0x2050
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
cmpq %rax, %r14
je 0x44ac6
movsd 0x8(%rbp,%rax,8), %xmm0
xorpd 0x6605(%rip), %xmm0     # 0x4b0c0
movlpd %xmm0, 0x8(%rbp,%rax,8)
incq %rax
jmp 0x44aa8
addq $0x8, %rdi
movq %rdi, %rbx
movl %r13d, %esi
movq 0x10(%rsp), %rdx
callq 0x4442d
movapd 0x65de(%rip), %xmm1     # 0x4b0c0
xorl %eax, %eax
cmpq %rax, %r14
je 0x449a6
movsd (%rbx,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, (%rbx,%rax,8)
incq %rax
jmp 0x44ae4