// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/hmm.c
// Function: add_state
void add_state(Hmmptr hmm, int count, Instanceptr traindata)
{
 /*!Last Changed 06.04.2007*/
 int i, j, componentcount;
 double* pi, randvalue;
 hmm->states = alloc(hmm->states, (hmm->statecount + count) * sizeof(State), hmm->statecount + count);
 /*Reshuffle pi's*/
 pi = safemalloc(hmm->statecount * sizeof(double), "add_state", 5);
 for (i = 0; i < hmm->statecount; i++)
   pi[i] = hmm->states[i].pi;
 pi = add_probability_conserving(pi, hmm->statecount, count);
 for (i = 0; i < hmm->statecount + count; i++)
   hmm->states[i].pi = pi[i];   
 safe_free(pi);
 /*Reshuffle a_i_dot*/
 switch (hmm->hmmtype)
  {
   case    FULL_MODEL:for (i = 0; i < hmm->statecount; i++)
                        hmm->states[i].a_i_dot = add_probability_conserving(hmm->states[i].a_i_dot, hmm->statecount, count);
                      for (i = hmm->statecount; i < hmm->statecount + count; i++)
                        hmm->states[i].a_i_dot = random_normalized_array(hmm->statecount + count);
                      break;
   case      LR_MODEL:
   case LR_LOOP_MODEL:for (i = 0; i < hmm->statecount; i++)
                       {
                        hmm->states[i].a_i_dot = alloc(hmm->states[i].a_i_dot, (hmm->statecount + count) * sizeof(double), hmm->statecount + count);
                        for (j = hmm->statecount; j < hmm->statecount + count; j++)
                          hmm->states[i].a_i_dot[j] = 0.0;
                       }
                      for (i = hmm->statecount; i < hmm->statecount + count; i++)
                        hmm->states[i].a_i_dot = safecalloc(hmm->statecount + count, sizeof(double), "add_state", 27);
                      for (i = hmm->statecount - 1; i < hmm->statecount + count - 1; i++)
                       {
                        randvalue = produce_random_value(0.0, 1.0);
                        hmm->states[i].a_i_dot[i] = randvalue;
                        hmm->states[i].a_i_dot[i + 1] = 1 - randvalue;
                       }
                      if (hmm->hmmtype == LR_MODEL)
                        hmm->states[hmm->statecount + count - 1].a_i_dot[hmm->statecount + count - 1] = 1.0;
                      else
                       {
                        hmm->states[hmm->statecount - 1].a_i_dot[0] = 0.0;
                        randvalue = produce_random_value(0.0, 1.0);
                        hmm->states[hmm->statecount + count - 1].a_i_dot[hmm->statecount + count - 1] = randvalue;
                        hmm->states[hmm->statecount + count - 1].a_i_dot[0] = 1 - randvalue;
                       }
                      break;
  }
 /*Reshuffle a_dot_j*/
 for (i = 0; i < hmm->statecount; i++)
   hmm->states[i].a_dot_j = alloc(hmm->states[i].a_dot_j, (hmm->statecount + count) * sizeof(double), hmm->statecount + count);
 for (i = hmm->statecount; i < hmm->statecount + count; i++)
   hmm->states[i].a_dot_j = safemalloc((hmm->statecount + count) * sizeof(double), "add_state", 49);
 for (i = 0; i < hmm->statecount + count; i++)
   for (j = 0; j < hmm->statecount + count; j++)
     hmm->states[j].a_dot_j[i] = hmm->states[i].a_i_dot[j];
 /*Add b's*/
 for (i = hmm->statecount; i < hmm->statecount + count; i++)
  {
   componentcount = get_component_count(hmm->states[0], hmm->statetype);
   hmm->states[i].b = random_state_model(hmm->statetype, componentcount, traindata);
  }
 /*Update statecount*/
 hmm->statecount += count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, %r14
movq (%rdi), %rdi
movslq 0x8(%r14), %rdx
movslq %esi, %r12
addq %r12, %rdx
movq %rdx, %rsi
shlq $0x5, %rsi
callq 0x34f30
movq %rax, (%r14)
movslq 0x8(%r14), %rdi
shlq $0x3, %rdi
leaq 0x725cf(%rip), %rsi     # 0x98547
pushq $0x5
popq %rdx
callq 0x34f59
movl 0x8(%r14), %esi
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
shlq $0x3, %rdx
cmpq %rcx, %rdx
je 0x25fad
movq (%r14), %rdi
movsd 0x10(%rdi,%rcx,4), %xmm0
movsd %xmm0, (%rax,%rcx)
addq $0x8, %rcx
jmp 0x25f94
movq %rax, %rdi
movl %r15d, %edx
callq 0x25c60
movl 0x8(%r14), %ecx
addl %r15d, %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
shlq $0x3, %rcx
cmpq %rdx, %rcx
je 0x25fe3
movsd (%rax,%rdx), %xmm0
movq (%r14), %rsi
movsd %xmm0, 0x10(%rsi,%rdx,4)
addq $0x8, %rdx
jmp 0x25fca
movq %rax, %rdi
callq 0x34ff7
movl 0x10(%r14), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
movq %rbx, 0x10(%rsp)
jae 0x26060
movq %r12, 0x8(%rsp)
movl 0x8(%r14), %ecx
xorl %r12d, %r12d
movslq %ecx, %rbx
cmpq %rbx, %r12
jge 0x2609e
movq (%r14), %rax
movq %r12, %rbx
shlq $0x5, %rbx
movq 0x8(%rax,%rbx), %rdi
addl %r15d, %ecx
movslq %ecx, %rdx
leaq (,%rdx,8), %rsi
callq 0x34f30
movq (%r14), %rcx
movq %rax, 0x8(%rcx,%rbx)
movl 0x8(%r14), %ecx
leal (%rcx,%r15), %esi
movslq %ecx, %rdx
movslq %esi, %rsi
cmpq %rsi, %rdx
jge 0x2605b
andq $0x0, (%rax,%rdx,8)
incq %rdx
jmp 0x2604c
incq %r12
jmp 0x26008
testl %eax, %eax
jne 0x261ed
pushq $0x8
popq %r12
xorl %r13d, %r13d
movl 0x8(%r14), %esi
movslq %esi, %rbx
cmpq %rbx, %r13
jge 0x26166
movq (%r14), %rax
movq (%rax,%r12), %rdi
movl %r15d, %edx
callq 0x25c60
movq (%r14), %rcx
movq %rax, (%rcx,%r12)
incq %r13
addq $0x20, %r12
jmp 0x2606f
movslq %ecx, %r12
shlq $0x5, %r12
orq $0x8, %r12
pushq $0x8
popq %r13
pushq $0x1b
popq %rbp
leal (%rcx,%r15), %eax
movslq %eax, %rdi
cmpq %rdi, %rbx
jge 0x260e1
movq %r13, %rsi
leaq 0x72481(%rip), %rdx     # 0x98547
movl %ebp, %ecx
callq 0x35067
movq (%r14), %rcx
movq %rax, (%rcx,%r12)
incq %rbx
movl 0x8(%r14), %ecx
addq $0x20, %r12
jmp 0x260b0
leal -0x1(%rcx), %eax
leal -0x1(%r15), %ebx
movslq %eax, %r12
leaq (,%r12,8), %r13
leal (%rbx,%rcx), %eax
cltq
cmpq %rax, %r12
jge 0x2613d
xorpd %xmm0, %xmm0
movsd 0x6f417(%rip), %xmm1    # 0x95520
callq 0x35cae
movq (%r14), %rax
movq 0x8(%rax,%r13,4), %rax
movsd %xmm0, (%r13,%rax)
movsd 0x6f3fb(%rip), %xmm1    # 0x95520
subsd %xmm0, %xmm1
incq %r12
movsd %xmm1, 0x8(%r13,%rax)
movl 0x8(%r14), %ecx
addq $0x8, %r13
jmp 0x260f3
cmpl $0x1, 0x10(%r14)
movq (%r14), %rdx
jne 0x26197
movq %rax, %rcx
shlq $0x5, %rcx
movq 0x8(%rdx,%rcx), %rcx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rcx,%rax,8)
jmp 0x261ed
movq %rbx, %r12
shlq $0x5, %r12
orq $0x8, %r12
addl %r15d, %esi
movslq %esi, %rax
cmpq %rax, %rbx
jge 0x261ed
movl %esi, %edi
callq 0x35e36
movq (%r14), %rcx
movq %rax, (%rcx,%r12)
incq %rbx
movl 0x8(%r14), %esi
addq $0x20, %r12
jmp 0x26171
movslq %ecx, %rax
shlq $0x5, %rax
movq -0x18(%rdx,%rax), %rax
andq $0x0, (%rax)
xorpd %xmm0, %xmm0
movsd 0x6f36d(%rip), %xmm1    # 0x95520
callq 0x35cae
movq (%r14), %rax
movslq 0x8(%r14), %rcx
movq 0x8(%rsp), %rsi
leaq (%rcx,%rsi), %rdx
addq %rsi, %rcx
decq %rcx
shlq $0x5, %rcx
movq 0x8(%rax,%rcx), %rax
movsd %xmm0, -0x8(%rax,%rdx,8)
movsd 0x6f33b(%rip), %xmm1    # 0x95520
subsd %xmm0, %xmm1
movsd %xmm1, (%rax)
xorl %r12d, %r12d
xorl %r13d, %r13d
movl 0x8(%r14), %eax
movslq %eax, %rbx
cmpq %rbx, %r13
jge 0x26229
movq (%r14), %rcx
movq (%rcx,%r12), %rdi
addl %r15d, %eax
movslq %eax, %rdx
leaq (,%rdx,8), %rsi
callq 0x34f30
movq (%r14), %rcx
movq %rax, (%rcx,%r12)
incq %r13
addq $0x20, %r12
jmp 0x261f3
movq %rbx, %r13
shlq $0x5, %r13
leaq 0x72310(%rip), %r12     # 0x98547
pushq $0x31
popq %rbp
leal (%rax,%r15), %esi
movslq %esi, %rdi
cmpq %rdi, %rbx
jge 0x26268
shlq $0x3, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x34f59
movq (%r14), %rcx
movq %rax, (%rcx,%r13)
incq %rbx
movl 0x8(%r14), %eax
addq $0x20, %r13
jmp 0x2623a
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
movl %esi, %esi
shlq $0x3, %rsi
movq 0x10(%rsp), %rbx
cmpq %rdx, %rcx
je 0x262b6
movq %rcx, %rdi
shlq $0x5, %rdi
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x262b1
movq (%r14), %r9
movq 0x8(%r9,%rdi), %r10
movsd (%r10,%r8), %xmm0
movq (%r9,%r8,4), %r9
movsd %xmm0, (%r9,%rcx,8)
addq $0x8, %r8
jmp 0x2628e
incq %rcx
jmp 0x2627f
movslq %eax, %r12
movq %r12, %r13
shlq $0x5, %r13
orq $0x18, %r13
addl %r15d, %eax
movslq %eax, %rcx
cmpq %rcx, %r12
jge 0x26305
movl 0xc(%r14), %edi
cmpl $0x4, %edi
je 0x262df
xorl %esi, %esi
cmpl $0x2, %edi
jne 0x262e9
movq (%r14), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %esi
movq %rbx, %rdx
callq 0x24864
movq (%r14), %rcx
movq %rax, (%rcx,%r13)
incq %r12
movl 0x8(%r14), %eax
addq $0x20, %r13
jmp 0x262c4
movl %eax, 0x8(%r14)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq