#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*/ __m128;

/* Variables and functions */
int /*<<< orphan*/ _mm_and_ps(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_andnot_ps(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_cmpgt_ps(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_load_ps(float *);
int /*<<< orphan*/ _mm_or_ps(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_setzero_ps();
int /*<<< orphan*/ _mm_sqrt_ps(int /*<<< orphan*/);
double sqrtf(float);
void compute_sqrt_sse(float *in, float *out, int n) {
  __m128 *in_vec = (__m128 *)in;
  __m128 *out_vec = (__m128 *)out;
  int k = n / 4;
  __m128 zero = _mm_setzero_ps();
  for (int i = 0; i < k; i++) {
    __m128 v = _mm_load_ps((float *)&in_vec[i]);
    __m128 sqrt_vec = _mm_sqrt_ps(v);
    __m128 mask = _mm_cmpgt_ps(v, zero);
    __m128 gtzero_vec = _mm_and_ps(mask, sqrt_vec);
    __m128 lezero_vec = _mm_andnot_ps(mask, zero);
    out_vec[i] = _mm_or_ps(gtzero_vec, lezero_vec);
  }
  for (int i = k * 4; i < n; i++)
    out[i] = in[i] > 0 ? sqrtf(in[i]) : 0.0;
}


x86 assembly (clang, -O0):
<compute_sqrt_sse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   10a0 <_mm_setzero_ps@plt>
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1240 <compute_sqrt_sse+0xc0>
mov    -0x20(%rbp),%rdi
movslq -0x34(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
call   1030 <_mm_load_ps@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   1090 <_mm_sqrt_ps@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1040 <_mm_cmpgt_ps@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
call   1080 <_mm_and_ps@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1070 <_mm_andnot_ps@plt>
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x48(%rbp),%esi
call   1060 <_mm_or_ps@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11c2 <compute_sqrt_sse+0x42>
mov    -0x2c(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12bb <compute_sqrt_sse+0x13b>
mov    -0x8(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    128a <compute_sqrt_sse+0x10a>
mov    -0x8(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
call   1050 <sqrtf@plt>
movsd  %xmm0,-0x58(%rbp)
jmp    1297 <compute_sqrt_sse+0x117>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
jmp    1297 <compute_sqrt_sse+0x117>
movsd  -0x58(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
mov    -0x10(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1249 <compute_sqrt_sse+0xc9>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_sqrt_sse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,0x10(%rsp)
lea    0x3(%rbx),%eax
test   %edx,%edx
cmovns %edx,%eax
sar    $0x2,%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   10a0 <_mm_setzero_ps@plt>
mov    %rbx,0x18(%rsp)
cmp    $0x4,%ebx
jl     122a <compute_sqrt_sse+0xaa>
mov    %eax,%r13d
mov    0xc(%rsp),%eax
mov    %rax,0x20(%rsp)
xor    %r12d,%r12d
mov    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <_mm_load_ps@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <_mm_sqrt_ps@plt>
mov    %r15,%r14
mov    %eax,%r15d
mov    %ebp,%edi
mov    %r13d,%esi
call   1040 <_mm_cmpgt_ps@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15d,%esi
call   1080 <_mm_and_ps@plt>
mov    %eax,%r15d
mov    %ebp,%edi
mov    %r13d,%esi
call   1070 <_mm_andnot_ps@plt>
mov    %r15d,%edi
mov    %r14,%r15
mov    %eax,%esi
call   1060 <_mm_or_ps@plt>
mov    %eax,(%r14,%r12,4)
add    $0x1,%r12
add    $0x4,%rbx
cmp    %r12,0x20(%rsp)
jne    11d0 <compute_sqrt_sse+0x50>
mov    0xc(%rsp),%r12d
shl    $0x2,%r12d
mov    0x18(%rsp),%r14
cmp    %r14d,%r12d
mov    0x10(%rsp),%rbp
jge    1277 <compute_sqrt_sse+0xf7>
movslq %r12d,%rbx
not    %r12d
test   $0x1,%r14b
je     1272 <compute_sqrt_sse+0xf2>
movss  0x0(%rbp,%rbx,4),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1268 <compute_sqrt_sse+0xe8>
call   1050 <sqrtf@plt>
xorps  %xmm1,%xmm1
cvtsd2ss %xmm0,%xmm1
movss  %xmm1,(%r15,%rbx,4)
or     $0x1,%rbx
add    %r14d,%r12d
jne    1286 <compute_sqrt_sse+0x106>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%r14d
xorps  %xmm2,%xmm2
jmp    12a7 <compute_sqrt_sse+0x127>
xchg   %ax,%ax
xorps  %xmm0,%xmm0
cvtsd2ss %xmm3,%xmm0
movss  %xmm0,0x4(%r15,%rbx,4)
add    $0x2,%rbx
cmp    %ebx,%r14d
je     1277 <compute_sqrt_sse+0xf7>
movss  0x0(%rbp,%rbx,4),%xmm0
xorps  %xmm3,%xmm3
ucomiss %xmm2,%xmm0
xorps  %xmm1,%xmm1
jbe    12c6 <compute_sqrt_sse+0x146>
call   1050 <sqrtf@plt>
xorps  %xmm3,%xmm3
xorps  %xmm2,%xmm2
movaps %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsd2ss %xmm1,%xmm0
movss  %xmm0,(%r15,%rbx,4)
movss  0x4(%rbp,%rbx,4),%xmm0
ucomiss %xmm2,%xmm0
jbe    1290 <compute_sqrt_sse+0x110>
call   1050 <sqrtf@plt>
xorps  %xmm2,%xmm2
movaps %xmm0,%xmm3
jmp    1290 <compute_sqrt_sse+0x110>

LLVM IR (-O0):
define void @compute_sqrt_sse(float* %0, float* %1, i32 %2) {
%4 = alloca float*
%5 = alloca float*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store float* %0, float** %4
store float* %1, float** %5
store i32 %2, i32* %6
%18 = load float*, float** %4
%19 = bitcast float* %18 to i32*
store i32* %19, i32** %7
%20 = load float*, float** %5
%21 = bitcast float* %20 to i32*
store i32* %21, i32** %8
%22 = load i32, i32* %6
%23 = sdiv i32 %22, 4
store i32 %23, i32* %9
%24 = call i32 (...) @_mm_setzero_ps()
store i32 %24, i32* %10
store i32 0, i32* %11
br label %25
25: ; preds = %54, %3
%26 = load i32, i32* %11
%27 = load i32, i32* %9
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %57
29: ; preds = %25
%30 = load i32*, i32** %7
%31 = load i32, i32* %11
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = bitcast i32* %33 to float*
%35 = call i32 @_mm_load_ps(float* %34)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = call i32 @_mm_sqrt_ps(i32 %36)
store i32 %37, i32* %13
%38 = load i32, i32* %12
%39 = load i32, i32* %10
%40 = call i32 @_mm_cmpgt_ps(i32 %38, i32 %39)
store i32 %40, i32* %14
%41 = load i32, i32* %14
%42 = load i32, i32* %13
%43 = call i32 @_mm_and_ps(i32 %41, i32 %42)
store i32 %43, i32* %15
%44 = load i32, i32* %14
%45 = load i32, i32* %10
%46 = call i32 @_mm_andnot_ps(i32 %44, i32 %45)
store i32 %46, i32* %16
%47 = load i32, i32* %15
%48 = load i32, i32* %16
%49 = call i32 @_mm_or_ps(i32 %47, i32 %48)
%50 = load i32*, i32** %8
%51 = load i32, i32* %11
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
store i32 %49, i32* %53
br label %54
54: ; preds = %29
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %25
57: ; preds = %25
%58 = load i32, i32* %9
%59 = mul nsw i32 %58, 4
store i32 %59, i32* %17
br label %60
60: ; preds = %86, %57
%61 = load i32, i32* %17
%62 = load i32, i32* %6
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %89
64: ; preds = %60
%65 = load float*, float** %4
%66 = load i32, i32* %17
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds float, float* %65, i64 %67
%69 = load float, float* %68
%70 = fcmp ogt float %69, 0.000000e+00
br i1 %70, label %71, label %78
71: ; preds = %64
%72 = load float*, float** %4
%73 = load i32, i32* %17
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds float, float* %72, i64 %74
%76 = load float, float* %75
%77 = call double @sqrtf(float %76)
br label %79
78: ; preds = %64
br label %79
79: ; preds = %78, %71
%80 = phi double [ %77, %71 ], [ 0.000000e+00, %78 ]
%81 = fptrunc double %80 to float
%82 = load float*, float** %5
%83 = load i32, i32* %17
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds float, float* %82, i64 %84
store float %81, float* %85
br label %86
86: ; preds = %79
%87 = load i32, i32* %17
%88 = add nsw i32 %87, 1
store i32 %88, i32* %17
br label %60
89: ; preds = %60
ret void
}

LLVM IR (-O2):
define void @compute_sqrt_sse(float* %0, float* nocapture writeonly %1, i32 %2) {
%4 = sdiv i32 %2, 4
%5 = tail call i32 (...) @_mm_setzero_ps()
%6 = icmp sgt i32 %2, 3
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = zext i32 %4 to i64
br label %32
9: ; preds = %32, %3
%10 = shl nsw i32 %4, 2
%11 = icmp slt i32 %10, %2
br i1 %11, label %12, label %45
12: ; preds = %9
%13 = sext i32 %10 to i64
%14 = xor i32 %10, -1
%15 = and i32 %2, 1
%16 = icmp eq i32 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %12
%18 = getelementptr inbounds float, float* %0, i64 %13
%19 = load float, float* %18
%20 = fcmp ogt float %19, 0.000000e+00
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = tail call double @sqrtf(float %19)
%23 = fptrunc double %22 to float
br label %24
24: ; preds = %21, %17
%25 = phi float [ %23, %21 ], [ 0.000000e+00, %17 ]
%26 = getelementptr inbounds float, float* %1, i64 %13
store float %25, float* %26
%27 = or i64 %13, 1
br label %28
28: ; preds = %24, %12
%29 = phi i64 [ %13, %12 ], [ %27, %24 ]
%30 = sub i32 0, %2
%31 = icmp eq i32 %14, %30
br i1 %31, label %45, label %46
32: ; preds = %7, %32
%33 = phi i64 [ 0, %7 ], [ %43, %32 ]
%34 = getelementptr inbounds float, float* %0, i64 %33
%35 = tail call i32 @_mm_load_ps(float* %34)
%36 = tail call i32 @_mm_sqrt_ps(i32 %35)
%37 = tail call i32 @_mm_cmpgt_ps(i32 %35, i32 %5)
%38 = tail call i32 @_mm_and_ps(i32 %37, i32 %36)
%39 = tail call i32 @_mm_andnot_ps(i32 %37, i32 %5)
%40 = tail call i32 @_mm_or_ps(i32 %38, i32 %39)
%41 = getelementptr inbounds float, float* %1, i64 %33
%42 = bitcast float* %41 to i32*
store i32 %40, i32* %42
%43 = add nuw nsw i64 %33, 1
%44 = icmp eq i64 %43, %8
br i1 %44, label %9, label %32
45: ; preds = %28, %63, %9
ret void
46: ; preds = %28, %63
%47 = phi i64 [ %67, %63 ], [ %29, %28 ]
%48 = getelementptr inbounds float, float* %0, i64 %47
%49 = load float, float* %48
%50 = fcmp ogt float %49, 0.000000e+00
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = tail call double @sqrtf(float %49)
br label %53
53: ; preds = %46, %51
%54 = phi double [ %52, %51 ], [ 0.000000e+00, %46 ]
%55 = fptrunc double %54 to float
%56 = getelementptr inbounds float, float* %1, i64 %47
store float %55, float* %56
%57 = add nsw i64 %47, 1
%58 = getelementptr inbounds float, float* %0, i64 %57
%59 = load float, float* %58
%60 = fcmp ogt float %59, 0.000000e+00
br i1 %60, label %61, label %63
61: ; preds = %53
%62 = tail call double @sqrtf(float %59)
br label %63
63: ; preds = %61, %53
%64 = phi double [ %62, %61 ], [ 0.000000e+00, %53 ]
%65 = fptrunc double %64 to float
%66 = getelementptr inbounds float, float* %1, i64 %57
store float %65, float* %66
%67 = add nsw i64 %47, 2
%68 = trunc i64 %67 to i32
%69 = icmp eq i32 %68, %2
br i1 %69, label %45, label %46
}