// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, 0x88(%rdi)
movb $0x1, 0x80(%rdi)
movsbl 0x8b9(%rsi), %eax
movq %rax, -0x38(%rbp)
movslq %eax, %rdi
pushq $0x8
popq %rsi
movq %rdi, %r15
callq 0x4120
movq %rax, 0x18(%rbx)
andq $0x0, 0x30(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rbx)
cmpb $0x3, 0x3f(%r14)
jne 0xda49
cmpb $-0x1, 0x40(%r14)
jne 0xda49
movb $0x1, 0x40(%r14)
movsbl 0x8b8(%r14), %eax
movq %rax, -0x30(%rbp)
movslq %eax, %r13
leaq -0x78(%rbp), %rsi
leaq -0x70(%rbp), %rdx
leaq -0x68(%rbp), %rcx
movl %r13d, %r8d
movq -0x38(%rbp), %r12
movl %r12d, %r9d
callq 0xde8c
movl 0xc(%rbx), %edi
movl %r13d, %esi
callq 0x9328
movq %rax, 0x38(%rbx)
movl 0x10(%rbx), %edi
movl %r13d, %esi
movl %r13d, %edx
callq 0xa0be
movq %rax, 0x40(%rbx)
movl 0x14(%rbx), %edi
movl %r13d, %esi
movl %r12d, %edx
callq 0xa0be
movq %rsp, -0x60(%rbp)
movq %rax, 0x48(%rbx)
movq 0x88(%rbx), %rax
movzbl 0x8b8(%rax), %eax
leal 0xf(,%rax,8), %eax
andl $-0x10, %eax
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, %rsp
movq 0x88(%rbx), %rax
movzbl 0x8b8(%rax), %edx
leal 0xf(,%rdx,8), %edi
andl $-0x10, %edi
movq %rsp, %rcx
subq %rdi, %rcx
movq %rcx, %rsp
xorl %edi, %edi
testb %dl, %dl
cmovlel %edi, %edx
cmpq %rdi, %rdx
je 0xdb07
andq $0x0, (%rsi,%rdi,8)
andq $0x0, (%rcx,%rdi,8)
incq %rdi
jmp 0xdaf3
leal -0x1(%r13), %edx
xorpd %xmm1, %xmm1
movq -0x78(%rbp), %r9
xorl %edi, %edi
cmpl %edi, %edx
jle 0xdb32
movq 0x8(%rax), %r8
movsd (%r8,%rdi,8), %xmm0
movsd %xmm0, (%r9,%rdi,8)
addsd %xmm0, %xmm1
incq %rdi
jmp 0xdb15
movq %r14, -0x58(%rbp)
movsd 0x1edb2(%rip), %xmm0    # 0x2c8f0
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movq %r9, -0x40(%rbp)
movsd %xmm2, -0x8(%r9,%r13,8)
movq -0x70(%rbp), %r14
xorl %edi, %edi
testb %r13b, %r13b
movl $0x0, %r8d
cmovgl %r13d, %r8d
movslq %r8d, %r12
movl %edx, %r8d
cmpq %r12, %rdi
je 0xdbc0
movslq %r8d, %r9
shlq $0x3, %r9
xorl %r10d, %r10d
cmpl %r10d, %edx
jle 0xdba4
movq 0x8(%rax), %r11
addq %r9, %r11
movsd (%r11,%r10,8), %xmm1
movq (%r14,%rdi,8), %r11
movsd %xmm1, (%r11,%r10,8)
addsd (%rsi,%rdi,8), %xmm1
movsd %xmm1, (%rsi,%rdi,8)
incq %r10
jmp 0xdb79
movapd %xmm0, %xmm1
subsd (%rsi,%rdi,8), %xmm1
movq (%r14,%rdi,8), %r9
movsd %xmm1, -0x8(%r9,%r13,8)
incq %rdi
addl %edx, %r8d
jmp 0xdb6a
movq %r13, -0x50(%rbp)
movq -0x30(%rbp), %rsi
incl %esi
movl %esi, -0x44(%rbp)
imull %esi, %edx
movq -0x38(%rbp), %rsi
decl %esi
movq -0x68(%rbp), %r13
xorl %edi, %edi
cmpq %r12, %rdi
je 0xdc33
movslq %edx, %r8
shlq $0x3, %r8
xorl %r9d, %r9d
cmpl %r9d, %esi
jle 0xdc17
movq 0x8(%rax), %r10
addq %r8, %r10
movsd (%r10,%r9,8), %xmm1
movq (%r13,%rdi,8), %r10
movsd %xmm1, (%r10,%r9,8)
addsd (%rcx,%rdi,8), %xmm1
movsd %xmm1, (%rcx,%rdi,8)
incq %r9
jmp 0xdbeb
movapd %xmm0, %xmm1
subsd (%rcx,%rdi,8), %xmm1
movq (%r13,%rdi,8), %r8
movsd %xmm1, -0x8(%r8,%r15,8)
incq %rdi
addl %esi, %edx
jmp 0xdbdc
cmpb $0x0, 0x32(%rax)
jne 0xdc53
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0xded0
testb %al, %al
je 0xde68
shlq $0x3, -0x50(%rbp)
xorl %r15d, %r15d
movslq 0xc(%rbx), %rax
cmpq %rax, %r15
jge 0xdc7e
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x41c0
incq %r15
jmp 0xdc5b
xorl %r15d, %r15d
movslq 0x10(%rbx), %rax
cmpq %rax, %r15
jge 0xdca5
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq %r14, %rdi
movq -0x30(%rbp), %rcx
movl %ecx, %edx
callq 0x93ad
incq %r15
jmp 0xdc81
xorl %r15d, %r15d
movslq 0x14(%rbx), %rax
cmpq %rax, %r15
jge 0xdcce
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq %r13, %rdi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x93ad
incq %r15
jmp 0xdca8
movq -0x40(%rbp), %rdi
callq 0x4290
movq %r14, %rdi
movq -0x30(%rbp), %r14
movl %r14d, %esi
callq 0x7f2c
movq %r13, %rdi
movl %r14d, %esi
callq 0x7f2c
movq -0x58(%rbp), %rsi
cmpb $0x0, 0x6c(%rsi)
je 0xdd09
addq $0x6c, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe026
leaq 0x50(%rbx), %rsi
leaq 0x58(%rbx), %rdx
leaq 0x60(%rbx), %rcx
movq -0x30(%rbp), %r15
movl %r15d, %r8d
movq -0x38(%rbp), %r14
movl %r14d, %r9d
callq 0xde8c
leaq 0x68(%rbx), %rsi
leaq 0x70(%rbx), %rdx
leaq 0x78(%rbx), %rcx
movl %r15d, %r8d
movl %r14d, %r9d
callq 0xde8c
movq 0x88(%rbx), %rax
movq 0x50(%rbx), %rcx
movq 0x68(%rbx), %rdx
xorl %esi, %esi
movl -0x44(%rbp), %r15d
cmpq %rsi, %r12
je 0xdd7a
movq 0x18(%rax), %rdi
movsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, (%rcx,%rsi,8)
movq 0x20(%rax), %rdi
movsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
jmp 0xdd54
movq -0x30(%rbp), %rdx
movzbl %dl, %eax
xorl %ecx, %ecx
cmpq %r12, %rcx
je 0xdde1
movslq %edx, %rsi
shlq $0x3, %rsi
movq 0x88(%rbx), %rdi
movq 0x58(%rbx), %r8
movq 0x70(%rbx), %r9
xorl %r10d, %r10d
cmpq %r10, %rax
je 0xddd9
movq 0x18(%rdi), %r11
addq %rsi, %r11
movsd (%r11,%r10,8), %xmm0
movq (%r8,%rcx,8), %r11
movsd %xmm0, (%r11,%r10,8)
movq 0x20(%rdi), %r11
addq %rsi, %r11
movsd (%r11,%r10,8), %xmm0
movq (%r9,%rcx,8), %r11
movsd %xmm0, (%r11,%r10,8)
incq %r10
jmp 0xdda1
incq %rcx
addl -0x30(%rbp), %edx
jmp 0xdd83
imull -0x30(%rbp), %r15d
xorl %eax, %eax
testb %r14b, %r14b
movl $0x0, %ecx
cmovgl %r14d, %ecx
movzbl %cl, %ecx
cmpq %r12, %rax
je 0xde55
movslq %r15d, %rdx
shlq $0x3, %rdx
movq 0x88(%rbx), %rsi
movq 0x60(%rbx), %rdi
movq 0x78(%rbx), %r8
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0xde4d
movq 0x18(%rsi), %r10
addq %rdx, %r10
movsd (%r10,%r9,8), %xmm0
movq (%rdi,%rax,8), %r10
movsd %xmm0, (%r10,%r9,8)
movq 0x20(%rsi), %r10
addq %rdx, %r10
movsd (%r10,%r9,8), %xmm0
movq (%r8,%rax,8), %r10
movsd %xmm0, (%r10,%r9,8)
incq %r9
jmp 0xde15
incq %rax
addl %r14d, %r15d
jmp 0xddf7
movq -0x60(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2b169(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x1f5b2(%rip), %rdi     # 0x2d42b
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x44b0
pushq $0x1
popq %rdi
callq 0x42a0