// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::doLagrangeStep(FitBitSlicedAB*)
NUMBER HMMProblemSlicedAB::doLagrangeStep(FitBitSlicedAB *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     = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nO);
	NUMBER **  b_B_den = init2D<NUMBER>((NDAT)nZ, (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, B
    for(i=0; i<nS; i++) {
        if( fb->A != NULL ) {
            for(j=0; j<nS; j++){
                for(z=0; z<nZ; z++){
                    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;
                }
            }
        }
        if( fb->B != NULL ) {
            for(m=0; m<nO; m++) {
                for(z=0; z<nZ; z++) {
                    buf = -fb->B[z][i][m]*fb->gradB[z][i][m]; /*negatie since were going against gradient*/
                    b_B_den[z][i] += buf;
                    b_B[z][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++)
                for(z=0; z<nZ; z++)
                    fb->B[z][i][m] = (b_B_den[z][i]>0) ? (b_B[z][i][m] / b_B_den[z][i]) : fb->B[z][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[z][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[z][i], this->getLbB()[z][i], this->getUbB()[z][i], nO);
            }
        }
    }
    // compute LL
    computeAlphaAndPOParam(fb->xndat, fb->x_data);
    ll = HMMProblemSlicedAB::getSumLogPOPara(fb->xndat, fb->x_data);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %r14
movsbl 0x8b9(%rax), %r13d
movsbl 0x8c8(%rax), %r15d
movzbl %r15b, %r12d
movl $0x8, %esi
movq %r14, %rdi
callq 0x4130
movq %rax, 0x38(%rsp)
movl %r15d, %edi
movl %r14d, %esi
movl %r14d, %edx
callq 0xcaae
movq %rax, %rbp
movl %r15d, %edi
movl %r14d, %esi
callq 0xbb36
movq %rax, %rbx
movl %r15d, %edi
movl %r14d, %esi
movl %r13d, 0x34(%rsp)
movl %r13d, %edx
callq 0xcaae
movq %rax, %r13
movl %r15d, 0x30(%rsp)
movl %r15d, %edi
movl %r14d, %esi
callq 0xbb36
movq %rax, %r15
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
movq %r14, 0x10(%rsp)
je 0x2a0bc
testb %r14b, %r14b
jle 0x2a336
movq 0x8(%rsp), %rcx
movq 0x58(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movapd 0x98e6(%rip), %xmm1     # 0x33940
movq 0x38(%rsp), %rsi
movsd (%rax,%rdx,8), %xmm2
xorpd %xmm1, %xmm2
mulsd (%rcx,%rdx,8), %xmm2
addsd %xmm2, %xmm0
addsd (%rsi,%rdx,8), %xmm2
movsd %xmm2, (%rsi,%rdx,8)
incq %rdx
cmpq %rdx, %r14
jne 0x2a05f
testb %r14b, %r14b
movq 0x20(%rsp), %rdi
jle 0x2a345
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2a0a8
movsd (%rsi,%rcx,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x2a0ad
movsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x2a097
jmp 0x2a0c5
xorpd %xmm0, %xmm0
movq 0x20(%rsp), %rdi
testb %r14b, %r14b
setg %cl
jle 0x2a345
movl %ecx, 0x28(%rsp)
movslq 0x34(%rsp), %rcx
movq 0x8(%rsp), %rsi
movq 0x18(%rsi), %rdx
movq 0x20(%rsi), %rsi
testq %rsi, %rsi
sete %r8b
testb %cl, %cl
setle %dil
orb %r8b, %dil
xorl %r8d, %r8d
movapd 0x983e(%rip), %xmm1     # 0x33940
testq %rdx, %rdx
je 0x2a176
xorl %r9d, %r9d
testb %r12b, %r12b
jle 0x2a16c
movq 0x8(%rsp), %r10
movq 0x60(%r10), %r10
xorl %r11d, %r11d
movq (%rdx,%r11,8), %r14
movq (%r14,%r8,8), %r14
movsd (%r14,%r9,8), %xmm2
xorpd %xmm1, %xmm2
movq (%r10,%r11,8), %r14
movq (%r14,%r8,8), %r14
mulsd (%r14,%r9,8), %xmm2
movq (%rbx,%r11,8), %r14
movsd (%r14,%r8,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%r14,%r8,8)
movq (%rbp,%r11,8), %r14
movq (%r14,%r8,8), %r14
addsd (%r14,%r9,8), %xmm2
movsd %xmm2, (%r14,%r9,8)
incq %r11
cmpq %r11, %r12
jne 0x2a11b
incq %r9
cmpq 0x10(%rsp), %r9
jne 0x2a10a
testb %dil, %dil
jne 0x2a1e8
xorl %r9d, %r9d
testb %r12b, %r12b
jle 0x2a1e0
movq 0x8(%rsp), %r10
movq 0x68(%r10), %r10
xorl %r11d, %r11d
movq (%rsi,%r11,8), %r14
movq (%r14,%r8,8), %r14
movsd (%r14,%r9,8), %xmm2
xorpd %xmm1, %xmm2
movq (%r10,%r11,8), %r14
movq (%r14,%r8,8), %r14
mulsd (%r14,%r9,8), %xmm2
movq (%r15,%r11,8), %r14
movsd (%r14,%r8,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%r14,%r8,8)
movq (%r13,%r11,8), %r14
movq (%r14,%r8,8), %r14
addsd (%r14,%r9,8), %xmm2
movsd %xmm2, (%r14,%r9,8)
incq %r11
cmpq %r11, %r12
jne 0x2a18f
incq %r9
cmpq %rcx, %r9
jne 0x2a17e
incq %r8
cmpq 0x10(%rsp), %r8
jne 0x2a102
movq 0x10(%rsp), %r8
testb %r8b, %r8b
jle 0x2a336
movq 0x8(%rsp), %rsi
movq 0x18(%rsi), %rdx
movq 0x20(%rsi), %rsi
testq %rdx, %rdx
sete %dil
testb %r12b, %r12b
setle %r10b
testq %rsi, %rsi
sete %r9b
testb %cl, %cl
setle %r11b
orb %dil, %r10b
movb %r10b, 0x18(%rsp)
orb %r9b, %r11b
movb %r11b, 0x40(%rsp)
xorl %r9d, %r9d
xorpd %xmm1, %xmm1
testq %rax, %rax
je 0x2a26b
ucomisd %xmm1, %xmm0
jbe 0x2a25f
movq 0x38(%rsp), %rdi
movsd (%rdi,%r9,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x2a265
movsd (%rax,%r9,8), %xmm2
movsd %xmm2, (%rax,%r9,8)
cmpb $0x0, 0x18(%rsp)
jne 0x2a2c5
xorl %r10d, %r10d
movq (%rbx,%r10,8), %r11
movq (%rdx,%r10,8), %rdi
movq (%rdi,%r9,8), %r14
xorl %edi, %edi
movsd (%r11,%r9,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x2a2a9
movq (%rbp,%r10,8), %r8
movq (%r8,%r9,8), %r8
movsd (%r8,%rdi,8), %xmm3
movq 0x10(%rsp), %r8
divsd %xmm2, %xmm3
jmp 0x2a2af
movsd (%r14,%rdi,8), %xmm3
movsd %xmm3, (%r14,%rdi,8)
incq %rdi
cmpq %rdi, %r8
jne 0x2a283
incq %r10
cmpq %r12, %r10
jne 0x2a275
cmpb $0x0, 0x40(%rsp)
jne 0x2a328
xorl %r10d, %r10d
testb %r12b, %r12b
jle 0x2a320
xorl %r11d, %r11d
movq (%r15,%r11,8), %rdi
movsd (%rdi,%r9,8), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x2a304
movq (%r13,%r11,8), %rdi
movq (%rdi,%r9,8), %rdi
movsd (%rdi,%r10,8), %xmm2
divsd %xmm3, %xmm2
movq (%rsi,%r11,8), %rdi
movq (%rdi,%r9,8), %rdi
jmp 0x2a312
movq (%rsi,%r11,8), %rdi
movq (%rdi,%r9,8), %rdi
movsd (%rdi,%r10,8), %xmm2
movsd %xmm2, (%rdi,%r10,8)
incq %r11
cmpq %r11, %r12
jne 0x2a2d7
incq %r10
cmpq %rcx, %r10
jne 0x2a2cf
incq %r9
cmpq %r8, %r9
jne 0x2a243
jmp 0x2a33e
movl $0x0, 0x28(%rsp)
movq 0x20(%rsp), %rdi
jmp 0x2a34d
movl $0x0, 0x28(%rsp)
callq 0x1133a
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rdi
testb %al, %al
je 0x2a392
testq %rdi, %rdi
movq 0x10(%rsp), %r8
je 0x2a445
movq 0x20(%rsp), %rax
cmpb $0x0, 0x80(%rax)
je 0x2a434
movq 0xa0(%rax), %rsi
movq 0xb8(%rax), %rdx
jmp 0x2a438
testq %rdi, %rdi
movq 0x10(%rsp), %r8
je 0x2a3a9
movl %r8d, %esi
callq 0x6836
movq 0x10(%rsp), %r8
cmpb $0x0, 0x28(%rsp)
je 0x2a55e
movq $0x0, 0x18(%rsp)
testb %r12b, %r12b
jle 0x2a41d
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x2a3ed
movq (%rax,%r14,8), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl %r8d, %esi
callq 0x6836
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x2a415
movq (%rax,%r14,8), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl %r8d, %esi
callq 0x6836
movq 0x10(%rsp), %r8
incq %r14
cmpq %r14, %r12
jne 0x2a3c5
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq %r8, %rcx
jne 0x2a3bd
jmp 0x2a55e
xorl %esi, %esi
xorl %edx, %edx
movl %r8d, %ecx
callq 0x7997
movq 0x10(%rsp), %r8
cmpb $0x0, 0x28(%rsp)
je 0x2a55e
movslq %r8d, %rax
movq %rax, 0x28(%rsp)
movzbl 0x30(%rsp), %eax
movq %rax, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
testb %r12b, %r12b
jle 0x2a546
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x2a4d7
movq 0x20(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%r14,8), %rax
movq 0xa8(%rdx), %rcx
movl $0x0, %r9d
cmoveq %r9, %rcx
movq 0x18(%rsp), %r10
movq (%rax,%r10,8), %rdi
movq (%rcx,%r14,8), %rax
movq (%rax,%r10,8), %rsi
movq 0xc0(%rdx), %rax
cmoveq %r9, %rax
movq (%rax,%r14,8), %rax
movq (%rax,%r10,8), %rdx
movl %r8d, %ecx
callq 0x7997
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x2a538
movq 0x20(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%r14,8), %rax
movq 0xb0(%rdx), %rcx
movl $0x0, %r8d
cmoveq %r8, %rcx
movq 0x18(%rsp), %r9
movq (%rax,%r9,8), %rdi
movq (%rcx,%r14,8), %rax
movq (%rax,%r9,8), %rsi
movq 0xc8(%rdx), %rax
cmoveq %r8, %rax
movq (%rax,%r14,8), %rax
movq (%rax,%r9,8), %rdx
movl 0x34(%rsp), %ecx
callq 0x7997
movq 0x10(%rsp), %r8
incq %r14
cmpq %r14, 0x40(%rsp)
jne 0x2a477
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0x28(%rsp), %rcx
jne 0x2a46b
movq 0x8(%rsp), %r14
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x25910
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %r12, %rdi
callq 0x114ae
movsd %xmm0, 0x8(%rsp)
movq 0x38(%rsp), %rdi
callq 0x42a0
movq %rbp, %rdi
movl 0x30(%rsp), %ebp
movl %ebp, %esi
movq 0x10(%rsp), %r14
movl %r14d, %edx
callq 0xca4f
movq %rbx, %rdi
movl %ebp, %esi
callq 0xa5dd
movq %r13, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0xca4f
movq %r15, %rdi
movl %ebp, %esi
callq 0xa5dd
movsd 0x8(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq