// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::init(param*)
void HMMProblem::init(struct param *param) {
	this->p = param;
	this->non01constraints = true;
    this->null_obs_ratio = Calloc(NUMBER, (size_t)this->p->nO);
    this->neg_log_lik = 0;
    this->null_skill_obs = 0;
    this->null_skill_obs_prob = 0;
    if( this->p->solver == METHOD_CGD && this->p->solver_setting == -1)
        this->p->solver_setting = 1; // default Fletcher-Reeves
    
    NPAR nS = this->p->nS, nO = this->p->nO;
    NUMBER *a_PI, ** a_A, ** a_B;
    init3Params(a_PI, a_A, a_B, nS, nO);
    
    //
    // setup params
    //
    this->pi = init2D<NUMBER>((NDAT)this->sizes[0], (NDAT)nS);
    this->A =  init3D<NUMBER>((NDAT)this->sizes[1], (NDAT)nS, (NDAT)nS);
    this->B =  init3D<NUMBER>((NDAT)this->sizes[2], (NDAT)nS, (NDAT)nO);
    NPAR i, j;
    int offset, idx;

    // write default parameters first
    NUMBER sumPI = 0;
    NUMBER sumA[this->p->nS];
    NUMBER sumB[this->p->nS];
    for(i=0; i<this->p->nS; i++) {
        sumA[i] = 0;
        sumB[i] = 0;
    }
    // populate PI
    for(i=0; i<((nS)-1); i++) {
        a_PI[i] = this->p->init_params[i];
        sumPI  += this->p->init_params[i];
    }
    a_PI[nS-1] = 1 - sumPI;
    // populate A
    offset = (int)(nS-1);
    for(i=0; i<nS; i++) {
        for(j=0; j<((nS)-1); j++) {
            idx = (int)(offset + i*((nS)-1) + j);
            a_A[i][j] = this->p->init_params[idx];
            sumA[i]  += this->p->init_params[idx];
        }
        a_A[i][((nS)-1)]  = 1 - sumA[i];
    }
    // populate B
    offset = (int)((nS-1) + nS*(nS-1));
    for(i=0; i<nS; i++) {
        for(j=0; j<((nO)-1); j++) {
            idx = (int)(offset + i*((nO)-1) + j);
            a_B[i][j] = this->p->init_params[idx];
            sumB[i] += this->p->init_params[idx];
        }
        a_B[i][((nO)-1)]  = 1 - sumB[i];
    }
    
    // mass produce PI's, A's, B's
    if( this->p->do_not_check_constraints==0 && !checkPIABConstraints(a_PI, a_A, a_B)) {
        fprintf(stderr,"params do not meet constraints.\n");
        exit(1);
    }

    NCAT x;
    for(x=0; x<this->sizes[0]; x++)
        cpy1D<NUMBER>(a_PI, this->pi[x], (NDAT)nS);
    for(x=0; x<this->sizes[1]; x++)
        cpy2D<NUMBER>(a_A, this->A[x], (NDAT)nS, (NDAT)nS);
    for(x=0; x<this->sizes[2]; x++)
        cpy2D<NUMBER>(a_B, this->B[x], (NDAT)nS, (NDAT)nO);

    // destroy setup params
    free(a_PI);
    free2D<NUMBER>(a_A, (NDAT)nS);
    free2D<NUMBER>(a_B, (NDAT)nS);
    if(param->initfile[0]!=0) { // from setup file
        // if needs be -- read in init params from a file
        this->readModel(param->initfile, false /* read and upload but not overwrite*/);
    }
    
    // populate boundaries
	// populate lb*/ub*
	// *PI
    init3Params(this->lbPI, this->lbA, this->lbB, nS, nO);
    init3Params(this->ubPI, this->ubA, this->ubB, nS, nO);
	for(i=0; i<nS; i++) {
		lbPI[i] = this->p->param_lo[i];
		ubPI[i] = this->p->param_hi[i];
	}
	// *A
	offset = nS;
	for(i=0; i<nS; i++)
		for(j=0; j<nS; j++) {
			idx = (int)(offset + i*nS + j);
			lbA[i][j] = this->p->param_lo[idx];
			ubA[i][j] = this->p->param_hi[idx];
		}
	// *B
	offset = (int)(nS + nS*nS);
	for(i=0; i<nS; i++)
		for(j=0; j<nO; j++) {
			idx = (int)(offset + i*nO + j);
			lbB[i][j] = this->p->param_lo[idx];
			ubB[i][j] = this->p->param_hi[idx];
		}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, 0x88(%rdi)
movb $0x1, 0x80(%rdi)
movsbl 0x8b9(%rsi), %r14d
movslq %r14d, %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x4130
movq %rax, 0x18(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rbx)
movq %rbx, -0x30(%rbp)
movq $0x0, 0x30(%rbx)
cmpb $0x3, 0x3f(%r12)
jne 0x10bfe
cmpb $-0x1, 0x40(%r12)
jne 0x10bfe
movb $0x1, 0x40(%r12)
movq %r12, -0x88(%rbp)
movsbl 0x8b8(%r12), %eax
movq %rax, -0x40(%rbp)
movslq %eax, %rbx
leaq -0x78(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x58(%rbp), %rcx
movl %ebx, %r8d
movl %r14d, %r9d
callq 0x1112e
movq -0x30(%rbp), %r12
movl 0xc(%r12), %edi
movl %ebx, %esi
callq 0xbb5c
movq %rax, 0x38(%r12)
movl 0x10(%r12), %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xcaae
movq %rax, 0x40(%r12)
movl 0x14(%r12), %edi
movq %rbx, -0x38(%rbp)
movl %ebx, %esi
movl %r14d, -0x64(%rbp)
movl %r14d, %edx
callq 0xcaae
movq %rsp, -0x90(%rbp)
movq %rax, 0x48(%r12)
movq 0x88(%r12), %rax
movzbl 0x8b8(%rax), %eax
leal 0xf(,%rax,8), %eax
andl $-0x10, %eax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movq 0x88(%r12), %r11
movzbl 0x8b8(%r11), %r14d
leal 0xf(,%r14,8), %eax
andl $-0x10, %eax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
testb %r14b, %r14b
jle 0x10ce6
shll $0x3, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r11, %r12
callq 0x4120
movq %r13, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4120
movq %r12, %r11
movq -0x38(%rbp), %rcx
leal -0x1(%rcx), %eax
xorpd %xmm1, %xmm1
cmpb $0x2, %cl
jl 0x10d1e
movq 0x8(%r11), %rcx
movq -0x78(%rbp), %rdx
xorl %esi, %esi
movsbq %sil, %rsi
movsd (%rcx,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
addsd (%rcx,%rsi,8), %xmm1
incb %sil
movsbl %sil, %edi
cmpl %edi, %eax
jg 0x10d00
movsd 0x23bca(%rip), %xmm0    # 0x348f0
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movq -0x78(%rbp), %r12
movq -0x38(%rbp), %rdi
movsd %xmm2, -0x8(%r12,%rdi,8)
testb %dil, %dil
jle 0x10db3
movq -0x60(%rbp), %rcx
xorl %edx, %edx
cmpb $0x1, %dil
je 0x10d94
leal 0x1(%rdx), %esi
imull %eax, %esi
movq 0x8(%r11), %rdi
movq (%rcx,%rdx,8), %r8
movsd (%rbx,%rdx,8), %xmm1
xorl %r10d, %r10d
xorl %r9d, %r9d
addl %esi, %r10d
movslq %r10d, %r10
movsd (%rdi,%r10,8), %xmm2
movsbq %r9b, %r9
movsd %xmm2, (%r8,%r9,8)
addsd (%rdi,%r10,8), %xmm1
incb %r9b
movsbl %r9b, %r10d
cmpl %r10d, %eax
jg 0x10d67
movsd %xmm1, (%rbx,%rdx,8)
movapd %xmm0, %xmm1
subsd (%rbx,%rdx,8), %xmm1
movq (%rcx,%rdx,8), %rsi
movq -0x38(%rbp), %rdi
movsd %xmm1, -0x8(%rsi,%rdi,8)
incq %rdx
cmpq %rdi, %rdx
jne 0x10d48
movq -0x40(%rbp), %rcx
incl %ecx
movl %ecx, -0x4c(%rbp)
testb %dil, %dil
jle 0x10e49
imull -0x4c(%rbp), %eax
leal -0x1(%r15), %ecx
movq -0x58(%rbp), %rdx
xorl %esi, %esi
cmpb $0x2, %r15b
jl 0x10e2a
movl %esi, %edi
imull %ecx, %edi
addl %eax, %edi
movq %r11, %rbx
movq 0x8(%r11), %r8
movq (%rdx,%rsi,8), %r9
movsd (%r13,%rsi,8), %xmm1
xorl %r11d, %r11d
xorl %r10d, %r10d
addl %edi, %r11d
movslq %r11d, %r11
movsd (%r8,%r11,8), %xmm2
movsbq %r10b, %r10
movsd %xmm2, (%r9,%r10,8)
addsd (%r8,%r11,8), %xmm1
incb %r10b
movsbl %r10b, %r11d
cmpl %r11d, %ecx
jg 0x10df8
movsd %xmm1, (%r13,%rsi,8)
movq %rbx, %r11
movapd %xmm0, %xmm1
subsd (%r13,%rsi,8), %xmm1
movq (%rdx,%rsi,8), %rdi
movsd %xmm1, -0x8(%rdi,%r15,8)
incq %rsi
cmpq -0x38(%rbp), %rsi
jne 0x10dd3
cmpb $0x0, 0x32(%r11)
jne 0x10e6c
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rdi
movq %r12, %rsi
callq 0x1117c
testb %al, %al
je 0x11129
movq %r15, -0x70(%rbp)
movq -0x30(%rbp), %r15
cmpl $0x0, 0xc(%r15)
movq -0x38(%rbp), %rax
jle 0x10ea9
leaq (,%rax,8), %rbx
xorl %r14d, %r14d
movq 0x38(%r15), %rax
movq (%rax,%r14,8), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x41c0
incq %r14
movslq 0xc(%r15), %rax
cmpq %rax, %r14
jl 0x10e8a
movq %r12, -0x80(%rbp)
cmpl $0x0, 0x10(%r15)
movl -0x40(%rbp), %r13d
jle 0x10f19
movq -0x60(%rbp), %r12
movq -0x38(%rbp), %rax
leaq (,%rax,8), %rbx
movq $0x0, -0x48(%rbp)
cmpb $0x0, -0x38(%rbp)
jle 0x10f01
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %r15
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
movq (%r12,%r14,8), %rsi
movq %rbx, %rdx
callq 0x41c0
incq %r14
cmpq %r14, %r13
jne 0x10ee9
movq -0x48(%rbp), %rdx
incq %rdx
movq -0x30(%rbp), %r15
movslq 0x10(%r15), %rax
movq %rdx, -0x48(%rbp)
cmpq %rax, %rdx
jl 0x10ed0
cmpl $0x0, 0x14(%r15)
jle 0x10f81
movq -0x58(%rbp), %r12
movq -0x70(%rbp), %rax
leaq (,%rax,8), %rbx
movq $0x0, -0x48(%rbp)
cmpb $0x0, -0x38(%rbp)
jle 0x10f69
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %r15
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
movq (%r12,%r14,8), %rsi
movq %rbx, %rdx
callq 0x41c0
incq %r14
cmpq %r14, %r13
jne 0x10f51
movq -0x48(%rbp), %rdx
incq %rdx
movq -0x30(%rbp), %r15
movslq 0x14(%r15), %rax
movq %rdx, -0x48(%rbp)
cmpq %rax, %rdx
jl 0x10f38
movq -0x80(%rbp), %rdi
callq 0x42a0
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rbx
movl %ebx, %esi
callq 0xa5e0
movq -0x58(%rbp), %rdi
movl %ebx, %esi
callq 0xa5e0
movq -0x88(%rbp), %rsi
cmpb $0x0, 0x6c(%rsi)
je 0x10fbf
addq $0x6c, %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x112f0
leaq 0x50(%r15), %rsi
leaq 0x58(%r15), %rdx
leaq 0x60(%r15), %rcx
movq -0x40(%rbp), %r8
movq -0x70(%rbp), %r12
movl %r12d, %r9d
callq 0x1112e
leaq 0x68(%r15), %rsi
leaq 0x70(%r15), %rdx
leaq 0x78(%r15), %rcx
movq -0x38(%rbp), %r13
movl %r13d, %r8d
movl -0x64(%rbp), %r15d
movl %r15d, %r9d
callq 0x1112e
testb %r13b, %r13b
jle 0x11038
movq -0x30(%rbp), %rsi
movq 0x88(%rsi), %rcx
movq 0x18(%rcx), %rax
movq 0x20(%rcx), %rcx
movq 0x50(%rsi), %rdx
movq 0x68(%rsi), %rsi
xorl %edi, %edi
movsd (%rax,%rdi,8), %xmm0
movsd %xmm0, (%rdx,%rdi,8)
movsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %r13
jne 0x1101c
testb %r13b, %r13b
jle 0x110a0
movq -0x30(%rbp), %rcx
movq 0x88(%rcx), %rsi
movq 0x58(%rcx), %rax
movq 0x70(%rcx), %rcx
movq 0x18(%rsi), %rdx
movq 0x20(%rsi), %rsi
xorl %edi, %edi
movq -0x40(%rbp), %r8
movslq %r8d, %r10
leaq (%rsi,%r10,8), %r9
leaq (%rdx,%r10,8), %r10
movq (%rax,%rdi,8), %r11
movq (%rcx,%rdi,8), %rbx
xorl %r14d, %r14d
movsd (%r10,%r14,8), %xmm0
movsd %xmm0, (%r11,%r14,8)
movsd (%r9,%r14,8), %xmm0
movsd %xmm0, (%rbx,%r14,8)
incq %r14
cmpq %r14, %r13
jne 0x11074
incq %rdi
addl -0x40(%rbp), %r8d
cmpq %r13, %rdi
jne 0x1105e
testb %r13b, %r13b
movl -0x4c(%rbp), %r14d
jle 0x11113
imull -0x40(%rbp), %r14d
movq -0x30(%rbp), %rdx
movq 0x88(%rdx), %rax
movq 0x60(%rdx), %rcx
movq 0x78(%rdx), %rdx
movzbl %r15b, %esi
xorl %edi, %edi
testb %r12b, %r12b
jle 0x11108
movslq %r14d, %r8
shlq $0x3, %r8
movq (%rcx,%rdi,8), %r9
movq (%rdx,%rdi,8), %r10
movq 0x20(%rax), %r11
addq %r8, %r11
addq 0x18(%rax), %r8
xorl %ebx, %ebx
movsd (%r8,%rbx,8), %xmm0
movsd %xmm0, (%r9,%rbx,8)
movsd (%r11,%rbx,8), %xmm0
movsd %xmm0, (%r10,%rbx,8)
incq %rbx
cmpq %rbx, %rsi
jne 0x110e8
incq %rdi
addl %r15d, %r14d
cmpq %r13, %rdi
jne 0x110c7
movq -0x90(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4a47