// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemPiAGK.cpp
// Function: HMMProblemPiAGK::init(param*)
void HMMProblemPiAGK::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;
    
    // skill
    NUMBER *a_PI, ** a_A, ** a_B;
    init3Params(a_PI, a_A, a_B, nS, nO);
    // student
//    NUMBER *a_PIg, ** a_Ag;
//    a_PIg = init1D<NUMBER>((NDAT)nS);
//    a_Ag  = init2D<NUMBER>((NDAT)nS, (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][((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];
	}
    
//    if(this->p->init_params_n==( nS - 1 + (nS-1) * nS + (nO-1) * nS ) ) {
//        for(i=0; i<((nS)-1); i++) {
//            a_PIg[i] = (NUMBER)1/nS;
//            for(j=0; j<((nS)-1); j++) a_Ag[i][j] = (NUMBER)1/nS;
//        }
//    } else if(this->p->init_params_n==( (nS - 1 + (nS-1) * nS)*2 + (nO-1) * nS )) { // ... if they were specified, read them from the init_params
//        sumPI = 0;
//        for(i=0; i<nS; i++) {
//            sumA[i] = 0;
//        }
//        // populate PI
//        offset = (NPAR)( (nS-1) + nS*(nS-1) + nS*(nO-1) );
//        for(i=0; i<((nS)-1); i++) {
//            idx = (NPAR)(offset + i);
//            a_PIg[i] = this->p->init_params[idx];
//            sumPI  += this->p->init_params[idx];
//        }
//        a_PI[nS-1] = 1 - sumPI;
//        // populate A
//        offset = (NPAR)( (nS-1) + nS*(nS-1) + nS*(nO-1) + (nS-1) );
//        for(i=0; i<nS; i++) {
//            for(j=0; j<((nS)-1); j++) {
//                idx = (NPAR)(offset + i*((nS)-1) + j);
//                a_Ag[i][j] = this->p->init_params[idx];
//                sumA[i]  += this->p->init_params[idx];
//            }
//            a_A[i][((nS)-1)]  = 1 - sumA[i];
//        }
//    } else { // ... the n is wrong
//        fprintf(stderr,"The number of initial parameters is expected to be %i or %i, but it is %i.\n",
//                ( nS - 1 + (nS-1) * nS + (nO-1) * nS ), ( (nS - 1 + (nS-1) * nS)*2 + (nO-1) * nS ), this->p->init_params_n);
//        exit(1);
//    }
    
    
    
    // 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);
    this->Ag  = init3D<NUMBER>(nG, nS, 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 "no-effect" params,PI[i] = 1/nS, A[i][j] = 1/nS __IF__ they were not specified
//    for(x=0; x<nG; x++) {
//        cpy1D<NUMBER>(a_PIg, this->PIg[x], nS);
//        cpy2D<NUMBER>(a_Ag,  this->Ag[x],  nS, 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;
            for(j=0; j<nS; j++)
                this->Ag[x][i][j] = (NUMBER)1/nS;
        }
    }
    // destroy setup params
	free(a_PI);
	free2D<NUMBER>(a_A, nS);
	free2D<NUMBER>(a_B, nS);
//    free(a_PIg);
//    free2D<NUMBER>(a_Ag, 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 $0x88, %rsp
movq %rdi, %r14
movq %rsi, 0x88(%rdi)
movsbl 0x8b8(%rsi), %r12d
movzbl %r12b, %r15d
movsbq 0x8b9(%rsi), %rbx
movl 0x8bc(%rsi), %eax
movq %rax, -0x70(%rbp)
movq %rsi, -0x98(%rbp)
movl 0x8c0(%rsi), %eax
movq %rax, -0x88(%rbp)
movb $0x1, 0x80(%rdi)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq %rax, 0x18(%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%r14)
movq $0x0, 0x30(%r14)
leaq -0x68(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x58(%rbp), %rcx
movq %r14, -0x40(%rbp)
movq %r14, %rdi
movl %r12d, %r8d
movq %rbx, -0x38(%rbp)
movl %ebx, %r9d
callq 0x10fa2
movq %rsp, -0xa0(%rbp)
leal 0xf(,%r15,8), %eax
andl $-0x10, %eax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movslq %r12d, %rbx
testl %r12d, %r12d
movq %r15, -0x80(%rbp)
movq %rbx, -0x50(%rbp)
movq %r12, -0x48(%rbp)
jle 0x1fc28
leal (,%r15,8), %r12d
movq %r14, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4120
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4120
leaq -0x1(%r15), %rax
movq -0x68(%rbp), %rsi
cmpq $0x1, %r15
jne 0x1fc4b
movl %eax, %ecx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rsi,%rcx,8)
jmp 0x1fc9b
movq -0x68(%rbp), %rsi
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x8(%rsi,%rbx,8)
movl %r12d, -0x2c(%rbp)
movq -0x38(%rbp), %rbx
movq %r12, %r13
jmp 0x1fde2
movq -0x40(%rbp), %rcx
movq 0x88(%rcx), %rcx
movq 0x8(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movsbq %dl, %rdx
movsd (%rcx,%rdx,8), %xmm1
movsd %xmm1, (%rsi,%rdx,8)
addsd %xmm1, %xmm0
incb %dl
movsbl %dl, %edi
cmpl %edi, %eax
jg 0x1fc60
movsd 0x13c6d(%rip), %xmm1    # 0x338f0
subsd %xmm0, %xmm1
movsd %xmm1, -0x8(%rsi,%r15,8)
movq %rax, %rcx
cmpb $0x0, -0x48(%rbp)
jle 0x1fdd6
movq -0x40(%rbp), %rdx
movq 0x88(%rdx), %rdx
movq -0x60(%rbp), %rdi
leal -0x1(%r15), %r8d
xorl %r9d, %r9d
movsd 0x13c37(%rip), %xmm0    # 0x338f0
movl %r8d, %r10d
cmpb $0x1, %bl
jne 0x1fccd
movsd (%r14,%r9,8), %xmm1
movq (%rdi,%r9,8), %r11
jmp 0x1fd13
movq 0x8(%rdx), %r15
movq (%rdi,%r9,8), %r11
movsd (%r14,%r9,8), %xmm1
xorl %r12d, %r12d
leal (%r10,%r12), %ebx
movsbq %bl, %rbx
movsd (%r15,%rbx,8), %xmm2
movsbq %r12b, %r12
movsd %xmm2, (%r11,%r12,8)
addsd %xmm2, %xmm1
incb %r12b
movsbl %r12b, %ebx
cmpl %ebx, %eax
jg 0x1fcde
movsd %xmm1, (%r14,%r9,8)
movq -0x80(%rbp), %r15
movq -0x50(%rbp), %rbx
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, (%r11,%rcx,8)
incq %r9
addb %r8b, %r10b
cmpq %r15, %r9
jne 0x1fcbc
testb %bl, %bl
jle 0x1fdd6
movq %rbx, %r8
movq -0x38(%rbp), %rbx
leal -0x1(%rbx), %ecx
movq -0x40(%rbp), %rax
movq 0x88(%rax), %rdx
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rax
decl %eax
incl %r8d
mulb %r8b
leal -0x1(%rbx), %r8d
xorl %r9d, %r9d
cmpb $0x1, %bl
jle 0x1fdab
movq 0x8(%rdx), %r11
movq (%rdi,%r9,8), %r10
movsd (%r13,%r9,8), %xmm1
xorl %r14d, %r14d
leal (%rax,%r14), %ebx
movsbq %bl, %rbx
movsd (%r11,%rbx,8), %xmm2
movsbq %r14b, %r14
movsd %xmm2, (%r10,%r14,8)
addsd %xmm2, %xmm1
incb %r14b
movsbl %r14b, %ebx
cmpl %ebx, %ecx
jg 0x1fd77
movsd %xmm1, (%r13,%r9,8)
movq -0x38(%rbp), %rbx
jmp 0x1fdb6
movsd (%r13,%r9,8), %xmm1
movq (%rdi,%r9,8), %r10
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, -0x8(%r10,%rbx,8)
incq %r9
addb %r8b, %al
cmpq %r15, %r9
jne 0x1fd60
movl %r15d, -0x2c(%rbp)
jmp 0x1fdde
movl %r15d, -0x2c(%rbp)
movq -0x38(%rbp), %rbx
movq -0x48(%rbp), %r13
movq -0x40(%rbp), %r15
movq 0x88(%r15), %rax
cmpb $0x0, 0x32(%rax)
jne 0x1fe0b
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq %r15, %rdi
callq 0x10ff0
testb %al, %al
je 0x20206
movq -0x88(%rbp), %r14
movl %r14d, %edi
movl -0x2c(%rbp), %r12d
movl %r12d, %esi
callq 0xbb36
movq %rax, 0x38(%r15)
movl %r14d, %edi
movl %r12d, %esi
movl %r12d, %edx
callq 0xcaae
movq %rax, 0x40(%r15)
movl %r14d, %edi
movl %r12d, %esi
movl %ebx, %edx
callq 0xcaae
movq %rax, 0x48(%r15)
movq -0x70(%rbp), %rbx
movl %ebx, %edi
movl %r12d, %esi
callq 0xbb36
movq %rax, 0x90(%r15)
movl %ebx, %edi
movl %r12d, %esi
movl %r12d, %edx
callq 0xcaae
movq %rax, 0x98(%r15)
testl %r14d, %r14d
jle 0x1ff4f
movq -0x50(%rbp), %rax
leaq (,%rax,8), %rdx
movl -0x2c(%rbp), %r12d
movq -0x38(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0xa8(%rbp)
xorl %ecx, %ecx
movq %rdx, -0x90(%rbp)
movq -0x68(%rbp), %rsi
movq 0x38(%r15), %rax
movq %rcx, -0x78(%rbp)
movq (%rax,%rcx,8), %rdi
callq 0x41c0
testb %r13b, %r13b
jle 0x1ff2c
movq -0x60(%rbp), %rbx
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %r15
xorl %r13d, %r13d
movq -0x90(%rbp), %r14
movq (%r15,%r13,8), %rdi
movq (%rbx,%r13,8), %rsi
movq %r14, %rdx
callq 0x41c0
incq %r13
cmpq %r13, %r12
jne 0x1fede
movq -0x58(%rbp), %rbx
movq -0x40(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %r15
xorl %r13d, %r13d
movq -0xa8(%rbp), %r14
movq (%r15,%r13,8), %rdi
movq (%rbx,%r13,8), %rsi
movq %r14, %rdx
callq 0x41c0
incq %r13
cmpq %r13, %r12
jne 0x1ff14
movq -0x78(%rbp), %rcx
incq %rcx
cmpq -0x88(%rbp), %rcx
movq -0x40(%rbp), %r15
movq -0x48(%rbp), %r13
movq -0x90(%rbp), %rdx
jne 0x1fea6
cmpl $0x0, -0x70(%rbp)
movq -0x80(%rbp), %r12
movq -0x38(%rbp), %r14
jle 0x20063
movsbl %r13b, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd 0x1397c(%rip), %xmm0    # 0x338f0
divsd %xmm1, %xmm0
movq 0x90(%r15), %rax
leaq -0x1(%r12), %rcx
movq %rcx, %xmm1
pshufd $0x44, %xmm1, %xmm1     # xmm1 = xmm1[0,1,0,1]
leal 0x8(,%r12,8), %ecx
andl $0x7f0, %ecx            # imm = 0x7F0
xorl %edx, %edx
movdqa 0x148ea(%rip), %xmm2    # 0x34890
movdqa 0x148f2(%rip), %xmm3    # 0x348a0
pxor %xmm3, %xmm1
pcmpeqd %xmm4, %xmm4
movdqa 0x148f2(%rip), %xmm5    # 0x348b0
testb %r12b, %r12b
jle 0x20056
movq (%rax,%rdx,8), %rsi
movq 0x98(%r15), %rdi
movq (%rdi,%rdx,8), %rdi
xorl %r8d, %r8d
movsd %xmm0, (%rsi,%r8,8)
movq (%rdi,%r8,8), %r9
xorl %r10d, %r10d
movdqa %xmm2, %xmm6
movdqa %xmm6, %xmm7
pxor %xmm3, %xmm7
movdqa %xmm7, %xmm8
pcmpgtd %xmm1, %xmm8
pcmpeqd %xmm1, %xmm7
pshufd $0xf5, %xmm7, %xmm9     # xmm9 = xmm7[1,1,3,3]
pand %xmm8, %xmm9
pshufd $0xf5, %xmm8, %xmm7     # xmm7 = xmm8[1,1,3,3]
por %xmm9, %xmm7
movd %xmm7, %r11d
notl %r11d
testb $0x1, %r11b
je 0x2002a
movsd %xmm0, (%r9,%r10)
pxor %xmm4, %xmm7
pextrw $0x4, %xmm7, %r11d
testb $0x1, %r11b
je 0x20041
movsd %xmm0, 0x8(%r9,%r10)
paddq %xmm5, %xmm6
addq $0x10, %r10
cmpq %r10, %rcx
jne 0x1ffea
incq %r8
cmpq %r12, %r8
jne 0x1ffd9
incq %rdx
cmpq -0x70(%rbp), %rdx
jne 0x1ffbe
movq -0x68(%rbp), %rdi
callq 0x42a0
movq -0x60(%rbp), %rdi
movl -0x2c(%rbp), %ebx
movl %ebx, %esi
callq 0xa5dd
movq -0x58(%rbp), %rdi
movl %ebx, %esi
callq 0xa5dd
movq -0x98(%rbp), %rsi
cmpb $0x0, 0x6c(%rsi)
je 0x200a0
addq $0x6c, %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x11182
leaq 0x50(%r15), %rsi
leaq 0x58(%r15), %rdx
leaq 0x60(%r15), %rcx
movq %r15, %rdi
movq -0x50(%rbp), %rbx
movl %ebx, %r8d
movl %r14d, %r9d
callq 0x10fa2
leaq 0x68(%r15), %rsi
leaq 0x70(%r15), %rdx
leaq 0x78(%r15), %rcx
movq %r15, %rdi
movl %r13d, %r8d
movl %r14d, %r9d
callq 0x10fa2
testb %bl, %bl
jle 0x201f0
movq 0x88(%r15), %rcx
movq 0x18(%rcx), %rax
movq 0x20(%rcx), %rcx
movq 0x50(%r15), %rdx
movq 0x68(%r15), %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, %r12
jne 0x200f9
testb %r12b, %r12b
jle 0x201f0
movq 0x88(%r15), %rsi
movq 0x58(%r15), %rax
movq 0x70(%r15), %rcx
movq 0x18(%rsi), %rdx
movq 0x20(%rsi), %rsi
xorl %r8d, %r8d
movq %r12, %rdi
movq (%rax,%r8,8), %r9
movq (%rcx,%r8,8), %r10
incq %r8
xorl %r11d, %r11d
leal (%rdi,%r11), %ebx
movsbq %bl, %rbx
movsd (%rdx,%rbx,8), %xmm0
movsd %xmm0, (%r9,%r11,8)
movsd (%rsi,%rbx,8), %xmm0
movsd %xmm0, (%r10,%r11,8)
incq %r11
cmpq %r11, %r12
jne 0x20149
addq %r12, %rdi
cmpq %r12, %r8
jne 0x2013b
testb %r13b, %r13b
movq -0x50(%rbp), %r8
jle 0x201f0
movq 0x88(%r15), %rcx
movq 0x60(%r15), %rdx
movq 0x78(%r15), %rsi
movzbl %r14b, %edi
incb %r8b
movl %r12d, %eax
mulb %r8b
movzbl %al, %eax
xorl %r8d, %r8d
testb %r14b, %r14b
jle 0x201e1
movq 0x18(%rcx), %r9
movq 0x20(%rcx), %r10
movq (%rdx,%r8,8), %r11
movq (%rsi,%r8,8), %r14
xorl %ebx, %ebx
leal (%rax,%rbx), %r15d
movsbq %r15b, %r15
movsd (%r9,%r15,8), %xmm0
movsd %xmm0, (%r11,%rbx,8)
movsd (%r10,%r15,8), %xmm0
movsd %xmm0, (%r14,%rbx,8)
incq %rbx
cmpq %rbx, %rdi
jne 0x201b9
incq %r8
addq %r12, %rax
cmpq %r12, %r8
movq -0x38(%rbp), %r14
jne 0x201a2
movq -0xa0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4f58
nop