// File: /workspace/github_cpp_makefile_star2_O3/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 0x14501
movl %edx, %ebp
movq %rsi, %rbx
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
testl %ecx, %ecx
jle 0x14351
movq 0x8d0(%rax), %rdx
xorl %esi, %esi
movsd 0x1f605(%rip), %xmm1    # 0x338f0
xorl %edi, %edi
leaq (%rsi,%rsi,2), %r10
shlq $0x5, %r10
cmpl $0x0, 0x4(%rdx,%r10)
jne 0x14342
addq %rdx, %r10
movslq (%r10), %r8
testq %r8, %r8
jle 0x14342
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 0x1433a
movsd (%r11,%r12,8), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, (%r11,%r12,8)
incl %edi
incq %r15
cmpq %r15, %r8
jne 0x1431a
incq %rsi
cmpq %rcx, %rsi
jne 0x142ed
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
xorpd %xmm2, %xmm2
movupd %xmm2, 0x28(%r14)
movsbq 0x8b9(%rax), %rax
testq %rax, %rax
jle 0x143aa
movq 0x18(%r14), %rcx
xorl %edx, %edx
movsd (%rcx,%rdx,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rcx,%rdx,8)
movsd 0x30(%r14), %xmm0
ucomisd %xmm0, %xmm2
jbe 0x143a2
movsd %xmm2, 0x30(%r14)
movsbl %dl, %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x28(%r14)
movapd %xmm2, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x1436e
callq 0x8dd2
movsd %xmm0, 0x30(%r14)
movq 0x88(%r14), %rax
cmpl $0x0, 0x8d8(%rax)
jle 0x14514
movl %ebp, 0xc(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8d0(%rax), %r13
leaq (%r15,%r15,2), %rcx
shlq $0x5, %rcx
cmpl $0x0, 0x4(%r13,%rcx)
jne 0x144e0
addq %rcx, %r13
movl (%r13), %ecx
testl %ecx, %ecx
jle 0x144e0
xorl %ebp, %ebp
addl %ecx, %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 0x1f4a8(%rip), %xmm1    # 0x338f0
andpd %xmm1, %xmm2
movapd %xmm2, 0x30(%rsp)
callq 0x8e3d
movsd %xmm0, 0x28(%rsp)
movapd 0x10(%rsp), %xmm0
cmpneqsd 0x20(%rsp), %xmm0
movsd 0x1f47e(%rip), %xmm1    # 0x338f0
andpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm1, %xmm0
subsd 0x30(%r14), %xmm0
callq 0x8e3d
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), %rcx
cmpq %rcx, %rbp
jl 0x143ff
movq 0x88(%r14), %rax
incq %r15
movslq 0x8d8(%rax), %rcx
cmpq %rcx, %r15
jl 0x143d3
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movl 0xc(%rsp), %ebp
jmp 0x14518
movq 0x18(%r14), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
jmp 0x14540
xorpd %xmm1, %xmm1
testb %bpl, %bpl
jne 0x14540
movsd 0x8(%rbx), %xmm0
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x14536
sqrtsd %xmm0, %xmm0
jmp 0x1453b
callq 0x41a0
movsd %xmm0, 0x8(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop