// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %r8d, 0x28(%rsp)
movq %rcx, %r15
movq %rdx, 0x40(%rsp)
andl $0x0, 0x2c(%rsp)
movq %rsi, %r14
movl %edi, %r13d
movslq %edi, %r12
leaq (,%r12,4), %rdi
callq 0x21d0
movq %rax, 0x18(%rsp)
pushq $0x1
popq %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x44ea8
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x44ea8
movq %rax, %rbp
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x44f9e
xorl %esi, %esi
testl %r12d, %r12d
movl $0x0, %ebx
cmovgl %r13d, %ebx
movq %r12, 0x38(%rsp)
movl %r12d, %r12d
cmpq %rbx, %rsi
je 0x44594
leaq 0x1(%rsi), %rcx
movq 0x8(%rax,%rsi,8), %rdx
movq (%r14,%rsi,8), %rdi
xorl %esi, %esi
cmpq %rsi, %r12
je 0x4458f
movsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rdx,%rsi,8)
incq %rsi
jmp 0x4457a
movq %rcx, %rsi
jmp 0x44566
movq %rax, %rdi
movq %rax, 0x10(%rsp)
movl %r13d, %esi
movq %rbp, %rdx
movq 0x20(%rsp), %rcx
callq 0x45550
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movl %r13d, %edx
movq 0x10(%rsp), %rcx
callq 0x45e80
movq 0x10(%rsp), %rdi
testl %eax, %eax
je 0x445d8
movq $0x0, 0x30(%rsp)
movq %rdi, %r14
jmp 0x4463b
xorl %edx, %edx
cmpq %rbx, %rdx
je 0x44608
leaq 0x1(%rdx), %rax
movq 0x8(%rdi,%rdx,8), %rcx
movq (%r14,%rdx,8), %rdx
xorl %esi, %esi
cmpq %rsi, %r12
je 0x44603
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, 0x8(%rcx,%rsi,8)
incq %rsi
jmp 0x445ee
movq %rax, %rdx
jmp 0x445da
pushq $0x1
popq %rdi
movq 0x38(%rsp), %rcx
movq %rcx, %rsi
movq %rdi, %rdx
callq 0x44f9e
movq %rax, %r14
leaq 0x2c(%rsp), %r8
movq 0x10(%rsp), %rdi
movl %r13d, %esi
movq %rbp, %rdx
movq %rax, %rcx
callq 0x458f4
movq %r14, 0x30(%rsp)
movq 0x8(%rsp), %rdi
cmpl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rdx
js 0x447bd
addq $0x8, %rdi
movl %r13d, %esi
callq 0x4442d
leal 0x1(%rbx), %r12d
pushq $0x1
popq %rdi
leal 0x1(%r13), %eax
xorpd %xmm0, %xmm0
movapd 0x6a4d(%rip), %xmm1     # 0x4b0c0
movq %rdi, %rcx
cmpq %r12, %rcx
je 0x446bf
xorpd %xmm2, %xmm2
movq %rdi, %rdx
cmpq %rdx, %rax
je 0x44695
movq (%r14,%rdx,8), %rsi
addsd (%rsi,%rcx,8), %xmm2
incq %rdx
jmp 0x44682
ucomisd %xmm2, %xmm0
jbe 0x446ba
movq %rdi, %rdx
cmpq %rdx, %rax
je 0x446ba
movq (%r14,%rdx,8), %rsi
movsd (%rsi,%rcx,8), %xmm2
xorpd %xmm1, %xmm2
movlpd %xmm2, (%rsi,%rcx,8)
incq %rdx
jmp 0x4469e
incq %rcx
jmp 0x44676
decl %r13d
decq %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq 0x18(%rsp), %r12
cmpq %rbx, %rcx
je 0x4474e
movslq (%r12,%rcx,4), %rsi
movsd 0x8(%rbp,%rsi,8), %xmm1
xorpd %xmm2, %xmm2
cmpneqsd %xmm1, %xmm2
movq %xmm2, %rdi
andl $0x1, %edi
xorpd %xmm2, %xmm2
ucomisd %xmm1, %xmm0
cmpeqsd %xmm1, %xmm2
movl %r13d, %r9d
cmovnel %edx, %r9d
cmovpl %edx, %r9d
cvtsd2ss %xmm1, %xmm1
movq %xmm2, %r8
movslq %r9d, %r9
movq 0x40(%rsp), %r10
movss %xmm1, (%r10,%r9,4)
xorl %r10d, %r10d
cmpq %r10, %rax
je 0x44744
movq 0x8(%r14,%r10,8), %r11
cvtsd2ss 0x8(%r11,%rsi,8), %xmm1
movq (%r15,%r9,8), %r11
movss %xmm1, (%r11,%r10,4)
incq %r10
jmp 0x44724
addl %edi, %edx
addl %r8d, %r13d
incq %rcx
jmp 0x446d2
pushq $0x1
popq %r14
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdx
callq 0x45405
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x45405
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movq %rbx, %r8
callq 0x45435
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x447a6
pushq $0x1
popq %rcx
movq %rcx, %rsi
movq %rbx, %rdx
movq %rbx, %r8
callq 0x45435
movq %r12, %rdi
callq 0x2050
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal 0x1(%rbx), %r12d
xorl %eax, %eax
cmpq %rax, %rbx
je 0x447e1
movsd 0x8(%rbp,%rax,8), %xmm0
xorpd 0x68ea(%rip), %xmm0     # 0x4b0c0
movlpd %xmm0, 0x8(%rbp,%rax,8)
incq %rax
jmp 0x447c3
addq $0x8, %rdi
movq %rdi, 0x8(%rsp)
movl %r13d, %esi
movq 0x18(%rsp), %rdx
callq 0x4442d
movapd 0x68c1(%rip), %xmm1     # 0x4b0c0
movq 0x8(%rsp), %rcx
xorl %eax, %eax
pushq $0x1
popq %rdi
cmpq %rax, %rbx
je 0x44663
movsd (%rcx,%rax,8), %xmm0
xorpd %xmm1, %xmm0
movlpd %xmm0, (%rcx,%rax,8)
incq %rax
jmp 0x44809