// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %rdx, %rbp
movl %esi, %r13d
movq %rdi, %rbx
movq (%rdi), %rdi
movslq 0x8(%rbx), %rdx
movslq %esi, %r14
addq %r14, %rdx
movq %rdx, %rsi
shlq $0x5, %rsi
callq 0x3a0b4
movq %rax, (%rbx)
movslq 0x8(%rbx), %rdi
shlq $0x3, %rdi
leaq 0x7edf3(%rip), %rsi     # 0xa85c7
movl $0x5, %edx
callq 0x3a0e1
movslq 0x8(%rbx), %rsi
testq %rsi, %rsi
jle 0x29808
movq (%rbx), %rcx
leaq (,%rsi,8), %rdx
xorl %edi, %edi
movsd 0x10(%rcx,%rdi,4), %xmm0
movsd %xmm0, (%rax,%rdi)
addq $0x8, %rdi
cmpq %rdi, %rdx
jne 0x297f4
movq %rax, %rdi
movl %r13d, %edx
callq 0x293c7
movl 0x8(%rbx), %edx
addl %r13d, %edx
jle 0x2983b
movq (%rbx), %rcx
movl %edx, %edx
addq $0x10, %rcx
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%rcx)
incq %rsi
addq $0x20, %rcx
cmpq %rsi, %rdx
jne 0x29826
movq %rax, %rdi
callq 0x3a180
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
movq %r14, 0x18(%rsp)
movq %r13, 0x8(%rsp)
movq %rbp, 0x20(%rsp)
jae 0x298f1
movl 0x8(%rbx), %r12d
testl %r12d, %r12d
movq %rbx, 0x10(%rsp)
jle 0x29982
movq (%rbx), %rdx
movl $0x8, %r13d
xorl %ebp, %ebp
movq (%rdx,%r13), %rdi
movq 0x8(%rsp), %rbx
addl %ebx, %r12d
movslq %r12d, %rdx
leaq (,%rdx,8), %rsi
callq 0x3a0b4
movq 0x10(%rsp), %rcx
movq (%rcx), %rdx
movq %rax, (%rdx,%r13)
movl 0x8(%rcx), %r12d
leal (%r12,%rbx), %r14d
movslq %r12d, %r15
testl %ebx, %ebx
jle 0x298e0
leaq (%rax,%r15,8), %rdi
leal 0x1(%r15), %eax
cmpl %eax, %r14d
cmovgl %r14d, %eax
movl %r15d, %ecx
notl %ecx
addl %eax, %ecx
movq %rdx, %rbx
leaq 0x8(,%rcx,8), %rdx
xorl %esi, %esi
callq 0x4050
movq %rbx, %rdx
incq %rbp
addq $0x20, %r13
cmpq %r15, %rbp
jl 0x2987e
jmp 0x29986
testl %eax, %eax
jne 0x29979
movl 0x8(%rbx), %r12d
testl %r12d, %r12d
jle 0x29934
movq (%rbx), %rcx
movl $0x8, %r14d
xorl %r15d, %r15d
movq (%rcx,%r14), %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x293c7
movq (%rbx), %rcx
movq %rax, (%rcx,%r14)
incq %r15
movslq 0x8(%rbx), %r12
addq $0x20, %r14
cmpq %r12, %r15
jl 0x2990e
testl %r13d, %r13d
jle 0x29ad3
movslq %r12d, %r14
addl %r13d, %r12d
movq %r14, %r15
shlq $0x5, %r15
orq $0x8, %r15
movl %r12d, %edi
callq 0x3b25f
movq (%rbx), %rcx
movq %rax, (%rcx,%r15)
incq %r14
movl 0x8(%rbx), %r12d
leal (%r12,%r13), %edi
movslq %edi, %rax
addq $0x20, %r15
cmpq %rax, %r14
jl 0x29951
jmp 0x29ad3
movl 0x8(%rbx), %r12d
jmp 0x29ad3
leal (%r12,%r13), %r14d
cmpl %r14d, %r12d
jge 0x299dc
movslq %r12d, %r15
movq %r15, %rbp
shlq $0x5, %rbp
orq $0x8, %rbp
movq 0x8(%rsp), %r13
movq 0x10(%rsp), %rbx
movslq %r14d, %rdi
movl $0x8, %esi
leaq 0x7ec15(%rip), %rdx     # 0xa85c7
movl $0x1b, %ecx
callq 0x3a1fc
movq (%rbx), %rcx
movq %rax, (%rcx,%rbp)
incq %r15
movl 0x8(%rbx), %r12d
leal (%r12,%r13), %r14d
movslq %r14d, %rax
addq $0x20, %rbp
cmpq %rax, %r15
jl 0x299a3
jmp 0x299e6
movq 0x8(%rsp), %r13
movq 0x10(%rsp), %rbx
leal (%r12,%r13), %eax
decl %eax
cmpl %eax, %r12d
jg 0x29a4d
leal -0x1(%r13), %r14d
decl %r12d
movslq %r12d, %r15
leaq (,%r15,8), %r13
xorpd %xmm0, %xmm0
movsd 0x7bb11(%rip), %xmm1    # 0xa5520
callq 0x3af6a
movq (%rbx), %rax
movq 0x8(%rax,%r13,4), %rax
movsd %xmm0, (%r13,%rax)
movsd 0x7baf5(%rip), %xmm1    # 0xa5520
subsd %xmm0, %xmm1
incq %r15
movsd %xmm1, 0x8(%r13,%rax)
movl 0x8(%rbx), %r12d
leal (%r14,%r12), %eax
movslq %eax, %rcx
addq $0x8, %r13
cmpq %rcx, %r15
jl 0x29a03
cmpl $0x1, 0x10(%rbx)
movq (%rbx), %rcx
jne 0x29a79
cltq
movq %rax, %rdx
shlq $0x5, %rdx
movq 0x8(%rcx,%rdx), %rcx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rcx,%rax,8)
movq 0x8(%rsp), %r13
jmp 0x29ad3
movslq %r12d, %rax
shlq $0x5, %rax
movq -0x18(%rcx,%rax), %rax
movq $0x0, (%rax)
movsd 0x7ba8c(%rip), %xmm1    # 0xa5520
xorpd %xmm0, %xmm0
callq 0x3af6a
movq (%rbx), %rax
movl 0x8(%rbx), %r12d
movq 0x8(%rsp), %r13
leal (%r12,%r13), %ecx
movslq %ecx, %rcx
leaq -0x1(%rcx), %rdx
shlq $0x5, %rdx
movq 0x8(%rax,%rdx), %rax
movsd %xmm0, -0x8(%rax,%rcx,8)
movsd 0x7ba55(%rip), %xmm1    # 0xa5520
subsd %xmm0, %xmm1
movsd %xmm1, (%rax)
testl %r12d, %r12d
jle 0x29b0f
movq (%rbx), %rcx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%rcx,%r14), %rdi
addl %r13d, %r12d
movslq %r12d, %rdx
leaq (,%rdx,8), %rsi
callq 0x3a0b4
movq (%rbx), %rcx
movq %rax, (%rcx,%r14)
incq %r15
movslq 0x8(%rbx), %r12
addq $0x20, %r14
cmpq %r12, %r15
jl 0x29ae1
leal (%r12,%r13), %eax
testl %r13d, %r13d
jle 0x29b60
movslq %r12d, %r14
movq %r14, %r15
shlq $0x5, %r15
leaq 0x7ea9e(%rip), %r13     # 0xa85c7
movq 0x8(%rsp), %rbp
movslq %eax, %rdi
shlq $0x3, %rdi
movq %r13, %rsi
movl $0x31, %edx
callq 0x3a0e1
movq (%rbx), %rcx
movq %rax, (%rcx,%r15)
incq %r14
movl 0x8(%rbx), %r12d
leal (%r12,%rbp), %eax
movslq %eax, %rcx
addq $0x20, %r15
cmpq %rcx, %r14
jl 0x29b2e
testl %eax, %eax
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %rbp
jle 0x29bad
movl %eax, %eax
movq (%rbx), %rcx
leaq (,%rax,8), %rdx
xorl %esi, %esi
movq %rsi, %rdi
shlq $0x5, %rdi
movq 0x8(%rcx,%rdi), %rdi
xorl %r8d, %r8d
movsd (%rdi,%r8), %xmm0
movq (%rcx,%r8,4), %r9
movsd %xmm0, (%r9,%rsi,8)
addq $0x8, %r8
cmpq %r8, %rdx
jne 0x29b8c
incq %rsi
cmpq %rax, %rsi
jne 0x29b7d
movq 0x8(%rsp), %rax
testl %eax, %eax
jle 0x29c01
movslq %r12d, %r14
movq %r14, %r15
shlq $0x5, %r15
orq $0x18, %r15
movl 0xc(%rbx), %edi
cmpl $0x4, %edi
je 0x29bd3
xorl %esi, %esi
cmpl $0x2, %edi
jne 0x29bdd
movq (%rbx), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %esi
movq %rbp, %rdx
callq 0x27d4b
movq (%rbx), %rcx
movq %rax, (%rcx,%r15)
incq %r14
movslq 0x8(%rbx), %r12
addq %r13, %r12
addq $0x20, %r15
cmpq %r12, %r14
jl 0x29bc4
jmp 0x29c04
addl %eax, %r12d
movl %r12d, 0x8(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq