// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rsi, %r14
movq %rdi, 0x20(%rsp)
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %rbx
movzbl %bl, %r12d
movsbl 0x8b9(%rax), %r15d
movsbl 0x8c8(%rax), %ebp
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq %rax, 0x28(%rsp)
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xcaae
movq %rax, %r13
movl %ebp, 0x14(%rsp)
movl %ebp, %edi
movq %r14, %rbp
movl %ebx, %esi
callq 0xbb5c
movq %rax, 0x48(%rsp)
movl %ebx, %edi
movl %r15d, %esi
callq 0xbb5c
movq %rax, 0x18(%rsp)
movl $0x8, %esi
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0x4130
movq %rax, 0x30(%rsp)
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x304fd
testb %r12b, %r12b
jle 0x3050d
movq 0x58(%rbp), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movapd 0x4475(%rip), %xmm1     # 0x34940
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %rbx
movsd (%rax,%rdx,8), %xmm2
xorpd %xmm1, %xmm2
mulsd (%rcx,%rdx,8), %xmm2
addsd %xmm2, %xmm0
addsd (%r14,%rdx,8), %xmm2
movsd %xmm2, (%r14,%rdx,8)
incq %rdx
cmpq %rdx, %r12
jne 0x304d5
jmp 0x3051b
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %rbx
jmp 0x3054a
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %r14
movq 0x18(%rsp), %rbx
testb %r12b, %r12b
jle 0x3054a
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x30538
movsd (%r14,%rcx,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x3053d
movsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r12
jne 0x30526
movzbl %r15b, %ecx
movq %rcx, 0x40(%rsp)
movl 0x14(%rsp), %ecx
movslq %ecx, %rdx
movq %rdx, 0x8(%rsp)
testb %cl, %cl
jle 0x305f0
xorl %ecx, %ecx
movapd 0x43cf(%rip), %xmm1     # 0x34940
testb %r12b, %r12b
jle 0x305e6
movq 0x18(%rbp), %rdx
xorl %esi, %esi
testq %rdx, %rdx
je 0x305de
movq (%rdx,%rcx,8), %rdi
movq (%rdi,%rsi,8), %rdi
movq 0x60(%rbp), %r8
movq (%r8,%rcx,8), %r8
movq (%r8,%rsi,8), %r8
movq 0x48(%rsp), %r9
movq (%r9,%rcx,8), %r9
movq (%r13,%rcx,8), %r10
movq (%r10,%rsi,8), %r10
xorl %r11d, %r11d
movsd (%rdi,%r11,8), %xmm2
xorpd %xmm1, %xmm2
mulsd (%r8,%r11,8), %xmm2
movsd (%r9,%rsi,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%r9,%rsi,8)
addsd (%r10,%r11,8), %xmm2
movsd %xmm2, (%r10,%r11,8)
incq %r11
cmpq %r11, %r12
jne 0x305aa
incq %rsi
cmpq %r12, %rsi
jne 0x3057c
incq %rcx
cmpq 0x8(%rsp), %rcx
jne 0x30571
testb %r12b, %r12b
movq 0x40(%rsp), %r11
jle 0x30674
movq 0x20(%rbp), %rcx
testq %rcx, %rcx
sete %sil
testb %r11b, %r11b
setle %dl
orb %sil, %dl
xorl %esi, %esi
movapd 0x4328(%rip), %xmm1     # 0x34940
testb %dl, %dl
jne 0x3066c
movq (%rcx,%rsi,8), %rdi
movq 0x68(%rbp), %r8
movq (%r8,%rsi,8), %r8
movq (%rbx,%rsi,8), %r9
movq 0x30(%rsp), %r10
movsd (%r10,%rsi,8), %xmm2
xorl %r10d, %r10d
movsd (%rdi,%r10,8), %xmm3
xorpd %xmm1, %xmm3
mulsd (%r8,%r10,8), %xmm3
addsd %xmm3, %xmm2
addsd (%r9,%r10,8), %xmm3
movsd %xmm3, (%r9,%r10,8)
incq %r10
cmpq %r10, %r11
jne 0x3063a
movq 0x30(%rsp), %rdi
movsd %xmm2, (%rdi,%rsi,8)
incq %rsi
cmpq %r12, %rsi
jne 0x30618
testb %r12b, %r12b
jle 0x3078d
movq 0x18(%rbp), %rcx
movq 0x20(%rbp), %rdx
testq %rcx, %rcx
sete %r8b
cmpb $0x0, 0x8(%rsp)
setle %sil
testq %rdx, %rdx
sete %r9b
testb %r11b, %r11b
setle %dil
orb %r8b, %sil
orb %r9b, %dil
xorl %r8d, %r8d
xorpd %xmm1, %xmm1
testq %rax, %rax
je 0x306d3
ucomisd %xmm1, %xmm0
jbe 0x306c7
movsd (%r14,%r8,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x306cd
movsd (%rax,%r8,8), %xmm2
movsd %xmm2, (%rax,%r8,8)
testb %sil, %sil
jne 0x30735
xorl %r9d, %r9d
movq 0x48(%rsp), %r10
movq (%r10,%r9,8), %r10
movq (%rcx,%r9,8), %r11
movq (%r11,%r8,8), %r11
xorl %ebx, %ebx
movsd (%r10,%r8,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x3070f
movq (%r13,%r9,8), %r15
movq (%r15,%r8,8), %r15
movsd (%r15,%rbx,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x3071d
movq (%rcx,%r9,8), %r15
movq (%r15,%r8,8), %r15
movsd (%r15,%rbx,8), %xmm3
movsd %xmm3, (%r11,%rbx,8)
incq %rbx
cmpq %rbx, %r12
jne 0x306ee
incq %r9
cmpq 0x8(%rsp), %r9
jne 0x306db
testb %dil, %dil
movq 0x18(%rsp), %rbx
jne 0x30781
movq 0x30(%rsp), %r9
movsd (%r9,%r8,8), %xmm2
movq (%rdx,%r8,8), %r9
xorl %r10d, %r10d
ucomisd %xmm1, %xmm2
jbe 0x30767
movq (%rbx,%r8,8), %r11
movsd (%r11,%r10,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x30771
movq (%rdx,%r8,8), %r11
movsd (%r11,%r10,8), %xmm3
movsd %xmm3, (%r9,%r10,8)
incq %r10
cmpq %r10, 0x40(%rsp)
jne 0x30751
incq %r8
cmpq %r12, %r8
jne 0x306b0
movq 0x20(%rsp), %rdi
callq 0x114a8
movq 0x10(%rbp), %r14
testb %al, %al
je 0x308b0
testq %r14, %r14
je 0x307d2
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x11516
movq %rax, %r15
movq %rbx, %rdi
callq 0x1154c
movsbl %r12b, %ecx
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x79f5
testb %r12b, %r12b
jle 0x30919
movslq 0x38(%rsp), %rax
movq %rax, 0x50(%rsp)
movzbl 0x14(%rsp), %ebx
xorl %r15d, %r15d
movq %rbp, %r14
cmpb $0x0, 0x8(%rsp)
jle 0x30852
xorl %ebp, %ebp
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x3084a
movq (%rax,%rbp,8), %rax
movq 0x20(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq (%rax,%r15,8), %rdi
movq 0x98(%rcx), %rax
movl $0x0, %edx
cmoveq %rdx, %rax
movq (%rax,%rbp,8), %rax
movq 0xa0(%rcx), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r15,8), %rsi
movq (%rcx,%rbp,8), %rax
movq (%rax,%r15,8), %rdx
movsbl %r12b, %ecx
callq 0x79f5
incq %rbp
cmpq %rbp, %rbx
jne 0x307f9
movq %r14, %rbp
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x308a0
movq (%rax,%r15,8), %rax
movq %rax, 0x60(%rsp)
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
callq 0x1153a
movq (%rax,%r15,8), %rax
movq %rax, 0x58(%rsp)
movq %rbp, %rdi
movq %r14, %rbp
callq 0x11570
movq (%rax,%r15,8), %rdx
movsbl 0x40(%rsp), %ecx
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x79f5
incq %r15
cmpq 0x50(%rsp), %r15
jne 0x307ed
jmp 0x30919
testq %r14, %r14
je 0x308c2
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x692d
testb %r12b, %r12b
jle 0x30919
movzbl 0x8(%rsp), %ebx
xorl %r14d, %r14d
cmpb $0x0, 0x8(%rsp)
jle 0x308fb
xorl %r15d, %r15d
movq 0x18(%rbp), %rax
testq %rax, %rax
je 0x308f3
movq (%rax,%r15,8), %rax
movq (%rax,%r14,8), %rdi
movsbl %r12b, %esi
callq 0x692d
incq %r15
cmpq %r15, %rbx
jne 0x308d9
movq 0x20(%rbp), %rax
testq %rax, %rax
je 0x30911
movq (%rax,%r14,8), %rdi
movsbl %r12b, %esi
callq 0x692d
incq %r14
cmpq %r12, %r14
jne 0x308cf
movl 0xd0(%rbp), %esi
movq 0xd8(%rbp), %rdx
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bfa8
movl 0xd0(%rbp), %esi
movq 0xd8(%rbp), %rdx
movq %rbx, %rdi
callq 0x1161c
movsd %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %rdi
callq 0x42a0
movq %r13, %rdi
movl 0x14(%rsp), %ebp
movl %ebp, %esi
movq 0x38(%rsp), %rbx
movl %ebx, %edx
callq 0xca4f
movq 0x48(%rsp), %rdi
movl %ebp, %esi
callq 0xa5e0
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0xa5e0
movq 0x30(%rsp), %rdi
callq 0x42a0
movsd 0x8(%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq