// File: /workspace/github_c_makefile_star2_O1/debbiemarkslab[P]plmc/src/plm.c
// Function: OutputCouplingScores
void OutputCouplingScores(char *couplingsFile, const numeric_t *x,
    alignment_t *ali, options_t *options) {
    FILE *fpOutput = NULL;
    fpOutput = fopen(couplingsFile, "w");
    if (fpOutput != NULL) {
        /* Compute the norm of the coupling parameters between each pair */
        numeric_t *couplings =
        (numeric_t *) malloc((ali->nSites * (ali->nSites - 1) / 2)
                * sizeof(numeric_t));

        for (int i = 0; i < ali->nSites * (ali->nSites - 1) / 2;
            i++) couplings[i] = 0;
        for (int i = 0; i < ali->nSites - 1; i++)
            for (int j = i + 1; j < ali->nSites; j++) {
                /* Norm(eij) over ai, aj */
                numeric_t norm = 0.0;
                for (int ai = 0; ai < ali->nCodes; ai++)
                    for (int aj = 0; aj < ali->nCodes; aj++)
                        norm += xEij(i, j, ai, aj) * xEij(i, j, ai, aj);
                norm = sqrt(norm);
                coupling(i, j) = norm;
            }
        numeric_t nPairs =
            ((numeric_t) ((ali->nSites) * (ali->nSites - 1))) / 2.0;

        /* Remove first component of the norms (Average Product Correction) */
        if (!options->zeroAPC) {
            /* Determine the site-wise statistics of the norms */
            numeric_t C_avg = 0.0;
            numeric_t *C_pos_avg =
                (numeric_t *) malloc(ali->nSites * sizeof(numeric_t));
            for (int i = 0; i < ali->nSites; i++) {
                C_pos_avg[i] = 0.0;
            }
            for (int i = 0; i < ali->nSites - 1; i++) {
                for (int j = i + 1; j < ali->nSites; j++) {
                    C_pos_avg[i] +=
                        coupling(i, j) / (numeric_t) (ali->nSites - 1);
                    C_pos_avg[j] +=
                        coupling(i, j) / (numeric_t) (ali->nSites - 1);
                    C_avg += coupling(i, j) / nPairs;
                }
            }

            /* Remove the first component */
            for (int i = 0; i < ali->nSites - 1; i++)
                for (int j = i + 1; j < ali->nSites; j++)
                    coupling(i, j) =
                        coupling(i, j) - C_pos_avg[i] * C_pos_avg[j] / C_avg;    
        }

        /* Output scores */
        if (ali->target >= 0) {
            /* Focus mode */
            for (int i = 0; i < ali->nSites - 1; i++)
                for (int j = i + 1; j < ali->nSites; j++) {
                    char ai = (char) ali->alphabet[seq(ali->target, i)];
                    char aj = (char) ali->alphabet[seq(ali->target, j)];
                    fprintf(fpOutput, "%d %c %d %c 0 %f\n",
                        ali->offsets[i], ai, ali->offsets[j], aj,
                        coupling(i, j));
                }
        } else {
            for (int i = 0; i < ali->nSites - 1; i++)
                for (int j = i + 1; j < ali->nSites; j++)
                    fprintf(fpOutput, "%d - %d - 0 %f\n", i + 1, j + 1,
                        coupling(i, j));
        }

        fclose(fpOutput);
    } else {
        fprintf(stderr, "Error writing coupling scores\n");
        exit(1);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r13
leaq 0x8028(%rip), %rsi      # 0xf321
callq 0x11c0
testq %rax, %rax
je 0x796b
movq %rax, %r15
movl 0x4(%rbx), %eax
leal -0x1(%rax), %ebp
imull %eax, %ebp
movl %ebp, %r14d
shrl $0x1f, %r14d
addl %ebp, %r14d
sarl %r14d
movslq %r14d, %rdi
shlq $0x3, %rdi
callq 0x11a0
movq %rax, 0x10(%rsp)
cmpl $0x2, %ebp
jl 0x734d
decl %r14d
leaq 0x8(,%r14,8), %rdx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x1110
movq %r12, 0x18(%rsp)
movq %r15, 0x38(%rsp)
movl 0x4(%rbx), %r14d
cmpl $0x2, %r14d
jl 0x7547
xorl %eax, %eax
xorpd %xmm10, %xmm10
movq 0x10(%rsp), %r15
movl %eax, %r12d
leal 0x1(%r12), %eax
movl 0x4(%rbx), %ecx
movl %eax, 0x8(%rsp)
cmpl %ecx, %eax
jge 0x7531
movd %r12d, %xmm0
pshufd $0x50, %xmm0, %xmm1     # xmm1 = xmm0[0,0,1,1]
movdqa %xmm1, %xmm0
paddd 0x6e22(%rip), %xmm0     # 0xe1c0
movdqa %xmm1, 0x20(%rsp)
pmulld %xmm1, %xmm0
movdqa %xmm0, %xmm1
psrld $0x1f, %xmm1
paddd %xmm0, %xmm1
psrad $0x1, %xmm1
movdqa %xmm1, 0x40(%rsp)
leal -0x1(%r12), %eax
imull %r12d, %eax
movl %eax, %ebp
shrl $0x1f, %ebp
addl %eax, %ebp
sarl %ebp
movl 0x8(%rsp), %eax
movl %eax, %r14d
movl 0x8(%rbx), %eax
testl %eax, %eax
jle 0x74d9
imull %eax, %ecx
xorl %edx, %edx
cmpl %r14d, %r12d
setb %dl
movd %r14d, %xmm0
pshufd $0x50, %xmm0, %xmm2     # xmm2 = xmm0[0,0,1,1]
movdqa %xmm2, %xmm0
paddd 0x6dba(%rip), %xmm0     # 0xe1c0
pmulld %xmm2, %xmm0
paddd 0x40(%rsp), %xmm2
movd %eax, %xmm1
pshufd $0x50, %xmm1, %xmm3     # xmm3 = xmm1[0,0,1,1]
pmulld %xmm3, %xmm2
movdqa %xmm0, %xmm4
psrld $0x1f, %xmm4
paddd %xmm0, %xmm4
psrad $0x1, %xmm4
paddd 0x20(%rsp), %xmm4
pmulld %xmm3, %xmm4
movd %edx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movd %ecx, %xmm1
pshufd $0x50, %xmm1, %xmm5     # xmm5 = xmm1[0,0,1,1]
pxor %xmm1, %xmm1
xorl %ecx, %ecx
pshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
pslld $0x1f, %xmm0
movd %ecx, %xmm6
pshufd $0x50, %xmm6, %xmm6     # xmm6 = xmm6[0,0,1,1]
movdqa %xmm2, %xmm7
paddd %xmm6, %xmm7
pmulld %xmm3, %xmm7
xorl %edx, %edx
movd %edx, %xmm8
pshufd $0x50, %xmm8, %xmm8     # xmm8 = xmm8[0,0,1,1]
movdqa %xmm8, %xmm9
paddd %xmm4, %xmm9
paddd %xmm7, %xmm8
pmulld %xmm3, %xmm9
paddd %xmm6, %xmm9
blendvps %xmm0, %xmm9, %xmm8
paddd %xmm5, %xmm8
movd %xmm8, %esi
movslq %esi, %rsi
pextrd $0x1, %xmm8, %edi
movsd (%r13,%rsi,8), %xmm8
movslq %edi, %rsi
mulsd (%r13,%rsi,8), %xmm8
addsd %xmm8, %xmm1
incl %edx
cmpl %edx, %eax
jne 0x7476
incl %ecx
cmpl %eax, %ecx
jne 0x745e
jmp 0x74dd
pxor %xmm1, %xmm1
ucomisd %xmm10, %xmm1
jb 0x74ed
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0x74fb
movapd %xmm1, %xmm0
callq 0x1210
xorpd %xmm10, %xmm10
cmpl %r14d, %r12d
jae 0x7516
leal -0x1(%r14), %ecx
imull %r14d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
addl %r12d, %eax
jmp 0x751a
leal (%r14,%rbp), %eax
cltq
movsd %xmm0, (%r15,%rax,8)
incl %r14d
movl 0x4(%rbx), %ecx
cmpl %ecx, %r14d
jl 0x73da
movl 0x4(%rbx), %r14d
leal -0x1(%r14), %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jl 0x7371
jmp 0x7550
leal -0x1(%r14), %ecx
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rax
cmpl $0x0, 0x58(%rax)
jne 0x779c
movl %ecx, 0x8(%rsp)
movslq %r14d, %r13
leaq (,%r13,8), %rdi
callq 0x11a0
movq %rax, %r12
movl %r13d, %ebp
movq %r13, 0x20(%rsp)
testl %r13d, %r13d
jle 0x7595
leaq (,%rbp,8), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1110
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl 0x8(%rsp), %edx
movl %edx, %ecx
movq %rcx, 0x40(%rsp)
testl %edx, %edx
jle 0x76e0
imull %edx, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd 0x6a8f(%rip), %xmm1     # 0xe050
cvtsi2sd %edx, %xmm2
leaq 0x1(%rax), %rdx
xorpd %xmm0, %xmm0
movl $0x1, %esi
xorl %r13d, %r13d
movq %r13, %rdi
incq %r13
cmpq 0x20(%rsp), %r13
jge 0x76cd
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq (%rdi,%rax), %r10
imulq %rdi, %r10
movl %r10d, %eax
shrl $0x1f, %eax
addl %r10d, %eax
sarl %eax
movq %rdx, %r11
movq %rsi, %r14
cmpq %r14, %rdi
jae 0x763e
movq %r11, %r9
imulq %r14, %r9
movl %r9d, %r8d
shrl $0x1f, %r8d
addl %r9d, %r8d
sarl %r8d
addl %edi, %r8d
movslq %r8d, %r8
movsd (%r15,%r8,8), %xmm3
divsd %xmm2, %xmm3
addsd (%r12,%rdi,8), %xmm3
movsd %xmm3, (%r12,%rdi,8)
movq %rdi, %r8
jmp 0x7661
leal (%rax,%r14), %r8d
movslq %r8d, %r8
movsd (%r15,%r8,8), %xmm3
divsd %xmm2, %xmm3
addsd (%r12,%rdi,8), %xmm3
movsd %xmm3, (%r12,%rdi,8)
movq %r14, %r8
movq %r10, %r9
movl %r9d, %ecx
shrl $0x1f, %ecx
addl %r9d, %ecx
sarl %ecx
addl %ecx, %r8d
movl %r11d, %ecx
imull %r14d, %ecx
cmpq %r14, %rdi
movslq %r8d, %r8
movsd (%r15,%r8,8), %xmm3
divsd %xmm2, %xmm3
addsd (%r12,%r14,8), %xmm3
cmovael %r10d, %ecx
movsd %xmm3, (%r12,%r14,8)
movl %r14d, %r8d
cmovbl %edi, %r8d
movl %ecx, %r9d
shrl $0x1f, %r9d
addl %ecx, %r9d
sarl %r9d
addl %r8d, %r9d
movslq %r9d, %rcx
movsd (%r15,%rcx,8), %xmm3
divsd %xmm1, %xmm3
addsd %xmm3, %xmm0
incq %r14
incq %r11
cmpq %r14, %rbp
jne 0x7604
incq %rsi
incq %rdx
cmpq 0x40(%rsp), %r13
jne 0x75d5
jmp 0x76e4
xorpd %xmm0, %xmm0
cmpl $0x0, 0x8(%rsp)
jle 0x779c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq 0x1(%rax), %rdx
movl $0x1, %esi
xorl %edi, %edi
movq %rdi, %r8
incq %rdi
cmpq 0x20(%rsp), %rdi
jge 0x778b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leaq (%r8,%rax), %r9
imulq %r8, %r9
movsd (%r12,%r8,8), %xmm1
movq %rdx, %r10
movq %rsi, %r11
movl %r10d, %eax
imull %r11d, %eax
cmpq %r11, %r8
movl %r9d, %ecx
cmovbl %eax, %ecx
movl %r11d, %r14d
cmovbl %r8d, %r14d
cmovael %r9d, %eax
movl %ecx, %r13d
shrl $0x1f, %r13d
addl %ecx, %r13d
sarl %r13d
addl %r14d, %r13d
movslq %r13d, %rcx
movsd (%r15,%rcx,8), %xmm2
movsd (%r12,%r11,8), %xmm3
mulsd %xmm1, %xmm3
divsd %xmm0, %xmm3
subsd %xmm3, %xmm2
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
addl %r14d, %ecx
movslq %ecx, %rax
movsd %xmm2, (%r15,%rax,8)
incq %r11
incq %r10
cmpq %r11, %rbp
jne 0x7725
incq %rsi
incq %rdx
cmpq 0x40(%rsp), %rdi
jne 0x76ff
cmpl $0x0, 0x38(%rbx)
movl 0x4(%rbx), %eax
js 0x78d2
cmpl $0x2, %eax
jl 0x7953
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
incq %rax
movq %rax, 0x18(%rsp)
movl $0x1, %eax
movq %rax, 0x50(%rsp)
xorl %r14d, %r14d
leaq 0x6c87(%rip), %rsi      # 0xe45a
movq 0x38(%rsp), %r13
movq %r14, 0x20(%rsp)
leaq 0x1(%r14), %rcx
movslq 0x4(%rbx), %rax
movq %rcx, 0x8(%rsp)
cmpq %rax, %rcx
movq 0x10(%rsp), %r15
jge 0x78ae
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rdx
addq %rdx, %rcx
imulq %rdx, %rcx
movq %rcx, 0x40(%rsp)
movq 0x18(%rsp), %rbp
movq 0x50(%rsp), %r12
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %r8
imull 0x38(%rbx), %eax
movq 0x20(%rsp), %r14
leal (%rax,%r14), %ecx
movslq %ecx, %rcx
movslq (%r8,%rcx,4), %rcx
movsbl (%rdx,%rcx), %ecx
addl %r12d, %eax
cltq
movslq (%r8,%rax,4), %rax
movsbl (%rdx,%rax), %r9d
movq 0x40(%rbx), %rax
movl %ebp, %r10d
imull %r12d, %r10d
cmpq %r12, %r14
cmovael 0x40(%rsp), %r10d
movl (%rax,%r14,4), %edx
movl %r12d, %r11d
cmovbl %r14d, %r11d
movl (%rax,%r12,4), %r8d
movl %r10d, %eax
shrl $0x1f, %eax
addl %r10d, %eax
sarl %eax
addl %r11d, %eax
cltq
movsd (%r15,%rax,8), %xmm0
movq %r13, %rdi
movb $0x1, %al
movq %rbx, %r14
movq %r15, %rbx
movq %rsi, %r15
callq 0x1170
movq %r15, %rsi
movq %rbx, %r15
movq %r14, %rbx
incq %r12
movl 0x4(%r14), %eax
incq %rbp
cmpl %r12d, %eax
jg 0x7818
movslq 0x4(%rbx), %rax
decq %rax
incq 0x50(%rsp)
incq 0x18(%rsp)
movq 0x8(%rsp), %r14
cmpq %rax, %r14
jl 0x77d8
jmp 0x7953
cmpl $0x2, %eax
jl 0x7953
xorl %ebp, %ebp
movq 0x10(%rsp), %r15
movl %ebp, %r14d
leal 0x1(%r14), %ebp
cmpl 0x4(%rbx), %ebp
jge 0x794a
leal -0x1(%r14), %eax
imull %r14d, %eax
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
movl %ebp, %r13d
cmpl %r13d, %r14d
jae 0x791d
leal -0x1(%r13), %ecx
imull %r13d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
addl %r14d, %eax
jmp 0x7921
leal (%r12,%r13), %eax
incl %r13d
cltq
movsd (%r15,%rax,8), %xmm0
movq 0x38(%rsp), %rdi
leaq 0x6b34(%rip), %rsi      # 0xe46c
movl %ebp, %edx
movl %r13d, %ecx
movb $0x1, %al
callq 0x1170
cmpl 0x4(%rbx), %r13d
jl 0x7902
movl 0x4(%rbx), %eax
decl %eax
cmpl %eax, %ebp
jl 0x78de
movq 0x38(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10b0
callq 0x134d