// File: /workspace/github_c_makefile_star2_O2/debbiemarkslab[P]plmc/src/inference.c
// Function: ZeroAPCPriors
void ZeroAPCPriors(alignment_t *ali, options_t *options, numeric_t *lambdas,
    lbfgsfloatval_t *x) {
    /* Compute the variances of the couplings for each pair */
    for (int i = 0; i < ali->nSites - 1; i++)
        for (int j = i + 1; j < ali->nSites; j++) {
            /* Mean(eij) over ai, aj */
            numeric_t mean = 0.0;
            for (int ai = 0; ai < ali->nCodes; ai++)
                for (int aj = 0; aj < ali->nCodes; aj++)
                    mean += xEij(i, j, ai, aj);
            mean *= 1.0 / ((numeric_t) ali->nCodes * ali->nCodes);

            /* Var(eij) over ai, aj */
            numeric_t ssq = 0.0;
            for (int ai = 0; ai < ali->nCodes; ai++)
                for (int aj = 0; aj < ali->nCodes; aj++)
                    ssq += (xEij(i, j, ai, aj) - mean)
                         * (xEij(i, j, ai, aj) - mean);
            /* Use N rather than N-1 since N has better MSE */
            numeric_t var = ssq / ((numeric_t) (ali->nCodes * ali->nCodes));
            lambdaEij(i, j) = var;
        }

    /* Determine the site-wise statistics of the variances */
    numeric_t nPairs =  ((numeric_t) ((ali->nSites) * (ali->nSites - 1))) / 2.0;
    numeric_t V_avg = 0.0;
    numeric_t *V_pos_avg = (numeric_t *) malloc(ali->nSites * sizeof(numeric_t));
    for (int i = 0; i < ali->nSites; i++) {
        V_pos_avg[i] = 0.0;
    }
    for (int i = 0; i < ali->nSites - 1; i++) {
        for (int j = i + 1; j < ali->nSites; j++) {
            V_pos_avg[i] += lambdaEij(i, j) / (numeric_t) (ali->nSites - 1);
            V_pos_avg[j] += lambdaEij(i, j) / (numeric_t) (ali->nSites - 1);
            V_avg += lambdaEij(i, j) / nPairs;
        }
    }

    /* Remove the first component of the variances */
    for (int i = 0; i < ali->nSites - 1; i++)
        for (int j = i + 1; j < ali->nSites; j++)
            lambdaEij(i, j) =
                lambdaEij(i, j) - V_pos_avg[i] * V_pos_avg[j] / V_avg;

    /* Transform and truncate variances into lambda hyperparameters */
    numeric_t pcount = 0.0;
    numeric_t psum = 0.0;
    numeric_t inbounds = 0;
    numeric_t min = LAMBDA_J_MAX;
    numeric_t max = LAMBDA_J_MIN;
    for (int i = 0; i < ali->nSites - 1; i++) {
        for (int j = i + 1; j < ali->nSites; j++) {
            /* Lambda coefficients are 1/2 the inverse variance */
            if (lambdaEij(i, j) > 0) {
                lambdaEij(i, j) = 1.0 / (2.0 * lambdaEij(i, j));
                psum += lambdaEij(i, j);
                pcount += 1.0;
            } else {
                lambdaEij(i, j) = LAMBDA_J_MAX + 1.0;
            }

            /* Truncate lambda for numerical stability */
            if (lambdaEij(i, j) >= LAMBDA_J_MIN && lambdaEij(i, j) <= LAMBDA_J_MAX)
                inbounds += 1.0 / (numeric_t) ((ali->nSites)*(ali->nSites - 1) / 2.0);
            if (lambdaEij(i, j) < 0 || !isfinite(lambdaEij(i, j)))
                lambdaEij(i, j) = LAMBDA_J_MAX;
            if (lambdaEij(i, j) < LAMBDA_J_MIN) lambdaEij(i, j) = LAMBDA_J_MIN;
            if (lambdaEij(i, j) > LAMBDA_J_MAX) lambdaEij(i, j) = LAMBDA_J_MAX;

            /* Track extremes */
            if (lambdaEij(i, j) > max) max = lambdaEij(i, j);
            if (lambdaEij(i, j) < min) min = lambdaEij(i, j);
        }
    }
    fprintf(stderr, "Raw coupling hyperparameter statistics:\n"
                    "\tMean positive lambda: %f\n"
                    "\tPercent of ij's positive: %f\n"
                    "\tPercent in bounds (%f < L < %f): %f\n",
                    psum / pcount,
                    pcount / nPairs,
                    min, max, inbounds);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movq %rdi, 0x8(%rsp)
movslq 0x4(%rdi), %r14
movl %r14d, %r15d
leal -0x1(%r14), %eax
xorl %edi, %edi
testl %eax, %eax
movl $0x0, %edx
movl %eax, 0x10(%rsp)
cmovgl %eax, %edx
movl %edx, 0x14(%rsp)
pushq $0x2
popq %rbp
movsd 0x2966(%rip), %xmm3     # 0xc068
cmpl 0x14(%rsp), %edi
je 0x9801
leal 0x1(%rdi), %eax
movl %eax, 0x4(%rsp)
movl %eax, %r9d
cmpl %r14d, %r9d
je 0x97f6
movl %ebp, %r8d
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ebp
leal -0x1(%r9), %eax
imull %r9d, %eax
cltd
movl %r8d, %esi
idivl %r8d
addl %edi, %eax
xorl %r12d, %r12d
testl %ebp, %ebp
movl $0x0, %edx
cmovgl %ebp, %edx
movl %ebp, %r11d
imull %eax, %r11d
leal (%r14,%r11), %r13d
imull %ebp, %r13d
xorpd %xmm0, %xmm0
cmpl %edx, %r12d
je 0x9782
movl %r13d, %r10d
movl %ebp, %r8d
subl $0x1, %r8d
jb 0x977a
movslq %r10d, %r10
addsd (%rcx,%r10,8), %xmm0
addl %ebp, %r10d
jmp 0x9766
incl %r12d
incl %r13d
jmp 0x975b
cvtsi2sd %ebp, %xmm1
mulsd %xmm1, %xmm1
movapd %xmm3, %xmm2
divsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
addl %r14d, %r11d
imull %ebp, %r11d
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
cmpl %edx, %r12d
je 0x97d7
movl %r11d, %r13d
movl %ebp, %r10d
subl $0x1, %r10d
jb 0x97cf
movslq %r13d, %r13
movsd (%rcx,%r13,8), %xmm2
subsd %xmm0, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
addl %ebp, %r13d
jmp 0x97af
incl %r12d
incl %r11d
jmp 0x97a4
imull %ebp, %ebp
cvtsi2sd %ebp, %xmm0
divsd %xmm0, %xmm1
addl %r15d, %eax
cltq
movsd %xmm1, (%rbx,%rax,8)
incl %r9d
movl %esi, %ebp
jmp 0x9716
movl 0x4(%rsp), %eax
movl %eax, %edi
jmp 0x9702
movl 0x10(%rsp), %eax
imull %r15d, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq (,%r14,8), %rdi
callq 0x11a0
movq %rax, %rcx
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
cmpq %rax, %rdx
je 0x9840
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x9831
movsd 0x8(%rsp), %xmm12
mulsd 0x2800(%rip), %xmm12    # 0xc050
movabsq $0x100000000, %rdi      # imm = 0x100000000
cvtsi2sdl 0x10(%rsp), %xmm1
movl 0x14(%rsp), %r13d
xorpd %xmm0, %xmm0
pushq $0x1
popq %r8
xorl %r9d, %r9d
movq %rdi, %r10
movsd 0x27ec(%rip), %xmm11    # 0xc068
cmpq %r13, %r9
je 0x994b
leal (%rdi,%r9), %eax
decl %eax
imull %r9d, %eax
cltd
idivl %ebp
movl %eax, %esi
movq %r10, %r11
movq %r8, %r12
cmpq %r12, %r15
je 0x993d
cmpq %r12, %r9
jae 0x98b7
movl %r11d, %eax
imull %r12d, %eax
cltd
idivl %ebp
addl %r9d, %eax
jmp 0x98bb
leal (%rsi,%r12), %eax
addl %r15d, %eax
cltq
movsd (%rbx,%rax,8), %xmm2
divsd %xmm1, %xmm2
addsd (%rcx,%r9,8), %xmm2
movsd %xmm2, (%rcx,%r9,8)
cmpq %r12, %r9
jae 0x98e9
movl %r11d, %eax
imull %r12d, %eax
cltd
idivl %ebp
addl %r9d, %eax
jmp 0x98ed
leal (%rsi,%r12), %eax
addl %r14d, %eax
cltq
movsd (%rbx,%rax,8), %xmm2
divsd %xmm1, %xmm2
addsd (%rcx,%r12,8), %xmm2
movsd %xmm2, (%rcx,%r12,8)
cmpq %r12, %r9
jae 0x991b
movl %r11d, %eax
imull %r12d, %eax
cltd
idivl %ebp
addl %r9d, %eax
jmp 0x991f
leal (%rsi,%r12), %eax
addl %r15d, %eax
cltq
movsd (%rbx,%rax,8), %xmm2
divsd %xmm12, %xmm2
addsd %xmm2, %xmm0
incq %r12
incq %r11
jmp 0x989a
incq %r9
incq %r8
incq %r10
jmp 0x987c
pushq $0x1
popq %r8
xorl %r9d, %r9d
movq %rdi, %r10
cmpq %r13, %r9
je 0x99e6
leal (%rdi,%r9), %eax
decl %eax
imull %r9d, %eax
cltd
idivl %ebp
movl %eax, %esi
movq %r10, %r11
movq %r8, %r12
cmpq %r12, %r15
je 0x99d8
cmpq %r12, %r9
jae 0x998c
movl %r11d, %eax
imull %r12d, %eax
cltd
idivl %ebp
addl %r9d, %eax
jmp 0x9990
leal (%rsi,%r12), %eax
addl %r15d, %eax
cltq
movsd (%rbx,%rax,8), %xmm1
movsd (%rcx,%r9,8), %xmm2
mulsd (%rcx,%r12,8), %xmm2
cmpq %r12, %r9
jae 0x99ba
movl %r11d, %eax
imull %r12d, %eax
cltd
idivl %ebp
addl %r9d, %eax
jmp 0x99be
leal (%rsi,%r12), %eax
divsd %xmm0, %xmm2
subsd %xmm2, %xmm1
addl %r14d, %eax
cltq
movsd %xmm1, (%rbx,%rax,8)
incq %r12
incq %r11
jmp 0x9973
incq %r9
incq %r8
incq %r10
jmp 0x9955
movapd %xmm11, %xmm5
divsd %xmm12, %xmm5
leaq -0x1(%r15), %rsi
leal 0x1(%r14), %eax
movl %eax, 0x4(%rsp)
movsd 0x2a14(%rip), %xmm6     # 0xc418
movsd 0x2a4c(%rip), %xmm7     # 0xc458
xorpd %xmm8, %xmm8
xorl %r8d, %r8d
movabsq $0x40c3880000000000, %r11 # imm = 0x40C3880000000000
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
xorpd %xmm4, %xmm4
movapd %xmm7, %xmm2
movapd %xmm6, %xmm3
movq %r13, 0x8(%rsp)
cmpq %r13, %r8
je 0x9d71
leal (%rdi,%r8), %eax
decl %eax
imull %r8d, %eax
cltd
idivl %ebp
leal (%r15,%r8), %r12d
movslq %eax, %r13
leaq 0x1(%r13), %r9
addl 0x4(%rsp), %r13d
movq %r8, %r10
cmpq %r10, %rsi
je 0x9d64
leaq 0x1(%r10), %rcx
cmpq %rcx, %r8
jae 0x9a83
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9a87
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movsd (%rbx,%rax,8), %xmm9
ucomisd %xmm8, %xmm9
jbe 0x9aaf
cmpq %rcx, %r8
jae 0x9ac5
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9ac9
cmpq %rcx, %r8
jae 0x9b06
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9b0a
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movsd (%rbx,%rax,8), %xmm10
addsd %xmm10, %xmm10
movapd %xmm11, %xmm9
divsd %xmm10, %xmm9
cmpq %rcx, %r8
jae 0x9b1f
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
leal (%r12,%rax), %edx
movslq %edx, %rdx
movsd %xmm9, (%rbx,%rdx,8)
addl %r8d, %eax
jmp 0x9b2f
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movabsq $0x40c3888000000000, %rdx # imm = 0x40C3888000000000
movq %rdx, (%rbx,%rax,8)
jmp 0x9b3e
leal (%r10,%r13), %eax
cltq
movsd %xmm9, (%rbx,%rax,8)
leal (%r9,%r10), %eax
addl %r14d, %eax
cltq
addsd (%rbx,%rax,8), %xmm0
addsd %xmm11, %xmm1
cmpq %rcx, %r8
jae 0x9b54
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9b58
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movsd (%rbx,%rax,8), %xmm9
ucomisd %xmm6, %xmm9
jb 0x9b94
cmpq %rcx, %r8
jae 0x9b80
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9b84
leal (%r9,%r10), %eax
addl %r14d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm7
jb 0x9b94
addsd %xmm5, %xmm4
cmpq %rcx, %r8
jae 0x9baa
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9bae
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm8
ja 0x9bfa
cmpq %rcx, %r8
jae 0x9bd1
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9bd5
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movq (%rbx,%rax,8), %rax
movabsq $0x7fffffffffffffff, %rdx # imm = 0x7FFFFFFFFFFFFFFF
andq %rdx, %rax
movabsq $0x7ff0000000000000, %rdx # imm = 0x7FF0000000000000
cmpq %rdx, %rax
jl 0x9c1d
cmpq %rcx, %r8
jae 0x9c10
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9c14
leal (%r9,%r10), %eax
addl %r14d, %eax
cltq
movq %r11, (%rbx,%rax,8)
cmpq %rcx, %r8
jae 0x9c33
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9c37
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm6
jbe 0x9c70
cmpq %rcx, %r8
jae 0x9c59
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9c5d
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movabsq $0x3f847ae147ae147b, %rdx # imm = 0x3F847AE147AE147B
movq %rdx, (%rbx,%rax,8)
cmpq %rcx, %r8
jae 0x9c86
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9c8a
leal (%r9,%r10), %eax
addl %r14d, %eax
cltq
movsd (%rbx,%rax,8), %xmm9
ucomisd %xmm7, %xmm9
jbe 0x9cbf
cmpq %rcx, %r8
jae 0x9cb2
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9cb6
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movq %r11, (%rbx,%rax,8)
cmpq %rcx, %r8
jae 0x9cd5
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9cd9
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
movsd (%rbx,%rax,8), %xmm9
ucomisd %xmm3, %xmm9
jbe 0x9d0f
cmpq %rcx, %r8
jae 0x9d01
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9d05
leal (%r9,%r10), %eax
addl %r14d, %eax
cltq
movsd (%rbx,%rax,8), %xmm3
cmpq %rcx, %r8
jae 0x9d25
leal (%r10,%rdi), %edx
movl %ecx, %eax
imull %edx, %eax
cltd
idivl %ebp
addl %r8d, %eax
jmp 0x9d29
leal (%r9,%r10), %eax
addl %r15d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm2
jbe 0x9d5c
cmpq %rcx, %r8
jae 0x9d4e
addl %edi, %r10d
movl %ecx, %eax
imull %r10d, %eax
cltd
idivl %ebp
movl %eax, %r10d
addl %r8d, %r10d
jmp 0x9d51
addl %r9d, %r10d
leal (%r15,%r10), %eax
cltq
movsd (%rbx,%rax,8), %xmm2
movq %rcx, %r10
jmp 0x9a60
incq %r8
movq 0x8(%rsp), %r13
jmp 0x9a37
movq 0x6280(%rip), %rax      # 0xfff8
movq (%rax), %rdi
divsd %xmm1, %xmm0
divsd %xmm12, %xmm1
leaq 0x2825(%rip), %rsi      # 0xc5b0
movb $0x5, %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1170