// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::FitNullSkill(double*, bool)
void HMMProblem::FitNullSkill(NUMBER* loglik_rmse, bool keep_SE) {
    if(this->p->n_null_skill_group==0) {
        this->null_obs_ratio[0] = 1; // set first obs to 1, simplex preserved
        return; // 0 loglik
    }
    NDAT count_all_null_skill = 0;
    struct data *dat; // used as pointer
    // count occurrences
    NCAT g;
    NDAT t;
    NPAR isTarget, o, m;
    for(g=0; g<this->p->n_null_skill_group; g++) {
        dat = &this->p->null_skills[g];
        if(dat->cnt != 0)
            continue; // observe block
//        count_all_null_skill += dat->n; // does not work for item- and non- stratified cross-validations
        for(t=0; t<dat->n; t++) {
            o = this->p->dat_obs[ dat->ix[t] ];//->get( dat->ix[t] );
            if(((int)o)>=0) { // -1 we skip \xff in char notation
                this->null_obs_ratio[ o ]++;
                count_all_null_skill++; // crude, but works properly for all cross validations
            }
        }
    }
    // produce means
    this->null_skill_obs = 0;
    this->null_skill_obs_prob = 0;
    for(m=0; m<this->p->nO; m++) {
        this->null_obs_ratio[m] /= count_all_null_skill;
        if( this->null_obs_ratio[m] > this->null_skill_obs_prob ) {
            this->null_skill_obs_prob = this->null_obs_ratio[m];
            this->null_skill_obs = m;
        }
    }
    this->null_skill_obs_prob = safe01num(this->null_skill_obs_prob); // safety for logging
    // compute loglik
    NDAT N = 0;
    for(g=0; g<this->p->n_null_skill_group; g++) {
        dat = &this->p->null_skills[g];
        if(dat->cnt != 0)
            continue; // observe block
        for(t=0; t<dat->n; t++) {
            N += dat->n;
            isTarget = this->p->dat_obs[ dat->ix[t] ]/*->get( dat->ix[t] )*/ == this->null_skill_obs;
            loglik_rmse[0] -= isTarget*safelog(this->null_skill_obs_prob) + (1-isTarget)*safelog(1-this->null_skill_obs_prob);
            loglik_rmse[1] += pow(isTarget - this->null_skill_obs_prob, 2);
        }
    }
    if(!keep_SE) loglik_rmse[1] = sqrt(loglik_rmse[1]/N);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
movq 0x88(%rdi), %rax
movl 0x8d8(%rax), %ecx
testq %rcx, %rcx
je 0x144d9
movl %edx, %ebp
movq %rsi, %rbx
testl %ecx, %ecx
jle 0x144ef
movq 0x8d0(%rax), %rdx
xorl %esi, %esi
movsd 0x2047f(%rip), %xmm0    # 0x348f0
xorl %edi, %edi
leaq (%rsi,%rsi,2), %r10
shlq $0x5, %r10
cmpl $0x0, 0x4(%rdx,%r10)
jne 0x144c8
addq %rdx, %r10
movslq (%r10), %r8
testq %r8, %r8
jle 0x144c8
movq 0x870(%rax), %r9
movq 0x10(%r10), %r10
movq 0x18(%r14), %r11
xorl %r15d, %r15d
movslq (%r10,%r15,4), %r12
movsbq (%r9,%r12), %r12
testq %r12, %r12
js 0x144c0
movsd (%r11,%r12,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%r11,%r12,8)
incl %edi
incq %r15
cmpq %r15, %r8
jne 0x144a0
incq %rsi
cmpq %rcx, %rsi
jne 0x14473
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
jmp 0x144f3
movq 0x18(%r14), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
jmp 0x146cd
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movupd %xmm1, 0x28(%r14)
movsbq 0x8b9(%rax), %rax
testq %rax, %rax
jle 0x14544
movq 0x18(%r14), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rcx,%rdx,8)
ucomisd 0x30(%r14), %xmm1
jbe 0x1453c
movsd %xmm1, 0x30(%r14)
movsbl %dl, %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movsd %xmm1, 0x28(%r14)
incq %rdx
cmpq %rdx, %rax
jne 0x14510
movsd 0x30(%r14), %xmm0
callq 0x8d62
movsd %xmm0, 0x30(%r14)
movq 0x88(%r14), %rax
cmpl $0x0, 0x8d8(%rax)
jle 0x146a1
movl %ebp, 0xc(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8d0(%rax), %r13
leaq (%r15,%r15,2), %rax
shlq $0x5, %rax
cmpl $0x0, 0x4(%r13,%rax)
jne 0x14679
addq %rax, %r13
movl (%r13), %eax
testl %eax, %eax
jle 0x14679
xorl %ebp, %ebp
addl %eax, %r12d
movq 0x88(%r14), %rax
movq 0x870(%rax), %rax
movq 0x10(%r13), %rcx
movslq (%rcx,%rbp,4), %rcx
movsbl (%rax,%rcx), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x28(%r14), %xmm2
movapd %xmm2, 0x10(%rsp)
movsd 0x30(%r14), %xmm0
cmpeqsd %xmm1, %xmm2
movsd 0x20308(%rip), %xmm1    # 0x348f0
andpd %xmm1, %xmm2
movapd %xmm2, 0x30(%rsp)
callq 0x8dcd
movsd %xmm0, 0x28(%rsp)
movapd 0x10(%rsp), %xmm0
cmpneqsd 0x20(%rsp), %xmm0
movsd 0x202de(%rip), %xmm1    # 0x348f0
andpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm1, %xmm0
subsd 0x30(%r14), %xmm0
callq 0x8dcd
mulsd 0x10(%rsp), %xmm0
movapd 0x30(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd (%rbx), %xmm0
subsd %xmm2, %xmm0
movsd %xmm0, (%rbx)
movsd 0x30(%r14), %xmm0
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
addsd 0x8(%rbx), %xmm1
movsd %xmm1, 0x8(%rbx)
incq %rbp
movslq (%r13), %rax
cmpq %rax, %rbp
jl 0x1459f
incq %r15
movq 0x88(%r14), %rax
movslq 0x8d8(%rax), %rcx
cmpq %rcx, %r15
jl 0x14573
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movl 0xc(%rsp), %ebp
jmp 0x146a5
xorpd %xmm1, %xmm1
testb %bpl, %bpl
jne 0x146cd
movsd 0x8(%rbx), %xmm0
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x146c3
sqrtsd %xmm0, %xmm0
jmp 0x146c8
callq 0x41a0
movsd %xmm0, 0x8(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq