#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 */
float sqrt(float);
float *unit(float *v, float *r) {
  float len = 0;
  short i;
  for (i = 0; i < 3; i++)
    len += v[i] * v[i];
  len = sqrt(len);
  if (len)
    for (i = 0; i < 3; i++)
      r[i] = v[i] / len;
  else
    r[0] = r[1] = r[2] = 0;
  return (r);
}


x86 assembly (clang, -O0):
<unit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
movw   $0x0,-0x16(%rbp)
movswl -0x16(%rbp),%eax
cmp    $0x3,%eax
jge    117a <unit+0x6a>
mov    -0x8(%rbp),%rax
movswq -0x16(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
mov    -0x8(%rbp),%rax
movswq -0x16(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm2
movss  -0x14(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x16(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x16(%rbp)
jmp    112e <unit+0x1e>
movss  -0x14(%rbp),%xmm0
call   1030 <sqrt@plt>
movss  %xmm0,-0x14(%rbp)
movss  -0x14(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jne    11a5 <unit+0x95>
jp     11a5 <unit+0x95>
jmp    11ef <unit+0xdf>
movw   $0x0,-0x16(%rbp)
movswl -0x16(%rbp),%eax
cmp    $0x3,%eax
jge    11ea <unit+0xda>
mov    -0x8(%rbp),%rax
movswq -0x16(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
divss  -0x14(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movswq -0x16(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x16(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x16(%rbp)
jmp    11ab <unit+0x9b>
jmp    1212 <unit+0x102>
mov    -0x10(%rbp),%rax
xorps  %xmm0,%xmm0
movss  %xmm0,0x8(%rax)
mov    -0x10(%rbp),%rax
xorps  %xmm0,%xmm0
movss  %xmm0,0x4(%rax)
mov    -0x10(%rbp),%rax
xorps  %xmm0,%xmm0
movss  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unit>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movss  (%rdi),%xmm0
movss  0x4(%rdi),%xmm1
mulss  %xmm0,%xmm0
xorps  %xmm2,%xmm2
addss  %xmm2,%xmm0
mulss  %xmm1,%xmm1
addss  %xmm0,%xmm1
movss  0x8(%rdi),%xmm0
mulss  %xmm0,%xmm0
addss  %xmm1,%xmm0
ucomiss %xmm2,%xmm0
jb     114e <unit+0x3e>
sqrtss %xmm0,%xmm0
jmp    1156 <unit+0x46>
call   1030 <sqrt@plt>
xorps  %xmm2,%xmm2
ucomiss %xmm2,%xmm0
jne    115d <unit+0x4d>
jnp    118a <unit+0x7a>
movss  (%rbx),%xmm1
divss  %xmm0,%xmm1
movss  %xmm1,(%r14)
movss  0x4(%rbx),%xmm1
divss  %xmm0,%xmm1
movss  %xmm1,0x4(%r14)
movss  0x8(%rbx),%xmm1
divss  %xmm0,%xmm1
movss  %xmm1,0x8(%r14)
jmp    1199 <unit+0x89>
movl   $0x0,0x8(%r14)
movq   $0x0,(%r14)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define float* @unit(float* nocapture %0, float* returned writeonly %1) {
%3 = load float, float* %0
%4 = tail call float @llvm.fmuladd.f32(float %3, float %3, float 0.000000e+00)
%5 = getelementptr inbounds float, float* %0, i64 1
%6 = load float, float* %5
%7 = tail call float @llvm.fmuladd.f32(float %6, float %6, float %4)
%8 = getelementptr inbounds float, float* %0, i64 2
%9 = load float, float* %8
%10 = tail call float @llvm.fmuladd.f32(float %9, float %9, float %7)
%11 = tail call float @sqrt(float %10)
%12 = fcmp une float %11, 0.000000e+00
br i1 %12, label %13, label %22
13: ; preds = %2
%14 = load float, float* %0
%15 = fdiv float %14, %11
store float %15, float* %1
%16 = load float, float* %5
%17 = fdiv float %16, %11
%18 = getelementptr inbounds float, float* %1, i64 1
store float %17, float* %18
%19 = load float, float* %8
%20 = fdiv float %19, %11
%21 = getelementptr inbounds float, float* %1, i64 2
store float %20, float* %21
br label %25
22: ; preds = %2
%23 = getelementptr inbounds float, float* %1, i64 2
store float 0.000000e+00, float* %23
%24 = getelementptr inbounds float, float* %1, i64 1
store float 0.000000e+00, float* %24
store float 0.000000e+00, float* %1
br label %25
25: ; preds = %13, %22
ret float* %1
}