// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/extended/hmm.c
// Function: gt_hmm_decode
void gt_hmm_decode(const GtHMM *hmm,
                unsigned int *state_sequence,
                const unsigned int *emissions,
                unsigned int num_of_emissions)
{
  double **max_probabilities, tmp_prob;
  unsigned int **backtrace, colidx, precolidx, emission;
  int row, column, num_of_rows, num_of_columns, previous_row;

  gt_assert(hmm);
  gt_assert(gt_hmm_is_valid(hmm));
  gt_assert(num_of_emissions);

  /* alloc tables */
  num_of_rows = hmm->num_of_states;
  num_of_columns = num_of_emissions;
  gt_array2dim_malloc(max_probabilities, num_of_rows, 2);
  gt_array2dim_malloc(backtrace, num_of_rows, num_of_columns);

  /* fill DP table */
  for (row = 0; row < num_of_rows; row++) { /* first column */
    if (emissions[0] == GT_WILDCARD)
      emission = hmm->num_of_symbols - 1;
    else
      emission = emissions[0];
    gt_assert(emission < hmm->num_of_symbols);
    max_probabilities[row][0] = hmm->initial_state_prob[row] +
                                hmm->emission_prob[row][emission];
    backtrace[row][0] = row;
  }

  for (column = 1; column < num_of_columns; column++) { /* other columns */
    if (emissions[column] == GT_WILDCARD)
      emission = hmm->num_of_symbols - 1;
    else
      emission = emissions[column];
    gt_assert(emission < hmm->num_of_symbols);
    colidx = column & 1;
    precolidx = (column - 1) & 1;
    for (row = 0; row < num_of_rows; row++) {
      max_probabilities[row][colidx] = max_probabilities[0][precolidx] +
                                     hmm->transition_prob[0][row] +
                                     hmm->emission_prob[row][emission];
      backtrace[row][column] = 0;
      for (previous_row = 1; previous_row < num_of_rows; previous_row++) {
        tmp_prob = max_probabilities[previous_row][precolidx] +
                   hmm->transition_prob[previous_row][row] +
                   hmm->emission_prob[row][emission];
        if (tmp_prob - max_probabilities[row][colidx] > DBL_EPSILON) {
          max_probabilities[row][colidx] = tmp_prob;
          backtrace[row][column] = previous_row;
        }
      }
    }
  }

  /* backtracing, determine end state with maximum probability */
  colidx = (num_of_columns - 1) & 1;
  tmp_prob = max_probabilities[0][colidx];
  state_sequence[num_of_columns - 1] = 0;
  for (row = 1; row < num_of_rows; row++) {
    if (max_probabilities[row][colidx] - tmp_prob > DBL_EPSILON)
      state_sequence[num_of_columns - 1] = row;
  }

  /* backtracing, follow the links */
  for (column = num_of_columns - 2; column >= 0; column--)
    state_sequence[column] = backtrace[state_sequence[column + 1]][column + 1];

  /* free tables */
  gt_array2dim_delete(backtrace);
  gt_array2dim_delete(max_probabilities);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
testq %rdi, %rdi
je 0x158e21
movl %ecx, %ebx
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r15
callq 0x1587b0
testb %al, %al
je 0x158e26
movq %r14, 0x18(%rsp)
movq %rbx, (%rsp)
testl %ebx, %ebx
je 0x158e2b
movq %r15, 0x8(%rsp)
movslq (%r15), %rbx
leaq (,%rbx,8), %r14
leaq 0x233646(%rip), %rbp    # 0x38c15b
movq %r14, %rdi
movq %rbp, %rsi
movl $0x13f, %edx            # imm = 0x13F
callq 0x1092b2
movq %rax, %r15
movq %rbx, %rdi
shlq $0x4, %rdi
movq %rbp, %rsi
movl $0x13f, %edx            # imm = 0x13F
callq 0x1092b2
movl %ebx, %r12d
movq %rax, (%r15)
cmpq $0x2, %rbx
jb 0x158b64
movq (%r15), %rax
addq $0x10, %rax
movl $0x1, %ecx
movq %rax, (%r15,%rcx,8)
incq %rcx
addq $0x10, %rax
cmpq %rcx, %rbx
jne 0x158b54
leaq 0x2335f0(%rip), %rsi    # 0x38c15b
movq %r14, %rdi
movq %rsi, %r14
movl $0x140, %edx            # imm = 0x140
callq 0x1092b2
movq %rax, %rbp
movslq (%rsp), %rdi
movq %rdi, 0x10(%rsp)
imulq %rbx, %rdi
shlq $0x2, %rdi
movq %r14, %rsi
movl $0x140, %edx            # imm = 0x140
callq 0x1092b2
movq %rax, (%rbp)
cmpl $0x2, %r12d
jb 0x158bcf
movq 0x10(%rsp), %rax
leaq (,%rax,4), %rax
movq (%rbp), %rcx
addq %rax, %rcx
movl $0x1, %edx
movq %rcx, (%rbp,%rdx,8)
incq %rdx
addq %rax, %rcx
cmpq %rdx, %rbx
jne 0x158bbf
testl %r12d, %r12d
movq 0x8(%rsp), %rdi
jle 0x158c24
xorl %eax, %eax
movl (%r13), %ecx
cmpl $0xfe, %ecx
jne 0x158bec
movl 0x4(%rdi), %ecx
decl %ecx
cmpl 0x4(%rdi), %ecx
jae 0x158e17
movq 0x8(%rdi), %rdx
movq 0x18(%rdi), %rsi
movsd (%rdx,%rax,8), %xmm0
movq (%rsi,%rax,8), %rdx
movl %ecx, %ecx
addsd (%rdx,%rcx,8), %xmm0
movq (%r15,%rax,8), %rcx
movsd %xmm0, (%rcx)
movq (%rbp,%rax,8), %rcx
movl %eax, (%rcx)
incq %rax
cmpq %rax, %r12
jne 0x158bdb
cmpl $0x2, (%rsp)
jl 0x158d49
movl (%rsp), %eax
movq %rax, 0x20(%rsp)
movl $0x1, %ecx
movsd 0x2334e5(%rip), %xmm0   # 0x38c128
movq %rbx, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movl (%r13,%rcx,4), %eax
cmpl $0xfe, %eax
jne 0x158c5e
movl 0x4(%rdi), %eax
decl %eax
cmpl 0x4(%rdi), %eax
jae 0x158e1c
testl %r12d, %r12d
jle 0x158d2c
movl %ecx, %edx
andl $0x1, %edx
movq %rdx, %rsi
xorq $0x1, %rsi
movq (%r15), %rdi
movq %rdi, 0x40(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rdi), %r8
movq 0x18(%rdi), %rdi
movq %rdi, 0x38(%rsp)
movq (%r8), %r10
movl %eax, %r11d
xorl %eax, %eax
movq 0x40(%rsp), %rdi
movsd (%rdi,%rsi,8), %xmm1
addsd (%r10,%rax,8), %xmm1
movq 0x38(%rsp), %rdi
movq (%rdi,%rax,8), %r14
addsd (%r14,%r11,8), %xmm1
movq (%r15,%rax,8), %r13
movsd %xmm1, (%r13,%rdx,8)
movq %rbp, %r9
movq (%rbp,%rax,8), %rbx
movl $0x0, (%rbx,%rcx,4)
cmpl $0x2, %r12d
jl 0x158d1d
movl $0x1, %ebp
movq (%r15,%rbp,8), %rdi
movsd (%rdi,%rsi,8), %xmm1
movq (%r8,%rbp,8), %rdi
addsd (%rdi,%rax,8), %xmm1
addsd (%r14,%r11,8), %xmm1
movapd %xmm1, %xmm2
subsd (%r13,%rdx,8), %xmm2
ucomisd %xmm0, %xmm2
jbe 0x158d15
movsd %xmm1, (%r13,%rdx,8)
movl %ebp, (%rbx,%rcx,4)
incq %rbp
cmpq %rbp, %r12
jne 0x158ce2
incq %rax
cmpq %r12, %rax
movq %r9, %rbp
jne 0x158c9e
incq %rcx
cmpq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r13
movq 0x8(%rsp), %rdi
jne 0x158c4d
movq (%rsp), %rsi
leal -0x1(%rsi), %eax
andl $0x1, %eax
movq (%r15), %rcx
movsd (%rcx,%rax,8), %xmm0
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movl $0x0, -0x4(%rdi,%r8,4)
cmpl $0x2, %ebx
jl 0x158da2
decq %r8
movl $0x1, %ecx
movsd 0x2333a5(%rip), %xmm1   # 0x38c128
movq (%r15,%rcx,8), %rdx
movsd (%rdx,%rax,8), %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jbe 0x158d9a
movl %ecx, (%rdi,%r8,4)
incq %rcx
cmpq %rcx, %rbx
jne 0x158d83
cmpl $0x2, %esi
jl 0x158dc1
addl $-0x2, %esi
incq %rsi
movl (%rdi,%rsi,4), %eax
movq (%rbp,%rax,8), %rax
movl (%rax,%rsi,4), %eax
movl %eax, -0x4(%rdi,%rsi,4)
decq %rsi
jg 0x158dad
movq (%rbp), %rdi
leaq 0x23338f(%rip), %rbx    # 0x38c15b
movq %rbx, %rsi
movl $0x175, %edx            # imm = 0x175
callq 0x109670
movq %rbp, %rdi
movq %rbx, %rsi
movl $0x175, %edx            # imm = 0x175
callq 0x109670
movq (%r15), %rdi
movq %rbx, %rsi
movl $0x176, %edx            # imm = 0x176
callq 0x109670
movq %r15, %rdi
movq %rbx, %rsi
movl $0x176, %edx            # imm = 0x176
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x109670
callq 0x48f40
callq 0x48f79
callq 0x48feb
callq 0x48f07
callq 0x48fb2