// File: /workspace/github_c_makefile_star2_O2/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 $0xc8, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x78(%rdx), %rdi
xorl %esi, %esi
callq 0x10f0
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x16b5
movabsq $0x3f50624dd2f1a9fc, %rax # imm = 0x3F50624DD2F1A9FC
movq %rax, 0x8(%r12)
movl 0x1c(%r15), %eax
movl %eax, 0x20(%r12)
movl 0x18(%r15), %eax
decl %eax
cmpl $0x2, %eax
ja 0x5b61
movl %eax, %eax
leaq 0xa26d(%rip), %rcx      # 0xfdc8
movq (%rcx,%rax,8), %rax
jmp 0x5b68
leaq 0x346(%rip), %rax       # 0x5eae
movq %rax, 0xb991(%rip)      # 0x11500
cmpl $0x1, 0x58(%r15)
je 0x5d39
movq %r14, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r13, 0x20(%rsp)
cmpl $0x1, 0x20(%r15)
pushq $0x1
popq %rbp
pushq $0x2
popq %r12
jne 0x5d5a
cvtsi2sdl 0x24(%r15), %xmm0
divsd 0x48(%r14), %xmm0
movsd 0x50(%r15), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x50(%r15)
movl 0x4(%r14), %esi
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
cmpq %rax, %rcx
je 0x5be3
movsd (%r13,%rax,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rax,8)
incq %rax
jmp 0x5bc7
cmpl $0x2, %esi
movl %ebp, %edi
cmovgel %esi, %edi
decl %edi
xorl %r8d, %r8d
cmpl %edi, %r8d
je 0x5c49
leal 0x1(%r8), %r9d
leal -0x1(%r8), %eax
imull %r8d, %eax
cltd
idivl %r12d
movl %eax, %ecx
movl %r9d, %r10d
cmpl %esi, %r10d
jge 0x5c44
cmpl %r10d, %r8d
jae 0x5c25
leal -0x1(%r10), %eax
imull %r10d, %eax
cltd
idivl %r12d
addl %r8d, %eax
jmp 0x5c29
leal (%rcx,%r10), %eax
addl %esi, %eax
cltq
movsd (%r13,%rax,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rax,8)
incl %r10d
jmp 0x5c0a
movl %r9d, %r8d
jmp 0x5bf0
leaq 0x30(%rsp), %rsi
movq %r14, (%rsi)
movq %r15, 0x8(%rsi)
movq %r13, 0x10(%rsi)
movq 0xb8a0(%rip), %rax      # 0x11500
movq %rax, 0x18(%rsi)
movl 0x68(%r14), %ecx
movl 0x1c(%r15), %r8d
leaq 0x33ce(%rip), %rdi      # 0x9041
movsd 0x679d(%rip), %xmm0     # 0xc418
movq %rbx, %rdx
callq 0x8c3b
movsd 0x48(%r14), %xmm0
cvtsi2sdl 0x24(%r15), %xmm1
divsd %xmm1, %xmm0
movsd 0x50(%r15), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x50(%r15)
movl 0x4(%r14), %esi
xorl %eax, %eax
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
cmpq %rax, %rcx
je 0x5ccf
movsd (%r13,%rax,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rax,8)
incq %rax
jmp 0x5cb3
cmpl $0x2, %esi
movl %ebp, %edi
cmovgel %esi, %edi
decl %edi
xorl %r8d, %r8d
cmpl %edi, %r8d
je 0x5db3
leal 0x1(%r8), %r9d
leal -0x1(%r8), %eax
imull %r8d, %eax
cltd
idivl %r12d
movl %eax, %ecx
movl %r9d, %r10d
cmpl %esi, %r10d
jge 0x5d34
cmpl %r10d, %r8d
jae 0x5d15
leal -0x1(%r10), %eax
imull %r10d, %eax
cltd
idivl %r12d
addl %r8d, %eax
jmp 0x5d19
leal (%rcx,%r10), %eax
addl %esi, %eax
cltq
movsd (%r13,%rax,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rax,8)
incl %r10d
jmp 0x5cfa
movl %r9d, %r8d
jmp 0x5cdc
movq 0xa2b8(%rip), %rax      # 0xfff8
movq (%rax), %rcx
leaq 0x67d3(%rip), %rdi      # 0xc51d
pushq $0x3e
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
jmp 0x5b7a
movl 0x68(%r14), %edi
movq 0xb79b(%rip), %rcx      # 0x11500
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
leaq 0x35ce(%rip), %r8       # 0x9343
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %r9
movq %rbx, %rsi
callq 0x16c3
movq 0xa26a(%rip), %rcx      # 0xfff8
movq (%rcx), %rcx
movq %rcx, 0x8(%rsp)
movl %eax, %edi
callq 0x9597
leaq 0x67b8(%rip), %rsi      # 0xc55c
movq 0x8(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1170
cmpl $0x1, 0x58(%r15)
je 0x5dcc
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x96c2
movl 0x4(%r14), %ecx
cmpl $0x2, %ecx
cmovgel %ecx, %ebp
decl %ebp
xorl %esi, %esi
cmpl %ebp, %esi
je 0x5e4f
leal 0x1(%rsi), %edi
movl %edi, %r8d
cmpl %ecx, %r8d
je 0x5e4b
movl 0x8(%r14), %r9d
leal -0x1(%r8), %eax
imull %r8d, %eax
cltd
idivl %r12d
xorl %edx, %edx
testl %r9d, %r9d
movl $0x0, %r10d
cmovgl %r9d, %r10d
addl %esi, %eax
imull %r9d, %eax
addl %ecx, %eax
imull %r9d, %eax
movl %eax, %r11d
movl %r9d, %r13d
cmpl %r10d, %edx
jne 0x5e3a
jmp 0x5e46
movslq %r11d, %r11
andq $0x0, (%rbx,%r11,8)
addl %r9d, %r11d
subl $0x1, %r13d
jae 0x5e2f
incl %edx
incl %eax
jmp 0x5e22
incl %r8d
jmp 0x5df2
movl %edi, %esi
jmp 0x5de8
movl $0x2, 0x58(%r15)
movl 0x68(%r14), %edi
movq 0xb69e(%rip), %rcx      # 0x11500
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
leaq 0x34d1(%rip), %r8       # 0x9343
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %r9
movq %rbx, %rsi
callq 0x16c3
movq 0xa16d(%rip), %rcx      # 0xfff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0x9597
leaq 0x66c0(%rip), %rsi      # 0xc55c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0x5dba