#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 basic_filter(double const *, double const *, int);
double sse_filtering_aligned(double const *, double const *, int);
double sse_filtering_misaligned(double const *, double const *, int);
double filter(const double *restrict filt, const double *restrict signal,
              int K) {
  if (K < 8)
    return basic_filter(filt, signal, K);
  double v = 0;
  if ((((unsigned long)filt) & 15) != 0) {
    v += filt[0] * signal[0];
    ++filt;
    ++signal;
    --K;
  }
  if ((((unsigned long)signal) & 15) == 0) {
    return v + sse_filtering_aligned(filt, signal, K);
  }
  return v + sse_filtering_misaligned(filt, signal, K);
}


x86 assembly (clang, -O0):
<filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    1167 <filter+0x37>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <basic_filter@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1231 <filter+0x101>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
je     11c4 <filter+0x94>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
jne    1206 <filter+0xd6>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <sse_filtering_aligned@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1231 <filter+0x101>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <sse_filtering_misaligned@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filter>:
cmp    $0x7,%edx
jg     113a <filter+0xa>
jmp    1050 <basic_filter@plt>
push   %rax
test   $0xf,%dil
je     116e <filter+0x3e>
movsd  (%rdi),%xmm0
mulsd  (%rsi),%xmm0
xorpd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
add    $0x8,%rdi
add    $0x8,%rsi
add    $0xffffffff,%edx
test   $0xf,%sil
je     117d <filter+0x4d>
call   1030 <sse_filtering_misaligned@plt>
jmp    1182 <filter+0x52>
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
test   $0xf,%sil
jne    1167 <filter+0x37>
call   1040 <sse_filtering_aligned@plt>
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
pop    %rax
ret

LLVM IR (-O0):
define double @filter(double* noalias %0, double* noalias %1, i32 %2) {
%4 = alloca double
%5 = alloca double*
%6 = alloca double*
%7 = alloca i32
%8 = alloca double
store double* %0, double** %5
store double* %1, double** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 8
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = load double*, double** %5
%13 = load double*, double** %6
%14 = load i32, i32* %7
%15 = call double @basic_filter(double* %12, double* %13, i32 %14)
store double %15, double* %4
br label %55
16: ; preds = %3
store double 0.000000e+00, double* %8
%17 = load double*, double** %5
%18 = ptrtoint double* %17 to i64
%19 = and i64 %18, 15
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %36
21: ; preds = %16
%22 = load double*, double** %5
%23 = getelementptr inbounds double, double* %22, i64 0
%24 = load double, double* %23
%25 = load double*, double** %6
%26 = getelementptr inbounds double, double* %25, i64 0
%27 = load double, double* %26
%28 = load double, double* %8
%29 = call double @llvm.fmuladd.f64(double %24, double %27, double %28)
store double %29, double* %8
%30 = load double*, double** %5
%31 = getelementptr inbounds double, double* %30, i32 1
store double* %31, double** %5
%32 = load double*, double** %6
%33 = getelementptr inbounds double, double* %32, i32 1
store double* %33, double** %6
%34 = load i32, i32* %7
%35 = add nsw i32 %34, -1
store i32 %35, i32* %7
br label %36
36: ; preds = %21, %16
%37 = load double*, double** %6
%38 = ptrtoint double* %37 to i64
%39 = and i64 %38, 15
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %36
%42 = load double, double* %8
%43 = load double*, double** %5
%44 = load double*, double** %6
%45 = load i32, i32* %7
%46 = call double @sse_filtering_aligned(double* %43, double* %44, i32 %45)
%47 = fadd double %42, %46
store double %47, double* %4
br label %55
48: ; preds = %36
%49 = load double, double* %8
%50 = load double*, double** %5
%51 = load double*, double** %6
%52 = load i32, i32* %7
%53 = call double @sse_filtering_misaligned(double* %50, double* %51, i32 %52)
%54 = fadd double %49, %53
store double %54, double* %4
br label %55
55: ; preds = %48, %41, %11
%56 = load double, double* %4
ret double %56
}

LLVM IR (-O2):
define double @filter(double* noalias %0, double* noalias %1, i32 %2) {
%4 = icmp slt i32 %2, 8
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call double @basic_filter(double* %0, double* %1, i32 %2)
br label %33
7: ; preds = %3
%8 = ptrtoint double* %0 to i64
%9 = and i64 %8, 15
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = load double, double* %0
%13 = load double, double* %1
%14 = tail call double @llvm.fmuladd.f64(double %12, double %13, double 0.000000e+00)
%15 = getelementptr inbounds double, double* %0, i64 1
%16 = getelementptr inbounds double, double* %1, i64 1
%17 = add nsw i32 %2, -1
br label %18
18: ; preds = %11, %7
%19 = phi double* [ %15, %11 ], [ %0, %7 ]
%20 = phi double* [ %16, %11 ], [ %1, %7 ]
%21 = phi i32 [ %17, %11 ], [ %2, %7 ]
%22 = phi double [ %14, %11 ], [ 0.000000e+00, %7 ]
%23 = ptrtoint double* %20 to i64
%24 = and i64 %23, 15
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = tail call double @sse_filtering_aligned(double* %19, double* %20, i32 %21)
br label %30
28: ; preds = %18
%29 = tail call double @sse_filtering_misaligned(double* %19, double* %20, i32 %21)
br label %30
30: ; preds = %28, %26
%31 = phi double [ %27, %26 ], [ %29, %28 ]
%32 = fadd double %22, %31
br label %33
33: ; preds = %30, %5
%34 = phi double [ %6, %5 ], [ %32, %30 ]
ret double %34
}