// File: /workspace/github_c_makefile_star2_O0/ugobas[P]tnm/EC.c
// Function: EC_profile
float *EC_profile(float *PE, double **Mat, int N_in, char *name)
{
  N=N_in;
  float *EC_out=malloc(N*sizeof(float));
  EC=EC_out;
  int i,j;

  // Eigensystem
  eval=malloc(N*sizeof(float));
  evec=malloc(N*sizeof(float *));
  for(i=0; i<N; i++)evec[i]=malloc(N*sizeof(float));
  d_Diagonalize(N, Mat, eval, evec, 1);

  w_ev=malloc(N*sizeof(float));
  for(i=0; i<N; i++){
    float *v=evec[i];
    double c1=0; for(j=0; j<N; j++)c1+=v[j];
    c1/=N; for(j=0; j<N; j++)v[j]/=c1;
    w_ev[i]=N*c1*c1;
  }

  // c1c2 set equal to value for matrix
  float *ci=CV_profile(Mat, N);
  double cv1=0, cv2=0; float *c=ci;
  for(i=0; i<N; i++){cv1+=*c; cv2+=(*c)*(*c); c++;}
  if(isnan(cv1) || isnan(cv2) || cv2<=0){
    printf("ERROR in Compute_EC, N= %d CV1= %.3g CV2= %.3g\n",N,cv1/N,cv2/N);
    for(i=0; i<N; i++){
      printf("%d %.3g  ", i, ci[i]);
      for(j=0; j<N; j++){printf(" %.2g", Mat[i][j]);} printf("\n");
    }
    exit(8);
  }
  free(ci);
  if(1){
    c1c2=(cv1*cv1)/(N*cv2);
  }else{
    // Choose variance such that the expected minimum component is zero
    double eps=1./(2*log((float)N)-log(6.283));
    c1c2=1./(1+eps);
    printf("eps= %.3g c1c2= %.3g\n", eps, c1c2);
  }
  printf("<EC_i>^2/<EC_i^2>= %.3f\n", c1c2);

  // Compute lambda;
  Lambda=eval[0]*1.05;
  if(Find_zero_Newton(&Lambda, X_MIN, X_MAX))Find_zero_quad(&Lambda);

  //float E=Compute_score(Lambda);
  Compute_EC(EC, N, eval, evec, w_ev, Lambda, 1);
  if(name){printf("EC of %s", name);}else{printf("EC");}
  printf(" computed, lambda1= %.3g Lambda= %.3g\n",eval[0], Lambda);

  // Normalize so that mean is mean of matrix
  double c1=0;
  for(i=0; i<N; i++)c1+=EC[i];
  for(i=0; i<N; i++)EC[i]*=(cv1/c1);

  if(PE!=NULL){
    float *v=evec[0]; for(i=0; i<N; i++)PE[i]=v[i];
  }
  Empty_matrix_f(evec, N); free(eval); free(w_ev);
  return(EC_out);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl -0x14(%rbp), %eax
movl %eax, 0x29b38(%rip)     # 0x9da78
movslq 0x29b31(%rip), %rdi     # 0x9da78
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, 0x29b21(%rip)     # 0x9da80
movslq 0x29b12(%rip), %rdi     # 0x9da78
shlq $0x2, %rdi
callq 0x1180
movq %rax, 0x29b12(%rip)     # 0x9da88
movslq 0x29afb(%rip), %rdi     # 0x9da78
shlq $0x3, %rdi
callq 0x1180
movq %rax, 0x29b03(%rip)     # 0x9da90
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x29adb(%rip), %eax     # 0x9da78
jge 0x73fcc
movslq 0x29ad2(%rip), %rdi     # 0x9da78
shlq $0x2, %rdi
callq 0x1180
movq %rax, %rdx
movq 0x29ad7(%rip), %rax     # 0x9da90
movslq -0x2c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x73f94
movl 0x29aa6(%rip), %edi     # 0x9da78
movq -0x10(%rbp), %rsi
movq 0x29aab(%rip), %rdx     # 0x9da88
movq 0x29aac(%rip), %rcx     # 0x9da90
movl $0x1, %r8d
callq 0x6ac30
movslq 0x29a82(%rip), %rdi     # 0x9da78
shlq $0x2, %rdi
callq 0x1180
movq %rax, 0x29a92(%rip)     # 0x9da98
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x29a62(%rip), %eax     # 0x9da78
jge 0x740f9
movq 0x29a6d(%rip), %rax     # 0x9da90
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl 0x29a31(%rip), %eax     # 0x9da78
jge 0x7406f
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x7403e
cvtsi2sdl 0x29a01(%rip), %xmm1 # 0x9da78
movsd -0x40(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl 0x299e3(%rip), %eax     # 0x9da78
jge 0x740c5
movsd -0x40(%rbp), %xmm1
movq -0x38(%rbp), %rax
movslq -0x30(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x7408c
cvtsi2sdl 0x299ab(%rip), %xmm0 # 0x9da78
mulsd -0x40(%rbp), %xmm0
mulsd -0x40(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq 0x299b6(%rip), %rax     # 0x9da98
movslq -0x2c(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x7400d
movq -0x10(%rbp), %rdi
movl 0x29975(%rip), %esi     # 0x9da78
callq 0x744c0
movq %rax, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x29944(%rip), %eax     # 0x9da78
jge 0x74181
movq -0x60(%rbp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x60(%rbp), %rax
movss (%rax), %xmm0
movq -0x60(%rbp), %rax
mulss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x60(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x60(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x7412b
movsd -0x50(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x741af
movsd -0x58(%rbp), %xmm0
ucomisd %xmm0, %xmm0
setp %al
testb $0x1, %al
jne 0x741af
xorps %xmm0, %xmm0
ucomisd -0x58(%rbp), %xmm0
jb 0x7427f
movl 0x298c3(%rip), %esi     # 0x9da78
movsd -0x50(%rbp), %xmm0
cvtsi2sdl 0x298b6(%rip), %xmm1 # 0x9da78
divsd %xmm1, %xmm0
movsd -0x58(%rbp), %xmm1
cvtsi2sdl 0x298a5(%rip), %xmm2 # 0x9da78
divsd %xmm2, %xmm1
leaq 0xf512(%rip), %rdi      # 0x836f0
movb $0x2, %al
callq 0x10d0
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x29883(%rip), %eax     # 0x9da78
jge 0x74275
movl -0x2c(%rbp), %esi
movq -0x48(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xf50e(%rip), %rdi      # 0x83720
movb $0x1, %al
callq 0x10d0
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl 0x2984f(%rip), %eax     # 0x9da78
jge 0x74259
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x8690(%rip), %rdi      # 0x7c8d7
movb $0x1, %al
callq 0x10d0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x74220
leaq 0x9e18(%rip), %rdi      # 0x7e078
movb $0x0, %al
callq 0x10d0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x741ec
movl $0x8, %edi
callq 0x1210
movq -0x48(%rbp), %rdi
callq 0x1040
movsd -0x50(%rbp), %xmm0
mulsd -0x50(%rbp), %xmm0
cvtsi2sdl 0x297de(%rip), %xmm1 # 0x9da78
mulsd -0x58(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x297f1(%rip)    # 0x9daa0
movss 0x297e9(%rip), %xmm0    # 0x9daa0
cvtss2sd %xmm0, %xmm0
leaq 0xf468(%rip), %rdi      # 0x8372a
movb $0x1, %al
callq 0x10d0
movq 0x297b8(%rip), %rax     # 0x9da88
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xf3f8(%rip), %xmm1     # 0x836d8
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x297b4(%rip)    # 0x9daa4
leaq 0x297ad(%rip), %rdi     # 0x9daa4
movss 0xa815(%rip), %xmm0     # 0x7eb14
movss 0x7ced(%rip), %xmm1     # 0x7bff4
callq 0x74570
cmpl $0x0, %eax
je 0x7431d
leaq 0x2978c(%rip), %rdi     # 0x9daa4
callq 0x74880
movq 0x2975c(%rip), %rdi     # 0x9da80
movl 0x2974e(%rip), %esi     # 0x9da78
movq 0x29757(%rip), %rdx     # 0x9da88
movq 0x29758(%rip), %rcx     # 0x9da90
movq 0x29759(%rip), %r8      # 0x9da98
movss 0x2975d(%rip), %xmm0    # 0x9daa4
movss 0x1d41(%rip), %xmm1     # 0x76090
callq 0x74d30
cmpq $0x0, -0x20(%rbp)
je 0x7436f
movq -0x20(%rbp), %rsi
leaq 0xf3dd(%rip), %rdi      # 0x83743
movb $0x0, %al
callq 0x10d0
jmp 0x7437d
leaq 0xe220(%rip), %rdi      # 0x82596
movb $0x0, %al
callq 0x10d0
movq 0x29704(%rip), %rax     # 0x9da88
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x29710(%rip), %xmm1    # 0x9daa4
cvtss2sd %xmm1, %xmm1
leaq 0xf3ad(%rip), %rdi      # 0x8374c
movb $0x2, %al
callq 0x10d0
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x296ba(%rip), %eax     # 0x9da78
jge 0x743e9
movq 0x296b9(%rip), %rax     # 0x9da80
movslq -0x2c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x743b5
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x2967f(%rip), %eax     # 0x9da78
jge 0x74431
movsd -0x50(%rbp), %xmm1
divsd -0x68(%rbp), %xmm1
movq 0x29674(%rip), %rax     # 0x9da80
movslq -0x2c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x743f0
cmpq $0x0, -0x8(%rbp)
je 0x7447f
movq 0x29651(%rip), %rax     # 0x9da90
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x29622(%rip), %eax     # 0x9da78
jge 0x7447d
movq -0x70(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x7444d
jmp 0x7447f
movq 0x2960a(%rip), %rdi     # 0x9da90
movl 0x295ec(%rip), %esi     # 0x9da78
callq 0x69240
movq 0x295f0(%rip), %rdi     # 0x9da88
callq 0x1040
movq 0x295f4(%rip), %rdi     # 0x9da98
callq 0x1040
movq -0x28(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)