// File: /workspace/github_c_makefile_star2_O1/debbiemarkslab[P]plmc/src/plm.c
// Function: MSACountMarginals
void MSACountMarginals(alignment_t *ali, options_t *options) {
    /* Compute first and second order marginal distributions, according to the 
       sequence weights
     */
    if (options->estimatorMAP == INFER_MAP_PLM_GAPREDUCE) {
        /* Condition the marginals on ungapped */
        ali->nCodes = strlen(ali->alphabet) - 1;

        /* First-order marginals P_i(Ai) */
        int nFi = ali->nSites * ali->nCodes;
        ali->fi = (numeric_t *) malloc(nFi * sizeof(numeric_t));
        for (int i = 0; i < nFi; i++) ali->fi[i] = 0.0;

        for (int s = 0; s < ali->nSeqs; s++)
            for (int i = 0; i < ali->nSites; i++)
                if (seq(s, i) > 0)
                    fi(i, seq(s, i) - 1) += ali->weights[s];

        /* Second-order marginals P_ij(Ai, Aj) */
        int nFij = ali->nSites * (ali->nSites - 1) / 2 * ali->nCodes * ali->nCodes;
        ali->fij = (numeric_t *) malloc(nFij * sizeof(numeric_t));
        for (int i = 0; i < nFij; i++) ali->fij[i] = 0.0;

        for (int s = 0; s < ali->nSeqs; s++)
            for (int i = 0; i < ali->nSites - 1; i++)
                for (int j = i + 1; j < ali->nSites; j++)
                    if (seq(s, i) > 0) if(seq(s, j) > 0)
                        fij(i, j, seq(s, i) - 1, seq(s, j) - 1)
                            += ali->weights[s];

        /* Normalize conditional distributions */
        for (int i = 0; i < ali->nSites; i++) {
            double fsum = 0.0;
            for (int ai = 0; ai < ali->nCodes; ai++)
                fsum += fi(i, ai);
            if (fsum != 0) {
                double fsumInv = 1.0 / fsum;
                for (int ai = 0; ai < ali->nCodes; ai++)
                    fi(i, ai) *= fsumInv;
            } else {
                /* Handle empty columns */
                numeric_t flatF = 1.0 / ((numeric_t) ali->nCodes);
                for (int ai = 0; ai < ali->nCodes; ai++)
                    fi(i, ai) = flatF;
            }
        }
        for (int i = 0; i < ali->nSites - 1; i++)
            for (int j = i + 1; j < ali->nSites; j++) {
                double fsum = 0.0;
                for (int ai = 0; ai < ali->nCodes; ai++)
                    for (int aj = 0; aj < ali->nCodes; aj++)
                        fsum += fij(i, j, ai, aj);
                if (fsum != 0) {
                    double fsumInv = 1.0 / fsum;
                    for (int ai = 0; ai < ali->nCodes; ai++)
                        for (int aj = 0; aj < ali->nCodes; aj++)
                            fij(i, j, ai, aj) *= fsumInv;
                } else {
                    /* Handle pairs of empty columns */
                    numeric_t flatF = 1.0 / ((numeric_t) (ali->nCodes * ali->nCodes));
                    for (int ai = 0; ai < ali->nCodes; ai++)
                        for (int aj = 0; aj < ali->nCodes; aj++)
                            fij(i, j, ai, aj) = flatF;
                }
            }

    } else {
        /* Compute regular marginals */
        numeric_t Zinv = 1.0 / ali->nEff;

        /* First-order marginals P_i(Ai) */
        int nFi = ali->nSites * ali->nCodes;
        ali->fi = (numeric_t *) malloc(nFi * sizeof(numeric_t));
        for (int i = 0; i < nFi; i++) ali->fi[i] = 0.0;

        for (int s = 0; s < ali->nSeqs; s++)
            for (int i = 0; i < ali->nSites; i++)
                fi(i, seq(s, i)) += ali->weights[s] * Zinv;

        /* Second-order marginals P_ij(Ai, Aj) */
        int nFij = ali->nSites * (ali->nSites - 1) / 2 * ali->nCodes * ali->nCodes;
        ali->fij = (numeric_t *) malloc(nFij * sizeof(numeric_t));
        for (int i = 0; i < nFij; i++) ali->fij[i] = 0.0;

        for (int s = 0; s < ali->nSeqs; s++)
            for (int i = 0; i < ali->nSites - 1; i++)
                for (int j = i + 1; j < ali->nSites; j++)
                    fij(i, j, seq(s, i), seq(s, j)) += ali->weights[s] * Zinv;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %r14
cmpl $0x1, 0x18(%rsi)
jne 0x6a04
movq 0x10(%r14), %rdi
callq 0x10c0
movq %rax, 0x50(%rsp)
leal -0x1(%rax), %r13d
movl %r13d, 0x8(%r14)
movl 0x4(%r14), %ecx
movq %rcx, 0x8(%rsp)
movl %ecx, %ebx
imull %r13d, %ebx
movslq %ebx, %r15
leaq (,%r15,8), %rdi
callq 0x11a0
movq %rax, %r12
movq %rax, 0x58(%r14)
testl %r15d, %r15d
jle 0x645a
movl %ebx, %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1110
movslq (%r14), %rcx
movq %rcx, 0x20(%rsp)
testq %rcx, %rcx
jle 0x64ce
movq 0x8(%rsp), %rax
leaq (,%rax,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x8(%rsp), %r10
cmpl $0x0, 0x8(%rsp)
jle 0x64c1
movq 0x20(%r14), %rsi
addq %rcx, %rsi
xorl %edi, %edi
movl (%rsi,%rdi,4), %r8d
testl %r8d, %r8d
jle 0x64b9
movq 0x50(%r14), %r9
movsd (%r9,%rdx,8), %xmm0
decl %r8d
imull %r10d, %r8d
addl %edi, %r8d
movslq %r8d, %r8
addsd (%r12,%r8,8), %xmm0
movsd %xmm0, (%r12,%r8,8)
incq %rdi
cmpq %rdi, %r10
jne 0x648d
incq %rdx
addq %rax, %rcx
cmpq 0x20(%rsp), %rdx
jne 0x647d
movq %r12, 0x58(%rsp)
movq 0x8(%rsp), %rcx
leal -0x1(%rcx), %eax
movl %eax, 0x30(%rsp)
imull %ecx, %eax
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
movl %r13d, %r15d
imull %r13d, %r15d
imull %r15d, %r12d
movslq %r12d, %rbp
leaq (,%rbp,8), %rdi
callq 0x11a0
movq %rax, %rbx
movq %rax, 0x60(%r14)
testl %ebp, %ebp
jle 0x6526
movl %r12d, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1110
cmpl $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rcx
jle 0x666e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl 0x30(%rsp), %edx
movq %rdx, 0x18(%rsp)
leaq (,%rcx,4), %rdx
movq %rdx, 0x48(%rsp)
movq $0x0, 0x10(%rsp)
incq %rax
movq %rax, 0x60(%rsp)
xorl %r8d, %r8d
cmpl $0x2, %ecx
jl 0x6651
movq %r8, %rax
imulq %rcx, %rax
movq 0x20(%r14), %r9
leaq (%r9,%rax,4), %rax
movq %rax, 0x28(%rsp)
addq 0x10(%rsp), %r9
movl $0x1, %r10d
movq 0x60(%rsp), %r11
xorl %edx, %edx
leaq 0x1(%rdx), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rax
movl (%rax,%rdx,4), %eax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
addl %edx, %esi
imull %edx, %esi
movl %esi, %edi
shrl $0x1f, %edi
addl %esi, %edi
sarl %edi
addl %r10d, %edi
imull %r13d, %edi
leal (%rax,%rdi), %r12d
decl %r12d
imull %r13d, %r12d
movq %r11, 0x40(%rsp)
movq %r11, %rdi
movq %r10, %r11
testl %eax, %eax
jle 0x6628
movl (%r9,%r11,4), %esi
testl %esi, %esi
jle 0x6628
cmpq %r11, %rdx
jae 0x6607
movl %edi, %ebp
imull %r11d, %ebp
movl %ebp, %ecx
shrl $0x1f, %ecx
addl %ebp, %ecx
sarl %ecx
addl %edx, %ecx
imull %r13d, %ecx
addl %ecx, %esi
decl %esi
imull %r13d, %esi
addl %eax, %esi
jmp 0x660a
addl %r12d, %esi
movq 0x50(%r14), %rcx
movsd (%rcx,%r8,8), %xmm0
movslq %esi, %rcx
addsd -0x8(%rbx,%rcx,8), %xmm0
movsd %xmm0, -0x8(%rbx,%rcx,8)
movq 0x8(%rsp), %rcx
incq %r11
incq %rdi
addl %r15d, %r12d
cmpq %r11, %rcx
jne 0x65d5
incq %r10
movq 0x40(%rsp), %r11
incq %r11
movq 0x38(%rsp), %rdx
cmpq 0x18(%rsp), %rdx
jne 0x6594
incq %r8
movq 0x10(%rsp), %rax
addq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq 0x20(%rsp), %r8
jne 0x6565
testl %ecx, %ecx
movq 0x58(%rsp), %r11
jle 0x67a4
cvtsi2sd %r13d, %xmm2
movsd 0x79e0(%rip), %xmm0     # 0xe068
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movl %r13d, %eax
leal 0x1(%r13), %ecx
andl $-0x2, %ecx
leaq -0x1(%rax), %rdx
movq %rdx, %xmm2
pshufd $0x44, %xmm2, %xmm2     # xmm2 = xmm2[0,1,0,1]
movq 0x8(%rsp), %rsi
leaq (,%rsi,8), %rdx
shlq $0x4, %rsi
xorl %edi, %edi
xorpd %xmm3, %xmm3
pmovsxbq 0x7b20(%rip), %xmm4     # 0xe1e8
movdqa 0x79e0(%rip), %xmm5     # 0xe0b0
pxor %xmm5, %xmm2
pcmpeqd %xmm6, %xmm6
pmovsxbq 0x7b09(%rip), %xmm7     # 0xe1ea
xorpd %xmm8, %xmm8
testl %r13d, %r13d
jle 0x66fe
movq %rax, %r8
movq %r11, %r9
addsd (%r9), %xmm8
addq %rdx, %r9
decq %r8
jne 0x66f1
ucomisd %xmm3, %xmm8
jne 0x6707
jnp 0x6739
testl %r13d, %r13d
jle 0x6792
movapd %xmm0, %xmm9
divsd %xmm8, %xmm9
movq %rax, %r8
movq %r11, %r9
movsd (%r9), %xmm8
mulsd %xmm9, %xmm8
movsd %xmm8, (%r9)
addq %rdx, %r9
decq %r8
jne 0x6720
jmp 0x6792
testl %r13d, %r13d
jle 0x6792
movq %rcx, %r8
movq %r11, %r9
movdqa %xmm4, %xmm8
movdqa %xmm8, %xmm9
pxor %xmm5, %xmm9
pcmpgtq %xmm2, %xmm9
movd %xmm9, %r10d
notl %r10d
testb $0x1, %r10b
je 0x676c
movsd %xmm1, (%r9)
pxor %xmm6, %xmm9
pextrb $0x8, %xmm9, %r10d
testb $0x1, %r10b
je 0x6784
movsd %xmm1, (%r9,%rdx)
paddq %xmm7, %xmm8
addq %rsi, %r9
addq $-0x2, %r8
jne 0x6749
incq %rdi
addq $0x8, %r11
cmpq 0x8(%rsp), %rdi
jne 0x66e1
cmpl $0x2, 0x8(%rsp)
jl 0x6c55
xorps %xmm2, %xmm2
cvtsi2sd %r15d, %xmm2
movsd 0x78a9(%rip), %xmm0     # 0xe068
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movq 0x50(%rsp), %rcx
movl %ecx, %eax
andl $-0x2, %eax
movl %eax, 0x40(%rsp)
leal -0x2(%rcx), %eax
movd %eax, %xmm2
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
leal -0x2(,%rcx,2), %ecx
xorl %eax, %eax
movl $0x1, %edx
xorpd %xmm3, %xmm3
pmovsxbd 0x79d8(%rip), %xmm4     # 0xe1d4
pmovsxbd 0x79d3(%rip), %xmm5     # 0xe1d8
movl %eax, %edi
leal 0x1(%rdi), %esi
leal -0x1(%rdi), %eax
imull %edi, %eax
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
movl %edx, 0x10(%rsp)
addl %edx, %r8d
movl %r15d, %edx
imull %r8d, %edx
movl %r15d, %eax
imull %r8d, %eax
movl %eax, 0x18(%rsp)
imull %r15d, %r8d
movl %r8d, 0x28(%rsp)
movl %esi, 0x20(%rsp)
movl %esi, %r11d
testl %r13d, %r13d
movl %edx, 0x38(%rsp)
jle 0x689f
leal -0x1(%r11), %eax
imull %r11d, %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
addl %edi, %esi
imull %r15d, %esi
pxor %xmm6, %xmm6
xorl %eax, %eax
movl %esi, %r8d
xorl %r9d, %r9d
leal (%rdx,%r9), %r10d
cmpl %r11d, %edi
cmovbl %r8d, %r10d
movslq %r10d, %r10
addsd (%rbx,%r10,8), %xmm6
incl %r9d
addl %r13d, %r8d
cmpl %r9d, %r13d
jne 0x6872
incl %eax
addl %r13d, %edx
incl %esi
cmpl %r13d, %eax
jne 0x686c
jmp 0x68a3
pxor %xmm6, %xmm6
ucomisd %xmm3, %xmm6
jne 0x68ab
jnp 0x6919
testl %r13d, %r13d
jle 0x69cc
movapd %xmm0, %xmm7
divsd %xmm6, %xmm7
leal -0x1(%r11), %edx
imull %r11d, %edx
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
addl %edi, %eax
imull %r15d, %eax
xorl %edx, %edx
movl 0x28(%rsp), %esi
movl %eax, %r8d
xorl %r9d, %r9d
leal (%rsi,%r9), %r10d
cmpl %r11d, %edi
cmovbl %r8d, %r10d
movslq %r10d, %r10
movsd (%rbx,%r10,8), %xmm6
mulsd %xmm7, %xmm6
movsd %xmm6, (%rbx,%r10,8)
incl %r9d
addl %r13d, %r8d
cmpl %r9d, %r13d
jne 0x68df
incl %edx
addl %r13d, %esi
incl %eax
cmpl %r13d, %edx
jne 0x68d9
jmp 0x69cc
testl %r13d, %r13d
jle 0x69cc
leal -0x1(%r11), %eax
imull %r11d, %eax
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
addl %edi, %r8d
movl %r13d, %r9d
imull %r8d, %r9d
incl %r9d
imull %r13d, %r9d
imull %r15d, %r8d
xorl %esi, %esi
movl 0x18(%rsp), %r10d
movl 0x40(%rsp), %eax
movl %r8d, %r14d
movl %r10d, %edx
movl %r9d, %r12d
movdqa %xmm4, %xmm6
movdqa %xmm6, %xmm7
pminud %xmm2, %xmm7
pcmpeqd %xmm6, %xmm7
movd %xmm7, %ebp
testb $0x1, %bpl
je 0x698c
cmpl %r11d, %edi
movl %edx, %ebp
cmovbl %r14d, %ebp
movslq %ebp, %rbp
movsd %xmm1, (%rbx,%rbp,8)
pextrb $0x4, %xmm7, %ebp
testb $0x1, %bpl
je 0x69aa
leal 0x1(%rdx), %ebp
cmpl %r11d, %edi
cmovbl %r12d, %ebp
movslq %ebp, %rbp
movsd %xmm1, (%rbx,%rbp,8)
paddd %xmm5, %xmm6
addl %ecx, %r12d
addl $0x2, %edx
addl %ecx, %r14d
addl $-0x2, %eax
jne 0x6964
incl %esi
incl %r9d
addl %r13d, %r10d
incl %r8d
cmpl %r13d, %esi
jne 0x6953
incl %r11d
movl 0x38(%rsp), %edx
addl %r15d, %edx
addl %r15d, 0x18(%rsp)
addl %r15d, 0x28(%rsp)
cmpl 0x8(%rsp), %r11d
jne 0x6846
movl 0x10(%rsp), %edx
incl %edx
movl 0x20(%rsp), %eax
cmpl 0x30(%rsp), %eax
jne 0x6805
jmp 0x6c55
movsd 0x765c(%rip), %xmm0     # 0xe068
divsd 0x48(%r14), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x4(%r14), %r15d
movl 0x8(%r14), %ebp
movl %r15d, %r12d
imull %ebp, %r12d
movslq %r12d, %r13
leaq (,%r13,8), %rdi
callq 0x11a0
movq %rax, %rbx
movq %rax, 0x58(%r14)
testl %r13d, %r13d
jle 0x6a54
movl %r12d, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1110
movslq (%r14), %rcx
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
movsd 0x30(%rsp), %xmm1
jle 0x6abe
leaq (,%r15,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
testl %r15d, %r15d
jle 0x6ab1
movq 0x50(%r14), %rsi
movq 0x20(%r14), %rdi
addq %rcx, %rdi
xorl %r8d, %r8d
movslq (%rdi,%r8,4), %r9
movslq %r15d, %r10
imulq %r9, %r10
addq %r8, %r10
movsd (%rsi,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rbx,%r10,8), %xmm0
movsd %xmm0, (%rbx,%r10,8)
incq %r8
cmpq %r8, %r15
jne 0x6a86
incq %rdx
addq %rax, %rcx
cmpq 0x10(%rsp), %rdx
jne 0x6a73
leal -0x1(%r15), %eax
movl %eax, 0x8(%rsp)
imull %r15d, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movl %ebp, %r13d
imull %ebp, %r13d
imull %ecx, %r13d
movslq %r13d, %r12
leaq (,%r12,8), %rdi
callq 0x11a0
movq %rax, %rbx
movq %rax, 0x60(%r14)
testl %r12d, %r12d
jle 0x6b0b
movl %r13d, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1110
cmpl $0x0, 0x10(%rsp)
movsd 0x30(%rsp), %xmm1
jle 0x6c55
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl 0x8(%rsp), %ecx
movq %rcx, 0x28(%rsp)
leaq (,%r15,4), %rcx
movq %rcx, 0x20(%rsp)
movl $0x4, %ecx
movq %rcx, 0x18(%rsp)
xorl %edi, %edi
incq %rax
movq %rax, 0x48(%rsp)
cmpl $0x2, %r15d
jl 0x6c38
movq %rdi, %r9
imulq %r15, %r9
movq 0x50(%r14), %r10
movl $0x1, %r12d
movq 0x48(%rsp), %r13
movq 0x18(%rsp), %rsi
xorl %edx, %edx
leaq 0x1(%rdx), %rax
movq %rax, 0x38(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addl %edx, %eax
imull %edx, %eax
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
addl %r12d, %r8d
imull %ebp, %r8d
movq %r13, 0x8(%rsp)
movq %rsi, 0x40(%rsp)
movq %r12, %r11
cmpq %r11, %rdx
jae 0x6bd1
movl %r13d, %ecx
imull %r11d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
addl %edx, %eax
imull %ebp, %eax
movq 0x20(%r14), %rcx
addl (%rcx,%rsi), %eax
leaq (%rcx,%rdx,4), %rcx
leaq (%rcx,%r9,4), %rcx
jmp 0x6be3
movq 0x20(%r14), %rcx
leaq (%rcx,%rdx,4), %rax
movl (%rax,%r9,4), %eax
addl %r8d, %eax
addq %rsi, %rcx
imull %ebp, %eax
movslq (%rcx), %rcx
cltq
addq %rcx, %rax
movsd (%r10,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rbx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %r11
addq $0x4, %rsi
incq %r13
addl %ebp, %r8d
cmpq %r11, %r15
jne 0x6ba6
incq %r12
movq 0x40(%rsp), %rsi
addq $0x4, %rsi
movq 0x8(%rsp), %r13
incq %r13
movq 0x38(%rsp), %rdx
cmpq 0x28(%rsp), %rdx
jne 0x6b72
incq %rdi
movq 0x18(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq 0x10(%rsp), %rdi
jne 0x6b4b
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq