// File: /workspace/github_cpp_makefile_star2_O2/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
testl %ecx, %ecx
je 0x10e13
movq %rsi, %rbx
movl %edx, 0xc(%rsp)
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
xorl %edi, %edi
xorl %esi, %esi
movsd 0x1bce8(%rip), %xmm1    # 0x2c8f0
cmpq %rcx, %rdi
je 0x10c6e
movq 0x8d0(%rax), %r8
imulq $0x60, %rdi, %r9
cmpl $0x0, 0x4(%r8,%r9)
je 0x10c25
incq %rdi
jmp 0x10c08
addq %r9, %r8
movl (%r8), %r9d
movq 0x18(%r14), %r10
testl %r9d, %r9d
cmovlel %edx, %r9d
xorl %r11d, %r11d
cmpq %r11, %r9
je 0x10c20
movq 0x870(%rax), %r15
movq 0x10(%r8), %r12
movslq (%r12,%r11,4), %r12
movsbq (%r15,%r12), %r15
testq %r15, %r15
js 0x10c69
movsd (%r10,%r15,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%r10,%r15,8)
incl %esi
incq %r11
jmp 0x10c39
xorpd %xmm0, %xmm0
movupd %xmm0, 0x28(%r14)
cvtsi2sd %esi, %xmm1
movzbl 0x8b9(%rax), %edx
movq 0x18(%r14), %rax
xorl %ecx, %ecx
testb %dl, %dl
cmovlel %ecx, %edx
movzbl %dl, %edx
xorpd %xmm0, %xmm0
cmpq %rcx, %rdx
je 0x10cd0
movsd (%rax,%rcx,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rax,%rcx,8)
movsd 0x30(%r14), %xmm0
ucomisd %xmm0, %xmm2
jbe 0x10ccb
movsd %xmm2, 0x30(%r14)
movsbl %cl, %esi
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x28(%r14)
movapd %xmm2, %xmm0
incq %rcx
jmp 0x10c95
callq 0x67a5
movsd %xmm0, 0x30(%r14)
xorl %r15d, %r15d
xorl %eax, %eax
movq 0x88(%r14), %rcx
movslq 0x8d8(%rcx), %rdx
cmpq %rdx, %r15
jge 0x10def
movq 0x8d0(%rcx), %r12
imulq $0x60, %r15, %rcx
cmpl $0x0, 0x4(%r12,%rcx)
je 0x10d0f
incq %r15
jmp 0x10ce0
addq %rcx, %r12
xorl %r13d, %r13d
movslq (%r12), %rbp
cmpq %rbp, %r13
jge 0x10d0a
addl %eax, %ebp
movq 0x88(%r14), %rax
movq 0x870(%rax), %rax
movq 0x10(%r12), %rcx
movslq (%rcx,%r13,4), %rcx
movsbl (%rax,%rcx), %eax
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x28(%r14), %xmm2
movapd %xmm2, 0x10(%rsp)
movsd 0x30(%r14), %xmm0
cmpeqsd %xmm1, %xmm2
movsd 0x1bb8c(%rip), %xmm1    # 0x2c8f0
andpd %xmm1, %xmm2
movapd %xmm2, 0x30(%rsp)
callq 0x67ff
movsd %xmm0, 0x28(%rsp)
movapd 0x10(%rsp), %xmm0
cmpneqsd 0x20(%rsp), %xmm0
movsd 0x1bb62(%rip), %xmm1    # 0x2c8f0
andpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm1, %xmm0
subsd 0x30(%r14), %xmm0
callq 0x67ff
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 %r13
movl %ebp, %eax
jmp 0x10d15
cmpb $0x0, 0xc(%rsp)
jne 0x10e30
movsd 0x8(%rbx), %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x10e26
sqrtsd %xmm0, %xmm0
jmp 0x10e2b
movq 0x18(%r14), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
jmp 0x10e30
callq 0x41a0
movsd %xmm0, 0x8(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop