// File: /workspace/github_cpp_makefile_star2_O1/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 0xbb5c
movq %rax, %r15
movq 0x88(%r14), %r14
movsbq 0x8b8(%r14), %r13
movl $0x8, %esi
movq %r13, %rdi
callq 0x4130
movq %rax, 0x10(%rsp)
movsbl 0x8b9(%r14), %esi
movl %r13d, %edi
callq 0xbb5c
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, 0x18(%rsp)
testq %rbx, %rbx
jle 0x18faa
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 0x1ba94(%rip), %xmm1    # 0x34940
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %r14
testq %rax, %rax
je 0x18ecd
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 0x18f35
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 0x18eff
movsd %xmm4, (%r14,%rdi,8)
movq (%rsp), %r11
testb %sil, %sil
jne 0x18f94
movq (%rdx,%rdi,8), %r8
movq 0x68(%r11), %r9
movq (%r9,%rdi,8), %r9
movq (%r13,%rdi,8), %r10
movq 0x18(%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 0x18f59
movq 0x18(%rsp), %r8
movsd %xmm4, (%r8,%rdi,8)
movq (%rsp), %r11
addsd %xmm2, %xmm0
incq %rdi
movapd %xmm3, %xmm2
cmpq %rbx, %rdi
jne 0x18eb5
jmp 0x18fb3
xorpd %xmm0, %xmm0
movq 0x10(%rsp), %r14
testb %bl, %bl
jle 0x19091
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 0x18fff
ucomisd %xmm1, %xmm0
jbe 0x18ff5
movsd (%rbp,%rdi,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x18ffa
movsd (%rax,%rdi,8), %xmm2
movsd %xmm2, (%rax,%rdi,8)
testq %rcx, %rcx
je 0x1903f
movsd (%r14,%rdi,8), %xmm2
movq (%rcx,%rdi,8), %r8
xorl %r9d, %r9d
ucomisd %xmm1, %xmm2
jbe 0x19027
movq (%r15,%rdi,8), %r10
movsd (%r10,%r9,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x19031
movq (%rcx,%rdi,8), %r10
movsd (%r10,%r9,8), %xmm3
movsd %xmm3, (%r8,%r9,8)
incq %r9
cmpq %r9, %rbx
jne 0x19011
testb %sil, %sil
jne 0x19085
movq 0x18(%rsp), %r8
movsd (%r8,%rdi,8), %xmm2
movq (%rdx,%rdi,8), %r8
xorl %r9d, %r9d
ucomisd %xmm1, %xmm2
jbe 0x1906d
movq (%r13,%rdi,8), %r10
movsd (%r10,%r9,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x19077
movq (%rdx,%rdi,8), %r10
movsd (%r10,%r9,8), %xmm3
movsd %xmm3, (%r8,%r9,8)
incq %r9
cmpq %r9, %r12
jne 0x19056
incq %rdi
cmpq %rbx, %rdi
jne 0x18fde
movq 0x8(%rsp), %rax
cmpb $0x1, 0x80(%rax)
movq %rbp, 0x20(%rsp)
jne 0x1916c
cmpb $0x0, 0xf9(%r11)
je 0x1916c
movq 0x10(%r11), %rdi
testq %rdi, %rdi
je 0x190d7
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x68(%rax), %rdx
movl %ebx, %ecx
callq 0x79f5
movq (%rsp), %r11
testb %bl, %bl
jle 0x191c3
xorl %ebp, %ebp
xorl %r14d, %r14d
movq 0x18(%r11), %rax
testq %rax, %rax
je 0x19120
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 0x79f5
movq (%rsp), %r11
movq 0x20(%r11), %rax
testq %rax, %rax
je 0x1915e
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 0x79f5
movq (%rsp), %r11
incq %r14
cmpq %r14, %rbx
jne 0x190e4
jmp 0x191c3
movq 0x10(%r11), %rdi
testq %rdi, %rdi
je 0x19180
movl %ebx, %esi
callq 0x692d
movq (%rsp), %r11
testb %bl, %bl
jle 0x191c3
movslq %ebx, %r14
xorl %ebp, %ebp
movq 0x18(%r11), %rax
testq %rax, %rax
je 0x191a1
movq (%rax,%rbp,8), %rdi
movl %ebx, %esi
callq 0x692d
movq (%rsp), %r11
movq 0x20(%r11), %rax
testq %rax, %rax
je 0x191bb
movq (%rax,%rbp,8), %rdi
movsbl %r12b, %esi
callq 0x692d
movq (%rsp), %r11
incq %rbp
cmpq %rbp, %r14
jne 0x19189
movl 0xe8(%r11), %esi
movq 0xf0(%r11), %rdx
movq 0x8(%rsp), %rdi
callq 0x11a86
movq (%rsp), %rcx
movslq 0xe8(%rcx), %rax
testq %rax, %rax
jle 0x19223
movq 0xf0(%rcx), %rcx
xorpd %xmm1, %xmm1
xorl %edx, %edx
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r14
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm0, %xmm0
jne 0x19215
movsd 0x50(%rsi), %xmm0
addsd %xmm0, %xmm1
incq %rdx
cmpq %rdx, %rax
jne 0x19202
jmp 0x19231
xorpd %xmm1, %xmm1
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r14
movsd %xmm1, (%rsp)
callq 0x42a0
movq %r15, %rdi
movl %ebx, %esi
callq 0xa5e0
movq %r14, %rdi
callq 0x42a0
movq %r13, %rdi
movl %ebx, %esi
callq 0xa5e0
movq 0x18(%rsp), %rdi
callq 0x42a0
movsd (%rsp), %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop