void xmm_triad(float *x, float *y, float *z, float num, int size) {
  int k;
  float temp[4] = {num, num, num, num};
  asm volatile("movups (%0), %%xmm1\n\t" : : "r"(temp) :);
  for (k = 0; k < size; k = k + 4) {
    asm volatile("movups (%0), %%xmm0\n\t"
                 "mulps %%xmm1, %%xmm0\n\t"
                 "addps (%1), %%xmm0\n\t"
                 "movups %%xmm0, (%2)\n\t"
                 :
                 : "r"(z + k), "r"(y + k), "r"(x + k)
                 : "%xmm0", "%xmm1");
  }
}


x86 assembly (clang, -O0):
<xmm_triad>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movss  %xmm0,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x40(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x3c(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x38(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x34(%rbp)
lea    -0x40(%rbp),%rax
movups (%rax),%xmm1
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11a1 <xmm_triad+0xa1>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
mov    -0x8(%rbp),%rdx
movslq -0x24(%rbp),%rsi
shl    $0x2,%rsi
add    %rsi,%rdx
movups (%rax),%xmm0
mulps  %xmm1,%xmm0
addps  (%rcx),%xmm0
movups %xmm0,(%rdx)
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
jmp    114e <xmm_triad+0x4e>
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmm_triad>:
shufps $0x0,%xmm0,%xmm0
movaps %xmm0,-0x18(%rsp)
lea    -0x18(%rsp),%rax
movups (%rax),%xmm1
test   %ecx,%ecx
jle    113f <xmm_triad+0x3f>
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
movups (%rdx),%xmm0
mulps  %xmm1,%xmm0
addps  (%rsi),%xmm0
movups %xmm0,(%rdi)
add    $0x10,%rdi
add    $0x10,%rsi
add    $0x10,%rdx
add    $0x4,%eax
cmp    %ecx,%eax
jl     1120 <xmm_triad+0x20>
ret

LLVM IR (-O0):
define void @xmm_triad(float* %0, float* %1, float* %2, float %3, i32 %4) {
%6 = alloca float*
%7 = alloca float*
%8 = alloca float*
%9 = alloca float
%10 = alloca i32
%11 = alloca i32
%12 = alloca [4 x float]
store float* %0, float** %6
store float* %1, float** %7
store float* %2, float** %8
store float %3, float* %9
store i32 %4, i32* %10
%13 = getelementptr inbounds [4 x float], [4 x float]* %12, i64 0, i64 0
%14 = load float, float* %9
store float %14, float* %13
%15 = getelementptr inbounds float, float* %13, i64 1
%16 = load float, float* %9
store float %16, float* %15
%17 = getelementptr inbounds float, float* %15, i64 1
%18 = load float, float* %9
store float %18, float* %17
%19 = getelementptr inbounds float, float* %17, i64 1
%20 = load float, float* %9
store float %20, float* %19
%21 = getelementptr inbounds [4 x float], [4 x float]* %12, i64 0, i64 0
store i32 0, i32* %11
br label %22
22: ; preds = %39, %5
%23 = load i32, i32* %11
%24 = load i32, i32* %10
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %42
26: ; preds = %22
%27 = load float*, float** %8
%28 = load i32, i32* %11
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds float, float* %27, i64 %29
%31 = load float*, float** %7
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds float, float* %31, i64 %33
%35 = load float*, float** %6
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds float, float* %35, i64 %37
br label %39
39: ; preds = %26
%40 = load i32, i32* %11
%41 = add nsw i32 %40, 4
store i32 %41, i32* %11
br label %22
42: ; preds = %22
ret void
}

LLVM IR (-O2):
define void @xmm_triad(float* %0, float* %1, float* %2, float %3, i32 %4) {
%6 = alloca <4 x float>
%7 = bitcast <4 x float>* %6 to i8*
%8 = getelementptr inbounds <4 x float>, <4 x float>* %6, i64 0, i64 0
%9 = insertelement <4 x float> poison, float %3, i64 0
%10 = shufflevector <4 x float> %9, <4 x float> poison, <4 x i32> zeroinitializer
store <4 x float> %10, <4 x float>* %6
%11 = icmp sgt i32 %4, 0
br i1 %11, label %12, label %20
12: ; preds = %5, %12
%13 = phi i64 [ %17, %12 ], [ 0, %5 ]
%14 = getelementptr inbounds float, float* %2, i64 %13
%15 = getelementptr inbounds float, float* %1, i64 %13
%16 = getelementptr inbounds float, float* %0, i64 %13
%17 = add nuw i64 %13, 4
%18 = trunc i64 %17 to i32
%19 = icmp slt i32 %18, %4
br i1 %19, label %12, label %20
20: ; preds = %12, %5
ret void
}