// File: 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
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, 0x88(%rax)
movq 0x88(%rax), %rcx
movb 0x8b8(%rcx), %cl
movb %cl, -0x11(%rbp)
movq 0x88(%rax), %rcx
movb 0x8b9(%rcx), %cl
movb %cl, -0x12(%rbp)
movq 0x88(%rax), %rcx
movl 0x8c0(%rcx), %ecx
movl %ecx, -0x18(%rbp)
movq 0x88(%rax), %rcx
movl 0x8bc(%rcx), %ecx
movl %ecx, -0x1c(%rbp)
movb $0x1, 0x80(%rax)
movq 0x88(%rax), %rax
movsbq 0x8b9(%rax), %rdi
movl $0x8, %esi
callq 0x5180
movq -0x80(%rbp), %rdi
movq %rax, 0x18(%rdi)
movq $0x0, 0x20(%rdi)
movq $0x0, 0x28(%rdi)
movq $0x0, 0x30(%rdi)
movsbl -0x11(%rbp), %r8d
movsbl -0x12(%rbp), %r9d
leaq -0x28(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x38(%rbp), %rcx
callq 0x19de0
movq $0x0, -0x48(%rbp)
movzbl -0x11(%rbp), %edx
movl %edx, %eax
movq %rsp, %rcx
movq %rcx, -0x50(%rbp)
movl %edx, %ecx
leal 0xf(,%rcx,8), %ecx
andl $-0x10, %ecx
movl %ecx, %edx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movzbl -0x11(%rbp), %edx
movl %edx, %eax
movl %edx, %ecx
leal 0xf(,%rcx,8), %ecx
andl $-0x10, %ecx
movl %ecx, %edx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x28096
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
movsbq -0x39(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movsbq -0x39(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x2805e
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x280f9
movq -0x80(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x8(%rcx), %rcx
movsbq -0x39(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq -0x28(%rbp), %rcx
movsbq -0x39(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rax
movq 0x8(%rax), %rax
movsbq -0x39(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x2809a
movsd 0x27907(%rip), %xmm0    # 0x4fa08
subsd -0x48(%rbp), %xmm0
movq -0x28(%rbp), %rax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movsbl -0x11(%rbp), %eax
subl $0x1, %eax
movb %al, -0x3c(%rbp)
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2821d
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x281d0
movq -0x78(%rbp), %rax
movq -0x80(%rbp), %rcx
movsbl -0x3c(%rbp), %edx
movsbl -0x39(%rbp), %esi
movsbl -0x11(%rbp), %edi
subl $0x1, %edi
imull %edi, %esi
addl %esi, %edx
movsbl -0x3a(%rbp), %esi
addl %esi, %edx
movb %dl, -0x3b(%rbp)
movq 0x88(%rcx), %rdx
movq 0x8(%rdx), %rdx
movsbq -0x3b(%rbp), %rsi
movsd (%rdx,%rsi,8), %xmm0
movq -0x30(%rbp), %rdx
movsbq -0x39(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movsbq -0x3a(%rbp), %rsi
movsd %xmm0, (%rdx,%rsi,8)
movq 0x88(%rcx), %rcx
movq 0x8(%rcx), %rcx
movsbq -0x3b(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movsbq -0x39(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x3a(%rbp), %al
addb $0x1, %al
movb %al, -0x3a(%rbp)
jmp 0x2813b
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %rax
movsbq -0x39(%rbp), %rdx
movsd 0x27823(%rip), %xmm0    # 0x4fa08
subsd (%rax,%rdx,8), %xmm0
movq -0x30(%rbp), %rax
movsbq -0x39(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq 0x88(%rcx), %rcx
movsbl 0x8b8(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x28127
movsbl -0x11(%rbp), %eax
subl $0x1, %eax
movsbl -0x11(%rbp), %ecx
movsbl -0x11(%rbp), %edx
subl $0x1, %edx
imull %edx, %ecx
addl %ecx, %eax
movb %al, -0x3c(%rbp)
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x28323
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x12(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x282e4
movq -0x70(%rbp), %rax
movq -0x80(%rbp), %rcx
movsbl -0x3c(%rbp), %edx
movsbl -0x39(%rbp), %esi
movsbl -0x12(%rbp), %edi
subl $0x1, %edi
imull %edi, %esi
addl %esi, %edx
movsbl -0x3a(%rbp), %esi
addl %esi, %edx
movb %dl, -0x3b(%rbp)
movq 0x88(%rcx), %rdx
movq 0x8(%rdx), %rdx
movsbq -0x3b(%rbp), %rsi
movsd (%rdx,%rsi,8), %xmm0
movq -0x38(%rbp), %rdx
movsbq -0x39(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movsbq -0x3a(%rbp), %rsi
movsd %xmm0, (%rdx,%rsi,8)
movq 0x88(%rcx), %rcx
movq 0x8(%rcx), %rcx
movsbq -0x3b(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movsbq -0x39(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x3a(%rbp), %al
addb $0x1, %al
movb %al, -0x3a(%rbp)
jmp 0x2824f
movq -0x70(%rbp), %rax
movsbq -0x39(%rbp), %rcx
movsd 0x27713(%rip), %xmm0    # 0x4fa08
subsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movsbq -0x39(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl -0x12(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x2823b
movq -0x80(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x32(%rax), %eax
cmpl $0x0, %eax
jne 0x28372
movq -0x80(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x19e80
testb $0x1, %al
jne 0x28372
movq 0x32c81(%rip), %rax     # 0x5afd8
movq (%rax), %rdi
leaq 0x2810e(%rip), %rsi     # 0x5046f
movb $0x0, %al
callq 0x5440
movl $0x1, %edi
callq 0x5390
movl -0x18(%rbp), %edi
movsbl -0x11(%rbp), %esi
callq 0xf580
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x38(%rax)
movl -0x18(%rbp), %edi
movsbl -0x11(%rbp), %esi
movsbl -0x11(%rbp), %edx
callq 0x11310
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x40(%rax)
movl -0x18(%rbp), %edi
movsbl -0x11(%rbp), %esi
movsbl -0x12(%rbp), %edx
callq 0x11310
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x1c(%rbp), %edi
movsbl -0x11(%rbp), %esi
callq 0xf580
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x90(%rax)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x28452
movq -0x80(%rbp), %rax
movq -0x28(%rbp), %rdi
movq 0x38(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movsbl -0x11(%rbp), %edx
callq 0xc440
movq -0x80(%rbp), %rax
movq -0x30(%rbp), %rdi
movq 0x40(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movsbl -0x11(%rbp), %edx
movsbl -0x11(%rbp), %ecx
callq 0xf650
movq -0x80(%rbp), %rax
movq -0x38(%rbp), %rdi
movq 0x48(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movsbl -0x11(%rbp), %edx
movsbl -0x12(%rbp), %ecx
callq 0xf650
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x283e0
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x284b9
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x284ac
movq -0x80(%rbp), %rax
movsbl -0x11(%rbp), %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x27583(%rip), %xmm0    # 0x4fa08
divsd %xmm1, %xmm0
movq 0x90(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x39(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x28465
jmp 0x284ae
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x28459
movq -0x28(%rbp), %rdi
callq 0x5380
movq -0x30(%rbp), %rdi
movsbl -0x11(%rbp), %esi
callq 0xc7f0
movq -0x38(%rbp), %rdi
movsbl -0x11(%rbp), %esi
callq 0xc7f0
movq -0x10(%rbp), %rax
movsbl 0x6c(%rax), %eax
cmpl $0x0, %eax
je 0x284fc
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x6c, %rsi
xorl %edx, %edx
callq 0x1a1f0
movq -0x80(%rbp), %rdi
movq %rdi, %rsi
addq $0x50, %rsi
movq %rdi, %rdx
addq $0x58, %rdx
movq %rdi, %rcx
addq $0x60, %rcx
movb -0x11(%rbp), %al
movsbl %al, %r8d
movsbl -0x12(%rbp), %r9d
callq 0x19de0
movq -0x80(%rbp), %rdi
movq %rdi, %rsi
addq $0x68, %rsi
movq %rdi, %rdx
addq $0x70, %rdx
movq %rdi, %rcx
addq $0x78, %rcx
movb -0x11(%rbp), %al
movsbl %al, %r8d
movsbl -0x12(%rbp), %r9d
callq 0x19de0
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x285b4
movq -0x80(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x18(%rcx), %rcx
movsbq -0x39(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x50(%rax), %rcx
movsbq -0x39(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rcx
movq 0x20(%rcx), %rcx
movsbq -0x39(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x68(%rax), %rax
movsbq -0x39(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x28554
movb -0x11(%rbp), %al
movb %al, -0x3c(%rbp)
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x28674
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x28665
movq -0x80(%rbp), %rax
movsbl -0x3c(%rbp), %ecx
movsbl -0x39(%rbp), %edx
movsbl -0x11(%rbp), %esi
imull %esi, %edx
addl %edx, %ecx
movsbl -0x3a(%rbp), %edx
addl %edx, %ecx
movb %cl, -0x3b(%rbp)
movq 0x88(%rax), %rcx
movq 0x18(%rcx), %rcx
movsbq -0x3b(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x58(%rax), %rcx
movsbq -0x39(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x3a(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rcx
movq 0x20(%rcx), %rcx
movsbq -0x3b(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x70(%rax), %rax
movsbq -0x39(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x3a(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x3a(%rbp), %al
addb $0x1, %al
movb %al, -0x3a(%rbp)
jmp 0x285d2
jmp 0x28667
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x285be
movsbl -0x11(%rbp), %eax
movsbl -0x11(%rbp), %ecx
movsbl -0x11(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movb %al, -0x3c(%rbp)
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x28742
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x28733
movq -0x80(%rbp), %rax
movsbl -0x3c(%rbp), %ecx
movsbl -0x39(%rbp), %edx
movsbl -0x11(%rbp), %esi
imull %esi, %edx
addl %edx, %ecx
movsbl -0x3a(%rbp), %edx
addl %edx, %ecx
movb %cl, -0x3b(%rbp)
movq 0x88(%rax), %rcx
movq 0x18(%rcx), %rcx
movsbq -0x3b(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x60(%rax), %rcx
movsbq -0x39(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x3a(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rcx
movq 0x20(%rcx), %rcx
movsbq -0x3b(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x78(%rax), %rax
movsbq -0x39(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x3a(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x3a(%rbp), %al
addb $0x1, %al
movb %al, -0x3a(%rbp)
jmp 0x286a0
jmp 0x28735
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x2868c
movq -0x50(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop