// File: myudelson[P]hmm-scalable/HMMProblemAGK.cpp
// Function: HMMProblemAGK::init(param*)
void HMMProblemAGK::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;
    NPAR nS = this->p->nS, nO = this->p->nO; NCAT nK = this->p->nK, nG = this->p->nG;
    
    NUMBER *a_PI, ** a_A, ** a_B;
    init3Params(a_PI, a_A, a_B, nS, nO);
    //
    // 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][((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>((NDAT)nK, (NDAT)nS);
    this->A   = init3D<NUMBER>((NDAT)nK, (NDAT)nS, (NDAT)nS);
    this->Ag  = init3D<NUMBER>((NDAT)nG, (NDAT)nS, (NDAT)nS);
    this->B   = init3D<NUMBER>((NDAT)nK, (NDAT)nS, (NDAT)nO);
    NCAT x;
    for(x=0; x<nK; x++) {
        cpy1D<NUMBER>(a_PI, this->pi[x], (NDAT)nS);
        cpy2D<NUMBER>(a_A,  this->A[x],  (NDAT)nS, (NDAT)nS);
        cpy2D<NUMBER>(a_B,  this->B[x],  (NDAT)nS, (NDAT)nO);
    }
    // PIg start with "no-effect" params,PI[i] = 1/nS
    for(x=0; x<nG; x++) {
        for(i=0; i<nS; i++) {
            for(j=0; j<nS; j++)
                this->Ag[x][i][j] = (NUMBER)1/nS;
        }
    }
    // destroy setup params
	free(a_PI);
	free2D<NUMBER>(a_A, (NDAT)nS);
	free2D<NUMBER>(a_B, (NDAT)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)
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)
movq 0x88(%rdi), %rax
movb 0x8b8(%rax), %al
movb %al, -0x11(%rbp)
movq 0x88(%rdi), %rax
movb 0x8b9(%rax), %al
movb %al, -0x12(%rbp)
movq 0x88(%rdi), %rax
movl 0x8c0(%rax), %eax
movl %eax, -0x18(%rbp)
movq 0x88(%rdi), %rax
movl 0x8bc(%rax), %eax
movl %eax, -0x1c(%rbp)
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 0x2dd66
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 0x2dd2e
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2ddc9
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 0x2dd6a
movsd 0x21c37(%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 0x2dedf
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2dea0
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 0x2de0b
movq -0x78(%rbp), %rax
movsbq -0x39(%rbp), %rcx
movsd 0x21b57(%rip), %xmm0    # 0x4fa08
subsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movsbq -0x39(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl -0x11(%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 0x2ddf7
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 0x2dfe5
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x12(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2dfa6
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 0x2df11
movq -0x70(%rbp), %rax
movsbq -0x39(%rbp), %rcx
movsd 0x21a51(%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 0x2defd
movq -0x80(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x32(%rax), %eax
cmpl $0x0, %eax
jne 0x2e034
movq -0x80(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x19e80
testb $0x1, %al
jne 0x2e034
movq 0x2cfbf(%rip), %rax     # 0x5afd8
movq (%rax), %rdi
leaq 0x2244c(%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 -0x1c(%rbp), %edi
movsbl -0x11(%rbp), %esi
movsbl -0x11(%rbp), %edx
callq 0x11310
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x90(%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 $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x2e118
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 0x2e0a6
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2e1ab
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2e19b
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2e18f
movq -0x80(%rbp), %rax
movsbl -0x11(%rbp), %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x218a9(%rip), %xmm0    # 0x4fa08
divsd %xmm1, %xmm0
movq 0x90(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %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 0x2e13f
jmp 0x2e191
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x2e12f
jmp 0x2e19d
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2e11f
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 0x2e1ee
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 0x2e2a6
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 0x2e246
movb -0x11(%rbp), %al
movb %al, -0x3c(%rbp)
movb $0x0, -0x39(%rbp)
movsbl -0x39(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2e366
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2e357
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 0x2e2c4
jmp 0x2e359
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x2e2b0
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 0x2e434
movb $0x0, -0x3a(%rbp)
movsbl -0x3a(%rbp), %eax
movsbl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2e425
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 0x2e392
jmp 0x2e427
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0x2e37e
movq -0x50(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq