// File: /workspace/github_cpp_makefile_star2_O1/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), %r15d
movsbl 0x8b9(%rsi), %eax
movl %eax, -0x3c(%rbp)
movslq %eax, %rbx
movl 0x8bc(%rsi), %eax
movq %rax, -0x78(%rbp)
movq %rsi, -0x90(%rbp)
movl 0x8c0(%rsi), %eax
movq %rax, -0x80(%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 -0x60(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movq %r14, -0x38(%rbp)
movq %r14, %rdi
movl %r15d, %r8d
movq %rbx, -0x68(%rbp)
movl %ebx, %r9d
callq 0x1112e
movq %rsp, -0x98(%rbp)
movzbl %r15b, %ebx
leal 0xf(,%rbx,8), %eax
andl $-0x10, %eax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movslq %r15d, %rax
movq %rax, -0x30(%rbp)
movq %r15, -0x48(%rbp)
testl %r15d, %r15d
jle 0x1ff97
leal (,%rbx,8), %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4120
leal (,%rbx,8), %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x4120
movq -0x30(%rbp), %rax
decl %eax
xorpd %xmm0, %xmm0
cmpb $0x2, %bl
jl 0x1ffd9
movq -0x38(%rbp), %rcx
movq 0x88(%rcx), %rcx
movq 0x8(%rcx), %rcx
movq -0x60(%rbp), %rdx
xorl %esi, %esi
movsbq %sil, %rsi
movsd (%rcx,%rsi,8), %xmm1
movsd %xmm1, (%rdx,%rsi,8)
addsd (%rcx,%rsi,8), %xmm0
incb %sil
movsbl %sil, %edi
cmpl %edi, %eax
jg 0x1ffbb
movsd 0x1490f(%rip), %xmm2    # 0x348f0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movq -0x60(%rbp), %rsi
movq -0x30(%rbp), %rcx
movsd %xmm1, -0x8(%rsi,%rcx,8)
testb %bl, %bl
jle 0x20084
movq -0x38(%rbp), %rcx
movq 0x88(%rcx), %rcx
movq -0x58(%rbp), %rdx
leal -0x1(%rbx), %edi
xorl %r8d, %r8d
movl %edi, %r9d
cmpl $0x1, %ebx
je 0x2005f
movq 0x8(%rcx), %r10
movq (%rdx,%r8,8), %r11
movsd (%r13,%r8,8), %xmm0
xorl %r14d, %r14d
leal (%r9,%r14), %r15d
movsbq %r15b, %r15
movsd (%r10,%r15,8), %xmm1
movsbq %r14b, %r14
movsd %xmm1, (%r11,%r14,8)
addsd (%r10,%r15,8), %xmm0
incb %r14b
movsbl %r14b, %r15d
cmpl %r15d, %eax
jg 0x2002e
movsd %xmm0, (%r13,%r8,8)
movapd %xmm2, %xmm0
subsd (%r13,%r8,8), %xmm0
movq (%rdx,%r8,8), %r10
movq -0x30(%rbp), %r11
movsd %xmm0, -0x8(%r10,%r11,8)
incq %r8
addb %dil, %r9b
cmpq %rbx, %r8
jne 0x20017
cmpb $0x0, -0x30(%rbp)
movq -0x68(%rbp), %r13
jle 0x20120
leal -0x1(%r13), %ecx
movq -0x38(%rbp), %rax
movq 0x88(%rax), %rdx
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rax
decl %eax
leal 0x1(%rbx), %r8d
mulb %r8b
leal -0x1(%r13), %r8d
xorl %r9d, %r9d
cmpb $0x2, %r13b
jl 0x20100
movq 0x8(%rdx), %r10
movq (%rdi,%r9,8), %r11
movsd (%r12,%r9,8), %xmm0
xorl %r14d, %r14d
leal (%rax,%r14), %r15d
movsbq %r15b, %r15
movsd (%r10,%r15,8), %xmm1
movsbq %r14b, %r14
movsd %xmm1, (%r11,%r14,8)
addsd (%r10,%r15,8), %xmm0
incb %r14b
movsbl %r14b, %r15d
cmpl %r15d, %ecx
jg 0x200d0
movsd %xmm0, (%r12,%r9,8)
movapd %xmm2, %xmm0
subsd (%r12,%r9,8), %xmm0
movq (%rdi,%r9,8), %r10
movsd %xmm0, -0x8(%r10,%r13,8)
incq %r9
addb %r8b, %al
cmpq %rbx, %r9
jne 0x200b9
movq -0x38(%rbp), %r15
movq 0x88(%r15), %rax
cmpb $0x0, 0x32(%rax)
jne 0x20149
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq %r15, %rdi
callq 0x1117c
testb %al, %al
je 0x204af
movq -0x80(%rbp), %r13
movl %r13d, %edi
movq -0x48(%rbp), %r14
movl %r14d, %esi
callq 0xbb5c
movq %rax, 0x38(%r15)
movl %r13d, %edi
movl %r14d, %esi
movl %r14d, %edx
callq 0xcaae
movq %rax, 0x40(%r15)
movl %r13d, %edi
movl %r14d, %esi
movl -0x3c(%rbp), %edx
callq 0xcaae
movq %rax, 0x48(%r15)
movq -0x78(%rbp), %r12
movl %r12d, %edi
movl %r14d, %esi
callq 0xbb5c
movq %rax, 0x90(%r15)
movl %r12d, %edi
movl %r14d, %esi
movl %r14d, %edx
callq 0xcaae
movq %rax, 0x98(%r15)
testl %r13d, %r13d
jle 0x202a6
movq -0x30(%rbp), %rax
leaq (,%rax,8), %r14
movl -0x48(%rbp), %eax
movq %rax, -0xa8(%rbp)
movq -0x68(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0xa0(%rbp)
xorl %ecx, %ecx
movq %r14, -0x88(%rbp)
movq -0x60(%rbp), %rsi
movq 0x38(%r15), %rax
movq %rcx, -0x70(%rbp)
movq (%rax,%rcx,8), %rdi
movq %r14, %rdx
callq 0x41c0
testb %bl, %bl
movq -0xa8(%rbp), %r15
jle 0x20241
movq -0x58(%rbp), %r14
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %r13
xorl %r12d, %r12d
movq (%r13,%r12,8), %rdi
movq (%r14,%r12,8), %rsi
movq -0x88(%rbp), %rdx
callq 0x41c0
incq %r12
cmpq %r12, %r15
jne 0x20224
testb %bl, %bl
movq -0xa0(%rbp), %r14
jle 0x2028a
movq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %r13
xorl %r12d, %r12d
movq (%r13,%r12,8), %rdi
movq -0xb0(%rbp), %rax
movq (%rax,%r12,8), %rsi
movq %r14, %rdx
callq 0x41c0
incq %r12
cmpq %r12, %r15
jne 0x2026a
movq -0x70(%rbp), %rcx
incq %rcx
cmpq -0x80(%rbp), %rcx
movq -0x38(%rbp), %r15
movq -0x88(%rbp), %r14
jne 0x201ea
movq -0x78(%rbp), %r10
testl %r10d, %r10d
movq -0x68(%rbp), %r12
movsd 0x14637(%rip), %xmm1    # 0x348f0
jle 0x20311
movsbl %bl, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movq 0x90(%r15), %rax
xorl %ecx, %ecx
testb %bl, %bl
jle 0x20309
movq (%rax,%rcx,8), %rdx
movq 0x98(%r15), %rsi
movq (%rsi,%rcx,8), %rsi
xorl %edi, %edi
movsd %xmm1, (%rdx,%rdi,8)
movq (%rsi,%rdi,8), %r8
xorl %r9d, %r9d
movsd %xmm1, (%r8,%r9,8)
incq %r9
cmpq %r9, %rbx
jne 0x202f3
incq %rdi
cmpq %rbx, %rdi
jne 0x202e7
incq %rcx
cmpq %r10, %rcx
jne 0x202d2
movq -0x60(%rbp), %rdi
callq 0x42a0
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %r14
movl %r14d, %esi
callq 0xa5e0
movq -0x50(%rbp), %rdi
movl %r14d, %esi
callq 0xa5e0
movq -0x90(%rbp), %rsi
cmpb $0x0, 0x6c(%rsi)
je 0x20351
addq $0x6c, %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x112f0
leaq 0x50(%r15), %rsi
leaq 0x58(%r15), %rdx
leaq 0x60(%r15), %rcx
movq %r15, %rdi
movq -0x30(%rbp), %r8
movl -0x3c(%rbp), %r14d
movl %r14d, %r9d
callq 0x1112e
leaq 0x68(%r15), %rsi
leaq 0x70(%r15), %rdx
leaq 0x78(%r15), %rcx
movq %r15, %rdi
movq -0x48(%rbp), %r8
movl %r14d, %r9d
callq 0x1112e
testb %bl, %bl
jle 0x203c4
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, %rbx
jne 0x203a8
testb %bl, %bl
jle 0x20422
movq 0x88(%r15), %rsi
movq 0x58(%r15), %rax
movq 0x70(%r15), %rcx
movq 0x18(%rsi), %rdx
movq 0x20(%rsi), %rsi
xorl %edi, %edi
movq %rbx, %r8
movq (%rax,%rdi,8), %r9
movq (%rcx,%rdi,8), %r10
xorl %r11d, %r11d
leal (%r8,%r11), %r14d
movsbq %r14b, %r14
movsd (%rdx,%r14,8), %xmm0
movsd %xmm0, (%r9,%r11,8)
movsd (%rsi,%r14,8), %xmm0
movsd %xmm0, (%r10,%r11,8)
incq %r11
cmpq %r11, %rbx
jne 0x203ef
incq %rdi
addq %rbx, %r8
cmpq %rbx, %rdi
jne 0x203e4
testb %bl, %bl
jle 0x20499
movq 0x88(%r15), %rcx
movq 0x60(%r15), %rdx
movq 0x78(%r15), %rsi
movzbl -0x3c(%rbp), %edi
leal 0x1(%rbx), %r8d
movq -0x30(%rbp), %rax
mulb %r8b
movzbl %al, %eax
xorl %r8d, %r8d
testb %r12b, %r12b
jle 0x2048a
movq 0x18(%rcx), %r9
movq 0x20(%rcx), %r10
movq (%rdx,%r8,8), %r11
movq (%rsi,%r8,8), %rbx
xorl %r14d, %r14d
leal (%rax,%r14), %r15d
movsbq %r15b, %r15
movsd (%r9,%r15,8), %xmm0
movsd %xmm0, (%r11,%r14,8)
movsd (%r10,%r15,8), %xmm0
movsd %xmm0, (%rbx,%r14,8)
incq %r14
cmpq %r14, %rdi
jne 0x20462
incq %r8
movq -0x30(%rbp), %r9
addq %r9, %rax
cmpq %r9, %r8
jne 0x2044a
movq -0x98(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4f8c