// File: myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::init(param*)
void HMMProblem::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;
    if( this->p->solver == METHOD_CGD && this->p->solver_setting == -1)
        this->p->solver_setting = 1; // default Fletcher-Reeves
    
    NPAR nS = this->p->nS, nO = this->p->nO;
    NUMBER *a_PI, ** a_A, ** a_B;
    init3Params(a_PI, a_A, a_B, nS, nO);
    
    //
    // setup params
    //
    this->pi = init2D<NUMBER>((NDAT)this->sizes[0], (NDAT)nS);
    this->A =  init3D<NUMBER>((NDAT)this->sizes[1], (NDAT)nS, (NDAT)nS);
    this->B =  init3D<NUMBER>((NDAT)this->sizes[2], (NDAT)nS, (NDAT)nO);
    NPAR i, j;
    int offset, idx;

    // write default parameters first
    NUMBER sumPI = 0;
    NUMBER sumA[this->p->nS];
    NUMBER sumB[this->p->nS];
    for(i=0; i<this->p->nS; i++) {
        sumA[i] = 0;
        sumB[i] = 0;
    }
    // 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 = (int)(nS-1);
    for(i=0; i<nS; i++) {
        for(j=0; j<((nS)-1); j++) {
            idx = (int)(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];
    }
    // populate B
    offset = (int)((nS-1) + nS*(nS-1));
    for(i=0; i<nS; i++) {
        for(j=0; j<((nO)-1); j++) {
            idx = (int)(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, 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);
    }

    NCAT x;
    for(x=0; x<this->sizes[0]; x++)
        cpy1D<NUMBER>(a_PI, this->pi[x], (NDAT)nS);
    for(x=0; x<this->sizes[1]; x++)
        cpy2D<NUMBER>(a_A, this->A[x], (NDAT)nS, (NDAT)nS);
    for(x=0; x<this->sizes[2]; x++)
        cpy2D<NUMBER>(a_B, this->B[x], (NDAT)nS, (NDAT)nO);

    // destroy setup params
    free(a_PI);
    free2D<NUMBER>(a_A, (NDAT)nS);
    free2D<NUMBER>(a_B, (NDAT)nS);
    if(param->initfile[0]!=0) { // from setup file
        // if needs be -- read in init params from a file
        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 = (int)(offset + i*nS + j);
			lbA[i][j] = this->p->param_lo[idx];
			ubA[i][j] = this->p->param_hi[idx];
		}
	// *B
	offset = (int)(nS + nS*nS);
	for(i=0; i<nS; i++)
		for(j=0; j<nO; j++) {
			idx = (int)(offset + i*nO + 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, -0x70(%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 %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x18(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rax)
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x1968c
movq -0x70(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x40(%rax), %eax
cmpl $-0x1, %eax
jne 0x1968c
movq -0x70(%rbp), %rax
movq 0x88(%rax), %rax
movb $0x1, 0x40(%rax)
movq -0x70(%rbp), %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)
movsbl -0x11(%rbp), %r8d
movsbl -0x12(%rbp), %r9d
leaq -0x20(%rbp), %rsi
leaq -0x28(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0x19de0
movq -0x70(%rbp), %rax
movl 0xc(%rax), %edi
movsbl -0x11(%rbp), %esi
callq 0xf580
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x38(%rax)
movl 0x10(%rax), %edi
movsbl -0x11(%rbp), %edx
movl %edx, %esi
callq 0x11310
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x40(%rax)
movl 0x14(%rax), %edi
movsbl -0x11(%rbp), %esi
movsbl -0x12(%rbp), %edx
callq 0x11310
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x48(%rax)
movq $0x0, -0x48(%rbp)
movq 0x88(%rax), %rcx
movzbl 0x8b8(%rcx), %esi
movl %esi, %ecx
movq %rsp, %rdx
movq %rdx, -0x50(%rbp)
movl %esi, %edx
leal 0xf(,%rdx,8), %edx
andl $-0x10, %edx
movl %edx, %esi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0x80(%rbp)
movq %rdx, %rsp
movq %rcx, -0x58(%rbp)
movq 0x88(%rax), %rax
movzbl 0x8b8(%rax), %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, -0x78(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movb $0x0, -0x31(%rbp)
movq -0x70(%rbp), %rcx
movsbl -0x31(%rbp), %eax
movq 0x88(%rcx), %rcx
movsbl 0x8b8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x197d1
movq -0x78(%rbp), %rax
movq -0x80(%rbp), %rcx
movsbq -0x31(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movsbq -0x31(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x31(%rbp), %al
addb $0x1, %al
movb %al, -0x31(%rbp)
jmp 0x1978b
movb $0x0, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x19834
movq -0x70(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x8(%rcx), %rcx
movsbq -0x31(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq -0x20(%rbp), %rcx
movsbq -0x31(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rax
movq 0x8(%rax), %rax
movsbq -0x31(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movb -0x31(%rbp), %al
addb $0x1, %al
movb %al, -0x31(%rbp)
jmp 0x197d5
movsd 0x361cc(%rip), %xmm0    # 0x4fa08
subsd -0x48(%rbp), %xmm0
movq -0x20(%rbp), %rax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movsbl -0x11(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x38(%rbp)
movb $0x0, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19943
movb $0x0, -0x32(%rbp)
movsbl -0x32(%rbp), %eax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x19904
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rcx
movl -0x38(%rbp), %edx
movsbl -0x31(%rbp), %esi
movsbl -0x11(%rbp), %edi
subl $0x1, %edi
imull %edi, %esi
addl %esi, %edx
movsbl -0x32(%rbp), %esi
addl %esi, %edx
movl %edx, -0x3c(%rbp)
movq 0x88(%rcx), %rdx
movq 0x8(%rdx), %rdx
movslq -0x3c(%rbp), %rsi
movsd (%rdx,%rsi,8), %xmm0
movq -0x28(%rbp), %rdx
movsbq -0x31(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movsbq -0x32(%rbp), %rsi
movsd %xmm0, (%rdx,%rsi,8)
movq 0x88(%rcx), %rcx
movq 0x8(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movsbq -0x31(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x32(%rbp), %al
addb $0x1, %al
movb %al, -0x32(%rbp)
jmp 0x19876
movq -0x80(%rbp), %rax
movsbq -0x31(%rbp), %rcx
movsd 0x360f3(%rip), %xmm0    # 0x4fa08
subsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movsbq -0x31(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl -0x11(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x31(%rbp), %al
addb $0x1, %al
movb %al, -0x31(%rbp)
jmp 0x19862
movsbl -0x11(%rbp), %eax
subl $0x1, %eax
movsbl -0x11(%rbp), %ecx
movsbl -0x11(%rbp), %edx
subl $0x1, %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movb $0x0, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19a42
movb $0x0, -0x32(%rbp)
movsbl -0x32(%rbp), %eax
movsbl -0x12(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x19a03
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
movl -0x38(%rbp), %edx
movsbl -0x31(%rbp), %esi
movsbl -0x12(%rbp), %edi
subl $0x1, %edi
imull %edi, %esi
addl %esi, %edx
movsbl -0x32(%rbp), %esi
addl %esi, %edx
movl %edx, -0x3c(%rbp)
movq 0x88(%rcx), %rdx
movq 0x8(%rdx), %rdx
movslq -0x3c(%rbp), %rsi
movsd (%rdx,%rsi,8), %xmm0
movq -0x30(%rbp), %rdx
movsbq -0x31(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movsbq -0x32(%rbp), %rsi
movsd %xmm0, (%rdx,%rsi,8)
movq 0x88(%rcx), %rcx
movq 0x8(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movsbq -0x31(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x32(%rbp), %al
addb $0x1, %al
movb %al, -0x32(%rbp)
jmp 0x19975
movq -0x78(%rbp), %rax
movsbq -0x31(%rbp), %rcx
movsd 0x35ff4(%rip), %xmm0    # 0x4fa08
subsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movsbq -0x31(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl -0x12(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x31(%rbp), %al
addb $0x1, %al
movb %al, -0x31(%rbp)
jmp 0x19961
movq -0x70(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x32(%rax), %eax
cmpl $0x0, %eax
jne 0x19a91
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x19e80
testb $0x1, %al
jne 0x19a91
movq 0x41562(%rip), %rax     # 0x5afd8
movq (%rax), %rdi
leaq 0x369ef(%rip), %rsi     # 0x5046f
movb $0x0, %al
callq 0x5440
movl $0x1, %edi
callq 0x5390
movl $0x0, -0x64(%rbp)
movq -0x70(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x19acc
movq -0x70(%rbp), %rax
movq -0x20(%rbp), %rdi
movq 0x38(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movsbl -0x11(%rbp), %edx
callq 0xc440
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x19a98
movl $0x0, -0x64(%rbp)
movq -0x70(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0x10(%rcx), %eax
jge 0x19b0b
movq -0x70(%rbp), %rax
movq -0x28(%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
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x19ad3
movl $0x0, -0x64(%rbp)
movq -0x70(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0x14(%rcx), %eax
jge 0x19b4a
movq -0x70(%rbp), %rax
movq -0x30(%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 0x19b12
movq -0x20(%rbp), %rdi
callq 0x5380
movq -0x28(%rbp), %rdi
movsbl -0x11(%rbp), %esi
callq 0xc7f0
movq -0x30(%rbp), %rdi
movsbl -0x11(%rbp), %esi
callq 0xc7f0
movq -0x10(%rbp), %rax
movsbl 0x6c(%rax), %eax
cmpl $0x0, %eax
je 0x19b8d
movq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x6c, %rsi
xorl %edx, %edx
callq 0x1a1f0
movq -0x70(%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 -0x70(%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, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19c45
movq -0x70(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x18(%rcx), %rcx
movsbq -0x31(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x50(%rax), %rcx
movsbq -0x31(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rcx
movq 0x20(%rcx), %rcx
movsbq -0x31(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x68(%rax), %rax
movsbq -0x31(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x31(%rbp), %al
addb $0x1, %al
movb %al, -0x31(%rbp)
jmp 0x19be5
movsbl -0x11(%rbp), %eax
movl %eax, -0x38(%rbp)
movb $0x0, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19d03
movb $0x0, -0x32(%rbp)
movsbl -0x32(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19cf4
movq -0x70(%rbp), %rax
movl -0x38(%rbp), %ecx
movsbl -0x31(%rbp), %edx
movsbl -0x11(%rbp), %esi
imull %esi, %edx
addl %edx, %ecx
movsbl -0x32(%rbp), %edx
addl %edx, %ecx
movl %ecx, -0x3c(%rbp)
movq 0x88(%rax), %rcx
movq 0x18(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x58(%rax), %rcx
movsbq -0x31(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x32(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rcx
movq 0x20(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x70(%rax), %rax
movsbq -0x31(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x32(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x32(%rbp), %al
addb $0x1, %al
movb %al, -0x32(%rbp)
jmp 0x19c64
jmp 0x19cf6
movb -0x31(%rbp), %al
addb $0x1, %al
movb %al, -0x31(%rbp)
jmp 0x19c50
movsbl -0x11(%rbp), %eax
movsbl -0x11(%rbp), %ecx
movsbl -0x11(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movb $0x0, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
movsbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19dce
movb $0x0, -0x32(%rbp)
movsbl -0x32(%rbp), %eax
movsbl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jge 0x19dbf
movq -0x70(%rbp), %rax
movl -0x38(%rbp), %ecx
movsbl -0x31(%rbp), %edx
movsbl -0x12(%rbp), %esi
imull %esi, %edx
addl %edx, %ecx
movsbl -0x32(%rbp), %edx
addl %edx, %ecx
movl %ecx, -0x3c(%rbp)
movq 0x88(%rax), %rcx
movq 0x18(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x60(%rax), %rcx
movsbq -0x31(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x32(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x88(%rax), %rcx
movq 0x20(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x78(%rax), %rax
movsbq -0x31(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x32(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x32(%rbp), %al
addb $0x1, %al
movb %al, -0x32(%rbp)
jmp 0x19d2f
jmp 0x19dc1
movb -0x31(%rbp), %al
addb $0x1, %al
movb %al, -0x31(%rbp)
jmp 0x19d1b
movq -0x50(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)