// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rdi, %rbx
movq %rsi, 0x88(%rdi)
movzbl 0x8b8(%rsi), %r14d
movsbq %r14b, %rax
movq %rax, -0x30(%rbp)
movsbl %r14b, %r12d
movsbl 0x8b9(%rsi), %eax
movq %rax, -0x48(%rbp)
movslq %eax, %r15
movl 0x8bc(%rsi), %eax
movq %rax, -0x78(%rbp)
movq %rsi, -0x90(%rbp)
movl 0x8c0(%rsi), %eax
movq %rax, -0x58(%rbp)
movb $0x1, 0x80(%rdi)
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x4120
movq %rax, 0x18(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rbx)
andq $0x0, 0x30(%rbx)
leaq -0x68(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movq %rbx, %rdi
movl %r12d, %r8d
movq %r15, -0x80(%rbp)
movl %r15d, %r9d
callq 0xde8c
movq %rsp, -0x98(%rbp)
leal 0xf(,%r14,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 %r12d, -0x34(%rbp)
testb %r12b, %r12b
movl %r14d, %r13d
cmovlel %edx, %r13d
cmpq %rdx, %r13
je 0x1afdc
andq $0x0, (%rax,%rdx,8)
andq $0x0, (%rcx,%rdx,8)
incq %rdx
jmp 0x1afc8
movq -0x30(%rbp), %rdx
leal -0x1(%rdx), %edi
movq 0x88(%rbx), %rdx
xorpd %xmm0, %xmm0
movq -0x68(%rbp), %rsi
xorl %r8d, %r8d
cmpl %r8d, %edi
jle 0x1b013
movq 0x8(%rdx), %r9
movsd (%r9,%r8,8), %xmm1
movsd %xmm1, (%rsi,%r8,8)
addsd %xmm1, %xmm0
incq %r8
jmp 0x1aff5
movsd 0x118d5(%rip), %xmm2    # 0x2c8f0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
movq -0x68(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq %rsi, -0x88(%rbp)
movsd %xmm1, -0x8(%rsi,%rdx,8)
movq -0x60(%rbp), %rsi
leal -0x1(%r14), %edx
movzbl %dl, %r11d
xorl %r10d, %r10d
movq %r11, -0x40(%rbp)
movq %rsi, %r9
movq %rsi, %rdx
cmpq %r13, %r10
je 0x1b0b8
movq 0x88(%rbx), %r15
movq -0x60(%rbp), %rdx
xorl %r12d, %r12d
cmpl %r12d, %edi
jle 0x1b096
leal (%r11,%r12), %r8d
movq 0x8(%r15), %rsi
movsbq %r8b, %r8
movsd (%rsi,%r8,8), %xmm0
movq (%rdx,%r10,8), %rsi
movsd %xmm0, (%rsi,%r12,8)
addsd (%rax,%r10,8), %xmm0
movsd %xmm0, (%rax,%r10,8)
incq %r12
jmp 0x1b064
movapd %xmm2, %xmm0
subsd (%rax,%r10,8), %xmm0
movq (%r9,%r10,8), %rsi
movq -0x30(%rbp), %r8
movsd %xmm0, -0x8(%rsi,%r8,8)
incq %r10
addq -0x40(%rbp), %r11
jmp 0x1b051
movq -0x48(%rbp), %rsi
leal -0x1(%rsi), %edi
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
decl %eax
leal 0x1(%r14), %r8d
movl %r8d, -0x6c(%rbp)
mulb %r8b
movzbl %al, %eax
decl %esi
movzbl %sil, %r9d
xorl %r10d, %r10d
cmpq %r13, %r10
je 0x1b14e
movq 0x88(%rbx), %r11
movq -0x50(%rbp), %r15
xorl %r12d, %r12d
cmpl %r12d, %edi
jle 0x1b129
leal (%rax,%r12), %esi
movq 0x8(%r11), %r8
movsbq %sil, %rsi
movsd (%r8,%rsi,8), %xmm0
movq (%r15,%r10,8), %rsi
movsd %xmm0, (%rsi,%r12,8)
addsd (%rcx,%r10,8), %xmm0
movsd %xmm0, (%rcx,%r10,8)
incq %r12
jmp 0x1b0f7
movapd %xmm2, %xmm0
subsd (%rcx,%r10,8), %xmm0
movq -0x40(%rbp), %rsi
movq (%rsi,%r10,8), %rsi
movq -0x80(%rbp), %r8
movsd %xmm0, -0x8(%rsi,%r8,8)
incq %r10
addq %r9, %rax
jmp 0x1b0e4
movq 0x88(%rbx), %rax
cmpb $0x0, 0x32(%rax)
jne 0x1b176
movq -0x50(%rbp), %rcx
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
callq 0xded0
testb %al, %al
je 0x1b45e
movq -0x58(%rbp), %r12
movl %r12d, %edi
movl -0x34(%rbp), %r15d
movl %r15d, %esi
callq 0x9328
movq %rax, 0x38(%rbx)
movl %r12d, %edi
movl %r15d, %esi
movl %r15d, %edx
callq 0xa0be
movq %rax, 0x40(%rbx)
movl %r12d, %edi
movl %r15d, %esi
movq -0x48(%rbp), %rdx
callq 0xa0be
movq %rax, 0x48(%rbx)
movq -0x78(%rbp), %r12
movl %r12d, %edi
movl %r15d, %esi
callq 0x9328
movq %rax, 0x90(%rbx)
movl %r12d, %edi
movl %r15d, %esi
movl %r15d, %edx
callq 0xa0be
movq %rax, 0x98(%rbx)
movq -0x30(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0x40(%rbp)
xorl %r12d, %r12d
movq -0x58(%rbp), %rax
testl %eax, %eax
cmovlel %r12d, %eax
movq %rax, -0x58(%rbp)
movl -0x34(%rbp), %r15d
cmpq %r12, -0x58(%rbp)
je 0x1b251
movq -0x68(%rbp), %rsi
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rdi
movq -0x40(%rbp), %rdx
callq 0x41c0
movq -0x60(%rbp), %rdi
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rsi
movl %r15d, %edx
movl %r15d, %ecx
callq 0x93ad
movq -0x50(%rbp), %rdi
movq 0x48(%rbx), %rax
movq (%rax,%r12,8), %rsi
movl %r15d, %edx
movq -0x48(%rbp), %rcx
callq 0x93ad
incq %r12
jmp 0x1b202
cvtsi2sdl -0x30(%rbp), %xmm0
movsd 0x11692(%rip), %xmm1    # 0x2c8f0
divsd %xmm0, %xmm1
xorl %eax, %eax
movq -0x78(%rbp), %r9
testl %r9d, %r9d
cmovlel %eax, %r9d
cmpq %r9, %rax
je 0x1b2b6
movq 0x90(%rbx), %rcx
xorl %edx, %edx
cmpq %r13, %rdx
je 0x1b2b1
movq (%rcx,%rax,8), %rsi
movsd %xmm1, (%rsi,%rdx,8)
movq 0x98(%rbx), %rsi
xorl %edi, %edi
cmpq %rdi, %r14
je 0x1b2ac
movq (%rsi,%rax,8), %r8
movq (%r8,%rdx,8), %r8
movsd %xmm1, (%r8,%rdi,8)
incq %rdi
jmp 0x1b294
incq %rdx
jmp 0x1b27d
incq %rax
jmp 0x1b26f
movq -0x68(%rbp), %rdi
callq 0x4290
movq -0x60(%rbp), %rdi
movl -0x34(%rbp), %r15d
movl %r15d, %esi
callq 0x7f2c
movq -0x50(%rbp), %rdi
movl %r15d, %esi
callq 0x7f2c
movq -0x90(%rbp), %rsi
cmpb $0x0, 0x6c(%rsi)
je 0x1b2f6
addq $0x6c, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe026
leaq 0x50(%rbx), %rsi
leaq 0x58(%rbx), %rdx
leaq 0x60(%rbx), %rcx
movsbl %r14b, %r8d
movq %rbx, %rdi
movq -0x48(%rbp), %r15
movl %r15d, %r9d
callq 0xde8c
leaq 0x68(%rbx), %rsi
leaq 0x70(%rbx), %rdx
leaq 0x78(%rbx), %rcx
movq %rbx, %rdi
movq -0x30(%rbp), %r8
movl %r15d, %r9d
callq 0xde8c
movq 0x88(%rbx), %rax
movq 0x50(%rbx), %rcx
movq 0x68(%rbx), %rdx
xorl %esi, %esi
cmpq %rsi, %r13
je 0x1b367
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 0x1b341
movq %r14, %rax
negq %rax
xorl %edx, %edx
movq %r14, %rcx
cmpq %r13, %rdx
je 0x1b3d2
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 0x1b3ca
leal (%rcx,%r10), %r11d
movq 0x18(%rdi), %r15
movsbq %r11b, %r11
movsd (%r15,%r11,8), %xmm0
movq (%r8,%rdx,8), %r15
movsd %xmm0, (%r15,%r10,8)
movq 0x20(%rdi), %r15
movsd (%r15,%r11,8), %xmm0
movq (%r9,%rdx,8), %r11
movsd %xmm0, (%r11,%r10,8)
incq %r10
jmp 0x1b38d
addq %r14, %rcx
movq %rsi, %rdx
jmp 0x1b372
xorl %ecx, %ecx
cmpb $0x0, -0x80(%rbp)
movq -0x48(%rbp), %rax
cmovlel %ecx, %eax
movzbl %al, %edx
movl %r14d, %eax
mulb -0x6c(%rbp)
movzbl %al, %eax
movzbl %r14b, %esi
cmpq %r13, %rcx
je 0x1b448
movq 0x88(%rbx), %rdi
movq 0x60(%rbx), %r8
movq 0x78(%rbx), %r9
xorl %r10d, %r10d
cmpq %r10, %rdx
je 0x1b440
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 0x1b406
incq %rcx
addq %rsi, %rax
jmp 0x1b3ef
movq -0x98(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1db73(%rip), %rax     # 0x38fd8
movq (%rax), %rcx
leaq 0x11fbc(%rip), %rdi     # 0x2d42b
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x44b0
pushq $0x1
popq %rdi
callq 0x42a0