// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, 0x68(%rdi)
movb $0x1, 0x50(%rdi)
movsbq 0x901(%rsi), %r15
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x3100
movq %rax, 0x10(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rbx)
cmpb $0x3, 0x4f(%r12)
jne 0x9c5b
cmpb $-0x1, 0x50(%r12)
jne 0x9c5b
movb $0x1, 0x50(%r12)
movsbq 0x900(%r12), %r14
movl 0x904(%r12), %eax
movl %eax, 0x20(%rsp)
movq %r12, 0x30(%rsp)
movl 0x908(%r12), %r12d
leal 0x1(%r14), %ebp
leal (%r14,%r15), %edi
incl %edi
imull %r14d, %edi
movl %edi, 0x38(%rbx)
imull %r12d, %edi
callq 0x6a57
movq %rax, 0x28(%rbx)
movl 0x38(%rbx), %edi
imull %r12d, %edi
callq 0x6a57
movq %rax, 0x30(%rbx)
movq %rbx, 0x10(%rsp)
pushq $0x1
popq %r13
movl %r12d, %edi
movl %r13d, %esi
callq 0xeb9a
movq 0x10(%rsp), %rcx
movq %rax, 0x58(%rcx)
leal 0x1(,%r14,2), %edi
imull %r12d, %edi
movl %r13d, %esi
movq 0x10(%rsp), %r13
callq 0xeb9a
movq %rax, 0x60(%r13)
leal -0x1(%r14), %eax
movq 0x28(%r13), %rcx
movl 0x38(%r13), %r8d
imull %eax, %ebp
leal -0x1(%r15), %edx
movl %r14d, %esi
imull %esi, %esi
leal (%rsi,%r15), %edi
movl %edi, 0x24(%rsp)
testb %r14b, %r14b
movl $0x0, %edi
cmovgl %r14d, %edi
movslq %ebp, %r9
movslq %r15d, %r10
movl %r8d, 0xc(%rsp)
movslq %r8d, %r8
testl %r12d, %r12d
movl $0x0, %r11d
movl %r12d, 0x1c(%rsp)
cmovgl %r12d, %r11d
movq %r11, 0x50(%rsp)
movslq %edi, %rbp
leaq (,%r14,8), %rdi
movq %rdi, 0x78(%rsp)
leaq (%rcx,%r14,8), %r11
movq %r8, 0x40(%rsp)
leaq (,%r8,8), %rdi
movq %rdi, 0x38(%rsp)
shlq $0x3, %rsi
leaq (%rsi,%r14,8), %rsi
addq %rcx, %rsi
movq %rsi, 0x28(%rsp)
shlq $0x3, %r9
movq %r9, 0x48(%rsp)
xorpd %xmm1, %xmm1
movsd 0x6bce(%rip), %xmm0     # 0x10950
movq %r14, (%rsp)
leaq -0x8(,%r14,8), %rdi
movq %r11, %r14
xorl %r11d, %r11d
leaq (,%r10,8), %rsi
movq %rsi, 0x70(%rsp)
movq %r10, 0x80(%rsp)
leaq -0x8(,%r10,8), %rsi
movq %rsi, 0x68(%rsp)
movq %rcx, %rsi
movq 0x68(%r13), %r8
cmpq 0x50(%rsp), %r11
je 0x9f22
movq 0x28(%r13), %rsi
movl 0x38(%r13), %r9d
movl %r9d, 0xc(%rsp)
imull %r11d, %r9d
xorl %r10d, %r10d
cmpl %r10d, %eax
jle 0x9dff
movq (%r8), %rbx
movsd (%rbx,%r10,8), %xmm2
leal (%r9,%r10), %ebx
movslq %ebx, %rbx
movsd %xmm2, (%rsi,%rbx,8)
addsd %xmm2, %xmm1
incq %r10
jmp 0x9ddc
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movq %r11, 0x60(%rsp)
movq %r11, %r8
imulq 0x40(%rsp), %r8
movq (%rsp), %r9
addq %r9, %r8
movsd %xmm2, -0x8(%rcx,%r8,8)
addl %r8d, %r9d
movq %rdi, %r15
movq %r14, 0x58(%rsp)
xorl %r10d, %r10d
cmpq %rbp, %r10
je 0x9e91
leaq 0x1(%r10), %r12
movq 0x68(%r13), %rbx
imulq (%rsp), %r10
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
cmpl %r13d, %eax
jle 0x9e6a
movq (%rbx), %r11
addq %r15, %r11
movsd (%r11,%r13,8), %xmm2
movsd %xmm2, (%r14,%r13,8)
addsd %xmm2, %xmm1
incq %r13
jmp 0x9e4a
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
addl %r9d, %r10d
movslq %r10d, %r10
movsd %xmm2, -0x8(%rcx,%r10,8)
addq 0x78(%rsp), %r14
addq %rdi, %r15
movq %r12, %r10
movq 0x10(%rsp), %r13
jmp 0x9e31
addl 0x24(%rsp), %r8d
movq 0x48(%rsp), %r9
movq 0x28(%rsp), %r15
xorl %r14d, %r14d
cmpq %rbp, %r14
je 0x9f03
movq 0x68(%r13), %rbx
movq %r14, %r10
imulq 0x80(%rsp), %r10
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
cmpl %r12d, %edx
jle 0x9edf
movq (%rbx), %r11
addq %r9, %r11
movsd (%r11,%r12,8), %xmm2
movsd %xmm2, (%r15,%r12,8)
addsd %xmm2, %xmm1
incq %r12
jmp 0x9ebf
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
addl %r8d, %r10d
movslq %r10d, %r10
movsd %xmm2, -0x8(%rcx,%r10,8)
incq %r14
addq 0x70(%rsp), %r15
addq 0x68(%rsp), %r9
jmp 0x9ea3
movq 0x60(%rsp), %r11
incq %r11
movq 0x58(%rsp), %r14
movq 0x38(%rsp), %r8
addq %r8, %r14
addq %r8, 0x28(%rsp)
jmp 0x9db9
cmpb $0x0, 0x42(%r8)
movl 0x20(%rsp), %r14d
movq 0x30(%rsp), %rbx
jne 0x9f43
movq %r13, %rdi
callq 0xa0d6
testb %al, %al
je 0xa0b1
cmpb $0x0, 0x7c(%rbx)
movl 0x1c(%rsp), %ebp
movl 0xc(%rsp), %edi
je 0x9f66
addq $0x7c, %rbx
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xa14e
movl 0x38(%r13), %edi
callq 0x6a57
movq %rax, 0x40(%r13)
movq 0x68(%r13), %rcx
movq 0x10(%rcx), %rsi
movslq 0x38(%r13), %rdx
shlq $0x3, %rdx
movq %rax, %rdi
callq 0x3180
movl 0x38(%r13), %edi
callq 0x6a57
movq %rax, 0x48(%r13)
movq 0x68(%r13), %rcx
movq 0x18(%rcx), %rsi
movslq 0x38(%r13), %rdx
shlq $0x3, %rdx
movq %rax, %rdi
callq 0x3180
movq 0x68(%r13), %rax
movl 0x8fc(%rax), %r12d
movl %r12d, %edi
movq (%rsp), %rbx
movl %ebx, %esi
callq 0xebcc
movq %rax, 0x70(%r13)
movl %r14d, %edi
movl %ebp, %esi
callq 0xebcc
movq %rax, 0x78(%r13)
movl %ebp, %edi
callq 0x6a57
movq %rax, 0x80(%r13)
movl %ebp, %edi
callq 0xec25
movq %rax, 0x88(%r13)
movl %r12d, %edi
movl %ebx, %esi
callq 0xebcc
movq %rax, 0x90(%r13)
movq 0x68(%r13), %rcx
cmpb $0x1, 0x41(%rcx)
jne 0xa020
xorpd %xmm0, %xmm0
movl %r12d, %edi
callq 0xec30
movq 0x68(%r13), %rcx
jmp 0xa022
xorl %eax, %eax
movq %rax, 0xa8(%r13)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x98(%r13)
cmpb $0x1, 0x4f(%rcx)
jne 0xa064
movl %r12d, %edi
movq (%rsp), %rbx
movl %ebx, %esi
callq 0xebcc
movq %rax, 0x98(%r13)
movl %r12d, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xec6b
movq %rax, 0xa0(%r13)
movl %r12d, %edi
callq 0xec25
movq %rax, 0xb0(%r13)
movl %r12d, %edi
callq 0xec25
movq %rax, 0xb8(%r13)
movb $0x0, 0xc0(%r13)
movl %r14d, %edi
movl %ebp, %esi
movq (%rsp), %rdx
callq 0xec6b
movq %rax, 0xc8(%r13)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xbf20(%rip), %rax      # 0x15fd8
movq (%rax), %rcx
leaq 0x71b5(%rip), %rdi      # 0x11277
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x3400
pushq $0x1
popq %rdi
callq 0x3240
nop