// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemSt.cpp
// Function: HMMProblemSt::init(task*)
void HMMProblemSt::init(struct task *task) {
//    printf("HMMProblemSt::init\n");
   // init
    this->task = task;
    this->non01constraints = true;
    this->null_obs_ratios = Calloc(NUMBER, (size_t)this->task->nO);
//    this->neg_log_lik = 0;
    this->null_skill_obs = 0;
    this->null_skill_obs_prob = 0;
    if( this->task->solver == METHOD_CGD && this->task->solver_setting == -1)
        this->task->solver_setting = 1; // default Fletcher-Reeves
    
    NPAR nS = this->task->nS, nO = this->task->nO;
    NCAT nK = this->task->nK, nG = this->task->nG;
    
    //
    // setup params
    //
    this->skill1_n = nS * (1 + nS + nO); // number of params per 1 skill slot
    this->param_skill = init1D<NUMBER>( (NDAT)(this->skill1_n*nK) );
    this->gradient_skill = init1D<NUMBER>( (NDAT)(this->skill1_n*nK) );
    this->active_set_block = initToValue1D<NPAR>((NDAT)nK, 1);
    this->unblocked_simplex = initToValue1D<NPAR>((NDAT)(nK * (1 + 2 * nS)), 1);
    
    NPAR i, j, m;
    int offset, idx;

    // write default parameters first
    NUMBER sum = 0;
    
    // vv populate skill parameters
    for(NCAT k=0; k<nK; k++) {
        // vv populate PI
        for(i=0; i<((nS)-1); i++) {
            this->param_skill[ k*this->skill1_n + i ] = this->task->init_param_values[i];
            sum  += this->task->init_param_values[i];
        } // ^^ populate PI
        this->param_skill[ k*this->skill1_n + nS - 1 ] = 1 - sum;
        // vv populate A
        offset = (int)(nS-1);
        for(i=0; i<nS; i++) {
            sum = 0;
            for(j=0; j<((nS)-1); j++) {
                idx = (int)(offset + i*((nS)-1) + j);
                this->param_skill[ k*this->skill1_n + nS + i*nS + j ] = this->task->init_param_values[idx];
                sum  += this->task->init_param_values[idx];
            }
            this->param_skill[ k*this->skill1_n + nS + i*nS + nS-1 ] = 1 - sum;
        } // ^^ populate A
        // vv populate B
        offset = (int)((nS-1) + nS*(nS-1));
        for(i=0; i<nS; i++) {
            sum = 0;
            for(m=0; m<((nO)-1); m++) {
                idx = (int)(offset + i*((nO)-1) + m);
                this->param_skill[ k*this->skill1_n + nS + nS*nS + i*nO + m ] = this->task->init_param_values[idx];
                sum += this->task->init_param_values[idx];
            }
            this->param_skill[ k*this->skill1_n + nS + nS*nS + i*nO + nO - 1 ] = 1 - sum;
        } // ^^ populate B
    } // ^^ populate skill parameters
    
    // check only first mass-produced skill
    if( this->task->do_not_check_constraints==0 && !checkSkillConstraints(this->param_skill)) {
        fprintf(stderr,"params do not meet constraints.\n");
        exit(1);
    }

    if(task->initfile[0]!=0) { // from setup file
        // if needs be -- read in init params from a file
        this->readModel(task->initfile, false /* read and upload but not overwrite*/);
    }
    
    // init and populate boundaries
    this->lb_param_skill = init1D<NUMBER>(this->skill1_n);
    cpy1D(this->task->param_values_lb, this->lb_param_skill, this->skill1_n);
    this->ub_param_skill = init1D<NUMBER>(this->skill1_n);
    cpy1D(this->task->param_values_ub, this->ub_param_skill, this->skill1_n);
    
    // init supportive data structures
    NDAT Nst = this->task->Nst;
    this->alpha = init2D<NUMBER>(Nst, nS); // forward variables, 2D array of Nst rows nS values in each ((1..nS)){Nst}
    this->po_param_gk = init2D<NUMBER>(nG, nK); //
    this->loglik_k = init1D<NUMBER>(nK); //
    this->ndat_k = init1D<NDAT>(nK);
    this->beta = init2D<NUMBER>(Nst, nS); // backward variables, 2D array of Nst rows nS values in each ((1..nS)){Nst}
    this->scale = (this->task->is_scaled==1) ? initToValue1D<NUMBER>(Nst, 0) : NULL;
    this->gamma = NULL;
    this->xi = NULL;
    if(this->task->solver == METHOD_BW) {
        this->gamma = init2D<NUMBER>(Nst, nS);
        this->xi = init3D<NUMBER>(Nst, nS, nS);
    }
    this->backward_ix = init1D<NDAT>(Nst); // Nst sized array of pointing to previous student-skill repetition, -1 means none before
    this->forward_ix = init1D<NDAT>(Nst); // Nst sized array of pointing to next student-skill repetition, -1 means no more
    this->is_fwd_bwd_built=false; // flag for noting whether backward_ix & forward_ix are already built
    
    this->group_skill_map = init3D<NUMBER>(nG, nK, nS);//UNBOOST

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, 0x68(%rdi)
movb $0x1, 0x50(%rdi)
movsbl 0x901(%rsi), %ecx
movq %rcx, 0x20(%rsp)
movslq %ecx, %r13
movl $0x8, %esi
movq %r13, %rdi
callq 0x2110
movq %rax, 0x10(%rbx)
xorpd %xmm0, %xmm0
movq %rbx, 0x18(%rsp)
movupd %xmm0, 0x18(%rbx)
cmpb $0x3, 0x4f(%r12)
jne 0xace0
cmpb $-0x1, 0x50(%r12)
jne 0xace0
movb $0x1, 0x50(%r12)
movsbl 0x900(%r12), %ecx
movslq 0x908(%r12), %r15
movl 0x904(%r12), %eax
movl %eax, 0x34(%rsp)
movq 0x20(%rsp), %rax
leal (%rcx,%rax), %edx
incl %edx
movq %rcx, 0x8(%rsp)
imull %ecx, %edx
movq 0x18(%rsp), %rbx
movl %edx, 0x38(%rbx)
movl %edx, 0x14(%rsp)
movl %edx, %eax
imull %r15d, %eax
movslq %eax, %r14
movl $0x8, %esi
movq %r14, %rdi
callq 0x2110
movq %rax, %rbp
movq %rax, 0x28(%rbx)
movl $0x8, %esi
movq %r14, %rdi
callq 0x2110
movq %rax, 0x30(%rbx)
movl $0x1, %esi
movq %r15, %rdi
callq 0x2110
movq %rax, %r14
testq %r15, %r15
jle 0xad6e
movq %r14, %rdi
movl $0x1, %esi
movq %r15, %rdx
callq 0x2100
movq 0x18(%rsp), %rax
movq %r14, 0x58(%rax)
movq 0x8(%rsp), %rax
movq %r15, %rcx
leal 0x1(,%rax,2), %r15d
movq %rcx, 0x28(%rsp)
imull %ecx, %r15d
movslq %r15d, %rbx
movl $0x1, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, %r14
testl %ebx, %ebx
jle 0xadb7
movl %r15d, %edx
movq %r14, %rdi
movl $0x1, %esi
callq 0x2100
movq 0x18(%rsp), %rax
movq %r14, 0x60(%rax)
movq 0x28(%rsp), %r15
testl %r15d, %r15d
jle 0xafc9
movq 0x8(%rsp), %rdi
movslq %edi, %rax
leal 0x1(%rax), %esi
leaq -0x1(%rax), %rcx
imull %ecx, %esi
leaq -0x1(%r13), %rdx
imull %edi, %edi
movq 0x20(%rsp), %r8
addl %edi, %r8d
movq %r8, 0x20(%rsp)
movslq %esi, %rsi
movq %rsi, 0x50(%rsp)
movslq 0x14(%rsp), %rsi
movq %rsi, 0x38(%rsp)
leaq (,%rdi,8), %rsi
addq %rbp, %rsi
movq %rsi, 0x40(%rsp)
xorpd %xmm1, %xmm1
xorl %r10d, %r10d
movsd 0x7b2b(%rip), %xmm0     # 0x12950
cmpb $0x1, %al
movq %r10, 0x48(%rsp)
jle 0xae6b
movq (%r12), %r8
imulq 0x38(%rsp), %r10
leaq (,%r10,8), %r11
addq %rbp, %r11
xorl %esi, %esi
xorl %edi, %edi
movsbq %dil, %rdi
movsd (%r8,%rdi,8), %xmm2
movslq %esi, %rsi
movsd %xmm2, (%r11,%rsi,8)
addsd %xmm2, %xmm1
incb %dil
movsbl %dil, %esi
cmpl %esi, %ecx
jg 0xae47
jmp 0xae71
imulq 0x38(%rsp), %r10
addq %rax, %r10
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movsd %xmm2, -0x8(%rbp,%r10,8)
testb %al, %al
jle 0xafb8
movq 0x8(%rsp), %rsi
leal (%r10,%rsi), %r11d
leaq (,%r10,8), %rbx
addq %rbp, %rbx
xorl %r14d, %r14d
cmpb $0x1, %al
jle 0xaeec
leaq 0x1(%r14), %r15
movq %r15, %r8
imulq %rcx, %r8
imulq %rax, %r14
shlq $0x3, %r8
addq (%r12), %r8
leaq (%rbx,%r14,8), %rdi
xorpd %xmm1, %xmm1
xorl %r9d, %r9d
movb $0x1, %sil
movslq %r9d, %r9
movsd (%r8,%r9,8), %xmm2
movsd %xmm2, (%rdi,%r9,8)
addsd %xmm2, %xmm1
movsbl %sil, %r9d
incb %sil
cmpl %r9d, %ecx
jg 0xaecb
jmp 0xaf00
movq %r14, %rsi
imulq %rax, %rsi
incq %r14
xorpd %xmm1, %xmm1
movq %r14, %r15
movq %rsi, %r14
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
addl %r11d, %r14d
movslq %r14d, %rsi
movsd %xmm2, -0x8(%rbp,%rsi,8)
movq %r15, %r14
cmpq %rax, %r15
jne 0xaea2
testb %al, %al
movq 0x28(%rsp), %r15
jle 0xafb8
movq 0x20(%rsp), %rsi
leal (%rsi,%r10), %r11d
movq 0x40(%rsp), %rsi
leaq (%rsi,%r10,8), %r10
xorl %ebx, %ebx
cmpb $0x1, %r13b
jle 0xaf91
movq %rbx, %rsi
imulq %rdx, %rsi
movq %rbx, %r8
imulq %r13, %r8
shlq $0x3, %rsi
addq (%r12), %rsi
movq 0x50(%rsp), %rdi
leaq (%rsi,%rdi,8), %r14
leaq (%r10,%r8,8), %rdi
xorpd %xmm1, %xmm1
xorl %r9d, %r9d
movb $0x1, %sil
movslq %r9d, %r9
movsd (%r14,%r9,8), %xmm2
movsd %xmm2, (%rdi,%r9,8)
addsd %xmm2, %xmm1
movsbl %sil, %r9d
incb %sil
cmpl %r9d, %edx
jg 0xaf70
jmp 0xaf9c
movq %rbx, %r8
imulq %r13, %r8
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
addl %r11d, %r8d
movslq %r8d, %rsi
movsd %xmm2, -0x8(%rbp,%rsi,8)
incq %rbx
cmpq %rax, %rbx
jne 0xaf3d
movq 0x48(%rsp), %r10
incq %r10
cmpq %r15, %r10
jne 0xae25
cmpb $0x0, 0x42(%r12)
movq 0x18(%rsp), %r15
jne 0xafe9
movq %r15, %rdi
movq %rbp, %rsi
callq 0xb186
testb %al, %al
je 0xb181
cmpb $0x0, 0x7c(%r12)
movl 0x34(%rsp), %r13d
je 0xb011
addq $0x7c, %r12
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xb208
movl 0x38(%r15), %eax
movq 0x68(%r15), %r12
jmp 0xb015
movl 0x14(%rsp), %eax
movslq %eax, %rbx
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, 0x40(%r15)
movq 0x10(%r12), %rsi
leaq (,%rbx,8), %r14
movq %rax, %rdi
movq %r14, %rdx
callq 0x2180
movl $0x8, %esi
movq %rbx, %rdi
callq 0x2110
movq %rax, 0x48(%r15)
movq 0x18(%r12), %rsi
movq %rax, %rdi
movq %r14, %rdx
callq 0x2180
movl 0x8fc(%r12), %ebp
movslq %ebp, %r12
movl %r12d, %edi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
callq 0x10834
movq %rax, 0x70(%r15)
movl %r13d, %edi
movq 0x28(%rsp), %r14
movl %r14d, %esi
callq 0x10834
movq %rax, 0x78(%r15)
movl $0x8, %esi
movq %r14, %rdi
callq 0x2110
movq %rax, 0x80(%r15)
movl $0x4, %esi
movq %r14, %rdi
callq 0x2110
movq %rax, 0x88(%r15)
movl %r12d, %edi
movl %ebx, %esi
callq 0x10834
movq %rax, 0x90(%r15)
movq 0x68(%r15), %rbx
cmpb $0x1, 0x41(%rbx)
jne 0xb0e6
movl $0x8, %esi
movq %r12, %rdi
callq 0x2110
jmp 0xb0e8
xorl %eax, %eax
movq %rax, 0xa8(%r15)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x98(%r15)
cmpb $0x1, 0x4f(%rbx)
jne 0xb129
movl %ebp, %edi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
callq 0x10834
movq %rax, 0x98(%r15)
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x10887
movq %rax, 0xa0(%r15)
movl $0x4, %esi
movq %r12, %rdi
callq 0x2110
movq %rax, 0xb0(%r15)
movl $0x4, %esi
movq %r12, %rdi
callq 0x2110
movq %rax, 0xb8(%r15)
movb $0x0, 0xc0(%r15)
movl %r13d, %edi
movq 0x28(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x10887
movq %rax, 0xc8(%r15)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x29d1