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

	free(b_PI);
	free3D<NUMBER>(b_A, nZ, nS);
	free2D<NUMBER>(b_A_den, nZ);
	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 $0x58, %rsp
movq %rsi, %r15
movq %rdi, 0x10(%rsp)
movq 0x88(%rdi), %rax
movsbl 0x8b8(%rax), %ebx
movzbl %bl, %r12d
movb 0x8b9(%rax), %r14b
movsbl 0x8c8(%rax), %ebp
movl %ebx, %edi
callq 0x7dde
movq %rax, 0x28(%rsp)
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xa0be
movq %rax, %r13
movl %ebp, 0x24(%rsp)
movl %ebp, %edi
movl %ebx, %esi
callq 0x9328
movq %rax, %rbp
movb %r14b, (%rsp)
movsbl %r14b, %esi
movl %ebx, %edi
movl %esi, 0x3c(%rsp)
callq 0x9328
movq %rax, %r14
movl %ebx, %edi
callq 0x7dde
movq 0x10(%r15), %r11
testq %r11, %r11
movl %ebx, 0xc(%rsp)
je 0x29530
xorl %edx, %edx
testb %r12b, %r12b
movl $0x0, %ecx
cmovgl %r12d, %ecx
xorpd %xmm0, %xmm0
movapd 0x3474(%rip), %xmm1     # 0x2c940
movq 0x28(%rsp), %r10
movb (%rsp), %r9b
cmpq %rdx, %rcx
je 0x29502
movsd (%r11,%rdx,8), %xmm2
xorpd %xmm1, %xmm2
movq 0x58(%r15), %rsi
mulsd (%rsi,%rdx,8), %xmm2
addsd %xmm2, %xmm0
addsd (%r10,%rdx,8), %xmm2
movsd %xmm2, (%r10,%rdx,8)
incq %rdx
jmp 0x294d5
xorl %edx, %edx
xorpd %xmm1, %xmm1
cmpq %rdx, %rcx
je 0x29544
ucomisd %xmm1, %xmm0
jbe 0x2951f
movsd (%r10,%rdx,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x29525
movsd (%r11,%rdx,8), %xmm2
movsd %xmm2, (%r11,%rdx,8)
incq %rdx
jmp 0x29508
xorl %ecx, %ecx
testb %bl, %bl
cmovgl %ebx, %ecx
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %r10
movb (%rsp), %r9b
xorl %edx, %edx
movl 0x24(%rsp), %edi
testb %dil, %dil
movl $0x0, %esi
cmovgl %edi, %esi
movslq %esi, %rsi
movq %rsi, (%rsp)
movzbl %cl, %ebx
movapd 0x33d9(%rip), %xmm1     # 0x2c940
cmpq (%rsp), %rdx
je 0x295e1
xorl %ecx, %ecx
cmpq %rbx, %rcx
je 0x295dc
movq 0x18(%r15), %rsi
testq %rsi, %rsi
je 0x295d7
xorl %edi, %edi
cmpq %rdi, %r12
je 0x295d7
movq (%rsi,%rdx,8), %r8
movq (%r8,%rcx,8), %r8
movsd (%r8,%rdi,8), %xmm2
xorpd %xmm1, %xmm2
movq 0x60(%r15), %r8
movq (%r8,%rdx,8), %r8
movq (%r8,%rcx,8), %r8
mulsd (%r8,%rdi,8), %xmm2
movq (%rbp,%rdx,8), %r8
movsd (%r8,%rcx,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%r8,%rcx,8)
movq (%r13,%rdx,8), %r8
movq (%r8,%rcx,8), %r8
addsd (%r8,%rdi,8), %xmm2
movsd %xmm2, (%r8,%rdi,8)
incq %rdi
jmp 0x2957f
incq %rcx
jmp 0x2956f
incq %rdx
jmp 0x29567
movzbl %r9b, %ecx
xorl %edx, %edx
testb %r9b, %r9b
cmovlel %edx, %ecx
movzbl %cl, %ecx
cmpq %rbx, %rdx
je 0x29649
movq 0x20(%r15), %rsi
testq %rsi, %rsi
je 0x29644
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x29644
movq (%rsi,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm2
xorpd %xmm1, %xmm2
movq 0x68(%r15), %r8
movq (%r8,%rdx,8), %r8
mulsd (%r8,%rdi,8), %xmm2
movsd (%rax,%rdx,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%rax,%rdx,8)
movq (%r14,%rdx,8), %r8
addsd (%r8,%rdi,8), %xmm2
movsd %xmm2, (%r8,%rdi,8)
incq %rdi
jmp 0x29600
incq %rdx
jmp 0x295f0
xorl %edx, %edx
xorpd %xmm1, %xmm1
cmpq %rbx, %rdx
je 0x2972c
testq %r11, %r11
je 0x2967b
ucomisd %xmm1, %xmm0
jbe 0x2966f
movsd (%r10,%rdx,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x29675
movsd (%r11,%rdx,8), %xmm2
movsd %xmm2, (%r11,%rdx,8)
movq 0x18(%r15), %rsi
testq %rsi, %rsi
je 0x296e0
xorl %edi, %edi
cmpq (%rsp), %rdi
je 0x296e0
xorl %r8d, %r8d
cmpq %r8, %rbx
je 0x296db
movq (%rbp,%rdi,8), %r9
movsd (%r9,%rdx,8), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x296c2
movq (%r13,%rdi,8), %r9
movq (%r9,%rdx,8), %r9
movsd (%r9,%r8,8), %xmm2
divsd %xmm3, %xmm2
movq (%rsi,%rdi,8), %r9
movq (%r9,%rdx,8), %r9
jmp 0x296d0
movq (%rsi,%rdi,8), %r9
movq (%r9,%rdx,8), %r9
movsd (%r9,%r8,8), %xmm2
movsd %xmm2, (%r9,%r8,8)
incq %r8
jmp 0x2968f
incq %rdi
jmp 0x29686
movq 0x20(%r15), %rsi
testq %rsi, %rsi
je 0x29724
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x29724
movsd (%rax,%rdx,8), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x2970f
movq (%r14,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm2
divsd %xmm3, %xmm2
movq (%rsi,%rdx,8), %r8
jmp 0x29719
movq (%rsi,%rdx,8), %r8
movsd (%r8,%rdi,8), %xmm2
movsd %xmm2, (%r8,%rdi,8)
incq %rdi
jmp 0x296eb
incq %rdx
jmp 0x2964f
movq %rax, 0x40(%rsp)
movq 0x10(%rsp), %rdi
callq 0xe1de
movq 0x10(%r15), %rdi
testb %al, %al
movq %r14, 0x48(%rsp)
je 0x2984f
testq %rdi, %rdi
je 0x29786
movq 0x10(%rsp), %r14
movq %rdi, 0x18(%rsp)
movq %r14, %rdi
callq 0xe2a0
movq %rax, 0x30(%rsp)
movq %r14, %rdi
callq 0xe2d6
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %rax, %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
xorl %r8d, %r8d
cmpq %rbx, %r8
je 0x298b7
xorl %r14d, %r14d
movq %r8, 0x18(%rsp)
cmpq %r14, (%rsp)
je 0x297fb
movq 0x18(%r15), %rax
testq %rax, %rax
je 0x297f6
movq (%rax,%r14,8), %rax
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq (%rax,%r8,8), %rdi
movq 0x98(%rcx), %rax
movl $0x0, %edx
cmoveq %rdx, %rax
movq (%rax,%r14,8), %rax
movq 0xa0(%rcx), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r8,8), %rsi
movq (%rcx,%r14,8), %rax
movq (%rax,%r8,8), %rdx
movsbl %r12b, %ecx
callq 0x5d80
movq 0x18(%rsp), %r8
incq %r14
jmp 0x2979a
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x29847
movq (%rax,%r8,8), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rdi
movq %r8, %r14
callq 0xe2c4
movq (%rax,%r14,8), %rax
movq %rax, 0x50(%rsp)
movq 0x10(%rsp), %rdi
callq 0xe2fa
movq (%rax,%r14,8), %rdx
movq 0x30(%rsp), %rdi
movq 0x50(%rsp), %rsi
movl 0x3c(%rsp), %ecx
callq 0x5d80
movq %r14, %r8
incq %r8
jmp 0x29789
testq %rdi, %rdi
je 0x2985d
movsbl %r12b, %esi
callq 0x5600
xorl %ecx, %ecx
cmpq %rbx, %rcx
je 0x298b7
xorl %r14d, %r14d
movq %rcx, 0x18(%rsp)
cmpq %r14, (%rsp)
je 0x29896
movq 0x18(%r15), %rax
testq %rax, %rax
je 0x29891
movq (%rax,%r14,8), %rax
movq (%rax,%rcx,8), %rdi
movl 0xc(%rsp), %esi
callq 0x5600
movq 0x18(%rsp), %rcx
incq %r14
jmp 0x2986c
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x298b2
movq (%rax,%rcx,8), %rdi
movsbl %r12b, %esi
movq %rcx, %r14
callq 0x5600
movq %r14, %rcx
incq %rcx
jmp 0x2985f
movl 0xd0(%r15), %esi
movq 0xd8(%r15), %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x25848
movl 0xd0(%r15), %esi
movq 0xd8(%r15), %rdx
movq %rbx, %rdi
callq 0xe400
movsd %xmm0, (%rsp)
movq 0x28(%rsp), %rdi
callq 0x4290
movq %r13, %rdi
movl 0x24(%rsp), %r14d
movl %r14d, %esi
movl 0xc(%rsp), %ebx
movl %ebx, %edx
callq 0xa064
movq %rbp, %rdi
movl %r14d, %esi
callq 0x7f2c
movq 0x48(%rsp), %rdi
movl %ebx, %esi
callq 0x7f2c
movq 0x40(%rsp), %rdi
callq 0x4290
movsd (%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq