#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 */
int /*<<< orphan*/ assert(double const *);
double sqrt(double);
double vectord_length(double const *, size_t);
int /*<<< orphan*/ vectord_multiply(double const *, size_t, int, double *);
int /*<<< orphan*/ vectord_remove_mean(double const *, size_t, double *);
void vectord_normalize_to_unit_length(const double *self, size_t size,
                                      bool remove_mean, double *output) {
  assert(self);
  assert(output);
  if (remove_mean) {
    vectord_remove_mean(self, size, output);
    double length = 0.0;
    for (size_t i = 0; i < size; i++)
      length += output[i] * output[i];
    length = sqrt(length);
    if (length > 0) {
      for (size_t i = 0; i < size; i++)
        output[i] /= length;
    }
  } else {
    double length = vectord_length(self, size);
    if (length > 0)
      vectord_multiply(self, size, 1 / length, output);
    else
      vectord_multiply(self, size, 1, output);
  }
}


x86 assembly (clang, -O0):
<vectord_normalize_to_unit_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rdi
call   1050 <assert@plt>
cmpl   $0x0,-0x14(%rbp)
je     125c <vectord_normalize_to_unit_length+0x10c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <vectord_remove_mean@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ef <vectord_normalize_to_unit_length+0x9f>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a4 <vectord_normalize_to_unit_length+0x54>
movsd  -0x28(%rbp),%xmm0
call   1060 <sqrt@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1257 <vectord_normalize_to_unit_length+0x107>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1252 <vectord_normalize_to_unit_length+0x102>
movsd  -0x28(%rbp),%xmm1
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1218 <vectord_normalize_to_unit_length+0xc8>
jmp    1257 <vectord_normalize_to_unit_length+0x107>
jmp    12c2 <vectord_normalize_to_unit_length+0x172>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <vectord_length@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12a7 <vectord_normalize_to_unit_length+0x157>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movsd  0xd70(%rip),%xmm0
divsd  -0x40(%rbp),%xmm0
cvttsd2si %xmm0,%edx
mov    -0x20(%rbp),%rcx
call   1040 <vectord_multiply@plt>
jmp    12bd <vectord_normalize_to_unit_length+0x16d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <vectord_multiply@plt>
jmp    12c2 <vectord_normalize_to_unit_length+0x172>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vectord_normalize_to_unit_length>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1050 <assert@plt>
test   %ebp,%ebp
je     11ad <vectord_normalize_to_unit_length+0x5d>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <vectord_remove_mean@plt>
xorpd  %xmm1,%xmm1
xorpd  %xmm0,%xmm0
test   %r14,%r14
je     1266 <vectord_normalize_to_unit_length+0x116>
lea    -0x1(%r14),%rcx
mov    %r14d,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    11ef <vectord_normalize_to_unit_length+0x9f>
xorpd  %xmm0,%xmm0
xor    %ecx,%ecx
jmp    1240 <vectord_normalize_to_unit_length+0xf0>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <vectord_length@plt>
mov    $0x1,%edx
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11d7 <vectord_normalize_to_unit_length+0x87>
movsd  0xe31(%rip),%xmm1
divsd  %xmm0,%xmm1
cvttsd2si %xmm1,%edx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <vectord_multiply@plt>
mov    %r14,%rdx
and    $0xfffffffffffffffc,%rdx
xorpd  %xmm0,%xmm0
xor    %ecx,%ecx
nopl   0x0(%rax)
movsd  (%rbx,%rcx,8),%xmm2
movsd  0x8(%rbx,%rcx,8),%xmm3
mulsd  %xmm2,%xmm2
addsd  %xmm0,%xmm2
mulsd  %xmm3,%xmm3
addsd  %xmm2,%xmm3
movsd  0x10(%rbx,%rcx,8),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm3,%xmm2
movsd  0x18(%rbx,%rcx,8),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm2,%xmm0
add    $0x4,%rcx
cmp    %rcx,%rdx
jne    1200 <vectord_normalize_to_unit_length+0xb0>
test   %rax,%rax
je     1266 <vectord_normalize_to_unit_length+0x116>
lea    (%rbx,%rcx,8),%rcx
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movsd  (%rcx,%rdx,8),%xmm2
mulsd  %xmm2,%xmm2
addsd  %xmm2,%xmm0
add    $0x1,%rdx
cmp    %rdx,%rax
jne    1250 <vectord_normalize_to_unit_length+0x100>
ucomisd %xmm1,%xmm0
jb     1272 <vectord_normalize_to_unit_length+0x122>
sqrtsd %xmm0,%xmm0
jmp    1277 <vectord_normalize_to_unit_length+0x127>
call   1060 <sqrt@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1327 <vectord_normalize_to_unit_length+0x1d7>
test   %r14,%r14
je     1327 <vectord_normalize_to_unit_length+0x1d7>
cmp    $0x2,%r14
jae    1298 <vectord_normalize_to_unit_length+0x148>
xor    %eax,%eax
jmp    1310 <vectord_normalize_to_unit_length+0x1c0>
mov    %r14,%rax
and    $0xfffffffffffffffe,%rax
movapd %xmm0,%xmm1
unpcklpd %xmm0,%xmm1
lea    -0x2(%rax),%rdx
mov    %rdx,%rcx
shr    %rcx
add    $0x1,%rcx
test   %rdx,%rdx
je     1332 <vectord_normalize_to_unit_length+0x1e2>
mov    %rcx,%rsi
and    $0xfffffffffffffffe,%rsi
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movupd (%rbx,%rdx,8),%xmm2
movupd 0x10(%rbx,%rdx,8),%xmm3
divpd  %xmm1,%xmm2
movupd %xmm2,(%rbx,%rdx,8)
divpd  %xmm1,%xmm3
movupd %xmm3,0x10(%rbx,%rdx,8)
add    $0x4,%rdx
add    $0xfffffffffffffffe,%rsi
jne    12d0 <vectord_normalize_to_unit_length+0x180>
test   $0x1,%cl
je     130b <vectord_normalize_to_unit_length+0x1bb>
movupd (%rbx,%rdx,8),%xmm2
divpd  %xmm1,%xmm2
movupd %xmm2,(%rbx,%rdx,8)
cmp    %r14,%rax
je     1327 <vectord_normalize_to_unit_length+0x1d7>
movsd  (%rbx,%rax,8),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rax,8)
add    $0x1,%rax
cmp    %rax,%r14
jne    1310 <vectord_normalize_to_unit_length+0x1c0>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edx,%edx
test   $0x1,%cl
jne    12fd <vectord_normalize_to_unit_length+0x1ad>
jmp    130b <vectord_normalize_to_unit_length+0x1bb>

LLVM IR (-O0):
define void @vectord_normalize_to_unit_length(double* %0, i64 %1, i32 %2, double* %3) {
%5 = alloca double*
%6 = alloca i64
%7 = alloca i32
%8 = alloca double*
%9 = alloca double
%10 = alloca i64
%11 = alloca i64
%12 = alloca double
store double* %0, double** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store double* %3, double** %8
%13 = load double*, double** %5
%14 = call i32 @assert(double* %13)
%15 = load double*, double** %8
%16 = call i32 @assert(double* %15)
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %64
19: ; preds = %4
%20 = load double*, double** %5
%21 = load i64, i64* %6
%22 = load double*, double** %8
%23 = call i32 @vectord_remove_mean(double* %20, i64 %21, double* %22)
store double 0.000000e+00, double* %9
store i64 0, i64* %10
br label %24
24: ; preds = %39, %19
%25 = load i64, i64* %10
%26 = load i64, i64* %6
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %42
28: ; preds = %24
%29 = load double*, double** %8
%30 = load i64, i64* %10
%31 = getelementptr inbounds double, double* %29, i64 %30
%32 = load double, double* %31
%33 = load double*, double** %8
%34 = load i64, i64* %10
%35 = getelementptr inbounds double, double* %33, i64 %34
%36 = load double, double* %35
%37 = load double, double* %9
%38 = call double @llvm.fmuladd.f64(double %32, double %36, double %37)
store double %38, double* %9
br label %39
39: ; preds = %28
%40 = load i64, i64* %10
%41 = add i64 %40, 1
store i64 %41, i64* %10
br label %24
42: ; preds = %24
%43 = load double, double* %9
%44 = call double @sqrt(double %43)
store double %44, double* %9
%45 = load double, double* %9
%46 = fcmp ogt double %45, 0.000000e+00
br i1 %46, label %47, label %63
47: ; preds = %42
store i64 0, i64* %11
br label %48
48: ; preds = %59, %47
%49 = load i64, i64* %11
%50 = load i64, i64* %6
%51 = icmp ult i64 %49, %50
br i1 %51, label %52, label %62
52: ; preds = %48
%53 = load double, double* %9
%54 = load double*, double** %8
%55 = load i64, i64* %11
%56 = getelementptr inbounds double, double* %54, i64 %55
%57 = load double, double* %56
%58 = fdiv double %57, %53
store double %58, double* %56
br label %59
59: ; preds = %52
%60 = load i64, i64* %11
%61 = add i64 %60, 1
store i64 %61, i64* %11
br label %48
62: ; preds = %48
br label %63
63: ; preds = %62, %42
br label %84
64: ; preds = %4
%65 = load double*, double** %5
%66 = load i64, i64* %6
%67 = call double @vectord_length(double* %65, i64 %66)
store double %67, double* %12
%68 = load double, double* %12
%69 = fcmp ogt double %68, 0.000000e+00
br i1 %69, label %70, label %78
70: ; preds = %64
%71 = load double*, double** %5
%72 = load i64, i64* %6
%73 = load double, double* %12
%74 = fdiv double 1.000000e+00, %73
%75 = fptosi double %74 to i32
%76 = load double*, double** %8
%77 = call i32 @vectord_multiply(double* %71, i64 %72, i32 %75, double* %76)
br label %83
78: ; preds = %64
%79 = load double*, double** %5
%80 = load i64, i64* %6
%81 = load double*, double** %8
%82 = call i32 @vectord_multiply(double* %79, i64 %80, i32 1, double* %81)
br label %83
83: ; preds = %78, %70
br label %84
84: ; preds = %83, %63
ret void
}

LLVM IR (-O2):
define void @vectord_normalize_to_unit_length(double* %0, i64 %1, i32 %2, double* %3) {
%5 = tail call i32 @assert(double* %0)
%6 = tail call i32 @assert(double* %3)
%7 = icmp eq i32 %2, 0
br i1 %7, label %110, label %8
8: ; preds = %4
%9 = tail call i32 @vectord_remove_mean(double* %0, i64 %1, double* %3)
%10 = icmp eq i64 %1, 0
br i1 %10, label %32, label %11
11: ; preds = %8
%12 = add i64 %1, -1
%13 = and i64 %1, 3
%14 = icmp ult i64 %12, 3
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = and i64 %1, -4
br label %81
17: ; preds = %81, %11
%18 = phi double [ undef, %11 ], [ %99, %81 ]
%19 = phi double [ 0.000000e+00, %11 ], [ %99, %81 ]
%20 = phi i64 [ 0, %11 ], [ %100, %81 ]
%21 = icmp eq i64 %13, 0
br i1 %21, label %32, label %22
22: ; preds = %17, %22
%23 = phi double [ %28, %22 ], [ %19, %17 ]
%24 = phi i64 [ %29, %22 ], [ %20, %17 ]
%25 = phi i64 [ %30, %22 ], [ 0, %17 ]
%26 = getelementptr inbounds double, double* %3, i64 %24
%27 = load double, double* %26
%28 = tail call double @llvm.fmuladd.f64(double %27, double %27, double %23)
%29 = add nuw i64 %24, 1
%30 = add i64 %25, 1
%31 = icmp eq i64 %30, %13
br i1 %31, label %32, label %22
32: ; preds = %17, %22, %8
%33 = phi double [ 0.000000e+00, %8 ], [ %18, %17 ], [ %28, %22 ]
%34 = tail call double @sqrt(double %33)
%35 = fcmp ogt double %34, 0.000000e+00
%36 = icmp ne i64 %1, 0
%37 = and i1 %35, %36
br i1 %37, label %38, label %119
38: ; preds = %32
%39 = icmp ult i64 %1, 2
br i1 %39, label %79, label %40
40: ; preds = %38
%41 = and i64 %1, -2
%42 = insertelement <2 x double> poison, double %34, i64 0
%43 = shufflevector <2 x double> %42, <2 x double> poison, <2 x i32> zeroinitializer
%44 = add i64 %41, -2
%45 = lshr exact i64 %44, 1
%46 = add nuw i64 %45, 1
%47 = and i64 %46, 1
%48 = icmp eq i64 %44, 0
br i1 %48, label %68, label %49
49: ; preds = %40
%50 = and i64 %46, -2
br label %51
51: ; preds = %51, %49
%52 = phi i64 [ 0, %49 ], [ %65, %51 ]
%53 = phi i64 [ 0, %49 ], [ %66, %51 ]
%54 = getelementptr inbounds double, double* %3, i64 %52
%55 = bitcast double* %54 to <2 x double>*
%56 = load <2 x double>, <2 x double>* %55
%57 = fdiv <2 x double> %56, %43
%58 = bitcast double* %54 to <2 x double>*
store <2 x double> %57, <2 x double>* %58
%59 = or i64 %52, 2
%60 = getelementptr inbounds double, double* %3, i64 %59
%61 = bitcast double* %60 to <2 x double>*
%62 = load <2 x double>, <2 x double>* %61
%63 = fdiv <2 x double> %62, %43
%64 = bitcast double* %60 to <2 x double>*
store <2 x double> %63, <2 x double>* %64
%65 = add nuw i64 %52, 4
%66 = add nuw i64 %53, 2
%67 = icmp eq i64 %66, %50
br i1 %67, label %68, label %51
68: ; preds = %51, %40
%69 = phi i64 [ 0, %40 ], [ %65, %51 ]
%70 = icmp eq i64 %47, 0
br i1 %70, label %77, label %71
71: ; preds = %68
%72 = getelementptr inbounds double, double* %3, i64 %69
%73 = bitcast double* %72 to <2 x double>*
%74 = load <2 x double>, <2 x double>* %73
%75 = fdiv <2 x double> %74, %43
%76 = bitcast double* %72 to <2 x double>*
store <2 x double> %75, <2 x double>* %76
br label %77
77: ; preds = %68, %71
%78 = icmp eq i64 %41, %1
br i1 %78, label %119, label %79
79: ; preds = %38, %77
%80 = phi i64 [ 0, %38 ], [ %41, %77 ]
br label %103
81: ; preds = %81, %15
%82 = phi double [ 0.000000e+00, %15 ], [ %99, %81 ]
%83 = phi i64 [ 0, %15 ], [ %100, %81 ]
%84 = phi i64 [ 0, %15 ], [ %101, %81 ]
%85 = getelementptr inbounds double, double* %3, i64 %83
%86 = load double, double* %85
%87 = tail call double @llvm.fmuladd.f64(double %86, double %86, double %82)
%88 = or i64 %83, 1
%89 = getelementptr inbounds double, double* %3, i64 %88
%90 = load double, double* %89
%91 = tail call double @llvm.fmuladd.f64(double %90, double %90, double %87)
%92 = or i64 %83, 2
%93 = getelementptr inbounds double, double* %3, i64 %92
%94 = load double, double* %93
%95 = tail call double @llvm.fmuladd.f64(double %94, double %94, double %91)
%96 = or i64 %83, 3
%97 = getelementptr inbounds double, double* %3, i64 %96
%98 = load double, double* %97
%99 = tail call double @llvm.fmuladd.f64(double %98, double %98, double %95)
%100 = add nuw i64 %83, 4
%101 = add i64 %84, 4
%102 = icmp eq i64 %101, %16
br i1 %102, label %17, label %81
103: ; preds = %79, %103
%104 = phi i64 [ %108, %103 ], [ %80, %79 ]
%105 = getelementptr inbounds double, double* %3, i64 %104
%106 = load double, double* %105
%107 = fdiv double %106, %34
store double %107, double* %105
%108 = add nuw i64 %104, 1
%109 = icmp eq i64 %108, %1
br i1 %109, label %119, label %103
110: ; preds = %4
%111 = tail call double @vectord_length(double* %0, i64 %1)
%112 = fcmp ogt double %111, 0.000000e+00
br i1 %112, label %113, label %116
113: ; preds = %110
%114 = fdiv double 1.000000e+00, %111
%115 = fptosi double %114 to i32
br label %116
116: ; preds = %110, %113
%117 = phi i32 [ %115, %113 ], [ 1, %110 ]
%118 = tail call i32 @vectord_multiply(double* %0, i64 %1, i32 %117, double* %3)
br label %119
119: ; preds = %103, %77, %116, %32
ret void
}