// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rdx, %r14
movl %esi, %r15d
movq %rdi, %rbx
movq (%rdi), %rdi
movslq 0x8(%rbx), %rdx
movslq %esi, %r13
addq %r13, %rdx
movq %rdx, %rsi
shlq $0x5, %rsi
callq 0x390c8
movq %rax, (%rbx)
movslq 0x8(%rbx), %rdi
shlq $0x3, %rdi
leaq 0x7c532(%rip), %rsi     # 0xa556f
movl $0x5, %edx
callq 0x390f5
movslq 0x8(%rbx), %rsi
testq %rsi, %rsi
jle 0x29071
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 0x2905d
movq %rax, %rdi
movl %r15d, %edx
callq 0x28cda
movl 0x8(%rbx), %edx
addl %r15d, %edx
jle 0x290a4
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 0x2908f
movq %rax, %rdi
callq 0x39194
movl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
movq %r14, (%rsp)
jae 0x29211
movl 0x8(%rbx), %eax
testl %eax, %eax
jle 0x2912b
movl $0x8, %r12d
xorl %ebp, %ebp
movq (%rbx), %rcx
movq (%rcx,%r12), %rdi
addl %r15d, %eax
movslq %eax, %rdx
leaq (,%rdx,8), %rsi
callq 0x390c8
movq (%rbx), %rcx
movq %rax, (%rcx,%r12)
testl %r15d, %r15d
jle 0x2911b
movslq 0x8(%rbx), %rcx
leal (%rcx,%r15), %edx
leal 0x1(%rcx), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
leaq (%rax,%rcx,8), %rdi
notl %ecx
addl %esi, %ecx
leaq 0x8(,%rcx,8), %rdx
xorl %esi, %esi
callq 0x4050
incq %rbp
movslq 0x8(%rbx), %rax
addq $0x20, %r12
cmpq %rax, %rbp
jl 0x290ce
movl 0x8(%rbx), %ecx
leal (%rcx,%r15), %edx
cmpl %edx, %eax
jge 0x2917d
movslq %eax, %r14
movq %r14, %rbp
shlq $0x5, %rbp
orq $0x8, %rbp
leaq 0x7c424(%rip), %r12     # 0xa556f
movslq %edx, %rdi
movl $0x8, %esi
movq %r12, %rdx
movl $0x1b, %ecx
callq 0x39210
movq (%rbx), %rcx
movq %rax, (%rcx,%rbp)
incq %r14
movl 0x8(%rbx), %ecx
leal (%rcx,%r15), %edx
movslq %edx, %rax
addq $0x20, %rbp
cmpq %rax, %r14
jl 0x2914b
movl 0x8(%rbx), %eax
leal (%r15,%rax), %edx
decl %edx
cmpl %edx, %ecx
jg 0x291e2
leal -0x1(%r15), %r14d
decl %ecx
movslq %ecx, %r12
leaq (,%r12,8), %rbp
xorpd %xmm0, %xmm0
movsd 0x79379(%rip), %xmm1    # 0xa2520
callq 0x39f62
movq (%rbx), %rax
movq 0x8(%rax,%rbp,4), %rax
movsd %xmm0, (%rbp,%rax)
movsd 0x7935e(%rip), %xmm1    # 0xa2520
subsd %xmm0, %xmm1
incq %r12
movsd %xmm1, 0x8(%rbp,%rax)
movl 0x8(%rbx), %eax
leal (%r14,%rax), %edx
movslq %edx, %rcx
addq $0x8, %rbp
cmpq %rcx, %r12
jl 0x2919b
cmpl $0x1, 0x10(%rbx)
movq (%rbx), %rcx
jne 0x2928c
movslq %edx, %rax
movq %rax, %rdx
shlq $0x5, %rdx
movq 0x8(%rcx,%rdx), %rcx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rcx,%rax,8)
jmp 0x292eb
testl %eax, %eax
jne 0x292eb
movl 0x8(%rbx), %esi
testl %esi, %esi
jle 0x2924f
movl $0x8, %r14d
xorl %r12d, %r12d
movq (%rbx), %rax
movq (%rax,%r14), %rdi
movl %r15d, %edx
callq 0x28cda
movq (%rbx), %rcx
movq %rax, (%rcx,%r14)
incq %r12
movslq 0x8(%rbx), %rsi
addq $0x20, %r14
cmpq %rsi, %r12
jl 0x29229
movl 0x8(%rbx), %edi
addl %r15d, %edi
cmpl %edi, %esi
jge 0x292eb
movslq %esi, %r14
movq %r14, %r12
shlq $0x5, %r12
orq $0x8, %r12
callq 0x3a1d2
movq (%rbx), %rcx
movq %rax, (%rcx,%r12)
incq %r14
movslq 0x8(%rbx), %rdi
addq %r13, %rdi
addq $0x20, %r12
cmpq %rdi, %r14
jl 0x2926b
jmp 0x292eb
cltq
shlq $0x5, %rax
movq -0x18(%rcx,%rax), %rax
movq $0x0, (%rax)
movsd 0x7927a(%rip), %xmm1    # 0xa2520
xorpd %xmm0, %xmm0
callq 0x39f62
movq (%rbx), %rax
movslq 0x8(%rbx), %rcx
leaq (%rcx,%r13), %rdx
decq %rdx
addq %r13, %rcx
shlq $0x5, %rdx
movq 0x8(%rax,%rdx), %rdx
movsd %xmm0, -0x8(%rdx,%rcx,8)
movsd 0x79249(%rip), %xmm1    # 0xa2520
subsd %xmm0, %xmm1
movslq %ecx, %rcx
shlq $0x5, %rcx
movq -0x18(%rax,%rcx), %rax
movsd %xmm1, (%rax)
movl 0x8(%rbx), %ecx
testl %ecx, %ecx
jle 0x29329
xorl %r14d, %r14d
xorl %r12d, %r12d
movq (%rbx), %rax
movq (%rax,%r14), %rdi
addl %r15d, %ecx
movslq %ecx, %rdx
leaq (,%rdx,8), %rsi
callq 0x390c8
movq (%rbx), %rcx
movq %rax, (%rcx,%r14)
incq %r12
movslq 0x8(%rbx), %rcx
addq $0x20, %r14
cmpq %rcx, %r12
jl 0x292f8
movl 0x8(%rbx), %eax
leal (%rax,%r15), %edx
cmpl %edx, %ecx
jge 0x29376
movslq %ecx, %r14
movq %r14, %rbp
shlq $0x5, %rbp
leaq 0x7c22a(%rip), %r12     # 0xa556f
movslq %edx, %rdi
shlq $0x3, %rdi
movq %r12, %rsi
movl $0x31, %edx
callq 0x390f5
movq (%rbx), %rcx
movq %rax, (%rcx,%rbp)
incq %r14
movl 0x8(%rbx), %eax
leal (%rax,%r15), %edx
movslq %edx, %rcx
addq $0x20, %rbp
cmpq %rcx, %r14
jl 0x29345
testl %edx, %edx
movq (%rsp), %r14
jle 0x293bd
movl %edx, %ecx
movq (%rbx), %rdx
leaq (,%rcx,8), %rsi
xorl %edi, %edi
movq %rdi, %r8
shlq $0x5, %r8
movq 0x8(%rdx,%r8), %r8
xorl %r9d, %r9d
movsd (%r8,%r9), %xmm0
movq (%rdx,%r9,4), %r10
movsd %xmm0, (%r10,%rdi,8)
addq $0x8, %r9
cmpq %r9, %rsi
jne 0x2939c
incq %rdi
cmpq %rcx, %rdi
jne 0x2938d
addl 0x8(%rbx), %r15d
cmpl %r15d, %eax
jge 0x2940f
movslq %eax, %r12
movq %r12, %rbp
shlq $0x5, %rbp
orq $0x18, %rbp
movl 0xc(%rbx), %edi
cmpl $0x4, %edi
je 0x293e3
xorl %esi, %esi
cmpl $0x2, %edi
jne 0x293ed
movq (%rbx), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %esi
movq %r14, %rdx
callq 0x2763c
movq (%rbx), %rcx
movq %rax, (%rcx,%rbp)
incq %r12
movslq 0x8(%rbx), %r15
addq %r13, %r15
addq $0x20, %rbp
cmpq %r15, %r12
jl 0x293d4
movl %r15d, 0x8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq