// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rdi, 0x10(%rsp)
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %r14
movzbl %r14b, %ebx
movsbl 0x8b9(%rax), %r15d
movl $0xff, %ecx
movsbl 0x8c8(%rax), %ebp
movl %ebp, %eax
andl %ecx, %eax
movq %rax, (%rsp)
movl $0x8, %esi
movq %r14, %rdi
callq 0x4130
movq %rax, 0x38(%rsp)
movl %ebp, %edi
movl %r14d, %esi
movl %r14d, %edx
callq 0xcaae
movq %rax, %r13
movl %ebp, 0x1c(%rsp)
movl %ebp, %edi
movl %r14d, %esi
callq 0xbb36
movq %rax, 0x28(%rsp)
movl %r14d, %edi
movq %r15, 0x8(%rsp)
movl %r15d, %esi
callq 0xbb36
movq %rax, 0x48(%rsp)
movl $0x8, %esi
movq %r14, 0x40(%rsp)
movq %r14, %rdi
callq 0x4130
movq %rax, 0x20(%rsp)
movq 0x10(%r12), %rax
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x300f5
testb %bl, %bl
movq 0x38(%rsp), %rbp
movq 0x28(%rsp), %r14
movq 0x8(%rsp), %rsi
movl $0xff, %edi
jle 0x30109
movq 0x58(%r12), %rcx
xorl %edx, %edx
movapd 0x38a1(%rip), %xmm1     # 0x33940
movsd (%rax,%rdx,8), %xmm2
xorpd %xmm1, %xmm2
mulsd (%rcx,%rdx,8), %xmm2
addsd %xmm2, %xmm0
addsd (%rbp,%rdx,8), %xmm2
movsd %xmm2, (%rbp,%rdx,8)
incq %rdx
cmpq %rdx, %rbx
jne 0x3009f
testb %bl, %bl
jle 0x30109
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x300e1
movsd (%rbp,%rcx,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x300e6
movsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x300cf
jmp 0x30109
movq 0x38(%rsp), %rbp
movq 0x28(%rsp), %r14
movq 0x8(%rsp), %rsi
movl $0xff, %edi
andl %edi, %esi
movq %rsi, 0x8(%rsp)
cmpb $0x0, (%rsp)
jle 0x3019e
xorl %ecx, %ecx
movapd 0x381c(%rip), %xmm1     # 0x33940
testb %bl, %bl
jle 0x30195
movq 0x18(%r12), %rdx
xorl %esi, %esi
testq %rdx, %rdx
je 0x3018d
movq (%rdx,%rcx,8), %rdi
movq (%rdi,%rsi,8), %rdi
movq 0x60(%r12), %r8
movq (%r8,%rcx,8), %r8
movq (%r8,%rsi,8), %r8
movq (%r14,%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, %rbx
jne 0x30159
incq %rsi
cmpq %rbx, %rsi
jne 0x3012f
incq %rcx
cmpq (%rsp), %rcx
jne 0x30124
testb %bl, %bl
movq 0x8(%rsp), %r11
jle 0x30342
movq 0x20(%r12), %rcx
testq %rcx, %rcx
sete %sil
testb %r11b, %r11b
setle %dl
orb %sil, %dl
xorl %esi, %esi
movapd 0x3776(%rip), %xmm1     # 0x33940
testb %dl, %dl
jne 0x30224
movq (%rcx,%rsi,8), %rdi
movq 0x68(%r12), %r8
movq (%r8,%rsi,8), %r8
movq 0x48(%rsp), %r9
movq (%r9,%rsi,8), %r9
movq 0x20(%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 0x301f2
movq 0x20(%rsp), %rdi
movsd %xmm2, (%rdi,%rsi,8)
incq %rsi
cmpq %rbx, %rsi
jne 0x301ca
testb %bl, %bl
jle 0x30342
movq 0x18(%r12), %rcx
movq 0x20(%r12), %rdx
testq %rcx, %rcx
sete %r8b
cmpb $0x0, (%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 0x3028c
ucomisd %xmm1, %xmm0
jbe 0x30280
movsd (%rbp,%r8,8), %xmm2
divsd %xmm0, %xmm2
jmp 0x30286
movsd (%rax,%r8,8), %xmm2
movsd %xmm2, (%rax,%r8,8)
testb %sil, %sil
jne 0x302e6
xorl %r9d, %r9d
movq (%r14,%r9,8), %r10
movq (%rcx,%r9,8), %r11
movq (%r11,%r8,8), %r11
xorl %r14d, %r14d
movsd (%r10,%r8,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x302c4
movq (%r13,%r9,8), %r15
movq (%r15,%r8,8), %r15
movsd (%r15,%r14,8), %xmm3
divsd %xmm2, %xmm3
jmp 0x302ca
movsd (%r11,%r14,8), %xmm3
movsd %xmm3, (%r11,%r14,8)
incq %r14
cmpq %r14, %rbx
jne 0x302a3
incq %r9
cmpq (%rsp), %r9
movq 0x28(%rsp), %r14
jne 0x30294
testb %dil, %dil
movq 0x8(%rsp), %r11
jne 0x30336
movq 0x20(%rsp), %r9
movsd (%r9,%r8,8), %xmm2
movq (%rdx,%r8,8), %r9
xorl %r10d, %r10d
ucomisd %xmm1, %xmm2
jbe 0x30322
movq 0x48(%rsp), %r11
movq (%r11,%r8,8), %r11
movsd (%r11,%r10,8), %xmm3
movq 0x8(%rsp), %r11
divsd %xmm2, %xmm3
jmp 0x30328
movsd (%r9,%r10,8), %xmm3
movsd %xmm3, (%r9,%r10,8)
incq %r10
cmpq %r10, %r11
jne 0x30302
incq %r8
cmpq %rbx, %r8
jne 0x30268
movq 0x10(%rsp), %rdi
callq 0x1133a
movq 0x10(%r12), %r15
testb %al, %al
movq %r12, 0x30(%rsp)
je 0x30467
testq %r15, %r15
je 0x3038c
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x113a8
movq %rax, %r12
movq %r14, %rdi
callq 0x113de
movsbl %bl, %ecx
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x7997
testb %bl, %bl
jle 0x304cf
movslq 0x40(%rsp), %rax
movq %rax, 0x50(%rsp)
movzbl 0x1c(%rsp), %r14d
xorl %ebp, %ebp
xorl %r12d, %r12d
cmpb $0x0, (%rsp)
jle 0x3040a
xorl %r15d, %r15d
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x30402
movq (%rax,%r15,8), %rax
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq (%rax,%r12,8), %rdi
movq 0x98(%rcx), %rax
cmoveq %rbp, %rax
movq (%rax,%r15,8), %rax
movq 0xa0(%rcx), %rcx
cmoveq %rbp, %rcx
movq (%rax,%r12,8), %rsi
movq (%rcx,%r15,8), %rax
movq (%rax,%r12,8), %rdx
movsbl %bl, %ecx
callq 0x7997
incq %r15
cmpq %r15, %r14
jne 0x303b2
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x30457
movq (%rax,%r12,8), %rax
movq %rax, 0x60(%rsp)
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x113cc
movq (%rax,%r12,8), %rax
movq %rax, 0x58(%rsp)
movq %r15, %rdi
callq 0x11402
movq (%rax,%r12,8), %rdx
movsbl 0x8(%rsp), %ecx
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x7997
incq %r12
cmpq 0x50(%rsp), %r12
jne 0x303a9
jmp 0x304cf
testq %r15, %r15
je 0x30477
movsbl %bl, %esi
movq %r15, %rdi
callq 0x6836
testb %bl, %bl
jle 0x304cf
xorl %r14d, %r14d
movq 0x30(%rsp), %r12
cmpb $0x0, (%rsp)
jle 0x304af
xorl %r15d, %r15d
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x304a6
movq (%rax,%r15,8), %rax
movq (%rax,%r14,8), %rdi
movsbl %bl, %esi
callq 0x6836
incq %r15
cmpq %r15, (%rsp)
jne 0x3048c
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x304c7
movq (%rax,%r14,8), %rdi
movq 0x40(%rsp), %rsi
callq 0x6836
incq %r14
cmpq %rbx, %r14
jne 0x30483
movq 0x30(%rsp), %r14
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bba0
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %rbx, %rdi
callq 0x114ae
movsd %xmm0, (%rsp)
movq 0x38(%rsp), %rdi
callq 0x42a0
movq %r13, %rdi
movl 0x1c(%rsp), %ebp
movl %ebp, %esi
movq 0x40(%rsp), %rbx
movl %ebx, %edx
callq 0xca4f
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0xa5dd
movq 0x48(%rsp), %rdi
movl %ebx, %esi
callq 0xa5dd
movq 0x20(%rsp), %rdi
callq 0x42a0
movsd (%rsp), %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop