// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %r14
movsbl 0x8b9(%rax), %r15d
movsbl 0x8c8(%rax), %ebx
movzbl %bl, %r12d
movl $0x8, %esi
movq %r14, %rdi
callq 0x4130
movq %rax, 0x28(%rsp)
movl %ebx, %edi
movl %r14d, %esi
movl %r14d, %edx
callq 0xcaae
movq %rax, %rbp
movl %ebx, %edi
movl %r14d, %esi
callq 0xbb5c
movq %rax, 0x30(%rsp)
movl %ebx, %edi
movl %r14d, %esi
movl %r15d, 0x24(%rsp)
movl %r15d, %edx
callq 0xcaae
movq %rax, %r13
movl %ebx, 0x20(%rsp)
movl %ebx, %edi
movl %r14d, %esi
callq 0xbb5c
movq %rax, %r15
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x2a5b4
testb %r14b, %r14b
jle 0x2a5ba
movq 0x8(%rsp), %rcx
movq 0x58(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movapd 0xa3b7(%rip), %xmm1     # 0x34940
movq 0x28(%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 0x2a58e
jmp 0x2a5c3
xorpd %xmm0, %xmm0
jmp 0x2a5f1
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %rsi
testb %r14b, %r14b
jle 0x2a5f1
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2a5df
movsd (%rsi,%rcx,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x2a5e4
movsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x2a5ce
movslq 0x24(%rsp), %rcx
testb %r14b, %r14b
jle 0x2a723
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
movb %dil, 0x10(%rsp)
xorl %r8d, %r8d
movapd 0xa314(%rip), %xmm1     # 0x34940
movq 0x30(%rsp), %rdi
testq %rdx, %rdx
je 0x2a6a3
xorl %r9d, %r9d
testb %r12b, %r12b
jle 0x2a69b
movq 0x8(%rsp), %r10
movq 0x60(%r10), %r10
xorl %r11d, %r11d
movq (%rdx,%r11,8), %rbx
movq (%rbx,%r8,8), %rbx
movsd (%rbx,%r9,8), %xmm2
xorpd %xmm1, %xmm2
movq (%r10,%r11,8), %rbx
movq (%rbx,%r8,8), %rbx
mulsd (%rbx,%r9,8), %xmm2
movq (%rdi,%r11,8), %rbx
movsd (%rbx,%r8,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%rbx,%r8,8)
movq (%rbp,%r11,8), %rbx
movq (%rbx,%r8,8), %rbx
addsd (%rbx,%r9,8), %xmm2
movsd %xmm2, (%rbx,%r9,8)
incq %r11
cmpq %r11, %r12
jne 0x2a64a
incq %r9
cmpq %r14, %r9
jne 0x2a639
cmpb $0x0, 0x10(%rsp)
jne 0x2a717
xorl %r9d, %r9d
testb %r12b, %r12b
jle 0x2a70f
movq 0x8(%rsp), %r10
movq 0x68(%r10), %r10
xorl %r11d, %r11d
movq (%rsi,%r11,8), %rbx
movq (%rbx,%r8,8), %rbx
movsd (%rbx,%r9,8), %xmm2
xorpd %xmm1, %xmm2
movq (%r10,%r11,8), %rbx
movq (%rbx,%r8,8), %rbx
mulsd (%rbx,%r9,8), %xmm2
movq (%r15,%r11,8), %rbx
movsd (%rbx,%r8,8), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, (%rbx,%r8,8)
movq (%r13,%r11,8), %rbx
movq (%rbx,%r8,8), %rbx
addsd (%rbx,%r9,8), %xmm2
movsd %xmm2, (%rbx,%r9,8)
incq %r11
cmpq %r11, %r12
jne 0x2a6be
incq %r9
cmpq %rcx, %r9
jne 0x2a6ad
incq %r8
cmpq %r14, %r8
jne 0x2a631
testb %r14b, %r14b
movq 0x30(%rsp), %r8
jle 0x2a868
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, 0x10(%rsp)
orb %r9b, %r11b
movb %r11b, 0x38(%rsp)
xorl %r9d, %r9d
xorpd %xmm1, %xmm1
testq %rax, %rax
je 0x2a798
ucomisd %xmm1, %xmm0
jbe 0x2a78c
movq 0x28(%rsp), %rdi
movsd (%rdi,%r9,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x2a792
movsd (%rax,%r9,8), %xmm2
movsd %xmm2, (%rax,%r9,8)
cmpb $0x0, 0x10(%rsp)
jne 0x2a7f9
xorl %r10d, %r10d
movq (%r8,%r10,8), %r11
movq (%rdx,%r10,8), %rdi
movq (%rdi,%r9,8), %rbx
xorl %edi, %edi
movsd (%r11,%r9,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x2a7d1
movq (%rbp,%r10,8), %r8
movq (%r8,%r9,8), %r8
movsd (%r8,%rdi,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x2a7df
movq (%rdx,%r10,8), %r8
movq (%r8,%r9,8), %r8
movsd (%r8,%rdi,8), %xmm3
movsd %xmm3, (%rbx,%rdi,8)
incq %rdi
cmpq %rdi, %r14
jne 0x2a7b0
incq %r10
cmpq %r12, %r10
movq 0x30(%rsp), %r8
jne 0x2a7a2
cmpb $0x0, 0x38(%rsp)
jne 0x2a85c
xorl %r10d, %r10d
testb %r12b, %r12b
jle 0x2a854
xorl %r11d, %r11d
movq (%r15,%r11,8), %rdi
movsd (%rdi,%r9,8), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x2a830
movq (%r13,%r11,8), %rdi
movq (%rdi,%r9,8), %rdi
movsd (%rdi,%r10,8), %xmm2
divsd %xmm3, %xmm2
jmp 0x2a83e
movq (%rsi,%r11,8), %rdi
movq (%rdi,%r9,8), %rdi
movsd (%rdi,%r10,8), %xmm2
movq (%rsi,%r11,8), %rdi
movq (%rdi,%r9,8), %rdi
movsd %xmm2, (%rdi,%r10,8)
incq %r11
cmpq %r11, %r12
jne 0x2a80b
incq %r10
cmpq %rcx, %r10
jne 0x2a803
incq %r9
cmpq %r14, %r9
jne 0x2a770
movq 0x18(%rsp), %rdi
callq 0x114a8
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rdi
testb %al, %al
je 0x2a8ad
testq %rdi, %rdi
je 0x2a944
movq 0x18(%rsp), %rax
cmpb $0x0, 0x80(%rax)
je 0x2a938
movq 0xa0(%rax), %rsi
movq 0xb8(%rax), %rdx
jmp 0x2a93c
testq %rdi, %rdi
je 0x2a8ba
movl %r14d, %esi
callq 0x692d
testb %r14b, %r14b
jle 0x2aa50
movq $0x0, 0x10(%rsp)
testb %r12b, %r12b
jle 0x2a921
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x2a8f6
movq (%rax,%rbx,8), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl %r14d, %esi
callq 0x692d
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x2a919
movq (%rax,%rbx,8), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movl %r14d, %esi
callq 0x692d
incq %rbx
cmpq %rbx, %r12
jne 0x2a8d3
movq 0x10(%rsp), %rcx
incq %rcx
movq %rcx, 0x10(%rsp)
cmpq %r14, %rcx
jne 0x2a8cc
jmp 0x2aa50
xorl %esi, %esi
xorl %edx, %edx
movl %r14d, %ecx
callq 0x79f5
testb %r14b, %r14b
jle 0x2aa50
movslq %r14d, %rax
movq %rax, 0x40(%rsp)
movzbl 0x20(%rsp), %eax
movq %rax, 0x38(%rsp)
movq $0x0, 0x10(%rsp)
testb %r12b, %r12b
jle 0x2aa38
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x2a9ce
movq 0x18(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%rbx,8), %rax
movq 0xa8(%rdx), %rcx
movl $0x0, %r8d
cmoveq %r8, %rcx
movq 0x10(%rsp), %r9
movq (%rax,%r9,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r9,8), %rsi
movq 0xc0(%rdx), %rax
cmoveq %r8, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r9,8), %rdx
movl %r14d, %ecx
callq 0x79f5
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x2aa2a
movq 0x18(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%rbx,8), %rax
movq 0xb0(%rdx), %rcx
movl $0x0, %r8d
cmoveq %r8, %rcx
movq 0x10(%rsp), %r9
movq (%rax,%r9,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r9,8), %rsi
movq 0xc8(%rdx), %rax
cmoveq %r8, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r9,8), %rdx
movl 0x24(%rsp), %ecx
callq 0x79f5
incq %rbx
cmpq %rbx, 0x38(%rsp)
jne 0x2a973
movq 0x10(%rsp), %rcx
incq %rcx
movq %rcx, 0x10(%rsp)
cmpq 0x40(%rsp), %rcx
jne 0x2a968
movq 0x8(%rsp), %r12
movl 0xd0(%r12), %esi
movq 0xd8(%r12), %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x25d92
movl 0xd0(%r12), %esi
movq 0xd8(%r12), %rdx
movq %rbx, %rdi
callq 0x1161c
movsd %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %rdi
callq 0x42a0
movq %rbp, %rdi
movl 0x20(%rsp), %ebx
movl %ebx, %esi
movl %r14d, %edx
callq 0xca4f
movq 0x30(%rsp), %rdi
movl %ebx, %esi
callq 0xa5e0
movq %r13, %rdi
movl %ebx, %esi
movl %r14d, %edx
callq 0xca4f
movq %r15, %rdi
movl %ebx, %esi
callq 0xa5e0
movsd 0x8(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop