// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdx, %rbx
movslq 0x4(%rdi), %r12
leal -0x1(%r12), %ebp
pushq $0x2
popq %r13
cmpq $0x2, %r12
movl %ebp, 0xc(%rsp)
jl 0x1cac
movl 0x8(%rdi), %edi
movl %edi, %r8d
imull %r12d, %r8d
cvtsi2sd %edi, %xmm0
mulsd %xmm0, %xmm0
movsd 0xc5f6(%rip), %xmm1     # 0xe068
divsd %xmm0, %xmm1
movl %r12d, %eax
movl %edi, %edx
imull %edi, %edx
movl %edx, (%rsp)
cvtsi2sd %edx, %xmm2
imull %edi, %eax
movd %edi, %xmm0
pshufd $0x50, %xmm0, %xmm3     # xmm3 = xmm0[0,0,1,1]
movd %eax, %xmm0
pshufd $0x50, %xmm0, %xmm4     # xmm4 = xmm0[0,0,1,1]
xorl %eax, %eax
pushq $0x1
popq %rsi
pcmpeqd %xmm5, %xmm5
movl %eax, %r14d
leal 0x1(%r14), %r9d
leal -0x1(%r14), %eax
imull %r14d, %eax
cltd
idivl %r13d
movd %r14d, %xmm0
pshufd $0x50, %xmm0, %xmm6     # xmm6 = xmm0[0,0,1,1]
movdqa %xmm6, %xmm0
paddd %xmm5, %xmm0
pmulld %xmm6, %xmm0
movdqa %xmm0, %xmm7
psrld $0x1f, %xmm7
paddd %xmm0, %xmm7
psrad $0x1, %xmm7
movq %rsi, 0x20(%rsp)
movq %rax, 0x10(%rsp)
leal (%rax,%rsi), %r15d
imull (%rsp), %r15d
movl %r9d, 0x1c(%rsp)
movl %r9d, %ebp
movl %r13d, %r9d
xorpd %xmm8, %xmm8
xorpd %xmm9, %xmm9
testl %edi, %edi
jle 0x1b55
leal -0x1(%rbp), %eax
imull %ebp, %eax
cltd
idivl %r9d
movl %eax, %r10d
addl %r14d, %r10d
imull (%rsp), %r10d
xorl %edx, %edx
movl %r15d, %r11d
movl %r10d, %eax
xorl %esi, %esi
leal (%r11,%rsi), %r13d
cmpl %ebp, %r14d
cmovbl %eax, %r13d
addl %r8d, %r13d
movslq %r13d, %r13
addsd (%rcx,%r13,8), %xmm9
incl %esi
addl %edi, %eax
cmpl %esi, %edi
jne 0x1b2a
incl %edx
addl %edi, %r11d
incl %r10d
cmpl %edi, %edx
jne 0x1b25
testl %edi, %edi
movl %r9d, %r13d
jle 0x1c5b
mulsd %xmm1, %xmm9
xorl %eax, %eax
cmpl %ebp, %r14d
setb %al
movd %ebp, %xmm0
pshufd $0x50, %xmm0, %xmm10    # xmm10 = xmm0[0,0,1,1]
movdqa %xmm10, %xmm0
paddd %xmm5, %xmm0
pmulld %xmm10, %xmm0
paddd %xmm7, %xmm10
pmulld %xmm3, %xmm10
movdqa %xmm0, %xmm11
psrld $0x1f, %xmm11
paddd %xmm0, %xmm11
psrad $0x1, %xmm11
paddd %xmm6, %xmm11
pmulld %xmm3, %xmm11
movd %eax, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
xorpd %xmm8, %xmm8
xorl %eax, %eax
pshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
pslld $0x1f, %xmm0
movd %eax, %xmm12
pshufd $0x50, %xmm12, %xmm12   # xmm12 = xmm12[0,0,1,1]
movdqa %xmm12, %xmm13
paddd %xmm10, %xmm13
pmulld %xmm3, %xmm13
xorl %edx, %edx
movd %edx, %xmm14
pshufd $0x50, %xmm14, %xmm14   # xmm14 = xmm14[0,0,1,1]
movdqa %xmm14, %xmm15
paddd %xmm11, %xmm15
paddd %xmm13, %xmm14
pmulld %xmm3, %xmm15
paddd %xmm12, %xmm15
blendvps %xmm0, %xmm15, %xmm14
paddd %xmm4, %xmm14
movd %xmm14, %esi
movslq %esi, %rsi
movsd (%rcx,%rsi,8), %xmm15
pextrd $0x1, %xmm14, %esi
subsd %xmm9, %xmm15
movslq %esi, %rsi
movsd (%rcx,%rsi,8), %xmm14
subsd %xmm9, %xmm14
mulsd %xmm15, %xmm14
addsd %xmm14, %xmm8
incl %edx
cmpl %edx, %edi
jne 0x1be9
incl %eax
cmpl %edi, %eax
jne 0x1bcc
cmpl %ebp, %r14d
jae 0x1c6f
leal -0x1(%rbp), %eax
imull %ebp, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x1c76
movq 0x10(%rsp), %rax
addl %ebp, %eax
divsd %xmm2, %xmm8
addl %r12d, %eax
cltq
movsd %xmm8, (%rbx,%rax,8)
incl %ebp
addl (%rsp), %r15d
cmpl %r12d, %ebp
jne 0x1afa
movq 0x20(%rsp), %rsi
incl %esi
movl 0xc(%rsp), %ebp
movl 0x1c(%rsp), %eax
cmpl %ebp, %eax
jne 0x1aa3
movl %ebp, %eax
imull %r12d, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, (%rsp)
leaq (,%r12,8), %r15
movq %r15, %rdi
callq 0x11a0
movq %rax, %r14
testl %r12d, %r12d
jle 0x1ce0
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x1110
movsd (%rsp), %xmm12
mulsd 0xc361(%rip), %xmm12    # 0xe050
xorpd %xmm0, %xmm0
movl %ebp, %eax
movq %rax, (%rsp)
cmpl $0x2, %r12d
jl 0x1e0a
cvtsi2sdl 0xc(%rsp), %xmm1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0x1(%rax), %r8
pushq $0x1
popq %r9
xorl %r15d, %r15d
leaq 0x1(%r15), %rax
movq %rax, 0x10(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq (%r15,%rax), %rcx
imulq %r15, %rcx
movl %ecx, %eax
cltd
idivl %r13d
movl %eax, %esi
addl %r12d, %esi
movq %r8, %r10
movq %r9, %r11
cmpq %r11, %r15
jae 0x1d74
movq %r10, %rdi
imulq %r11, %rdi
movl %edi, %eax
cltd
idivl %r13d
addl %r15d, %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm2
divsd %xmm1, %xmm2
addsd (%r14,%r15,8), %xmm2
movsd %xmm2, (%r14,%r15,8)
movq %r15, %rbp
jmp 0x1d95
leal (%rsi,%r11), %eax
cltq
movsd (%rbx,%rax,8), %xmm2
divsd %xmm1, %xmm2
addsd (%r14,%r15,8), %xmm2
movsd %xmm2, (%r14,%r15,8)
movq %r11, %rbp
movq %rcx, %rdi
movl %edi, %eax
cltd
idivl %r13d
addl %r12d, %ebp
addl %eax, %ebp
movl %r10d, %eax
imull %r11d, %eax
movslq %ebp, %rdx
movsd (%rbx,%rdx,8), %xmm2
divsd %xmm1, %xmm2
addsd (%r14,%r11,8), %xmm2
cmpq %r11, %r15
movsd %xmm2, (%r14,%r11,8)
cmovael %ecx, %eax
movl %r11d, %edi
cmovbl %r15d, %edi
cltd
idivl %r13d
addl %r12d, %edi
addl %eax, %edi
movslq %edi, %rax
movsd (%rbx,%rax,8), %xmm2
divsd %xmm12, %xmm2
addsd %xmm2, %xmm0
incq %r11
incq %r10
cmpq %r11, %r12
jne 0x1d40
incq %r9
incq %r8
movq 0x10(%rsp), %r15
cmpq (%rsp), %r15
jne 0x1d19
cmpl $0x2, %r12d
jl 0x1ead
pushq $0x1
popq %rsi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0x1(%rax), %rdi
xorl %r8d, %r8d
movq %r8, %r9
incq %r8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq (%r9,%rax), %r10
imulq %r9, %r10
movsd (%r14,%r9,8), %xmm1
movq %rdi, %r11
movq %rsi, %r15
movl %r11d, %ecx
imull %r15d, %ecx
cmpq %r15, %r9
movl %r10d, %eax
cmovbl %ecx, %eax
movl %r15d, %ebp
cmovbl %r9d, %ebp
cmovael %r10d, %ecx
cltd
idivl %r13d
addl %ebp, %eax
addl %r12d, %eax
cltq
movsd (%r14,%r15,8), %xmm2
mulsd %xmm1, %xmm2
divsd %xmm0, %xmm2
movsd (%rbx,%rax,8), %xmm3
movl %ecx, %eax
cltd
idivl %r13d
subsd %xmm2, %xmm3
addl %r12d, %ebp
addl %eax, %ebp
movslq %ebp, %rax
movsd %xmm3, (%rbx,%rax,8)
incq %r15
incq %r11
cmpq %r15, %r12
jne 0x1e42
incq %rsi
incq %rdi
cmpq (%rsp), %r8
jne 0x1e23
cmpl $0x2, %r12d
jl 0x222a
movsd 0xc1a9(%rip), %xmm5     # 0xe068
movapd %xmm5, %xmm6
divsd %xmm12, %xmm6
pxor %xmm7, %xmm7
movsd 0xc60b(%rip), %xmm8     # 0xe4e0
movsd 0xc5c2(%rip), %xmm9     # 0xe4a0
xorl %edi, %edi
movabsq $0x40c3880000000000, %rsi # imm = 0x40C3880000000000
movabsq $0x3f847ae147ae147b, %r8 # imm = 0x3F847AE147AE147B
movabsq $0x7fffffffffffffff, %r9 # imm = 0x7FFFFFFFFFFFFFFF
movabsq $0x7ff0000000000000, %r10 # imm = 0x7FF0000000000000
movabsq $0x40c3888000000000, %r11 # imm = 0x40C3888000000000
movapd %xmm9, %xmm3
movapd %xmm8, %xmm2
pxor %xmm4, %xmm4
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movl 0xc(%rsp), %ebp
movl %edi, %r14d
leal 0x1(%r14), %edi
leal -0x1(%r14), %eax
imull %r14d, %eax
cltd
idivl %r13d
movl %eax, %ecx
movl %edi, %r15d
cmpl %r15d, %r14d
jae 0x1f5a
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x1f5e
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm10
ucomisd %xmm7, %xmm10
jbe 0x1f86
cmpl %r15d, %r14d
jae 0x1f9c
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x1fa0
cmpl %r15d, %r14d
jae 0x1fc1
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x1fc5
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm10
cmpl %r15d, %r14d
jae 0x1fd0
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x1fd4
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movq %r11, (%rbx,%rax,8)
jmp 0x2016
leal (%rcx,%r15), %eax
addsd %xmm10, %xmm10
movapd %xmm5, %xmm11
divsd %xmm10, %xmm11
addl %r12d, %eax
cltq
movsd %xmm11, (%rbx,%rax,8)
cmpl %r15d, %r14d
jae 0x2004
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x2008
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
addsd (%rbx,%rax,8), %xmm0
addsd %xmm5, %xmm1
cmpl %r15d, %r14d
jae 0x202c
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x2030
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm10
ucomisd %xmm9, %xmm10
jb 0x206d
cmpl %r15d, %r14d
jae 0x2058
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x205c
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm8
jb 0x206d
addsd %xmm6, %xmm4
cmpl %r15d, %r14d
jae 0x2083
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x2087
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm7
ja 0x20be
cmpl %r15d, %r14d
jae 0x20a9
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x20ad
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movq (%rbx,%rax,8), %rax
andq %r9, %rax
cmpq %r10, %rax
jl 0x20e1
cmpl %r15d, %r14d
jae 0x20d4
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x20d8
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movq %rsi, (%rbx,%rax,8)
cmpl %r15d, %r14d
jae 0x20f7
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x20fb
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm9
jbe 0x212b
cmpl %r15d, %r14d
jae 0x211e
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x2122
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movq %r8, (%rbx,%rax,8)
cmpl %r15d, %r14d
jae 0x2141
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x2145
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm10
ucomisd %xmm8, %xmm10
jbe 0x217a
cmpl %r15d, %r14d
jae 0x216d
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x2171
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movq %rsi, (%rbx,%rax,8)
cmpl %r15d, %r14d
jae 0x2190
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x2194
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm10
ucomisd %xmm3, %xmm10
jbe 0x21ca
cmpl %r15d, %r14d
jae 0x21bc
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x21c0
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm3
cmpl %r15d, %r14d
jae 0x21e0
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x21e4
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
ucomisd (%rbx,%rax,8), %xmm2
jbe 0x2214
cmpl %r15d, %r14d
jae 0x2206
leal -0x1(%r15), %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r14d, %eax
jmp 0x220a
leal (%rcx,%r15), %eax
addl %r12d, %eax
cltq
movsd (%rbx,%rax,8), %xmm2
incl %r15d
cmpl %r15d, %r12d
jne 0x1f44
cmpl %ebp, %edi
jne 0x1f2c
jmp 0x2246
movsd 0xc26e(%rip), %xmm3     # 0xe4a0
movsd 0xc2a6(%rip), %xmm2     # 0xe4e0
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
pxor %xmm4, %xmm4
movq 0xfdab(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
divsd %xmm1, %xmm0
divsd %xmm12, %xmm1
leaq 0xc3ec(%rip), %rsi      # 0xe64c
movb $0x5, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1170