// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::doLagrangeStep(FitBit*)
NUMBER HMMProblem::doLagrangeStep(FitBit *fb) {
	NPAR nS = this->p->nS, nO = this->p->nO;
	NPAR i,j,m;
    
    NUMBER  ll = 0;
	NUMBER * b_PI = init1D<NUMBER>((NDAT)this->p->nS);
    NUMBER   b_PI_den = 0;
	NUMBER ** b_A     = init2D<NUMBER>((NDAT)this->p->nS, (NDAT)this->p->nS);
	NUMBER *  b_A_den = init1D<NUMBER>((NDAT)this->p->nS);
	NUMBER ** b_B     = init2D<NUMBER>((NDAT)this->p->nS, (NDAT)this->p->nO);
	NUMBER *  b_B_den = init1D<NUMBER>((NDAT)this->p->nS);
	// compute sums PI
    
    NUMBER buf = 0;
    // collapse
    for(i=0; i<nS; i++) {
        if(fb->pi != NULL)
            buf = -fb->pi[i]*fb->gradPI[i]; /*negatie since were going against gradient*/
        b_PI_den += buf;
        b_PI[i] += buf;
        if(fb->A != NULL)
            for(j=0; j<nS; j++){
                buf = -fb->A[i][j]*fb->gradA[i][j]; /*negatie since were going against gradient*/
                b_A_den[i] += buf;
                b_A[i][j] += buf;
            }
        if(fb->B != NULL)
            for(m=0; m<nO; m++) {
                buf = -fb->B[i][m]*fb->gradB[i][m]; /*negatie since were going against gradient*/
                b_B_den[i] += buf;
                b_B[i][m] += buf;
            }
    }
    // divide
    for(i=0; i<nS; i++) {
        if(fb->pi != NULL)
            fb->pi[i] = (b_PI_den>0) ? (b_PI[i] / b_PI_den) : fb->pi[i];
        if(fb->A != NULL)
            for(j=0; j<nS; j++)
                fb->A[i][j] = (b_A_den[i]>0) ? (b_A[i][j] / b_A_den[i]) : fb->A[i][j];
        if(fb->B != NULL)
            for(m=0; m<nO; m++)
                fb->B[i][m] = (b_B_den[i]>0) ? (b_B[i][m] / b_B_den[i]) : fb->B[i][m];
    }
    // scale
    if( !this->hasNon01Constraints() || fb->observe_non01_boundaries==0 ) {
        if(fb->pi != NULL) projectsimplex(fb->pi, nS);
        for(i=0; i<nS; i++) {
            if(fb->A != NULL) projectsimplex(fb->A[i], nS);
            if(fb->B != NULL) projectsimplex(fb->B[i], nO);
        }
    } else {
        if(fb->pi != NULL) projectsimplexbounded(fb->pi, this->getLbPI(), this->getUbPI(), nS);
        for(i=0; i<nS; i++) {
            if(fb->A != NULL) projectsimplexbounded(fb->A[i], this->getLbA()[i], this->getUbA()[i], nS);
            if(fb->B != NULL) projectsimplexbounded(fb->B[i], this->getLbB()[i], this->getUbB()[i], nO);
        }
    }
    // compute LL
    computeAlphaAndPOParam(fb->xndat, fb->x_data);
    ll = HMMProblem::getSumLogPOPara(fb->xndat, fb->x_data);

	free(b_PI);
	free2D<NUMBER>(b_A, nS);
	free(b_A_den);
	free2D<NUMBER>(b_B, nS);
	free(b_B_den);
    
    return ll;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, (%rsp)
movq 0x88(%rdi), %rax
movq %rdi, %r14
movq %rdi, 0x8(%rsp)
movsbq 0x8b8(%rax), %rbx
movzbl 0x8b9(%rax), %r12d
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq %rax, %rbp
movl %ebx, %edi
movl %ebx, %esi
callq 0xbb36
movq %rax, %r15
movq 0x88(%r14), %r14
movsbq 0x8b8(%r14), %r13
movl $0x8, %esi
movq %r13, %rdi
callq 0x4130
movsbl 0x8b9(%r14), %esi
movq %rax, %r14
movl %r13d, %edi
callq 0xbb36
movq %rax, %r13
movq 0x8(%rsp), %rax
movq 0x88(%rax), %rax
movsbq 0x8b8(%rax), %rdi
movl $0x8, %esi
callq 0x4130
movq (%rsp), %r11
movq %rax, 0x10(%rsp)
testq %rbx, %rbx
jle 0x18ed9
movq 0x10(%r11), %rax
movq 0x18(%r11), %rcx
movq 0x20(%r11), %rdx
testq %rdx, %rdx
sete %dil
testb %r12b, %r12b
setle %sil
orb %dil, %sil
xorpd %xmm2, %xmm2
xorl %edi, %edi
movapd 0x1ac34(%rip), %xmm1    # 0x33940
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x18d28
movsd (%rax,%rdi,8), %xmm2
xorpd %xmm1, %xmm2
movq 0x58(%r11), %r8
mulsd (%r8,%rdi,8), %xmm2
movsd (%rbp,%rdi,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%rbp,%rdi,8)
movapd %xmm2, %xmm3
testq %rcx, %rcx
je 0x18d90
movq (%rcx,%rdi,8), %r8
movq 0x60(%r11), %r9
movq (%r9,%rdi,8), %r9
movq (%r15,%rdi,8), %r10
movsd (%r14,%rdi,8), %xmm4
xorl %r11d, %r11d
movsd (%r8,%r11,8), %xmm3
xorpd %xmm1, %xmm3
mulsd (%r9,%r11,8), %xmm3
addsd %xmm3, %xmm4
movsd (%r10,%r11,8), %xmm5
addsd %xmm3, %xmm5
movsd %xmm5, (%r10,%r11,8)
incq %r11
cmpq %r11, %rbx
jne 0x18d5a
movsd %xmm4, (%r14,%rdi,8)
movq (%rsp), %r11
testb %sil, %sil
jne 0x18def
movq (%rdx,%rdi,8), %r8
movq 0x68(%r11), %r9
movq (%r9,%rdi,8), %r9
movq (%r13,%rdi,8), %r10
movq 0x10(%rsp), %r11
movsd (%r11,%rdi,8), %xmm4
xorl %r11d, %r11d
movsd (%r8,%r11,8), %xmm3
xorpd %xmm1, %xmm3
mulsd (%r9,%r11,8), %xmm3
addsd %xmm3, %xmm4
movsd (%r10,%r11,8), %xmm5
addsd %xmm3, %xmm5
movsd %xmm5, (%r10,%r11,8)
incq %r11
cmpq %r11, %r12
jne 0x18db4
movq 0x10(%rsp), %r8
movsd %xmm4, (%r8,%rdi,8)
movq (%rsp), %r11
addsd %xmm2, %xmm0
incq %rdi
movapd %xmm3, %xmm2
cmpq %rbx, %rdi
jne 0x18d10
testb %bl, %bl
jle 0x18ed9
movq 0x10(%r11), %rax
movq 0x18(%r11), %rcx
movq 0x20(%r11), %rdx
testq %rdx, %rdx
sete %dil
testb %r12b, %r12b
setle %sil
orb %dil, %sil
xorl %edi, %edi
xorpd %xmm1, %xmm1
testq %rax, %rax
je 0x18e4f
ucomisd %xmm1, %xmm0
jbe 0x18e45
movsd (%rbp,%rdi,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x18e4a
movsd (%rax,%rdi,8), %xmm2
movsd %xmm2, (%rax,%rdi,8)
testq %rcx, %rcx
je 0x18e8b
movsd (%r14,%rdi,8), %xmm2
movq (%rcx,%rdi,8), %r8
xorl %r9d, %r9d
ucomisd %xmm1, %xmm2
jbe 0x18e77
movq (%r15,%rdi,8), %r10
movsd (%r10,%r9,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x18e7d
movsd (%r8,%r9,8), %xmm3
movsd %xmm3, (%r8,%r9,8)
incq %r9
cmpq %r9, %rbx
jne 0x18e61
testb %sil, %sil
jne 0x18ecd
movq 0x10(%rsp), %r8
movsd (%r8,%rdi,8), %xmm2
movq (%rdx,%rdi,8), %r8
xorl %r9d, %r9d
ucomisd %xmm1, %xmm2
jbe 0x18eb9
movq (%r13,%rdi,8), %r10
movsd (%r10,%r9,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x18ebf
movsd (%r8,%r9,8), %xmm3
movsd %xmm3, (%r8,%r9,8)
incq %r9
cmpq %r9, %r12
jne 0x18ea2
incq %rdi
cmpq %rbx, %rdi
jne 0x18e2e
movq 0x8(%rsp), %rax
cmpb $0x1, 0x80(%rax)
movq %rbp, 0x20(%rsp)
movq %r14, 0x18(%rsp)
jne 0x18fb9
cmpb $0x0, 0xf9(%r11)
je 0x18fb9
movq 0x10(%r11), %rdi
testq %rdi, %rdi
je 0x18f24
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x68(%rax), %rdx
movl %ebx, %ecx
callq 0x7997
movq (%rsp), %r11
testb %bl, %bl
jle 0x19010
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x18(%r11), %rax
testq %rax, %rax
je 0x18f6d
movq (%rax,%r14,8), %rdi
movq 0x8(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x58(%rcx), %rax
cmoveq %rbp, %rax
movq (%rax,%r14,8), %rsi
movq 0x70(%rcx), %rax
cmoveq %rbp, %rax
movq (%rax,%r14,8), %rdx
movl %ebx, %ecx
callq 0x7997
movq (%rsp), %r11
movq 0x20(%r11), %rax
testq %rax, %rax
je 0x18fab
movq (%rax,%r14,8), %rdi
movq 0x8(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x60(%rcx), %rax
cmoveq %rbp, %rax
movq (%rax,%r14,8), %rsi
movq 0x78(%rcx), %rax
cmoveq %rbp, %rax
movq (%rax,%r14,8), %rdx
movsbl %r12b, %ecx
callq 0x7997
movq (%rsp), %r11
incq %r14
cmpq %r14, %rbx
jne 0x18f31
jmp 0x19010
movq 0x10(%r11), %rdi
testq %rdi, %rdi
je 0x18fcd
movl %ebx, %esi
callq 0x6836
movq (%rsp), %r11
testb %bl, %bl
jle 0x19010
movslq %ebx, %r14
xorl %ebp, %ebp
movq 0x18(%r11), %rax
testq %rax, %rax
je 0x18fee
movq (%rax,%rbp,8), %rdi
movl %ebx, %esi
callq 0x6836
movq (%rsp), %r11
movq 0x20(%r11), %rax
testq %rax, %rax
je 0x19008
movq (%rax,%rbp,8), %rdi
movsbl %r12b, %esi
callq 0x6836
movq (%rsp), %r11
incq %rbp
cmpq %rbp, %r14
jne 0x18fd6
movl 0xe8(%r11), %esi
movq 0xf0(%r11), %rdx
movq 0x8(%rsp), %rdi
callq 0x11908
movq (%rsp), %rcx
movslq 0xe8(%rcx), %rax
testq %rax, %rax
jle 0x19070
movq 0xf0(%rcx), %rcx
xorpd %xmm1, %xmm1
xorl %edx, %edx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %r14
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm0, %xmm0
jne 0x19062
movsd 0x50(%rsi), %xmm0
addsd %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rax
jne 0x1904f
jmp 0x1907e
xorpd %xmm1, %xmm1
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %r14
movsd %xmm1, (%rsp)
callq 0x42a0
movq %r15, %rdi
movl %ebx, %esi
callq 0xa5dd
movq %r14, %rdi
callq 0x42a0
movq %r13, %rdi
movl %ebx, %esi
callq 0xa5dd
movq 0x10(%rsp), %rdi
callq 0x42a0
movsd (%rsp), %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq