#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 */
double compute_dtw_full(float *, float *, int, int, double *, int);
int dtw_search_full(float *q, float *D, int M, int num_of_points, int dim,
                    double *Cost, double *min_distance, int DIM) {
  int i;
  double min_d = 0;
  int min_i = -1;
  double d = 0;
  float *v = D;
  int dim2 = dim;
  min_d = compute_dtw_full(v, q, num_of_points, num_of_points, Cost, DIM);
  min_i = 0;
  v += dim2;
  for (i = 1; i < M; i++) {
    d = compute_dtw_full(v, q, num_of_points, num_of_points, Cost, DIM);
    if (d < min_d) {
      min_d = d;
      min_i = i;
    }
    v += dim2;
  }
  *min_distance = min_d;
  return min_i;
}


x86 assembly (clang, -O0):
<dtw_search_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    0x18(%rbp),%r9d
call   1030 <compute_dtw_full@plt>
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x54(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1210 <dtw_search_full+0x100>
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    0x18(%rbp),%r9d
call   1030 <compute_dtw_full@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11ed <dtw_search_full+0xdd>
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x54(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119d <dtw_search_full+0x8d>
movsd  -0x38(%rbp),%xmm0
mov    0x10(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dtw_search_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r13
mov    %r8d,(%rsp)
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x58(%rsp),%r9d
mov    %rsi,%rdi
mov    %r15,%rsi
mov    %ecx,%edx
mov    %r13,%r8
call   1030 <compute_dtw_full@plt>
movapd %xmm0,%xmm1
xor    %ebx,%ebx
cmp    $0x2,%r14d
jl     11c3 <dtw_search_full+0xb3>
mov    %r13,0x10(%rsp)
mov    %r14d,0xc(%rsp)
movslq (%rsp),%r14
lea    (%r12,%r14,4),%r12
shl    $0x2,%r14
xor    %ebx,%ebx
mov    %r15,%r13
mov    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  %xmm1,(%rsp)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
mov    %ebp,%ecx
mov    0x10(%rsp),%r8
mov    0x58(%rsp),%r9d
call   1030 <compute_dtw_full@plt>
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
cmova  %r15d,%ebx
minsd  %xmm1,%xmm0
add    $0x1,%r15d
add    %r14,%r12
movapd %xmm0,%xmm1
cmp    %r15d,0xc(%rsp)
jne    1180 <dtw_search_full+0x70>
jmp    11c7 <dtw_search_full+0xb7>
movapd %xmm1,%xmm0
mov    0x50(%rsp),%rax
movsd  %xmm0,(%rax)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dtw_search_full(float* %0, float* %1, i32 %2, i32 %3, i32 %4, double* %5, double* %6, i32 %7) {
%9 = alloca float*
%10 = alloca float*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca double*
%15 = alloca double*
%16 = alloca i32
%17 = alloca i32
%18 = alloca double
%19 = alloca i32
%20 = alloca double
%21 = alloca float*
%22 = alloca i32
store float* %0, float** %9
store float* %1, float** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store double* %5, double** %14
store double* %6, double** %15
store i32 %7, i32* %16
store double 0.000000e+00, double* %18
store i32 -1, i32* %19
store double 0.000000e+00, double* %20
%23 = load float*, float** %10
store float* %23, float** %21
%24 = load i32, i32* %13
store i32 %24, i32* %22
%25 = load float*, float** %21
%26 = load float*, float** %9
%27 = load i32, i32* %12
%28 = load i32, i32* %12
%29 = load double*, double** %14
%30 = load i32, i32* %16
%31 = call double @compute_dtw_full(float* %25, float* %26, i32 %27, i32 %28, double* %29, i32 %30)
store double %31, double* %18
store i32 0, i32* %19
%32 = load i32, i32* %22
%33 = load float*, float** %21
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds float, float* %33, i64 %34
store float* %35, float** %21
store i32 1, i32* %17
br label %36
36: ; preds = %59, %8
%37 = load i32, i32* %17
%38 = load i32, i32* %11
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %62
40: ; preds = %36
%41 = load float*, float** %21
%42 = load float*, float** %9
%43 = load i32, i32* %12
%44 = load i32, i32* %12
%45 = load double*, double** %14
%46 = load i32, i32* %16
%47 = call double @compute_dtw_full(float* %41, float* %42, i32 %43, i32 %44, double* %45, i32 %46)
store double %47, double* %20
%48 = load double, double* %20
%49 = load double, double* %18
%50 = fcmp olt double %48, %49
br i1 %50, label %51, label %54
51: ; preds = %40
%52 = load double, double* %20
store double %52, double* %18
%53 = load i32, i32* %17
store i32 %53, i32* %19
br label %54
54: ; preds = %51, %40
%55 = load i32, i32* %22
%56 = load float*, float** %21
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds float, float* %56, i64 %57
store float* %58, float** %21
br label %59
59: ; preds = %54
%60 = load i32, i32* %17
%61 = add nsw i32 %60, 1
store i32 %61, i32* %17
br label %36
62: ; preds = %36
%63 = load double, double* %18
%64 = load double*, double** %15
store double %63, double* %64
%65 = load i32, i32* %19
ret i32 %65
}

LLVM IR (-O2):
define i32 @dtw_search_full(float* %0, float* %1, i32 %2, i32 %3, i32 %4, double* %5, double* nocapture writeonly %6, i32 %7) {
%9 = tail call double @compute_dtw_full(float* %1, float* %0, i32 %3, i32 %3, double* %5, i32 %7)
%10 = sext i32 %4 to i64
%11 = icmp sgt i32 %2, 1
br i1 %11, label %12, label %24
12: ; preds = %8, %12
%13 = phi float* [ %17, %12 ], [ %1, %8 ]
%14 = phi i32 [ %22, %12 ], [ 1, %8 ]
%15 = phi i32 [ %21, %12 ], [ 0, %8 ]
%16 = phi double [ %20, %12 ], [ %9, %8 ]
%17 = getelementptr inbounds float, float* %13, i64 %10
%18 = tail call double @compute_dtw_full(float* %17, float* %0, i32 %3, i32 %3, double* %5, i32 %7)
%19 = fcmp olt double %18, %16
%20 = select i1 %19, double %18, double %16
%21 = select i1 %19, i32 %14, i32 %15
%22 = add nuw nsw i32 %14, 1
%23 = icmp eq i32 %22, %2
br i1 %23, label %24, label %12
24: ; preds = %12, %8
%25 = phi double [ %9, %8 ], [ %20, %12 ]
%26 = phi i32 [ 0, %8 ], [ %21, %12 ]
store double %25, double* %6
ret i32 %26
}