// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r13
leaq 0x6d02(%rip), %rsi      # 0xd430
callq 0x11d0
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x6cc4
movl 0x4(%rbx), %r14d
leal -0x1(%r14), %ebp
movl %ebp, 0x4(%rsp)
imull %r14d, %ebp
movl %ebp, %r15d
shrl $0x1f, %r15d
addl %ebp, %r15d
sarl %r15d
movslq %r15d, %rdi
shlq $0x3, %rdi
callq 0x11b0
movq %rax, 0x10(%rsp)
cmpl $0x2, %ebp
jl 0x678b
decl %r15d
leaq 0x8(,%r15,8), %rdx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x1110
cmpl $0x2, %r14d
jl 0x68c0
movq %r12, 0x18(%rsp)
xorl %edx, %edx
movl $0x1, %ecx
xorpd %xmm2, %xmm2
movl %edx, %ebp
leal 0x1(%rbp), %edx
cmpl %r14d, %edx
movq %rcx, 0x28(%rsp)
movl %edx, 0x20(%rsp)
jge 0x689c
leal -0x1(%rbp), %eax
imull %ebp, %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movq %rsi, 0x8(%rsp)
leal (%rsi,%rcx), %r12d
movl %edx, %r15d
movl 0x8(%rbx), %eax
testl %eax, %eax
jle 0x6842
imull %eax, %r14d
leal -0x1(%r15), %edx
imull %r15d, %edx
movl %edx, %ecx
shrl $0x1f, %ecx
addl %edx, %ecx
sarl %ecx
movl %eax, %edx
imull %eax, %edx
addl %ebp, %ecx
imull %edx, %ecx
imull %r12d, %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movl %ecx, %edi
xorl %r8d, %r8d
leal (%rdx,%r8), %r9d
cmpl %r15d, %ebp
cmovbl %edi, %r9d
addl %r14d, %r9d
movslq %r9d, %r9
movsd (%r13,%r9,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incl %r8d
addl %eax, %edi
cmpl %r8d, %eax
jne 0x680c
incl %esi
addl %eax, %edx
incl %ecx
cmpl %eax, %esi
jne 0x6807
jmp 0x6846
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jb 0x6852
sqrtsd %xmm0, %xmm0
jmp 0x685b
callq 0x1220
xorpd %xmm2, %xmm2
cmpl %r15d, %ebp
jae 0x6875
leal -0x1(%r15), %ecx
imull %r15d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
addl %ebp, %eax
jmp 0x687d
movq 0x8(%rsp), %rax
addl %r15d, %eax
cltq
movq 0x10(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
incl %r15d
movl 0x4(%rbx), %r14d
incl %r12d
cmpl %r14d, %r15d
jl 0x67d7
leal -0x1(%r14), %eax
movq 0x28(%rsp), %rcx
incl %ecx
movl 0x20(%rsp), %edx
cmpl %eax, %edx
jl 0x67a5
movl %eax, %ebp
imull %r14d, %ebp
movq 0x18(%rsp), %r12
jmp 0x68c4
movl 0x4(%rsp), %eax
cmpl $0x0, 0x58(%r12)
movq 0x10(%rsp), %r13
je 0x69d8
cmpl $0x0, 0x38(%rbx)
js 0x6c29
cmpl $0x2, %r14d
jl 0x6cac
xorl %ebp, %ebp
leaq 0x6b5a(%rip), %r12      # 0xd44c
leaq 0x1(%rbp), %rcx
movslq %r14d, %rax
movq %rcx, 0x8(%rsp)
cmpq %rax, %rcx
jge 0x69bf
movabsq $0x100000000, %rax      # imm = 0x100000000
addl %ebp, %eax
decl %eax
imull %ebp, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %r13
incq %r13
movq %rbp, %rax
leaq 0x1(%rax), %r15
cmpq %r15, %rbp
jae 0x6952
movabsq $0x100000000, %rcx      # imm = 0x100000000
addl %eax, %ecx
movl %r15d, %edx
imull %ecx, %edx
movl %edx, %esi
shrl $0x1f, %esi
addl %edx, %esi
sarl %esi
addl %ebp, %esi
jmp 0x6956
leal (%rax,%r13), %esi
movq 0x10(%rbx), %rdx
movq 0x20(%rbx), %rdi
imull 0x38(%rbx), %r14d
leal (%r14,%rbp), %ecx
movslq %ecx, %rcx
movslq (%rdi,%rcx,4), %rcx
movsbl (%rdx,%rcx), %ecx
leal 0x1(%rax,%r14), %r8d
movslq %r8d, %r8
movslq (%rdi,%r8,4), %rdi
movsbl (%rdx,%rdi), %r9d
movq 0x40(%rbx), %rdi
movl (%rdi,%rbp,4), %edx
movl 0x4(%rdi,%rax,4), %r8d
movslq %esi, %rax
movq 0x10(%rsp), %rsi
movsd (%rsi,%rax,8), %xmm0
movq 0x30(%rsp), %rdi
movq %r12, %rsi
movb $0x1, %al
callq 0x1180
movl 0x4(%rbx), %r14d
leal 0x1(%r15), %ecx
movq %r15, %rax
cmpl %ecx, %r14d
jg 0x692a
leal -0x1(%r14), %eax
cltq
movq 0x8(%rsp), %rbp
cmpq %rax, %rbp
jl 0x68f2
jmp 0x6cac
movl %eax, 0x4(%rsp)
movslq %r14d, %r15
leaq (,%r15,8), %rdi
callq 0x11b0
movq %rax, %r12
movl %r14d, %eax
movq %r15, 0x20(%rsp)
testl %r15d, %r15d
jle 0x6a14
leaq (,%rax,8), %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %r15
callq 0x1110
movq %r15, %rax
movq %rax, 0x8(%rsp)
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
jle 0x68d5
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
mulsd 0x661c(%rip), %xmm1     # 0xd050
movabsq $0x100000000, %rax      # imm = 0x100000000
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
movl %ecx, %ecx
movq %rcx, 0x18(%rsp)
movq 0x8(%rsp), %rcx
leaq -0x1(%rcx), %rdx
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorl %r8d, %r8d
leaq 0x1(%r8), %rcx
movq %rcx, 0x28(%rsp)
cmpq 0x20(%rsp), %rcx
jge 0x6b62
leal (%rax,%r8), %ecx
decl %ecx
imull %r8d, %ecx
movl %ecx, %r9d
shrl $0x1f, %r9d
addl %ecx, %r9d
sarl %r9d
movslq %r9d, %r9
leaq 0x1(%r9), %r10
leaq (%rsi,%rax), %r11
leaq (,%r9,8), %r15
addq %r13, %r15
shlq $0x20, %r9
movq %r8, %rbp
leaq 0x1(%rbp), %rcx
cmpq %rcx, %r8
jae 0x6b05
leal (%rax,%rbp), %edi
movl %ecx, %r13d
imull %edi, %r13d
movl %r13d, %edi
shrl $0x1f, %edi
addl %r13d, %edi
movq 0x10(%rsp), %r13
sarl %edi
addl %r8d, %edi
movslq %edi, %rdi
movsd (%r13,%rdi,8), %xmm3
divsd %xmm2, %xmm3
addsd (%r12,%r8,8), %xmm3
movsd %xmm3, (%r12,%r8,8)
movsd (%r13,%rdi,8), %xmm3
movapd %xmm3, %xmm4
divsd %xmm2, %xmm4
addsd 0x8(%r12,%rbp,8), %xmm4
movsd %xmm4, 0x8(%r12,%rbp,8)
jmp 0x6b4b
leal (%r10,%rbp), %edi
movslq %edi, %rdi
movsd (%r13,%rdi,8), %xmm3
divsd %xmm2, %xmm3
addsd (%r12,%r8,8), %xmm3
movsd %xmm3, (%r12,%r8,8)
movsd 0x8(%r15,%rbp,8), %xmm3
divsd %xmm2, %xmm3
addsd 0x8(%r12,%rbp,8), %xmm3
movsd %xmm3, 0x8(%r12,%rbp,8)
leaq (%r11,%r9), %rdi
sarq $0x1d, %rdi
movsd (%r13,%rdi), %xmm3
divsd %xmm1, %xmm3
addsd %xmm3, %xmm0
addq %rax, %r9
movq %rcx, %rbp
cmpq %rcx, %rdx
jne 0x6aa6
addq %rax, %rsi
movq 0x28(%rsp), %r8
cmpq 0x18(%rsp), %r8
jne 0x6a5e
cmpl $0x0, 0x4(%rsp)
jle 0x68d5
movl $0x1, %edx
xorl %edi, %edi
movq %rax, %rsi
movq %rdi, %r8
incq %rdi
cmpq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rbp
jge 0x6c13
leal (%rax,%r8), %ecx
decl %ecx
imull %r8d, %ecx
movl %ecx, %r9d
shrl $0x1f, %r9d
addl %ecx, %r9d
sarl %r9d
movsd (%r12,%r8,8), %xmm1
movq %rsi, %r10
movq %rdx, %r11
cmpq %r11, %r8
jae 0x6bde
movl %r10d, %ebp
imull %r11d, %ebp
movl %ebp, %ecx
shrl $0x1f, %ecx
addl %ebp, %ecx
movq 0x8(%rsp), %rbp
sarl %ecx
addl %r8d, %ecx
jmp 0x6be2
leal (%r9,%r11), %ecx
movslq %ecx, %r15
movsd (%r13,%r15,8), %xmm2
movsd (%r12,%r11,8), %xmm3
mulsd %xmm1, %xmm3
divsd %xmm0, %xmm3
subsd %xmm3, %xmm2
movslq %ecx, %rcx
movsd %xmm2, (%r13,%rcx,8)
incq %r11
incq %r10
cmpq %r11, %rbp
jne 0x6bbf
incq %rdx
incq %rsi
cmpq 0x18(%rsp), %rdi
jne 0x6b8a
jmp 0x68d5
cmpl $0x2, %r14d
jl 0x6cac
xorl %ebp, %ebp
movl %ebp, %r15d
leal 0x1(%r15), %ebp
cmpl %r14d, %ebp
jge 0x6ca4
leal -0x1(%r15), %eax
imull %r15d, %eax
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
movl %ebp, %r13d
cmpl %r13d, %r15d
jae 0x6c70
leal -0x1(%r13), %ecx
imull %r13d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
addl %r15d, %eax
jmp 0x6c74
leal (%r12,%r13), %eax
incl %r13d
cltq
movq 0x10(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
movq 0x30(%rsp), %rdi
leaq 0x67cf(%rip), %rsi      # 0xd45e
movl %ebp, %edx
movl %r13d, %ecx
movb $0x1, %al
callq 0x1180
movl 0x4(%rbx), %r14d
cmpl %r14d, %r13d
jl 0x6c55
leal -0x1(%r14), %eax
cmpl %eax, %ebp
jl 0x6c31
movq 0x30(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10b0
callq 0x135d