// File: /workspace/github_c_makefile_star2_O3/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 0x1514a0
movl %ecx, %ebx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
callq 0x150e36
testb %al, %al
je 0x1514a5
movq %r14, 0x28(%rsp)
movq %r15, 0x18(%rsp)
testl %ebx, %ebx
je 0x1514aa
movq %r12, (%rsp)
movslq (%r12), %r13
leaq (,%r13,8), %rdi
leaq 0x231c0c(%rip), %rbp    # 0x382d9b
movq %rdi, %r14
movq %rbp, %rsi
movl $0x13f, %edx            # imm = 0x13F
callq 0x104226
movq %rax, %r15
movq %r13, %rdi
shlq $0x4, %rdi
movq %rbp, %rsi
movl $0x13f, %edx            # imm = 0x13F
callq 0x104226
movl %r13d, %r12d
movq %rax, (%r15)
cmpq $0x2, %r13
jb 0x1511de
movq (%r15), %rax
addq $0x10, %rax
movl $0x1, %ecx
movq %rax, (%r15,%rcx,8)
incq %rcx
addq $0x10, %rax
cmpq %rcx, %r13
jne 0x1511ce
leaq 0x231bb6(%rip), %rsi    # 0x382d9b
movq %r14, %rdi
movq %rsi, %r14
movl $0x140, %edx            # imm = 0x140
callq 0x104226
movq %rax, %rbp
movslq %ebx, %rdi
movq %rdi, 0x10(%rsp)
imulq %r13, %rdi
shlq $0x2, %rdi
movq %r14, %rsi
movl $0x140, %edx            # imm = 0x140
callq 0x104226
movq %rax, (%rbp)
cmpl $0x2, %r12d
movq 0x18(%rsp), %rdi
jb 0x15124d
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, %r13
jne 0x15123d
testl %r13d, %r13d
movq (%rsp), %r8
jle 0x1512a1
xorl %eax, %eax
movl (%rdi), %edx
movl 0x4(%r8), %esi
leal -0x1(%rsi), %ecx
cmpl $0xfe, %edx
cmovnel %edx, %ecx
cmpl %esi, %ecx
jae 0x151496
movq 0x8(%r8), %rdx
movq 0x18(%r8), %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 0x151258
movq %r13, 0x20(%rsp)
movq %rbx, 0x8(%rsp)
cmpl $0x2, %ebx
jl 0x1513c1
movl 0x8(%rsp), %eax
movq %rax, 0x30(%rsp)
movl $0x1, %ecx
movsd 0x231a9e(%rip), %xmm0   # 0x382d68
movl (%rdi,%rcx,4), %edx
movl 0x4(%r8), %esi
leal -0x1(%rsi), %eax
cmpl $0xfe, %edx
cmovnel %edx, %eax
cmpl %esi, %eax
jae 0x15149b
testl %r12d, %r12d
jle 0x1513aa
movl %ecx, %edx
andl $0x1, %edx
movq %rdx, %rsi
xorq $0x1, %rsi
movq (%r15), %rdi
movq %rdi, 0x40(%rsp)
movq (%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 0x15139b
movl $0x1, %ebp
movq (%r15,%rbp,8), %rdi
movsd (%rdi,%rsi,8), %xmm2
movq (%r8,%rbp,8), %rdi
addsd (%rdi,%rax,8), %xmm2
addsd (%r14,%r11,8), %xmm2
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
ucomisd %xmm0, %xmm3
jbe 0x151393
movsd %xmm2, (%r13,%rdx,8)
movl %ebp, (%rbx,%rcx,4)
movapd %xmm2, %xmm1
incq %rbp
cmpq %rbp, %r12
jne 0x15135f
incq %rax
cmpq %r12, %rax
movq %r9, %rbp
jne 0x15131b
incq %rcx
cmpq 0x30(%rsp), %rcx
movq 0x18(%rsp), %rdi
movq (%rsp), %r8
jne 0x1512ca
movq 0x8(%rsp), %rsi
leal -0x1(%rsi), %eax
andl $0x1, %eax
movq (%r15), %rcx
movsd (%rcx,%rax,8), %xmm0
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %r9
movl $0x0, -0x4(%rdi,%r9,4)
cmpl $0x2, %r12d
movq 0x20(%rsp), %r8
jl 0x151421
decq %r9
movl $0x1, %ecx
movsd 0x231966(%rip), %xmm1   # 0x382d68
movq (%r15,%rcx,8), %rdx
movsd (%rdx,%rax,8), %xmm2
subsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jbe 0x151419
movl %ecx, (%rdi,%r9,4)
incq %rcx
cmpq %rcx, %r8
jne 0x151402
cmpl $0x2, %esi
jl 0x151440
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
jne 0x15142c
movq (%rbp), %rdi
leaq 0x231950(%rip), %rbx    # 0x382d9b
movq %rbx, %rsi
movl $0x175, %edx            # imm = 0x175
callq 0x1045eb
movq %rbp, %rdi
movq %rbx, %rsi
movl $0x175, %edx            # imm = 0x175
callq 0x1045eb
movq (%r15), %rdi
movq %rbx, %rsi
movl $0x176, %edx            # imm = 0x176
callq 0x1045eb
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 0x1045eb
callq 0x480cc
callq 0x48105
callq 0x48177
callq 0x48093
callq 0x4813e