#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 */
typedef int /*<<< orphan*/ __m128d;

/* Variables and functions */
int /*<<< orphan*/ _mm_add_pd(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_load_pd(double const *);
int /*<<< orphan*/ _mm_mul_pd(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_setzero_pd();
int /*<<< orphan*/ _mm_storeu_pd(double *, int /*<<< orphan*/);
double sse_filtering_aligned(const double *restrict filt,
                             const double *restrict signal, const int K) {
  __m128d v = _mm_setzero_pd();
  int k = 0;
  for (; k < K - 1; k += 2) {
    __m128d f = _mm_load_pd(filt + k);
    __m128d s = _mm_load_pd(signal + k);
    v = _mm_add_pd(v, _mm_mul_pd(f, s));
  }
  double tmp[2];
  _mm_storeu_pd(tmp, v);
  if (k == K)
    return tmp[0] + tmp[1];
  return tmp[0] + tmp[1] + filt[k] * signal[k];
}


x86 assembly (clang, -O0):
<sse_filtering_aligned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <_mm_setzero_pd@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11df <sse_filtering_aligned+0x8f>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
call   1040 <_mm_load_pd@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
call   1040 <_mm_load_pd@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1070 <_mm_mul_pd@plt>
mov    -0x44(%rbp),%edi
mov    %eax,%esi
call   1030 <_mm_add_pd@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
jmp    1174 <sse_filtering_aligned+0x24>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <_mm_storeu_pd@plt>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    120b <sse_filtering_aligned+0xbb>
movsd  -0x40(%rbp),%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    123c <sse_filtering_aligned+0xec>
movsd  -0x40(%rbp),%xmm1
addsd  -0x38(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sse_filtering_aligned>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <_mm_setzero_pd@plt>
mov    %eax,%r13d
cmp    $0x2,%ebp
mov    %r14,0x10(%rsp)
mov    %rbx,0x8(%rsp)
mov    %rbp,0x18(%rsp)
jl     11db <sse_filtering_aligned+0x8b>
lea    -0x1(%rbp),%r12d
xor    %r15d,%r15d
mov    %r14,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <_mm_load_pd@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1040 <_mm_load_pd@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   1070 <_mm_mul_pd@plt>
mov    %r13d,%edi
mov    %eax,%esi
call   1030 <_mm_add_pd@plt>
mov    %eax,%r13d
add    $0x10,%rbp
add    $0x10,%rbx
add    $0x2,%r15d
cmp    %r15d,%r12d
jg     11a0 <sse_filtering_aligned+0x50>
lea    0x20(%rsp),%rdi
mov    %r13d,%esi
call   1060 <_mm_storeu_pd@plt>
movsd  0x20(%rsp),%xmm0
addsd  0x28(%rsp),%xmm0
cmp    0x18(%rsp),%r15d
je     1216 <sse_filtering_aligned+0xc6>
mov    %r15d,%eax
mov    0x8(%rsp),%rcx
movsd  (%rcx,%rax,8),%xmm1
mov    0x10(%rsp),%rcx
mulsd  (%rcx,%rax,8),%xmm1
addsd  %xmm1,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @sse_filtering_aligned(double* noalias %0, double* noalias %1, i32 %2) {
%4 = alloca double
%5 = alloca double*
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2 x double]
store double* %0, double** %5
store double* %1, double** %6
store i32 %2, i32* %7
%13 = call i32 (...) @_mm_setzero_pd()
store i32 %13, i32* %8
store i32 0, i32* %9
br label %14
14: ; preds = %35, %3
%15 = load i32, i32* %9
%16 = load i32, i32* %7
%17 = sub nsw i32 %16, 1
%18 = icmp slt i32 %15, %17
br i1 %18, label %19, label %38
19: ; preds = %14
%20 = load double*, double** %5
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
%24 = call i32 @_mm_load_pd(double* %23)
store i32 %24, i32* %10
%25 = load double*, double** %6
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds double, double* %25, i64 %27
%29 = call i32 @_mm_load_pd(double* %28)
store i32 %29, i32* %11
%30 = load i32, i32* %8
%31 = load i32, i32* %10
%32 = load i32, i32* %11
%33 = call i32 @_mm_mul_pd(i32 %31, i32 %32)
%34 = call i32 @_mm_add_pd(i32 %30, i32 %33)
store i32 %34, i32* %8
br label %35
35: ; preds = %19
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 2
store i32 %37, i32* %9
br label %14
38: ; preds = %14
%39 = getelementptr inbounds [2 x double], [2 x double]* %12, i64 0, i64 0
%40 = load i32, i32* %8
%41 = call i32 @_mm_storeu_pd(double* %39, i32 %40)
%42 = load i32, i32* %9
%43 = load i32, i32* %7
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %51
45: ; preds = %38
%46 = getelementptr inbounds [2 x double], [2 x double]* %12, i64 0, i64 0
%47 = load double, double* %46
%48 = getelementptr inbounds [2 x double], [2 x double]* %12, i64 0, i64 1
%49 = load double, double* %48
%50 = fadd double %47, %49
store double %50, double* %4
br label %68
51: ; preds = %38
%52 = getelementptr inbounds [2 x double], [2 x double]* %12, i64 0, i64 0
%53 = load double, double* %52
%54 = getelementptr inbounds [2 x double], [2 x double]* %12, i64 0, i64 1
%55 = load double, double* %54
%56 = fadd double %53, %55
%57 = load double*, double** %5
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds double, double* %57, i64 %59
%61 = load double, double* %60
%62 = load double*, double** %6
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds double, double* %62, i64 %64
%66 = load double, double* %65
%67 = call double @llvm.fmuladd.f64(double %61, double %66, double %56)
store double %67, double* %4
br label %68
68: ; preds = %51, %45
%69 = load double, double* %4
ret double %69
}

LLVM IR (-O2):
define double @sse_filtering_aligned(double* noalias %0, double* noalias %1, i32 %2) {
%4 = alloca [2 x double]
%5 = tail call i32 (...) @_mm_setzero_pd()
%6 = add nsw i32 %2, -1
%7 = icmp sgt i32 %2, 1
br i1 %7, label %8, label %22
8: ; preds = %3, %8
%9 = phi i64 [ %17, %8 ], [ 0, %3 ]
%10 = phi i32 [ %16, %8 ], [ %5, %3 ]
%11 = getelementptr inbounds double, double* %0, i64 %9
%12 = tail call i32 @_mm_load_pd(double* %11)
%13 = getelementptr inbounds double, double* %1, i64 %9
%14 = tail call i32 @_mm_load_pd(double* %13)
%15 = tail call i32 @_mm_mul_pd(i32 %12, i32 %14)
%16 = tail call i32 @_mm_add_pd(i32 %10, i32 %15)
%17 = add nuw i64 %9, 2
%18 = trunc i64 %17 to i32
%19 = icmp sgt i32 %6, %18
br i1 %19, label %8, label %20
20: ; preds = %8
%21 = trunc i64 %17 to i32
br label %22
22: ; preds = %20, %3
%23 = phi i32 [ 0, %3 ], [ %21, %20 ]
%24 = phi i32 [ %5, %3 ], [ %16, %20 ]
%25 = bitcast [2 x double]* %4 to i8*
%26 = getelementptr inbounds [2 x double], [2 x double]* %4, i64 0, i64 0
%27 = call i32 @_mm_storeu_pd(double* %26, i32 %24)
%28 = icmp eq i32 %23, %2
%29 = load double, double* %26
%30 = getelementptr inbounds [2 x double], [2 x double]* %4, i64 0, i64 1
%31 = load double, double* %30
%32 = fadd double %29, %31
br i1 %28, label %40, label %33
33: ; preds = %22
%34 = zext i32 %23 to i64
%35 = getelementptr inbounds double, double* %0, i64 %34
%36 = load double, double* %35
%37 = getelementptr inbounds double, double* %1, i64 %34
%38 = load double, double* %37
%39 = call double @llvm.fmuladd.f64(double %36, double %38, double %32)
br label %40
40: ; preds = %22, %33
%41 = phi double [ %39, %33 ], [ %32, %22 ]
ret double %41
}