// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %r14
cmpl $0x1, 0x18(%rsi)
jne 0x43ba
movq 0x10(%r14), %rdi
callq 0x10c0
movq %rax, %rbx
decl %ebx
movl %ebx, 0x8(%r14)
movslq 0x4(%r14), %r12
movslq %ebx, %r15
imulq %r12, %r15
movq %r15, %rdi
shlq $0x3, %rdi
callq 0x11a0
movq %rax, 0x58(%r14)
xorl %edx, %edx
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
cmpq %rdx, %rcx
je 0x4403
andq $0x0, (%rax,%rdx,8)
incq %rdx
jmp 0x43ab
movsd 0x48(%r14), %xmm0
movsd %xmm0, (%rsp)
movslq 0x4(%r14), %r15
movslq 0x8(%r14), %r12
movq %r12, %rbx
imulq %r15, %rbx
movq %rbx, %rdi
shlq $0x3, %rdi
callq 0x11a0
movq %rax, 0x58(%r14)
xorl %ecx, %ecx
testl %ebx, %ebx
movl $0x0, %edx
cmovgl %ebx, %edx
cmpq %rcx, %rdx
je 0x448b
andq $0x0, (%rax,%rcx,8)
incq %rcx
jmp 0x43f0
movl (%r14), %ecx
xorl %r10d, %r10d
testl %r12d, %r12d
movl $0x0, %r11d
cmovgl %r12d, %r11d
testl %ecx, %ecx
cmovlel %r10d, %ecx
movq %rcx, 0x8(%rsp)
leaq (,%r12,4), %rcx
movq %rcx, 0x18(%rsp)
xorl %ecx, %ecx
movq %r11, %rdx
movq %r10, %rsi
movq %rax, %rdi
cmpq 0x8(%rsp), %rcx
je 0x4511
subq $0x1, %rdx
jb 0x4481
movq 0x20(%r14), %r8
movl (%r8,%rsi), %r8d
testl %r8d, %r8d
jle 0x4477
movq 0x50(%r14), %r9
movsd (%r9,%rcx,8), %xmm0
decl %r8d
imull %r12d, %r8d
movslq %r8d, %r8
addsd (%rdi,%r8,8), %xmm0
movsd %xmm0, (%rdi,%r8,8)
addq $0x8, %rdi
addq $0x4, %rsi
jmp 0x4444
incq %rcx
addq 0x18(%rsp), %r10
jmp 0x4430
movsd 0x7bd5(%rip), %xmm1     # 0xc068
divsd (%rsp), %xmm1
movl (%r14), %r13d
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
testl %r13d, %r13d
cmovlel %ecx, %r13d
leaq (,%r15,4), %rsi
movq %rsi, 0x8(%rsp)
xorl %esi, %esi
movq %rdx, %rdi
movq %rcx, %r8
movq %rax, %r9
cmpq %r13, %rsi
je 0x4567
subq $0x1, %rdi
jb 0x4507
movq 0x20(%r14), %r10
movq 0x50(%r14), %r11
movslq (%r10,%r8), %r10
imulq %r15, %r10
movsd (%r11,%rsi,8), %xmm0
mulsd %xmm1, %xmm0
addsd (%r9,%r10,8), %xmm0
movsd %xmm0, (%r9,%r10,8)
addq $0x8, %r9
addq $0x4, %r8
jmp 0x44d1
incq %rsi
addq 0x8(%rsp), %rcx
jmp 0x44bf
movq %r11, 0x30(%rsp)
movq %rax, 0x28(%rsp)
leal -0x1(%r12), %eax
movq %rax, %r13
imull %r12d, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %ebx, %ecx
imull %ebx, %ecx
movl %ecx, (%rsp)
imull %ecx, %eax
movslq %eax, %r15
leaq (,%r15,8), %rdi
callq 0x11a0
movq %rax, %rcx
movq %rax, 0x60(%r14)
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
cmpq %rax, %r15
je 0x45b9
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x4558
movsd %xmm1, 0x28(%rsp)
leal -0x1(%r15), %eax
movq %rax, %rbp
imull %r15d, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %r12d, %ecx
imull %r12d, %ecx
imull %eax, %ecx
movslq %ecx, %rbx
leaq (,%rbx,8), %rdi
callq 0x11a0
movq %rax, %rcx
movq %rax, 0x60(%r14)
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
cmpq %rax, %rbx
je 0x469f
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x45a6
xorl %r8d, %r8d
testl %r13d, %r13d
cmovlel %r8d, %r13d
movq %r13, 0x20(%rsp)
xorl %r10d, %r10d
pushq $0x2
popq %r13
cmpq 0x8(%rsp), %r10
je 0x47a8
movq %r10, %r11
imulq %r12, %r11
xorl %r9d, %r9d
pushq $0x1
popq %rsi
cmpq 0x20(%rsp), %r9
je 0x4692
movslq %esi, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addl %r9d, %eax
imull %r9d, %eax
cltd
movl %esi, %edi
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %esi
movl %edi, 0x10(%rsp)
addl %edi, %esi
imull %ebx, %esi
decl %esi
cmpq %r12, %r15
jge 0x4684
movq 0x20(%r14), %rax
leaq (%rax,%r9,4), %rdx
movl (%rdx,%r11,4), %ebp
testl %ebp, %ebp
jle 0x467d
addq %r8, %rax
movl (%rax,%r15,4), %edi
testl %edi, %edi
jle 0x467d
movq 0x50(%r14), %rax
movsd (%rax,%r10,8), %xmm0
movl %r15d, %eax
cmpq %rax, %r9
jae 0x4667
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addl %r15d, %eax
imull %r15d, %eax
cltd
idivl %r13d
addl %r9d, %eax
imull %ebx, %eax
addl %eax, %edi
decl %edi
imull %ebx, %edi
jmp 0x466c
addl %esi, %ebp
imull %ebx, %ebp
addl %ebp, %edi
movslq %edi, %rax
addsd -0x8(%rcx,%rax,8), %xmm0
movsd %xmm0, -0x8(%rcx,%rax,8)
incq %r15
addl %ebx, %esi
jmp 0x4616
incq %r9
movl 0x10(%rsp), %esi
incl %esi
jmp 0x45e7
incq %r10
addq 0x18(%rsp), %r8
jmp 0x45cf
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %edx, %edx
testl %ebp, %ebp
movl $0x0, %eax
movq %rax, (%rsp)
cmovlel %edx, %ebp
movq %rbp, 0x10(%rsp)
xorl %r9d, %r9d
movsd 0x28(%rsp), %xmm1
movq %r13, 0x18(%rsp)
pushq $0x2
popq %rbp
cmpq %r13, %r9
je 0x4996
movq %r9, %rax
imulq %r15, %rax
movq %rax, 0x20(%rsp)
xorl %r11d, %r11d
pushq $0x1
popq %r13
cmpq 0x10(%rsp), %r11
je 0x478e
movslq %r13d, %rbx
movq (%rsp), %rax
leaq (%rax,%rbx,4), %r8
leal (%r11,%rdi), %eax
imull %r11d, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %esi
movq 0x20(%rsp), %rax
leaq (%r11,%rax), %r10
addl %r13d, %esi
imull %r12d, %esi
cmpq %r15, %rbx
jge 0x4783
movq 0x50(%r14), %rax
movsd (%rax,%r9,8), %xmm0
movl %ebx, %eax
cmpq %rax, %r11
jae 0x4750
leal (%rbx,%rdi), %eax
imull %ebx, %eax
cltd
idivl %ebp
addl %r11d, %eax
imull %r12d, %eax
movq 0x20(%r14), %rdx
addl (%rdx,%r8), %eax
leaq (%rdx,%r10,4), %rdx
jmp 0x475d
movq 0x20(%r14), %rdx
movl (%rdx,%r10,4), %eax
addl %esi, %eax
addq %r8, %rdx
imull %r12d, %eax
movslq (%rdx), %rdx
cltq
addq %rdx, %rax
mulsd %xmm1, %xmm0
addsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rbx
addq $0x4, %r8
addl %r12d, %esi
jmp 0x471c
incq %r11
incl %r13d
jmp 0x46e6
incq %r9
movq (%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %r13
jmp 0x46ca
cvtsi2sd %ebx, %xmm2
movsd 0x78b4(%rip), %xmm0     # 0xc068
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %edi
cmovgl %ebx, %edi
leaq (,%r12,8), %rdx
xorpd %xmm2, %xmm2
movq 0x28(%rsp), %r9
movq 0x30(%rsp), %r10
cmpq %r10, %rax
je 0x4845
xorpd %xmm3, %xmm3
movq %r9, %rsi
movq %rdi, %r8
subq $0x1, %r8
jb 0x47fc
addsd (%rsi), %xmm3
addq %rdx, %rsi
jmp 0x47ed
ucomisd %xmm2, %xmm3
movq %r9, %rsi
movq %rdi, %r8
jne 0x480a
jnp 0x4836
movapd %xmm0, %xmm4
divsd %xmm3, %xmm4
movq %r9, %rsi
movq %rdi, %r8
subq $0x1, %r8
jb 0x483c
movsd (%rsi), %xmm3
mulsd %xmm4, %xmm3
movsd %xmm3, (%rsi)
addq %rdx, %rsi
jmp 0x4818
movsd %xmm1, (%rsi)
addq %rdx, %rsi
subq $0x1, %r8
jae 0x482f
incq %rax
addq $0x8, %r9
jmp 0x47de
cvtsi2sdl (%rsp), %xmm2
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
xorl %r9d, %r9d
pushq $0x1
popq %r8
xorpd %xmm2, %xmm2
cmpl 0x20(%rsp), %r9d
je 0x4996
leal 0x1(%r9), %r10d
leal -0x1(%r9), %eax
imull %r9d, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %esi
movl %r8d, 0x10(%rsp)
addl %r8d, %esi
imull (%rsp), %esi
movl %r10d, %r11d
cmpl %r12d, %r11d
jge 0x4986
leal -0x1(%r11), %eax
imull %r11d, %eax
cltd
pushq $0x2
popq %r8
idivl %r8d
addl %r9d, %eax
movl (%rsp), %edx
imull %eax, %edx
xorl %r14d, %r14d
xorpd %xmm3, %xmm3
movl %esi, %r15d
cmpl %edi, %r14d
je 0x48eb
xorl %r13d, %r13d
movl %edx, %ebp
cmpl %r13d, %ebx
je 0x48e1
leal (%r15,%r13), %r8d
cmpl %r11d, %r9d
cmovbl %ebp, %r8d
movslq %r8d, %r8
addsd (%rcx,%r8,8), %xmm3
incl %r13d
addl %ebx, %ebp
jmp 0x48c1
incl %r14d
incl %edx
addl %ebx, %r15d
jmp 0x48b7
ucomisd %xmm2, %xmm3
jne 0x48f3
jnp 0x4940
movapd %xmm0, %xmm4
divsd %xmm3, %xmm4
imull (%rsp), %eax
xorl %edx, %edx
movl %esi, %r14d
cmpl %edi, %edx
je 0x497b
xorl %r15d, %r15d
movl %eax, %ebp
cmpl %r15d, %ebx
je 0x4937
leal (%r14,%r15), %r8d
cmpl %r11d, %r9d
cmovbl %ebp, %r8d
movslq %r8d, %r8
movsd (%rcx,%r8,8), %xmm3
mulsd %xmm4, %xmm3
movsd %xmm3, (%rcx,%r8,8)
incl %r15d
addl %ebx, %ebp
jmp 0x490d
incl %edx
incl %eax
addl %ebx, %r14d
jmp 0x4904
imull (%rsp), %eax
xorl %edx, %edx
movl %esi, %r14d
cmpl %edi, %edx
je 0x497b
xorl %r15d, %r15d
movl %eax, %ebp
cmpl %r15d, %ebx
je 0x4972
leal (%r14,%r15), %r8d
cmpl %r11d, %r9d
cmovbl %ebp, %r8d
movslq %r8d, %r8
movsd %xmm1, (%rcx,%r8,8)
incl %r15d
addl %ebx, %ebp
jmp 0x4952
incl %edx
incl %eax
addl %ebx, %r14d
jmp 0x4949
incl %r11d
addl (%rsp), %esi
jmp 0x488b
movl 0x10(%rsp), %r8d
incl %r8d
movl %r10d, %r9d
jmp 0x485d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq