#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 */

/* Variables and functions */
int /*<<< orphan*/ MatrixLikelihood(double **, double *, double *, double *);
scalar_t__ exp(double);
double log(double);
void BestMatrixDistance(double bmx[20][20], double *pri, double *ct,
                        int *ret_dist, double *ret_ll) {
  double smx[20][20];
  double nmx[20][20];
  int c, r, i;
  int dist;
  double ll;
  double best_ll;
  for (r = 0; r < 20; r++)
    for (c = 0; c < 20; c++)
      smx[r][c] = bmx[r][c];
  best_ll = -9999999.0;
  dist = 0;
  while (1) {
    dist++;
    MatrixLikelihood(smx, pri, ct, &ll);
    if (ll > best_ll)
      best_ll = ll;
    else {
      *ret_ll = best_ll;
      *ret_dist = dist - 1;
      return;
    }
    for (r = 0; r < 20; r++)
      for (c = 0; c < 20; c++) {
        nmx[r][c] = 0.0;
        for (i = 0; i < 20; i++)
          nmx[r][c] += exp(smx[r][i] + bmx[i][c]);
        nmx[r][c] = log(nmx[r][c]);
      }
    for (r = 0; r < 20; r++)
      for (c = 0; c < 20; c++)
        smx[r][c] = nmx[r][c];
  }
}


x86 assembly (clang, -O0):
<BestMatrixDistance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1950,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x1938(%rbp)
cmpl   $0x14,-0x1938(%rbp)
jge    11ef <BestMatrixDistance+0xbf>
movl   $0x0,-0x1934(%rbp)
cmpl   $0x14,-0x1934(%rbp)
jge    11d6 <BestMatrixDistance+0xa6>
mov    -0x8(%rbp),%rax
movslq -0x1938(%rbp),%rcx
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movslq -0x1938(%rbp),%rcx
lea    -0xcb0(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1934(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1934(%rbp)
jmp    1170 <BestMatrixDistance+0x40>
jmp    11db <BestMatrixDistance+0xab>
mov    -0x1938(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1938(%rbp)
jmp    1159 <BestMatrixDistance+0x29>
movsd  0xe09(%rip),%xmm0
movsd  %xmm0,-0x1950(%rbp)
ff
movl   $0x0,-0x1940(%rbp)
mov    -0x1940(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1940(%rbp)
lea    -0xcb0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x1948(%rbp),%rcx
call   1030 <MatrixLikelihood@plt>
movsd  -0x1948(%rbp),%xmm0
ff
ucomisd -0x1950(%rbp),%xmm0
ff
jbe    125e <BestMatrixDistance+0x12e>
movsd  -0x1948(%rbp),%xmm0
ff
movsd  %xmm0,-0x1950(%rbp)
ff
jmp    1286 <BestMatrixDistance+0x156>
movsd  -0x1950(%rbp),%xmm0
ff
mov    -0x28(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x1940(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
add    $0x1950,%rsp
pop    %rbp
ret
movl   $0x0,-0x1938(%rbp)
cmpl   $0x14,-0x1938(%rbp)
jge    13f8 <BestMatrixDistance+0x2c8>
movl   $0x0,-0x1934(%rbp)
cmpl   $0x14,-0x1934(%rbp)
jge    13df <BestMatrixDistance+0x2af>
movslq -0x1938(%rbp),%rcx
lea    -0x1930(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
movl   $0x0,-0x193c(%rbp)
cmpl   $0x14,-0x193c(%rbp)
jge    137e <BestMatrixDistance+0x24e>
movslq -0x1938(%rbp),%rcx
lea    -0xcb0(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x193c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x193c(%rbp),%rcx
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
call   1040 <exp@plt>
cvtsi2sd %rax,%xmm0
movslq -0x1938(%rbp),%rcx
lea    -0x1930(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x193c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x193c(%rbp)
jmp    12e5 <BestMatrixDistance+0x1b5>
movslq -0x1938(%rbp),%rcx
lea    -0x1930(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
call   1050 <log@plt>
movslq -0x1938(%rbp),%rcx
lea    -0x1930(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1934(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1934(%rbp)
jmp    12a7 <BestMatrixDistance+0x177>
jmp    13e4 <BestMatrixDistance+0x2b4>
mov    -0x1938(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1938(%rbp)
jmp    1290 <BestMatrixDistance+0x160>
movl   $0x0,-0x1938(%rbp)
cmpl   $0x14,-0x1938(%rbp)
jge    149b <BestMatrixDistance+0x36b>
movl   $0x0,-0x1934(%rbp)
cmpl   $0x14,-0x1934(%rbp)
jge    1482 <BestMatrixDistance+0x352>
movslq -0x1938(%rbp),%rcx
lea    -0x1930(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movslq -0x1938(%rbp),%rcx
lea    -0xcb0(%rbp),%rax
imul   $0xa0,%rcx,%rcx
add    %rcx,%rax
movslq -0x1934(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1934(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1934(%rbp)
jmp    1419 <BestMatrixDistance+0x2e9>
jmp    1487 <BestMatrixDistance+0x357>
mov    -0x1938(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1938(%rbp)
jmp    1402 <BestMatrixDistance+0x2d2>
jmp    1209 <BestMatrixDistance+0xd9>

x86 assembly (clang, -O2):
<BestMatrixDistance>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1958,%rsp
mov    %r8,0x18(%rsp)
mov    %rcx,0x20(%rsp)
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0xcd0(%rsp),%rbp
mov    $0xc80,%edx
mov    %rbp,%rdi
mov    %rsi,0x48(%rsp)
call   1030 <memcpy@plt>
lea    0x10(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,0x28(%rsp)
mov    %rbx,%rsi
mov    %r14,0x30(%rsp)
mov    %r14,%rdx
call   1040 <MatrixLikelihood@plt>
movsd  0x10(%rsp),%xmm0
movsd  0xe57(%rip),%xmm1
xor    %r14d,%r14d
ucomisd %xmm1,%xmm0
jbe    12f6 <BestMatrixDistance+0x1b6>
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
mov    %rax,0x40(%rsp)
movsd  %xmm0,0x38(%rsp)
xor    %eax,%eax
mov    %rax,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x48(%rsp),%r13
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rax
lea    (%rax,%rax,4),%rax
shl    $0x5,%rax
add    %rsp,%rax
add    $0x50,%rax
lea    (%rax,%r15,8),%r14
xorpd  %xmm0,%xmm0
mov    %r13,%rbx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
movsd  0x0(%rbp,%r12,8),%xmm0
addsd  (%rbx),%xmm0
call   1050 <exp@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,(%r14)
add    $0x1,%r12
add    $0xa0,%rbx
cmp    $0x14,%r12
jne    1220 <BestMatrixDistance+0xe0>
call   1060 <log@plt>
movsd  %xmm0,(%r14)
add    $0x1,%r15
add    $0x8,%r13
cmp    $0x14,%r15
jne    11f0 <BestMatrixDistance+0xb0>
mov    (%rsp),%rcx
add    $0x1,%rcx
add    $0xa0,%rbp
mov    %rcx,%rax
mov    %rcx,(%rsp)
cmp    $0x14,%rcx
jne    11e0 <BestMatrixDistance+0xa0>
mov    $0xc80,%edx
lea    0xcd0(%rsp),%rbp
mov    %rbp,%rdi
lea    0x50(%rsp),%rsi
call   1030 <memcpy@plt>
mov    0x40(%rsp),%r14
lea    0x1(%r14),%ebx
mov    %rbp,%rdi
mov    0x28(%rsp),%rsi
mov    0x30(%rsp),%rdx
lea    0x10(%rsp),%rcx
call   1040 <MatrixLikelihood@plt>
movsd  0x10(%rsp),%xmm0
movsd  0x38(%rsp),%xmm1
ucomisd %xmm1,%xmm0
ja     11c0 <BestMatrixDistance+0x80>
mov    0x18(%rsp),%rax
movsd  %xmm1,(%rax)
mov    0x20(%rsp),%rax
mov    %r14d,(%rax)
add    $0x1958,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @BestMatrixDistance([20 x double]* %0, double* %1, double* %2, i32* %3, double* %4) {
%6 = alloca [20 x double]*
%7 = alloca double*
%8 = alloca double*
%9 = alloca i32*
%10 = alloca double*
%11 = alloca [20 x [20 x double]]
%12 = alloca [20 x [20 x double]]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca double
%18 = alloca double
store [20 x double]* %0, [20 x double]** %6
store double* %1, double** %7
store double* %2, double** %8
store i32* %3, i32** %9
store double* %4, double** %10
store i32 0, i32* %14
br label %19
19: ; preds = %45, %5
%20 = load i32, i32* %14
%21 = icmp slt i32 %20, 20
br i1 %21, label %22, label %48
22: ; preds = %19
store i32 0, i32* %13
br label %23
23: ; preds = %41, %22
%24 = load i32, i32* %13
%25 = icmp slt i32 %24, 20
br i1 %25, label %26, label %44
26: ; preds = %23
%27 = load [20 x double]*, [20 x double]** %6
%28 = load i32, i32* %14
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [20 x double], [20 x double]* %27, i64 %29
%31 = load i32, i32* %13
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [20 x double], [20 x double]* %30, i64 0, i64 %32
%34 = load double, double* %33
%35 = load i32, i32* %14
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %11, i64 0, i64 %36
%38 = load i32, i32* %13
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [20 x double], [20 x double]* %37, i64 0, i64 %39
store double %34, double* %40
br label %41
41: ; preds = %26
%42 = load i32, i32* %13
%43 = add nsw i32 %42, 1
store i32 %43, i32* %13
br label %23
44: ; preds = %23
br label %45
45: ; preds = %44
%46 = load i32, i32* %14
%47 = add nsw i32 %46, 1
store i32 %47, i32* %14
br label %19
48: ; preds = %19
store double 0xC16312CFE0000000, double* %18
store i32 0, i32* %16
br label %49
49: ; preds = %48, %167
%50 = load i32, i32* %16
%51 = add nsw i32 %50, 1
store i32 %51, i32* %16
%52 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %11, i64 0, i64 0
%53 = bitcast [20 x double]* %52 to double**
%54 = load double*, double** %7
%55 = load double*, double** %8
%56 = call i32 @MatrixLikelihood(double** %53, double* %54, double* %55, double* %17)
%57 = load double, double* %17
%58 = load double, double* %18
%59 = fcmp ogt double %57, %58
br i1 %59, label %60, label %62
60: ; preds = %49
%61 = load double, double* %17
store double %61, double* %18
br label %68
62: ; preds = %49
%63 = load double, double* %18
%64 = load double*, double** %10
store double %63, double* %64
%65 = load i32, i32* %16
%66 = sub nsw i32 %65, 1
%67 = load i32*, i32** %9
store i32 %66, i32* %67
ret void
68: ; preds = %60
store i32 0, i32* %14
br label %69
69: ; preds = %135, %68
%70 = load i32, i32* %14
%71 = icmp slt i32 %70, 20
br i1 %71, label %72, label %138
72: ; preds = %69
store i32 0, i32* %13
br label %73
73: ; preds = %131, %72
%74 = load i32, i32* %13
%75 = icmp slt i32 %74, 20
br i1 %75, label %76, label %134
76: ; preds = %73
%77 = load i32, i32* %14
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %12, i64 0, i64 %78
%80 = load i32, i32* %13
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [20 x double], [20 x double]* %79, i64 0, i64 %81
store double 0.000000e+00, double* %82
store i32 0, i32* %15
br label %83
83: ; preds = %113, %76
%84 = load i32, i32* %15
%85 = icmp slt i32 %84, 20
br i1 %85, label %86, label %116
86: ; preds = %83
%87 = load i32, i32* %14
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %11, i64 0, i64 %88
%90 = load i32, i32* %15
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [20 x double], [20 x double]* %89, i64 0, i64 %91
%93 = load double, double* %92
%94 = load [20 x double]*, [20 x double]** %6
%95 = load i32, i32* %15
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [20 x double], [20 x double]* %94, i64 %96
%98 = load i32, i32* %13
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds [20 x double], [20 x double]* %97, i64 0, i64 %99
%101 = load double, double* %100
%102 = fadd double %93, %101
%103 = call i64 @exp(double %102)
%104 = sitofp i64 %103 to double
%105 = load i32, i32* %14
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %12, i64 0, i64 %106
%108 = load i32, i32* %13
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds [20 x double], [20 x double]* %107, i64 0, i64 %109
%111 = load double, double* %110
%112 = fadd double %111, %104
store double %112, double* %110
br label %113
113: ; preds = %86
%114 = load i32, i32* %15
%115 = add nsw i32 %114, 1
store i32 %115, i32* %15
br label %83
116: ; preds = %83
%117 = load i32, i32* %14
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %12, i64 0, i64 %118
%120 = load i32, i32* %13
%121 = sext i32 %120 to i64
%122 = getelementptr inbounds [20 x double], [20 x double]* %119, i64 0, i64 %121
%123 = load double, double* %122
%124 = call double @log(double %123)
%125 = load i32, i32* %14
%126 = sext i32 %125 to i64
%127 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %12, i64 0, i64 %126
%128 = load i32, i32* %13
%129 = sext i32 %128 to i64
%130 = getelementptr inbounds [20 x double], [20 x double]* %127, i64 0, i64 %129
store double %124, double* %130
br label %131
131: ; preds = %116
%132 = load i32, i32* %13
%133 = add nsw i32 %132, 1
store i32 %133, i32* %13
br label %73
134: ; preds = %73
br label %135
135: ; preds = %134
%136 = load i32, i32* %14
%137 = add nsw i32 %136, 1
store i32 %137, i32* %14
br label %69
138: ; preds = %69
store i32 0, i32* %14
br label %139
139: ; preds = %164, %138
%140 = load i32, i32* %14
%141 = icmp slt i32 %140, 20
br i1 %141, label %142, label %167
142: ; preds = %139
store i32 0, i32* %13
br label %143
143: ; preds = %160, %142
%144 = load i32, i32* %13
%145 = icmp slt i32 %144, 20
br i1 %145, label %146, label %163
146: ; preds = %143
%147 = load i32, i32* %14
%148 = sext i32 %147 to i64
%149 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %12, i64 0, i64 %148
%150 = load i32, i32* %13
%151 = sext i32 %150 to i64
%152 = getelementptr inbounds [20 x double], [20 x double]* %149, i64 0, i64 %151
%153 = load double, double* %152
%154 = load i32, i32* %14
%155 = sext i32 %154 to i64
%156 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %11, i64 0, i64 %155
%157 = load i32, i32* %13
%158 = sext i32 %157 to i64
%159 = getelementptr inbounds [20 x double], [20 x double]* %156, i64 0, i64 %158
store double %153, double* %159
br label %160
160: ; preds = %146
%161 = load i32, i32* %13
%162 = add nsw i32 %161, 1
store i32 %162, i32* %13
br label %143
163: ; preds = %143
br label %164
164: ; preds = %163
%165 = load i32, i32* %14
%166 = add nsw i32 %165, 1
store i32 %166, i32* %14
br label %139
167: ; preds = %139
br label %49
}

LLVM IR (-O2):
define void @BestMatrixDistance([20 x double]* nocapture %0, double* %1, double* %2, i32* nocapture writeonly %3, double* nocapture writeonly %4) {
%6 = bitcast [20 x double]* %0 to i8*
%7 = alloca [20 x [20 x double]]
%8 = bitcast [20 x [20 x double]]* %7 to i8*
%9 = alloca [20 x [20 x double]]
%10 = bitcast [20 x [20 x double]]* %9 to i8*
%11 = alloca double
%12 = bitcast double* %11 to i8*
%13 = bitcast [20 x [20 x double]]* %7 to double**
%14 = call i32 @MatrixLikelihood(double** %13, double* %1, double* %2, double* %11)
%15 = load double, double* %11
%16 = fcmp ogt double %15, 0xC16312CFE0000000
br i1 %16, label %17, label %20
17: ; preds = %5, %48
%18 = phi double [ %51, %48 ], [ %15, %5 ]
%19 = phi i32 [ %49, %48 ], [ 1, %5 ]
br label %23
20: ; preds = %48, %5
%21 = phi i32 [ 0, %5 ], [ %19, %48 ]
%22 = phi double [ 0xC16312CFE0000000, %5 ], [ %18, %48 ]
store double %22, double* %4
store i32 %21, i32* %3
ret void
23: ; preds = %17, %45
%24 = phi i64 [ 0, %17 ], [ %46, %45 ]
br label %25
25: ; preds = %23, %41
%26 = phi i64 [ 0, %23 ], [ %43, %41 ]
%27 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %9, i64 0, i64 %24, i64 %26
br label %28
28: ; preds = %25, %28
%29 = phi double [ 0.000000e+00, %25 ], [ %38, %28 ]
%30 = phi i64 [ 0, %25 ], [ %39, %28 ]
%31 = getelementptr inbounds [20 x [20 x double]], [20 x [20 x double]]* %7, i64 0, i64 %24, i64 %30
%32 = load double, double* %31
%33 = getelementptr inbounds [20 x double], [20 x double]* %0, i64 %30, i64 %26
%34 = load double, double* %33
%35 = fadd double %32, %34
%36 = call i64 @exp(double %35)
%37 = sitofp i64 %36 to double
%38 = fadd double %29, %37
store double %38, double* %27
%39 = add nuw nsw i64 %30, 1
%40 = icmp eq i64 %39, 20
br i1 %40, label %41, label %28
41: ; preds = %28
%42 = call double @log(double %38)
store double %42, double* %27
%43 = add nuw nsw i64 %26, 1
%44 = icmp eq i64 %43, 20
br i1 %44, label %45, label %25
45: ; preds = %41
%46 = add nuw nsw i64 %24, 1
%47 = icmp eq i64 %46, 20
br i1 %47, label %48, label %23
48: ; preds = %45
%49 = add nuw nsw i32 %19, 1
%50 = call i32 @MatrixLikelihood(double** %13, double* %1, double* %2, double* %11)
%51 = load double, double* %11
%52 = fcmp ogt double %51, %18
br i1 %52, label %17, label %20
}