// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl $0x0, -0x44(%rbp)
movslq -0x4(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0x68(%rbp)
movslq -0x4(%rbp), %rsi
movl $0x1, %edi
callq 0x6bc10
movq %rax, -0x70(%rbp)
movslq -0x4(%rbp), %rsi
movl $0x1, %edi
callq 0x6bc10
movq %rax, -0x78(%rbp)
movslq -0x4(%rbp), %rsi
movslq -0x4(%rbp), %rcx
movl $0x1, %edx
movq %rdx, %rdi
callq 0x6bdc0
movq %rax, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x6ad28
movq -0x80(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x6ad1b
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6ace9
jmp 0x6ad1d
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x6acb5
movq -0x80(%rbp), %rdi
movl -0x4(%rbp), %esi
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rcx
callq 0x6ca50
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x4(%rbp), %edx
movq -0x80(%rbp), %rcx
callq 0x6db90
cmpl $0x0, %eax
je 0x6ad62
movq -0x80(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x6ae1e
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x6addc
movq -0x80(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x6adcf
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6ad9d
jmp 0x6add1
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x6ad69
movslq -0x4(%rbp), %rsi
movslq -0x4(%rbp), %rcx
movl $0x1, %edx
movq %rdx, %rdi
callq 0x6bdc0
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movl -0x4(%rbp), %esi
movq -0x78(%rbp), %rdx
movq -0x88(%rbp), %rcx
leaq -0x44(%rbp), %r8
callq 0x6d0f0
movq -0x88(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x6ae71
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jg 0x6ae6f
movq -0x78(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x78(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6ae2b
jmp 0x6ae71
movq -0x78(%rbp), %rdi
addq $0x8, %rdi
movl -0x4(%rbp), %esi
movq -0x68(%rbp), %rdx
callq 0x6aae0
cmpl $0x0, -0x24(%rbp)
jge 0x6aed8
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jg 0x6aed6
movq -0x78(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x78(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6ae92
jmp 0x6aed8
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jg 0x6afaf
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
movl $0x1, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4(%rbp), %eax
jg 0x6af35
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x6aefd
xorps %xmm0, %xmm0
ucomisd -0x90(%rbp), %xmm0
jbe 0x6af9f
movl $0x1, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4(%rbp), %eax
jg 0x6af9d
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x4c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x6af49
jmp 0x6af9f
jmp 0x6afa1
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6aedf
xorps %xmm0, %xmm0
movss %xmm0, -0x94(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x4(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x6b0b3
movq -0x68(%rbp), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movq -0x78(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movaps 0x1148e(%rip), %xmm0    # 0x7c490
pand %xmm0, %xmm1
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jb 0x6b029
movl -0x58(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x6b038
movl -0x54(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x78(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x18(%rbp), %rax
movslq -0x50(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl $0x1, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jg 0x6b0a3
movq -0x30(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x20(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x4c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6b05d
jmp 0x6b0a5
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x6afd1
movq -0x70(%rbp), %rdi
movslq -0x4(%rbp), %rdx
movl $0x1, %esi
callq 0x6c650
movq -0x78(%rbp), %rdi
movslq -0x4(%rbp), %rdx
movl $0x1, %esi
callq 0x6c650
movq -0x80(%rbp), %rdi
movslq -0x4(%rbp), %rdx
movslq -0x4(%rbp), %r8
movl $0x1, %ecx
movq %rcx, %rsi
callq 0x6c6f0
cmpq $0x0, -0x88(%rbp)
je 0x6b116
movq -0x88(%rbp), %rdi
movslq -0x4(%rbp), %rdx
movslq -0x4(%rbp), %r8
movl $0x1, %ecx
movq %rcx, %rsi
callq 0x6c6f0
movq -0x68(%rbp), %rdi
callq 0x1040
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)