#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct trace_s {
  int dummy;
};
struct istate_s {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ allocate_mx(struct istate_s *, int, int, int ****, int ****);
int /*<<< orphan*/ free_mx(int ***, int ***, int, int);
int /*<<< orphan*/ init_mx(struct istate_s *, int, int, int ***, int ***);
int /*<<< orphan*/ recurse_mx(struct istate_s *, int, char *, int, int ***,
                              int ***);
int strlen(char *);
int /*<<< orphan*/ trace_mx(struct istate_s *, char *, int, int ***, int ***,
                            struct trace_s **);
int ViterbiAlign(struct istate_s *icm, int statenum, char *seq,
                 double *ret_score, struct trace_s **ret_trace) {
  int ***amx;
  int ***bmx;
  int N;
  N = strlen(seq);
  seq--;
  if (!allocate_mx(icm, statenum, N, &amx, &bmx))
    return 0;
  if (!init_mx(icm, statenum, N, amx, bmx))
    return 0;
  if (!recurse_mx(icm, statenum, seq, N, amx, bmx))
    return 0;
  *ret_score = ((double)bmx[0][N][N] / 1000.0);
  if (!trace_mx(icm, seq, N, amx, bmx, ret_trace))
    return 0;
  free_mx(amx, bmx, statenum, N);
  return 1;
}


x86 assembly (clang, -O0):
<ViterbiAlign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x44(%rbp),%edx
lea    -0x38(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1070 <allocate_mx@plt>
cmp    $0x0,%eax
jne    11bf <ViterbiAlign+0x5f>
movl   $0x0,-0x4(%rbp)
jmp    1292 <ViterbiAlign+0x132>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x44(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1060 <init_mx@plt>
cmp    $0x0,%eax
jne    11eb <ViterbiAlign+0x8b>
movl   $0x0,-0x4(%rbp)
jmp    1292 <ViterbiAlign+0x132>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x44(%rbp),%ecx
mov    -0x38(%rbp),%r8
mov    -0x40(%rbp),%r9
call   1050 <recurse_mx@plt>
cmp    $0x0,%eax
jne    121b <ViterbiAlign+0xbb>
movl   $0x0,-0x4(%rbp)
jmp    1292 <ViterbiAlign+0x132>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x44(%rbp),%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
movsd  0xdc5(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    -0x28(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
mov    -0x30(%rbp),%r9
call   1080 <trace_mx@plt>
cmp    $0x0,%eax
jne    1278 <ViterbiAlign+0x118>
movl   $0x0,-0x4(%rbp)
jmp    1292 <ViterbiAlign+0x132>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x44(%rbp),%ecx
call   1040 <free_mx@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ViterbiAlign>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
lea    0x8(%rsp),%rcx
mov    %rsp,%r8
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %eax,%edx
call   1070 <allocate_mx@plt>
xor    %r15d,%r15d
test   %eax,%eax
je     123d <ViterbiAlign+0xdd>
mov    0x8(%rsp),%rcx
mov    (%rsp),%r8
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r13d,%edx
call   1060 <init_mx@plt>
test   %eax,%eax
je     123d <ViterbiAlign+0xdd>
add    $0xffffffffffffffff,%rbx
mov    0x8(%rsp),%r8
mov    (%rsp),%r9
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   1050 <recurse_mx@plt>
test   %eax,%eax
je     123d <ViterbiAlign+0xdd>
mov    (%rsp),%r8
mov    (%r8),%rax
movslq %r13d,%rcx
mov    (%rax,%rcx,8),%rax
cvtsi2sdl (%rax,%rcx,4),%xmm0
divsd  0xdfe(%rip),%xmm0
movsd  %xmm0,(%r14)
mov    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
mov    0x10(%rsp),%r9
call   1080 <trace_mx@plt>
test   %eax,%eax
je     123d <ViterbiAlign+0xdd>
mov    0x8(%rsp),%rdi
mov    (%rsp),%rsi
mov    %r12d,%edx
mov    %r13d,%ecx
call   1040 <free_mx@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.istate_s = type { i32 }
%struct.trace_s = type { i32 }
define i32 @ViterbiAlign(%struct.istate_s* %0, i32 %1, i8* %2, double* %3, %struct.trace_s** %4) {
%6 = alloca i32
%7 = alloca %struct.istate_s*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca double*
%11 = alloca %struct.trace_s**
%12 = alloca i32***
%13 = alloca i32***
%14 = alloca i32
store %struct.istate_s* %0, %struct.istate_s** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store double* %3, double** %10
store %struct.trace_s** %4, %struct.trace_s*** %11
%15 = load i8*, i8** %9
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %14
%17 = load i8*, i8** %9
%18 = getelementptr inbounds i8, i8* %17, i32 -1
store i8* %18, i8** %9
%19 = load %struct.istate_s*, %struct.istate_s** %7
%20 = load i32, i32* %8
%21 = load i32, i32* %14
%22 = call i32 @allocate_mx(%struct.istate_s* %19, i32 %20, i32 %21, i32**** %12, i32**** %13)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %5
store i32 0, i32* %6
br label %74
25: ; preds = %5
%26 = load %struct.istate_s*, %struct.istate_s** %7
%27 = load i32, i32* %8
%28 = load i32, i32* %14
%29 = load i32***, i32**** %12
%30 = load i32***, i32**** %13
%31 = call i32 @init_mx(%struct.istate_s* %26, i32 %27, i32 %28, i32*** %29, i32*** %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %25
store i32 0, i32* %6
br label %74
34: ; preds = %25
%35 = load %struct.istate_s*, %struct.istate_s** %7
%36 = load i32, i32* %8
%37 = load i8*, i8** %9
%38 = load i32, i32* %14
%39 = load i32***, i32**** %12
%40 = load i32***, i32**** %13
%41 = call i32 @recurse_mx(%struct.istate_s* %35, i32 %36, i8* %37, i32 %38, i32*** %39, i32*** %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %34
store i32 0, i32* %6
br label %74
44: ; preds = %34
%45 = load i32***, i32**** %13
%46 = getelementptr inbounds i32**, i32*** %45, i64 0
%47 = load i32**, i32*** %46
%48 = load i32, i32* %14
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32*, i32** %47, i64 %49
%51 = load i32*, i32** %50
%52 = load i32, i32* %14
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = sitofp i32 %55 to double
%57 = fdiv double %56, 1.000000e+03
%58 = load double*, double** %10
store double %57, double* %58
%59 = load %struct.istate_s*, %struct.istate_s** %7
%60 = load i8*, i8** %9
%61 = load i32, i32* %14
%62 = load i32***, i32**** %12
%63 = load i32***, i32**** %13
%64 = load %struct.trace_s**, %struct.trace_s*** %11
%65 = call i32 @trace_mx(%struct.istate_s* %59, i8* %60, i32 %61, i32*** %62, i32*** %63, %struct.trace_s** %64)
%66 = icmp ne i32 %65, 0
br i1 %66, label %68, label %67
67: ; preds = %44
store i32 0, i32* %6
br label %74
68: ; preds = %44
%69 = load i32***, i32**** %12
%70 = load i32***, i32**** %13
%71 = load i32, i32* %8
%72 = load i32, i32* %14
%73 = call i32 @free_mx(i32*** %69, i32*** %70, i32 %71, i32 %72)
store i32 1, i32* %6
br label %74
74: ; preds = %68, %67, %43, %33, %24
%75 = load i32, i32* %6
ret i32 %75
}

LLVM IR (-O2):
%struct.istate_s = type { i32 }
%struct.trace_s = type { i32 }
define i32 @ViterbiAlign(%struct.istate_s* %0, i32 %1, i8* %2, double* nocapture writeonly %3, %struct.trace_s** %4) {
%6 = alloca i32***
%7 = alloca i32***
%8 = bitcast i32**** %6 to i8*
%9 = bitcast i32**** %7 to i8*
%10 = tail call i32 @strlen(i8* %2)
%11 = getelementptr inbounds i8, i8* %2, i64 -1
%12 = call i32 @allocate_mx(%struct.istate_s* %0, i32 %1, i32 %10, i32**** %6, i32**** %7)
%13 = icmp eq i32 %12, 0
br i1 %13, label %41, label %14
14: ; preds = %5
%15 = load i32***, i32**** %6
%16 = load i32***, i32**** %7
%17 = call i32 @init_mx(%struct.istate_s* %0, i32 %1, i32 %10, i32*** %15, i32*** %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %41, label %19
19: ; preds = %14
%20 = load i32***, i32**** %6
%21 = load i32***, i32**** %7
%22 = call i32 @recurse_mx(%struct.istate_s* %0, i32 %1, i8* %11, i32 %10, i32*** %20, i32*** %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %41, label %24
24: ; preds = %19
%25 = load i32***, i32**** %7
%26 = load i32**, i32*** %25
%27 = sext i32 %10 to i64
%28 = getelementptr inbounds i32*, i32** %26, i64 %27
%29 = load i32*, i32** %28
%30 = getelementptr inbounds i32, i32* %29, i64 %27
%31 = load i32, i32* %30
%32 = sitofp i32 %31 to double
%33 = fdiv double %32, 1.000000e+03
store double %33, double* %3
%34 = load i32***, i32**** %6
%35 = call i32 @trace_mx(%struct.istate_s* %0, i8* %11, i32 %10, i32*** %34, i32*** %25, %struct.trace_s** %4)
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %24
%38 = load i32***, i32**** %6
%39 = load i32***, i32**** %7
%40 = call i32 @free_mx(i32*** %38, i32*** %39, i32 %1, i32 %10)
br label %41
41: ; preds = %24, %19, %14, %5, %37
%42 = phi i32 [ 1, %37 ], [ 0, %5 ], [ 0, %14 ], [ 0, %19 ], [ 0, %24 ]
ret i32 %42
}