// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, %r13
leaq 0x7438(%rip), %rsi      # 0xc380
callq 0x11c0
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x5445
movq %rbx, 0x18(%rsp)
movl 0x4(%rbx), %r15d
leal -0x1(%r15), %eax
imull %r15d, %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
movslq %eax, %rbx
leaq (,%rbx,8), %rdi
callq 0x11a0
movq %rax, 0x8(%rsp)
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
movq 0x8(%rsp), %rcx
cmpq %rax, %rbx
je 0x4fa2
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x4f93
movq %r14, 0x38(%rsp)
xorl %r14d, %r14d
pushq $0x1
popq %rsi
xorpd %xmm2, %xmm2
movq 0x18(%rsp), %r11
leal -0x1(%r15), %r12d
cmpl %r12d, %r14d
jge 0x50c5
leal 0x1(%r14), %edi
leal -0x1(%r14), %eax
imull %r14d, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movq %rsi, 0x40(%rsp)
movq %rax, 0x28(%rsp)
addl %esi, %eax
movl %eax, 0x10(%rsp)
movl %edi, 0x34(%rsp)
movl %edi, %ebx
cmpl %r15d, %ebx
jge 0x50b2
movl 0x8(%r11), %ecx
imull %ecx, %r15d
movq 0x28(%rsp), %rax
leal (%rbx,%rax), %ebp
leal -0x1(%rbx), %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %r12d
addl %r14d, %r12d
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movl %ecx, %esi
imull %esi, %esi
movl %esi, %edi
imull %r12d, %edi
imull 0x10(%rsp), %esi
xorpd %xmm0, %xmm0
cmpl %edx, %eax
je 0x5075
xorl %r8d, %r8d
movl %edi, %r9d
cmpl %r8d, %ecx
je 0x506d
leal (%rsi,%r8), %r10d
cmpl %ebx, %r14d
cmovbl %r9d, %r10d
addl %r15d, %r10d
movslq %r10d, %r10
movsd (%r13,%r10,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incl %r8d
addl %ecx, %r9d
jmp 0x5040
incl %eax
incl %edi
addl %ecx, %esi
jmp 0x5036
ucomisd %xmm2, %xmm0
jb 0x5081
sqrtsd %xmm0, %xmm0
jmp 0x508f
callq 0x1210
xorpd %xmm2, %xmm2
movq 0x18(%rsp), %r11
cmpl %ebx, %r14d
cmovbl %r12d, %ebp
movslq %ebp, %rax
movq 0x8(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
incl %ebx
movl 0x4(%r11), %r15d
incl 0x10(%rsp)
jmp 0x4feb
movq 0x40(%rsp), %rsi
incl %esi
movl 0x34(%rsp), %eax
movl %eax, %r14d
jmp 0x4fb6
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x38(%rsp), %rax
cmpl $0x0, 0x58(%rax)
je 0x51d0
movq 0x18(%rsp), %rdi
cmpl $0x0, 0x38(%rdi)
js 0x5219
pushq $0x1
popq %rbx
leaq 0x1(%r13), %rbp
xorl %r14d, %r14d
leaq 0x72a2(%rip), %r12      # 0xc39c
leal -0x1(%r15), %eax
cltq
cmpq %rax, %r14
jge 0x5291
leal (%r14,%r13), %eax
imull %r14d, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %eax, %r13d
movq %rbp, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
movslq %r15d, %rax
cmpq %rax, %rbx
jge 0x51b2
movq 0x10(%rdi), %rax
movq 0x20(%rdi), %rdx
imull 0x38(%rdi), %r15d
leal (%r15,%r14), %ecx
movslq %ecx, %rcx
movslq (%rdx,%rcx,4), %rcx
movsbl (%rax,%rcx), %ecx
addl %ebx, %r15d
movslq %r15d, %rsi
movslq (%rdx,%rsi,4), %rdx
movsbl (%rax,%rdx), %r9d
movq 0x40(%rdi), %rax
movl (%rax,%r14,4), %r10d
movl (%rax,%rbx,4), %r8d
cmpq %rbx, %r14
jae 0x517c
movl %ebp, %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
addl %r14d, %eax
jmp 0x5180
leal (%rbx,%r13), %eax
cltq
movq 0x8(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm0
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movl %r10d, %edx
movb $0x1, %al
callq 0x1170
incq %rbx
movq 0x18(%rsp), %rdi
movl 0x4(%rdi), %r15d
incq %rbp
jmp 0x5124
incq %r14
movq 0x10(%rsp), %rbx
incq %rbx
movq 0x28(%rsp), %rbp
incq %rbp
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x50fa
movl %r12d, %eax
imull %r15d, %eax
cvtsi2sd %eax, %xmm0
mulsd 0x6e6d(%rip), %xmm0     # 0xc050
movsd %xmm0, 0x10(%rsp)
movslq %r15d, %rdi
shlq $0x3, %rdi
callq 0x11a0
movq %rax, %rcx
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
cmpq %rax, %rdx
je 0x52a9
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x5206
xorl %ebx, %ebx
leaq 0x718c(%rip), %r12      # 0xc3ae
leal -0x1(%r15), %eax
cmpl %eax, %ebx
jge 0x5291
leal 0x1(%rbx), %ebp
leal -0x1(%rbx), %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %eax, %r13d
movl %ebp, %r14d
cmpl %r15d, %r14d
jge 0x528d
cmpl %r14d, %ebx
jae 0x525b
leal -0x1(%r14), %eax
imull %r14d, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
addl %ebx, %eax
jmp 0x525f
leal (%r14,%r13), %eax
incl %r14d
cltq
movq 0x8(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
movl %r14d, %ecx
movb $0x1, %al
callq 0x1170
movq 0x18(%rsp), %rax
movl 0x4(%rax), %r15d
jmp 0x523f
movl %ebp, %ebx
jmp 0x5222
movq 0x20(%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10b0
cvtsi2sd %r12d, %xmm1
xorl %r8d, %r8d
testl %r12d, %r12d
cmovlel %r8d, %r12d
movl %r15d, %edi
leaq 0x1(%r13), %r9
xorpd %xmm0, %xmm0
pushq $0x1
popq %r10
movsd 0x10(%rsp), %xmm3
cmpq %r12, %r8
je 0x53a3
leal (%r8,%r13), %eax
imull %r8d, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %esi
movq %r9, %r11
movq %r10, %rbx
cmpq %rbx, %rdi
je 0x5395
cmpq %rbx, %r8
jae 0x530b
movl %r11d, %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rbp
idivl %ebp
addl %r8d, %eax
jmp 0x530e
leal (%rsi,%rbx), %eax
cltq
movq 0x8(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm2
divsd %xmm1, %xmm2
addsd (%rcx,%r8,8), %xmm2
movsd %xmm2, (%rcx,%r8,8)
cmpq %rbx, %r8
jae 0x5340
movl %r11d, %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rbp
idivl %ebp
addl %r8d, %eax
jmp 0x5343
leal (%rsi,%rbx), %eax
cltq
movq 0x8(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm2
divsd %xmm1, %xmm2
addsd (%rcx,%rbx,8), %xmm2
movsd %xmm2, (%rcx,%rbx,8)
cmpq %rbx, %r8
jae 0x5373
movl %r11d, %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rbp
idivl %ebp
addl %r8d, %eax
jmp 0x5376
leal (%rsi,%rbx), %eax
cltq
movq 0x8(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm2
divsd %xmm3, %xmm2
addsd %xmm2, %xmm0
incq %rbx
incq %r11
jmp 0x52ec
incq %r8
incq %r10
incq %r9
jmp 0x52cd
leaq 0x1(%r13), %r8
pushq $0x1
popq %r9
xorl %r10d, %r10d
cmpq %r12, %r10
je 0x50da
leal (%r10,%r13), %eax
imull %r10d, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %esi
movq %r8, %r11
movq %r9, %rbx
cmpq %rbx, %rdi
je 0x5437
cmpq %rbx, %r10
jae 0x53e8
movl %r11d, %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rbp
idivl %ebp
addl %r10d, %eax
jmp 0x53eb
leal (%rsi,%rbx), %eax
cltq
movq 0x8(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm1
movsd (%rcx,%r10,8), %xmm2
mulsd (%rcx,%rbx,8), %xmm2
cmpq %rbx, %r10
jae 0x5418
movl %r11d, %eax
imull %ebx, %eax
cltd
pushq $0x2
popq %rbp
idivl %ebp
addl %r10d, %eax
jmp 0x541b
leal (%rsi,%rbx), %eax
divsd %xmm0, %xmm2
subsd %xmm2, %xmm1
cltq
movq 0x8(%rsp), %rdx
movsd %xmm1, (%rdx,%rax,8)
incq %rbx
incq %r11
jmp 0x53cd
incq %r10
incq %r9
incq %r8
jmp 0x53ae
movq 0xabac(%rip), %rax      # 0xfff8
movq (%rax), %rcx
leaq 0x6f68(%rip), %rdi      # 0xc3be
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
pushq $0x1
popq %rdi
callq 0x11f0