// File: /workspace/github_c_makefile_star2_O3/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 0x5e22
movq 0x10(%r14), %rdi
callq 0x10c0
movq %rax, 0x58(%rsp)
leal -0x1(%rax), %r13d
movl %r13d, 0x8(%r14)
movl 0x4(%r14), %ebp
movl %ebp, %ebx
imull %r13d, %ebx
movslq %ebx, %r15
leaq (,%r15,8), %rdi
callq 0x11b0
movq %rax, %r12
movq %rax, 0x58(%r14)
testl %r15d, %r15d
jle 0x58c5
movl %ebx, %edx
shlq $0x3, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1110
movslq (%r14), %rcx
movq %rcx, 0x18(%rsp)
testq %rcx, %rcx
jle 0x592c
leaq (,%rbp,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
testl %ebp, %ebp
jle 0x591f
movq 0x20(%r14), %rsi
addq %rcx, %rsi
xorl %edi, %edi
movl (%rsi,%rdi,4), %r8d
testl %r8d, %r8d
jle 0x5917
movq 0x50(%r14), %r9
movsd (%r9,%rdx,8), %xmm0
decl %r8d
imull %ebp, %r8d
addl %edi, %r8d
movslq %r8d, %r8
addsd (%r12,%r8,8), %xmm0
movsd %xmm0, (%r12,%r8,8)
incq %rdi
cmpq %rdi, %rbp
jne 0x58eb
incq %rdx
addq %rax, %rcx
cmpq 0x18(%rsp), %rdx
jne 0x58de
movq %r12, 0x60(%rsp)
leal -0x1(%rbp), %eax
movl %eax, 0x48(%rsp)
imull %ebp, %eax
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
movl %r13d, %r15d
imull %r13d, %r15d
imull %r15d, %r12d
movslq %r12d, %rax
movq %rax, 0x8(%rsp)
leaq (,%rax,8), %rdi
callq 0x11b0
movq %rax, %rbx
movq %rax, 0x60(%r14)
cmpl $0x0, 0x8(%rsp)
jle 0x5987
movl %r12d, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1110
cmpl $0x0, 0x18(%rsp)
movq %rbp, 0x8(%rsp)
jle 0x5ad0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl 0x48(%rsp), %ecx
movq %rcx, 0x28(%rsp)
leaq (,%rbp,4), %rcx
movq %rcx, 0x50(%rsp)
movq $0x0, 0x20(%rsp)
incq %rax
movq %rax, 0x40(%rsp)
xorl %r8d, %r8d
cmpl $0x2, %ebp
jl 0x5ab3
movq %r8, %rax
imulq %rbp, %rax
movq 0x20(%r14), %r9
leaq (%r9,%rax,4), %rax
movq %rax, 0x30(%rsp)
addq 0x20(%rsp), %r9
movl $0x1, %r10d
movq 0x40(%rsp), %r11
xorl %edx, %edx
leaq 0x1(%rdx), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rax
movl (%rax,%rdx,4), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leal (%rcx,%rdx), %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, 0x38(%rsp)
movq %r11, %rdi
movq %r10, %r11
testl %eax, %eax
jle 0x5a8a
movl (%r9,%r11,4), %esi
testl %esi, %esi
jle 0x5a8a
cmpq %r11, %rdx
jae 0x5a6e
movl %edi, %ebp
imull %r11d, %ebp
movl %ebp, %ecx
shrl $0x1f, %ecx
addl %ebp, %ecx
movq 0x8(%rsp), %rbp
sarl %ecx
addl %edx, %ecx
imull %r13d, %ecx
addl %ecx, %esi
decl %esi
imull %r13d, %esi
addl %eax, %esi
jmp 0x5a71
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)
incq %r11
incq %rdi
addl %r15d, %r12d
cmpq %r11, %rbp
jne 0x5a37
incq %r10
movq 0x38(%rsp), %r11
incq %r11
movq 0x10(%rsp), %rdx
cmpq 0x28(%rsp), %rdx
jne 0x59f5
incq %r8
movq 0x20(%rsp), %rax
addq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq 0x18(%rsp), %r8
jne 0x59c6
testl %ebp, %ebp
jle 0x607c
cvtsi2sd %r13d, %xmm2
movsd 0x7583(%rip), %xmm0     # 0xd068
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]
leaq (,%rbp,8), %rdx
movq %rbp, %rsi
shlq $0x4, %rsi
xorl %edi, %edi
xorpd %xmm3, %xmm3
pmovsxbq 0x76b5(%rip), %xmm4     # 0xd1d8
movdqa 0x7585(%rip), %xmm5     # 0xd0b0
pxor %xmm5, %xmm2
pcmpeqd %xmm6, %xmm6
pmovsxbq 0x769e(%rip), %xmm7     # 0xd1da
movq 0x60(%rsp), %r11
testl %r13d, %r13d
jle 0x5bf6
xorpd %xmm8, %xmm8
movq %rax, %r8
movq %r11, %r9
addsd (%r9), %xmm8
addq %rdx, %r9
decq %r8
jne 0x5b55
ucomisd %xmm3, %xmm8
jne 0x5b6b
jnp 0x5b9d
testl %r13d, %r13d
jle 0x5bf6
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 0x5b84
jmp 0x5bf6
testl %r13d, %r13d
jle 0x5bf6
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 0x5bd0
movsd %xmm1, (%r9)
pxor %xmm6, %xmm9
pextrb $0x8, %xmm9, %r10d
testb $0x1, %r10b
je 0x5be8
movsd %xmm1, (%r9,%rdx)
paddq %xmm7, %xmm8
addq %rsi, %r9
addq $-0x2, %r8
jne 0x5bad
incq %rdi
addq $0x8, %r11
cmpq %rbp, %rdi
jne 0x5b41
cmpl $0x1, %ebp
je 0x607c
xorps %xmm2, %xmm2
cvtsi2sd %r15d, %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movq 0x58(%rsp), %rcx
movl %ecx, %eax
andl $-0x2, %eax
movl %eax, 0x38(%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 0x7570(%rip), %xmm4     # 0xd1c4
pmovsxbd 0x756b(%rip), %xmm5     # 0xd1c8
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, 0x30(%rsp)
addl %edx, %r8d
imull %r15d, %r8d
movl %r8d, 0x10(%rsp)
movl %esi, 0x28(%rsp)
movl %esi, %r9d
testl %r13d, %r13d
jle 0x5df8
leal -0x1(%r9), %eax
imull %r9d, %eax
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
sarl %edx
addl %edi, %edx
movl %r15d, %eax
imull %edx, %eax
pxor %xmm6, %xmm6
xorl %esi, %esi
movl 0x10(%rsp), %r8d
movl %eax, %r10d
xorl %r11d, %r11d
leal (%r8,%r11), %ebp
cmpl %r9d, %edi
cmovbl %r10d, %ebp
movslq %ebp, %r14
addsd (%rbx,%r14,8), %xmm6
incl %r11d
addl %r13d, %r10d
cmpl %r11d, %r13d
jne 0x5cbf
incl %esi
addl %r13d, %r8d
incl %eax
cmpl %r13d, %esi
jne 0x5cb9
ucomisd %xmm3, %xmm6
jne 0x5cf2
jnp 0x5d52
testl %r13d, %r13d
movq 0x8(%rsp), %rbp
jle 0x5df8
movapd %xmm0, %xmm7
divsd %xmm6, %xmm7
imull %r15d, %edx
xorl %eax, %eax
movl 0x10(%rsp), %esi
movl %edx, %r8d
xorl %r10d, %r10d
leal (%rsi,%r10), %r11d
cmpl %r9d, %edi
cmovbl %r8d, %r11d
movslq %r11d, %r11
movsd (%rbx,%r11,8), %xmm6
mulsd %xmm7, %xmm6
movsd %xmm6, (%rbx,%r11,8)
incl %r10d
addl %r13d, %r8d
cmpl %r10d, %r13d
jne 0x5d18
incl %eax
addl %r13d, %esi
incl %edx
cmpl %r13d, %eax
jne 0x5d12
jmp 0x5df8
testl %r13d, %r13d
movq 0x8(%rsp), %rbp
jle 0x5df8
movl %r13d, %r8d
imull %edx, %r8d
incl %r8d
imull %r13d, %r8d
imull %r15d, %edx
xorl %r14d, %r14d
movl 0x10(%rsp), %r12d
movl 0x38(%rsp), %eax
movl %edx, %r10d
movl %r12d, %esi
movl %r8d, %r11d
movdqa %xmm4, %xmm6
movdqa %xmm6, %xmm7
pminud %xmm2, %xmm7
pcmpeqd %xmm6, %xmm7
movd %xmm7, %ebp
testb $0x1, %bpl
je 0x5db3
cmpl %r9d, %edi
movl %esi, %ebp
cmovbl %r10d, %ebp
movslq %ebp, %rbp
movsd %xmm1, (%rbx,%rbp,8)
pextrb $0x4, %xmm7, %ebp
testb $0x1, %bpl
je 0x5dd1
leal 0x1(%rsi), %ebp
cmpl %r9d, %edi
cmovbl %r11d, %ebp
movslq %ebp, %rbp
movsd %xmm1, (%rbx,%rbp,8)
paddd %xmm5, %xmm6
addl %ecx, %r11d
addl $0x2, %esi
addl %ecx, %r10d
addl $-0x2, %eax
jne 0x5d8b
incl %r14d
incl %r8d
addl %r13d, %r12d
incl %edx
cmpl %r13d, %r14d
movq 0x8(%rsp), %rbp
jne 0x5d7a
incl %r9d
addl %r15d, 0x10(%rsp)
cmpl %ebp, %r9d
jne 0x5c8c
movl 0x30(%rsp), %edx
incl %edx
movl 0x28(%rsp), %eax
cmpl 0x48(%rsp), %eax
jne 0x5c5d
jmp 0x607c
movsd 0x723e(%rip), %xmm0     # 0xd068
divsd 0x48(%r14), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x4(%r14), %r15d
movl 0x8(%r14), %ebp
movl %r15d, %r12d
imull %ebp, %r12d
movslq %r12d, %r13
leaq (,%r13,8), %rdi
callq 0x11b0
movq %rax, %rbx
movq %rax, 0x58(%r14)
testl %r13d, %r13d
jle 0x5e72
movl %r12d, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1110
movslq (%r14), %rcx
movq %rcx, 0x18(%rsp)
testq %rcx, %rcx
movsd 0x40(%rsp), %xmm1
jle 0x5edc
leaq (,%r15,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
testl %r15d, %r15d
jle 0x5ecf
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 0x5ea4
incq %rdx
addq %rax, %rcx
cmpq 0x18(%rsp), %rdx
jne 0x5e91
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 0x11b0
movq %rax, %rbx
movq %rax, 0x60(%r14)
testl %r12d, %r12d
jle 0x5f29
movl %r13d, %edx
shlq $0x3, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1110
cmpl $0x0, 0x18(%rsp)
movsd 0x40(%rsp), %xmm1
jle 0x607c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl 0x8(%rsp), %ecx
movq %rcx, 0x28(%rsp)
leaq (,%r15,4), %rcx
movq %rcx, 0x50(%rsp)
movl $0x4, %ecx
movq %rcx, 0x20(%rsp)
xorl %edi, %edi
incq %rax
movq %rax, 0x48(%rsp)
cmpl $0x2, %r15d
jl 0x605f
movq %rdi, %rax
imulq %r15, %rax
movq %rax, 0x30(%rsp)
movq 0x50(%r14), %r10
movl $0x1, %r12d
movq 0x48(%rsp), %r13
movq 0x20(%rsp), %rsi
xorl %edx, %edx
leaq 0x1(%rdx), %rax
movq %rax, 0x10(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addl %edx, %eax
imull %edx, %eax
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
movq 0x30(%rsp), %rax
addq %rdx, %rax
addl %r12d, %r8d
imull %ebp, %r8d
movq %r13, 0x8(%rsp)
movq %rsi, 0x38(%rsp)
movq %r12, %r11
cmpq %r11, %rdx
jae 0x5ffb
movl %r13d, %r9d
imull %r11d, %r9d
movl %r9d, %ecx
shrl $0x1f, %ecx
addl %r9d, %ecx
sarl %ecx
addl %edx, %ecx
imull %ebp, %ecx
movq 0x20(%r14), %r9
addl (%r9,%rsi), %ecx
leaq (%r9,%rax,4), %r9
jmp 0x6009
movq 0x20(%r14), %r9
movl (%r9,%rax,4), %ecx
addl %r8d, %ecx
addq %rsi, %r9
imull %ebp, %ecx
movslq (%r9), %r9
movslq %ecx, %rcx
addq %r9, %rcx
movsd (%r10,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%rbx,%rcx,8), %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %r11
addq $0x4, %rsi
incq %r13
addl %ebp, %r8d
cmpq %r11, %r15
jne 0x5fd1
incq %r12
movq 0x38(%rsp), %rsi
addq $0x4, %rsi
movq 0x8(%rsp), %r13
incq %r13
movq 0x10(%rsp), %rdx
cmpq 0x28(%rsp), %rdx
jne 0x5f95
incq %rdi
movq 0x20(%rsp), %rax
addq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq 0x18(%rsp), %rdi
jne 0x5f69
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq