#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_sakoe(float *, float *, int, int, double *, int, int);
int dtw_search_sakoe(float *q, float *D, int M, int num_of_points, int dim,
                     double *Cost, double *min_distance, int r, int DIM) {
  int i;
  double min_d = 0;
  int min_i = -1;
  double d = 0;
  float *v = D;
  int dim2 = dim;
  double LB_Keogh = 0;
  min_d =
      compute_dtw_full_sakoe(v, q, num_of_points, num_of_points, Cost, r, DIM);
  min_i = 0;
  v += dim2;
  for (i = 1; i < M; i++) {
    d = compute_dtw_full_sakoe(v, q, num_of_points, num_of_points, Cost, r,
                               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_sakoe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x20(%rbp),%eax
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)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%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
mov    0x20(%rbp),%eax
mov    %eax,(%rsp)
call   1030 <compute_dtw_full_sakoe@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    1227 <dtw_search_sakoe+0x117>
mov    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    0x18(%rbp),%r9d
mov    0x20(%rbp),%eax
mov    %eax,(%rsp)
call   1030 <compute_dtw_full_sakoe@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1204 <dtw_search_sakoe+0xf4>
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    11ae <dtw_search_sakoe+0x9e>
movsd  -0x38(%rbp),%xmm0
mov    0x10(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dtw_search_sakoe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r13
mov    %r8d,0x10(%rsp)
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
mov    0x70(%rsp),%eax
mov    0x68(%rsp),%r9d
mov    %eax,(%rsp)
mov    %rsi,%rdi
mov    %r15,%rsi
mov    %ecx,%edx
mov    %r13,%r8
call   1030 <compute_dtw_full_sakoe@plt>
movapd %xmm0,%xmm1
xor    %ebx,%ebx
cmp    $0x2,%r14d
jl     11cc <dtw_search_sakoe+0xbc>
mov    %r13,0x20(%rsp)
mov    %r14d,0x1c(%rsp)
movslq 0x10(%rsp),%r14
lea    (%r12,%r14,4),%r12
shl    $0x2,%r14
xor    %ebx,%ebx
mov    %r15,%r13
mov    $0x1,%r15d
nopl   0x0(%rax,%rax,1)
movsd  %xmm1,0x10(%rsp)
mov    0x70(%rsp),%eax
mov    %eax,(%rsp)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
mov    %ebp,%ecx
mov    0x20(%rsp),%r8
mov    0x68(%rsp),%r9d
call   1030 <compute_dtw_full_sakoe@plt>
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
cmova  %r15d,%ebx
minsd  %xmm1,%xmm0
add    $0x1,%r15d
add    %r14,%r12
movapd %xmm0,%xmm1
cmp    %r15d,0x1c(%rsp)
jne    1180 <dtw_search_sakoe+0x70>
jmp    11d0 <dtw_search_sakoe+0xc0>
movapd %xmm1,%xmm0
mov    0x60(%rsp),%rax
movsd  %xmm0,(%rax)
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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