// File: /workspace/github_c_makefile_star2_O1/debbiemarkslab[P]plmc/src/inference.c
// Function: EstimatePairModelMAP
void EstimatePairModelMAP(numeric_t *x, numeric_t *lambdas, alignment_t *ali,
    options_t *options) {
    /* Computes Maximum a posteriori (MAP) estimates for the parameters of 
       and undirected graphical model by L-BFGS */

    /* Start timer */
    gettimeofday(&ali->start, NULL);

    /* Initialize L-BFGS */
    lbfgs_parameter_t param;
    lbfgs_parameter_init(&param);
    param.epsilon = 1E-3;
    param.max_iterations = options->maxIter; /* 0 is unbounded */

    /* Estimate parameters by optimization */
    static lbfgs_evaluate_t algo;
    switch(options->estimatorMAP) {
        case INFER_MAP_PLM:
            algo = PLMNegLogPosterior;
            break;
        case INFER_MAP_PLM_GAPREDUCE:
            algo = PLMNegLogPosteriorGapReduce;
            break;
        case INFER_MAP_PLM_BLOCK:
            algo = PLMNegLogPosteriorBlock;
            break;
        case INFER_MAP_PLM_DROPOUT:
            algo = PLMNegLogPosteriorDO;
            break;
        default:
            algo = PLMNegLogPosterior;
    }

    if (options->zeroAPC == 1) fprintf(stderr,
            "Estimating coupling hyperparameters le = 1/2 inverse variance\n");

    /* Problem instance in void array */
    void *d[3] = {(void *)ali, (void *)options, (void *)lambdas};

    if (options->sgd == 1) {
        /* Scale hyperparams for minibatch */
        numeric_t scale = (numeric_t) options->sgdBatchSize / ali->nEff;
        options->lambdaGroup *= scale;
        for (int i = 0; i < ali->nSites; i++) lambdaHi(i) *= scale;
        for (int i = 0; i < ali->nSites - 1; i++)
            for (int j = i + 1; j < ali->nSites; j++)
                lambdaEij(i, j) *= scale;

        /* SGD optimization */
        numeric_t crit = 0.01;
        void *d[4] = {(void *)ali, (void *)options, (void *)lambdas, (void *) algo};
        SGDOptimize(SGDWrapperPLM, d, x, ali->nParams, options->maxIter, crit);

        /* Unscale hyperparams for minibatch */
        numeric_t invScale = ali->nEff / (numeric_t) options->sgdBatchSize;
        options->lambdaGroup *= invScale;
        for (int i = 0; i < ali->nSites; i++) lambdaHi(i) *= invScale;
        for (int i = 0; i < ali->nSites - 1; i++)
            for (int j = i + 1; j < ali->nSites; j++)
                lambdaEij(i, j) *= invScale;
    } else {
        /* L-BFGS optimization */
        int ret = 0;
        lbfgsfloatval_t fx;
        ret = lbfgs(ali->nParams, x, &fx, algo, ReportProgresslBFGS,
            (void*)d, &param);
        fprintf(stderr, "Gradient optimization: %s\n", LBFGSErrorString(ret));
    }

    /* Optionally re-estimate parameters with adjusted hyperparameters */
    if (options->zeroAPC == 1) {
        /* Form new priors on the variances */
        ZeroAPCPriors(ali, options, lambdas, x);

        /* Reinitialize coupling parameters */
        for (int i = 0; i < ali->nSites - 1; i++)
            for (int j = i + 1; j < ali->nSites; j++)
                for (int ai = 0; ai < ali->nCodes; ai++)
                    for (int aj = 0; aj < ali->nCodes; aj++)
                        xEij(i, j, ai, aj) = 0.0;

        /* Iterate estimation with new hyperparameter estimates */
        options->zeroAPC = 2;
        lbfgsfloatval_t fx2;
        int ret2 = lbfgs(ali->nParams, x, &fx2, algo,
            ReportProgresslBFGS, (void*)d, &param);
        fprintf(stderr, "Gradient optimization: %s\n", LBFGSErrorString(ret2));
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x78(%rdx), %rdi
xorl %esi, %esi
callq 0x10f0
leaq 0x88(%rsp), %r12
movq %r12, %rdi
callq 0x32ca
movabsq $0x3f50624dd2f1a9fc, %rax # imm = 0x3F50624DD2F1A9FC
movq %rax, 0x8(%r12)
movl 0x1c(%r15), %eax
movl %eax, 0x20(%r12)
movl 0x18(%r15), %eax
cmpq $0x3, %rax
ja 0x80c4
leaq 0x9d02(%rip), %rcx      # 0x11dc0
movq (%rcx,%rax,8), %rax
jmp 0x80cb
leaq 0x442(%rip), %rax       # 0x850d
movq %rax, 0xb42e(%rip)      # 0x13500
cmpl $0x1, 0x58(%r15)
jne 0x80de
callq 0x229a
movq %r14, 0x40(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, 0x50(%rsp)
cmpl $0x1, 0x20(%r15)
jne 0x8299
cvtsi2sdl 0x24(%r15), %xmm0
divsd 0x48(%r14), %xmm0
movsd 0x50(%r15), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x50(%r15)
movslq 0x4(%r14), %rax
testq %rax, %rax
jle 0x8139
xorl %ecx, %ecx
movsd (%r13,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x811f
cmpl $0x2, %eax
jl 0x81a5
leal -0x2(%rax), %ecx
xorl %edx, %edx
movl %edx, %esi
leal 0x1(%rsi), %edx
leal -0x1(%rsi), %r8d
imull %esi, %r8d
movl %r8d, %edi
shrl $0x1f, %edi
addl %r8d, %edi
sarl %edi
movl %edx, %r8d
cmpl %r8d, %esi
jae 0x817d
leal -0x1(%r8), %r10d
imull %r8d, %r10d
movl %r10d, %r9d
shrl $0x1f, %r9d
addl %r10d, %r9d
sarl %r9d
addl %esi, %r9d
jmp 0x8181
leal (%rdi,%r8), %r9d
addl %eax, %r9d
movslq %r9d, %r9
movsd (%r13,%r9,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%r9,8)
incl %r8d
cmpl %r8d, %eax
jne 0x815e
cmpl %ecx, %esi
jne 0x8143
leaq 0x60(%rsp), %rsi
movq %r14, (%rsi)
movq %r15, 0x8(%rsi)
movq %r13, 0x10(%rsi)
movq 0xb344(%rip), %rax      # 0x13500
movq %rax, 0x18(%rsi)
movl 0x68(%r14), %ecx
movl 0x1c(%r15), %r8d
leaq 0x3a61(%rip), %rdi      # 0xbc30
movsd 0x62c9(%rip), %xmm0     # 0xe4a0
movq %rbx, %rdx
callq 0x1397
movsd 0x48(%r14), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x24(%r15), %xmm1
divsd %xmm1, %xmm0
movsd 0x50(%r15), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x50(%r15)
movslq 0x4(%r14), %rax
testq %rax, %rax
jle 0x8227
xorl %ecx, %ecx
movsd (%r13,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x820d
cmpl $0x2, %eax
jl 0x82e6
leal -0x2(%rax), %ecx
xorl %edx, %edx
movl %edx, %esi
leal 0x1(%rsi), %edx
leal -0x1(%rsi), %r8d
imull %esi, %r8d
movl %r8d, %edi
shrl $0x1f, %edi
addl %r8d, %edi
sarl %edi
movl %edx, %r8d
cmpl %r8d, %esi
jae 0x826f
leal -0x1(%r8), %r10d
imull %r8d, %r10d
movl %r10d, %r9d
shrl $0x1f, %r9d
addl %r10d, %r9d
sarl %r9d
addl %esi, %r9d
jmp 0x8273
leal (%rdi,%r8), %r9d
addl %eax, %r9d
movslq %r9d, %r9
movsd (%r13,%r9,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%r9,8)
incl %r8d
cmpl %r8d, %eax
jne 0x8250
cmpl %ecx, %esi
jne 0x8235
jmp 0x82e6
movl 0x68(%r14), %edi
movq 0xb25c(%rip), %rcx      # 0x13500
movq %r12, (%rsp)
leaq -0x6ae9(%rip), %r8      # 0x17c6
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %r9
movq %rbx, %rsi
callq 0x32da
movq 0x9d30(%rip), %rcx      # 0x11ff8
movq (%rcx), %rbp
movl %eax, %edi
callq 0xbfce
leaq 0x631f(%rip), %rsi      # 0xe5f8
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1170
cmpl $0x1, 0x58(%r15)
je 0x82ff
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x38(%rsp)
movq %r14, %rdi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1a2c
movq %r14, 0x28(%rsp)
movl 0x4(%r14), %eax
movq %rax, 0x30(%rsp)
cmpl $0x2, %eax
jl 0x84a3
movq 0x30(%rsp), %rcx
leal -0x2(%rcx), %eax
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %rax
movl 0x8(%rax), %edx
movl %edx, %esi
imull %ecx, %esi
leal 0x1(%rdx), %eax
andl $-0x2, %eax
movl %eax, 0x24(%rsp)
leal -0x1(%rdx), %eax
movd %eax, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
leal (%rdx,%rdx), %r8d
movl %edx, %eax
imull %edx, %eax
movl %eax, 0x10(%rsp)
xorl %eax, %eax
movl $0x1, %ecx
pmovsxbd 0x6203(%rip), %xmm1     # 0xe578
pmovsxbd 0x61fe(%rip), %xmm2     # 0xe57c
movl %eax, %r13d
leal 0x1(%r13), %edi
leal -0x1(%r13), %eax
imull %r13d, %eax
movl %eax, %r9d
shrl $0x1f, %r9d
addl %eax, %r9d
sarl %r9d
movl %ecx, 0x20(%rsp)
addl %ecx, %r9d
imull 0x10(%rsp), %r9d
movl %r9d, 0x14(%rsp)
movl %edi, 0x1c(%rsp)
movl %edi, %r12d
testl %edx, %edx
jle 0x8474
leal -0x1(%r12), %ecx
imull %r12d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
addl %r13d, %eax
movl %edx, %r11d
imull %eax, %r11d
incl %r11d
imull %edx, %r11d
imull 0x10(%rsp), %eax
xorl %ecx, %ecx
movl 0x14(%rsp), %ebp
movl 0x24(%rsp), %edi
movl %eax, %r14d
movl %ebp, %r9d
movl %r11d, %r15d
movdqa %xmm1, %xmm3
movdqa %xmm3, %xmm4
pminud %xmm0, %xmm4
pcmpeqd %xmm3, %xmm4
movd %xmm4, %r10d
testb $0x1, %r10b
je 0x842a
cmpl %r12d, %r13d
movl %r9d, %r10d
cmovbl %r14d, %r10d
addl %esi, %r10d
movslq %r10d, %r10
movq $0x0, (%rbx,%r10,8)
pextrb $0x4, %xmm4, %r10d
testb $0x1, %r10b
je 0x8450
leal 0x1(%r9), %r10d
cmpl %r12d, %r13d
cmovbl %r15d, %r10d
addl %esi, %r10d
movslq %r10d, %r10
movq $0x0, (%rbx,%r10,8)
paddd %xmm2, %xmm3
addl %r8d, %r15d
addl $0x2, %r9d
addl %r8d, %r14d
addl $-0x2, %edi
jne 0x83fa
incl %ecx
incl %r11d
addl %edx, %ebp
incl %eax
cmpl %edx, %ecx
jne 0x83e9
incl %r12d
movl 0x14(%rsp), %eax
addl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpl 0x30(%rsp), %r12d
jne 0x83b3
movl 0x20(%rsp), %ecx
incl %ecx
cmpl 0x18(%rsp), %r13d
movl 0x1c(%rsp), %eax
jne 0x837e
movq 0x38(%rsp), %rax
movl $0x2, 0x58(%rax)
movq 0x28(%rsp), %rax
movl 0x68(%rax), %edi
movq 0xb042(%rip), %rcx      # 0x13500
leaq 0x88(%rsp), %rax
movq %rax, (%rsp)
leaq -0x6d0b(%rip), %r8      # 0x17c6
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %r9
movq %rbx, %rsi
callq 0x32da
movq 0x9b0e(%rip), %rcx      # 0x11ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0xbfce
leaq 0x60fd(%rip), %rsi      # 0xe5f8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0x82ed