// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemPiGK.cpp
// Function: HMMProblemPiGK::init(param*)
void HMMProblemPiGK::init(struct param *param) {
	this->p = param;
    NPAR nS = this->p->nS, nO = this->p->nO; NCAT nK = this->p->nK, nG = this->p->nG;
	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;
    
    NUMBER *a_PI, ** a_A, ** a_B;
    init3Params(a_PI, a_A, a_B, nS, nO);
    // student
//    NUMBER *a_PIg;
//    a_PIg = init1D<NUMBER>((NDAT)nS);

    //
    // setup params
    //
	NPAR i, j, idx, offset;
	NUMBER sumPI = 0;
	NUMBER sumA[nS];
	NUMBER sumB[nS];
	for(i=0; i<nS; i++) {
		sumA[i] = 0;
		sumB[i] = 0;
	}
    
    // write default parameters first
    
	// 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 = (NPAR)(nS-1);
	for(i=0; i<nS; i++) {
		for(j=0; j<((nS)-1); j++) {
			idx = (NPAR)(offset + i*((nS)-1) + j);
			a_A[i][j] = this->p->init_params[idx];
			sumA[i]  += this->p->init_params[idx];
		}
		a_A[i][((this->p->nS)-1)]  = 1 - sumA[i];
	}
	// polupale B
	offset = (NPAR)((nS-1) + nS*(nS-1));
	for(i=0; i<nS; i++) {
		for(j=0; j<((nO)-1); j++) {
			idx = (NPAR)(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/PIg'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);
    }
    this->pi  = init2D<NUMBER>(nK, nS);
    this->A   = init3D<NUMBER>(nK, nS, nS);
    this->B   = init3D<NUMBER>(nK, nS, nO);
    this->PIg = init2D<NUMBER>(nG, nS);
    NCAT x;
    for(x=0; x<nK; x++) {
        cpy1D<NUMBER>(a_PI, this->pi[x], nS);
        cpy2D<NUMBER>(a_A,  this->A[x],  nS, nS);
        cpy2D<NUMBER>(a_B,  this->B[x],  nS, nO);
    }
//    // PIg start with same params
//    for(x=0; x<nG; x++)
//        cpy1D<NUMBER>(a_PI, this->PIg[x], nS);
    // PIg start with "no-effect" params,PI[i] = 1/nS
    for(x=0; x<nG; x++) {
        for(i=0; i<nS; i++) {
            this->PIg[x][i] = (NUMBER)1/nS;
        }
    }

    
    
    // destroy setup params
	free(a_PI);
	free2D<NUMBER>(a_A, nS);
	free2D<NUMBER>(a_B, nS);
	
    // if needs be -- read in init params from a file
    if(param->initfile[0]!=0)
        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 = (NPAR)(offset + i*nS + j);
			lbA[i][j] = this->p->param_lo[idx];
			ubA[i][j] = this->p->param_hi[idx];
		}
	// *B
	offset = (NPAR)(nS + nS*nS);
	for(i=0; i<nS; i++)
		for(j=0; j<nO; j++) {
			idx = (NPAR)(offset + i*nS + 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 $0x78, %rsp
movq %rdi, %rbx
movq %rsi, 0x88(%rdi)
movzbl 0x8b8(%rsi), %r12d
movsbq %r12b, %rax
movq %rax, -0x38(%rbp)
movsbl %r12b, %r15d
movsbl 0x8b9(%rsi), %eax
movq %rax, -0x48(%rbp)
movslq %eax, %r14
movl 0x8bc(%rsi), %eax
movq %rax, -0x78(%rbp)
movq %rsi, -0x98(%rbp)
movl 0x8c0(%rsi), %eax
movq %rax, -0x90(%rbp)
movb $0x1, 0x80(%rdi)
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x4120
movq %rax, 0x18(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rbx)
andq $0x0, 0x30(%rbx)
leaq -0x60(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movq %rbx, %rdi
movl %r15d, %r8d
movq %r14, -0x80(%rbp)
movl %r14d, %r9d
callq 0xde8c
movq %rsp, -0xa0(%rbp)
leal 0xf(,%r12,8), %edx
andl $-0x10, %edx
movq %rsp, %rax
subq %rdx, %rax
movq %rax, %rsp
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, %rsp
xorl %edx, %edx
movl %r15d, -0x2c(%rbp)
testb %r15b, %r15b
movq %r12, -0x40(%rbp)
cmovlel %edx, %r12d
cmpq %rdx, %r12
je 0x15cb6
andq $0x0, (%rax,%rdx,8)
andq $0x0, (%rcx,%rdx,8)
incq %rdx
jmp 0x15ca2
movq -0x38(%rbp), %rdx
leal -0x1(%rdx), %edi
movq 0x88(%rbx), %rdx
xorpd %xmm0, %xmm0
movq -0x60(%rbp), %rsi
xorl %r8d, %r8d
cmpl %r8d, %edi
jle 0x15ced
movq 0x8(%rdx), %r9
movsd (%r9,%r8,8), %xmm1
movsd %xmm1, (%rsi,%r8,8)
addsd %xmm1, %xmm0
incq %r8
jmp 0x15ccf
movsd 0x16bfb(%rip), %xmm2    # 0x2c8f0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movq -0x60(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq %rsi, -0x88(%rbp)
movsd %xmm1, -0x8(%rsi,%rdx,8)
movq -0x58(%rbp), %r8
movq 0x88(%rbx), %rdx
movq %rdx, -0x68(%rbp)
movq -0x40(%rbp), %rdx
decl %edx
movzbl %dl, %r10d
xorl %r11d, %r11d
movq %r10, %r14
movq %r8, %rdx
cmpq %r12, %r11
je 0x15da2
movq 0x88(%rbx), %r15
movq -0x58(%rbp), %rdx
xorl %r13d, %r13d
cmpl %r13d, %edi
jle 0x15d79
leal (%r14,%r13), %r9d
movq 0x8(%r15), %rsi
movsbq %r9b, %r9
movsd (%rsi,%r9,8), %xmm0
movq (%rdx,%r11,8), %rsi
movsd %xmm0, (%rsi,%r13,8)
addsd (%rax,%r11,8), %xmm0
movsd %xmm0, (%rax,%r11,8)
incq %r13
jmp 0x15d47
movapd %xmm2, %xmm0
subsd (%rax,%r11,8), %xmm0
movq (%r8,%r11,8), %rsi
movq -0x68(%rbp), %r9
movsbq 0x8b8(%r9), %r9
movsd %xmm0, -0x8(%rsi,%r9,8)
incq %r11
addq %r10, %r14
jmp 0x15d34
movq -0x48(%rbp), %r9
leal -0x1(%r9), %edi
movq -0x50(%rbp), %r8
movq -0x38(%rbp), %rax
decl %eax
movq -0x40(%rbp), %rsi
incl %esi
movl %esi, -0x6c(%rbp)
mulb %sil
movzbl %al, %eax
leal -0x1(%r9), %esi
movzbl %sil, %r9d
xorl %r10d, %r10d
cmpq %r12, %r10
je 0x15e35
movq 0x88(%rbx), %r11
movq -0x50(%rbp), %r14
xorl %r15d, %r15d
cmpl %r15d, %edi
jle 0x15e14
leal (%rax,%r15), %esi
movq 0x8(%r11), %r13
movsbq %sil, %rsi
movsd (%r13,%rsi,8), %xmm0
movq (%r14,%r10,8), %rsi
movsd %xmm0, (%rsi,%r15,8)
addsd (%rcx,%r10,8), %xmm0
movsd %xmm0, (%rcx,%r10,8)
incq %r15
jmp 0x15de1
movapd %xmm2, %xmm0
subsd (%rcx,%r10,8), %xmm0
movq (%r8,%r10,8), %rsi
movq -0x80(%rbp), %r11
movsd %xmm0, -0x8(%rsi,%r11,8)
incq %r10
addq %r9, %rax
jmp 0x15dce
movq 0x88(%rbx), %rax
cmpb $0x0, 0x32(%rax)
jne 0x15e5d
movq -0x50(%rbp), %rcx
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
callq 0xded0
testb %al, %al
je 0x1610e
movq -0x90(%rbp), %r13
movl %r13d, %edi
movl -0x2c(%rbp), %r14d
movl %r14d, %esi
callq 0x9328
movq %rax, 0x38(%rbx)
movl %r13d, %edi
movl %r14d, %esi
movl %r14d, %edx
callq 0xa0be
movq %rax, 0x40(%rbx)
movl %r13d, %edi
movl %r14d, %esi
movq -0x48(%rbp), %rdx
callq 0xa0be
movq %rax, 0x48(%rbx)
movq -0x78(%rbp), %rdi
movl %r14d, %esi
callq 0x9328
movq %rax, 0x90(%rbx)
movq -0x38(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0x68(%rbp)
xorl %r15d, %r15d
testl %r13d, %r13d
cmovlel %r15d, %r13d
movq %r13, %r14
movl -0x2c(%rbp), %r13d
cmpq %r15, %r14
je 0x15f1e
movq -0x60(%rbp), %rsi
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rdi
movq -0x68(%rbp), %rdx
callq 0x41c0
movq -0x58(%rbp), %rdi
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rsi
movl %r13d, %edx
movl %r13d, %ecx
callq 0x93ad
movq -0x50(%rbp), %rdi
movq 0x48(%rbx), %rax
movq (%rax,%r15,8), %rsi
movl %r13d, %edx
movq -0x48(%rbp), %rcx
callq 0x93ad
incq %r15
jmp 0x15ed0
cvtsi2sdl -0x38(%rbp), %xmm0
movsd 0x169c5(%rip), %xmm1    # 0x2c8f0
divsd %xmm0, %xmm1
xorl %eax, %eax
movq -0x78(%rbp), %rdi
testl %edi, %edi
cmovlel %eax, %edi
cmpq %rdi, %rax
je 0x15f60
movq 0x90(%rbx), %rcx
xorl %edx, %edx
cmpq %rdx, %r12
je 0x15f5b
movq (%rcx,%rax,8), %rsi
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
jmp 0x15f48
incq %rax
jmp 0x15f3a
movq -0x60(%rbp), %rdi
callq 0x4290
movq -0x58(%rbp), %rdi
movl -0x2c(%rbp), %r14d
movl %r14d, %esi
callq 0x7f2c
movq -0x50(%rbp), %rdi
movl %r14d, %esi
callq 0x7f2c
movq -0x98(%rbp), %rsi
cmpb $0x0, 0x6c(%rsi)
je 0x15fa0
addq $0x6c, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe026
leaq 0x50(%rbx), %rsi
leaq 0x58(%rbx), %rdx
leaq 0x60(%rbx), %rcx
movsbl -0x40(%rbp), %r8d
movq %rbx, %rdi
movq -0x48(%rbp), %r14
movl %r14d, %r9d
callq 0xde8c
leaq 0x68(%rbx), %rsi
leaq 0x70(%rbx), %rdx
leaq 0x78(%rbx), %rcx
movq %rbx, %rdi
movq -0x38(%rbp), %r8
movl %r14d, %r9d
callq 0xde8c
movq 0x88(%rbx), %rax
movq 0x50(%rbx), %rcx
movq 0x68(%rbx), %rdx
xorl %esi, %esi
cmpq %rsi, %r12
je 0x16012
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 0x15fec
movq -0x40(%rbp), %rcx
movq %rcx, %rax
negq %rax
xorl %edx, %edx
cmpq %r12, %rdx
je 0x1607f
leaq 0x1(%rdx), %rsi
movq 0x88(%rbx), %rdi
movq 0x58(%rbx), %r8
movq 0x70(%rbx), %r9
xorl %r10d, %r10d
movq %rax, %r11
addq %r10, %r11
je 0x16076
leal (%rcx,%r10), %r11d
movq 0x18(%rdi), %r14
movsbq %r11b, %r11
movsd (%r14,%r11,8), %xmm0
movq (%r8,%rdx,8), %r14
movsd %xmm0, (%r14,%r10,8)
movq 0x20(%rdi), %r14
movsd (%r14,%r11,8), %xmm0
movq (%r9,%rdx,8), %r11
movsd %xmm0, (%r11,%r10,8)
incq %r10
jmp 0x16039
addq -0x40(%rbp), %rcx
movq %rsi, %rdx
jmp 0x1601e
xorl %ecx, %ecx
cmpb $0x0, -0x80(%rbp)
movq -0x48(%rbp), %rax
cmovlel %ecx, %eax
movzbl %al, %edx
movq -0x40(%rbp), %rsi
movl %esi, %eax
mulb -0x6c(%rbp)
movzbl %al, %eax
movzbl %sil, %esi
cmpq %r12, %rcx
je 0x160f8
movq 0x88(%rbx), %rdi
movq 0x60(%rbx), %r8
movq 0x78(%rbx), %r9
xorl %r10d, %r10d
cmpq %r10, %rdx
je 0x160f0
leal (%rax,%r10), %r11d
movq 0x18(%rdi), %r14
movsbq %r11b, %r11
movsd (%r14,%r11,8), %xmm0
movq (%r8,%rcx,8), %r14
movsd %xmm0, (%r14,%r10,8)
movq 0x20(%rdi), %r14
movsd (%r14,%r11,8), %xmm0
movq (%r9,%rcx,8), %r11
movsd %xmm0, (%r11,%r10,8)
incq %r10
jmp 0x160b6
incq %rcx
addq %rsi, %rax
jmp 0x1609f
movq -0xa0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x22ec3(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x1730c(%rip), %rdi     # 0x2d42b
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x44b0
pushq $0x1
popq %rdi
callq 0x42a0