// File: /workspace/github_c_makefile_star2_O3/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 0x2886
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 0x7456
movl %eax, %eax
leaq 0x9978(%rip), %rcx      # 0x10dc8
movq (%rcx,%rax,8), %rax
jmp 0x745d
leaq 0x44a(%rip), %rax       # 0x78a7
movq %rax, 0xb09c(%rip)      # 0x12500
cmpl $0x1, 0x58(%r15)
jne 0x7470
callq 0x222d
movq %r14, 0x40(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, 0x50(%rsp)
cmpl $0x1, 0x20(%r15)
jne 0x7633
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 0x753b
xorl %ecx, %ecx
movsd (%r13,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x74b5
cmpl $0x1, %eax
je 0x753b
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 0x7513
leal -0x1(%r8), %r10d
imull %r8d, %r10d
movl %r10d, %r9d
shrl $0x1f, %r9d
addl %r10d, %r9d
sarl %r9d
addl %esi, %r9d
jmp 0x7517
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 0x74f4
cmpl %ecx, %esi
jne 0x74d9
leaq 0x60(%rsp), %rsi
movq %r14, (%rsi)
movq %r15, 0x8(%rsi)
movq %r13, 0x10(%rsi)
movq 0xafae(%rip), %rax      # 0x12500
movq %rax, 0x18(%rsi)
movl 0x68(%r14), %ecx
movl 0x1c(%r15), %r8d
leaq 0x36a5(%rip), %rdi      # 0xac0a
movsd 0x5f23(%rip), %xmm0     # 0xd490
movq %rbx, %rdx
callq 0x13a7
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 0x7680
xorl %ecx, %ecx
movsd (%r13,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x75a7
cmpl $0x1, %eax
je 0x7680
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 0x7609
leal -0x1(%r8), %r10d
imull %r8d, %r10d
movl %r10d, %r9d
shrl $0x1f, %r9d
addl %r10d, %r9d
sarl %r9d
addl %esi, %r9d
jmp 0x760d
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 0x75ea
cmpl %ecx, %esi
jne 0x75cf
jmp 0x7680
movl 0x68(%r14), %edi
movq 0xaec2(%rip), %rcx      # 0x12500
movq %r12, (%rsp)
leaq -0x5e2f(%rip), %r8      # 0x181a
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %r9
movq %rbx, %rsi
callq 0x2896
movq 0x9996(%rip), %rcx      # 0x10ff8
movq (%rcx), %rbp
movl %eax, %edi
callq 0xafbc
leaq 0x5f75(%rip), %rsi      # 0xd5e8
movq %rbp, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1180
cmpl $0x1, 0x58(%r15)
je 0x7699
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 0x1a7c
movq %r14, 0x28(%rsp)
movl 0x4(%r14), %eax
movq %rax, 0x30(%rsp)
cmpl $0x2, %eax
jl 0x783d
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 0x5e59(%rip), %xmm1     # 0xd568
pmovsxbd 0x5e54(%rip), %xmm2     # 0xd56c
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 0x780e
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 0x77c4
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 0x77ea
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 0x7794
incl %ecx
incl %r11d
addl %edx, %ebp
incl %eax
cmpl %edx, %ecx
jne 0x7783
incl %r12d
movl 0x14(%rsp), %eax
addl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpl 0x30(%rsp), %r12d
jne 0x774d
movl 0x20(%rsp), %ecx
incl %ecx
cmpl 0x18(%rsp), %r13d
movl 0x1c(%rsp), %eax
jne 0x7718
movq 0x38(%rsp), %rax
movl $0x2, 0x58(%rax)
movq 0x28(%rsp), %rax
movl 0x68(%rax), %edi
movq 0xaca8(%rip), %rcx      # 0x12500
leaq 0x88(%rsp), %rax
movq %rax, (%rsp)
leaq -0x6051(%rip), %r8      # 0x181a
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %r9
movq %rbx, %rsi
callq 0x2896
movq 0x9774(%rip), %rcx      # 0x10ff8
movq (%rcx), %rbx
movl %eax, %edi
callq 0xafbc
leaq 0x5d53(%rip), %rsi      # 0xd5e8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1180
jmp 0x7687