_Bool rotation_matrix_3D(double P[][3], double *p) {
  static double I[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
  double scale, C1, C2;
  C1 = sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]);
  C2 = sqrt(p[0] * p[0] + p[1] * p[1]);
  if ((C1 < 10e-10) || (C2 < (10e-5 * C1))) {
    memcpy(P, I, sizeof(I));
    return 0;
  } else {
    scale = 1.0 / (C1 * C2);
    P[0][0] = p[0] * p[2] * scale;
    P[0][1] = -p[1] * C1 * scale;
    P[0][2] = p[0] * C2 * scale;
    P[1][0] = p[1] * p[2] * scale;
    P[1][1] = p[0] * C1 * scale;
    P[1][2] = p[1] * C2 * scale;
    P[2][0] = -C2 * C2 * scale;
    P[2][1] = 0;
    P[2][2] = p[2] * C2 * scale;
    return 1;
  }
}


x86 assembly (clang, -O0):
<rotation_matrix_3D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm1
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm2
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm0
mov    -0x18(%rbp),%rax
mulsd  0x8(%rax),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
mov    -0x18(%rbp),%rax
movsd  0x10(%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  0x10(%rax),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1040 <sqrt@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm2
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm1
mov    -0x18(%rbp),%rax
mulsd  0x8(%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1040 <sqrt@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  0xe46(%rip),%xmm0
ucomisd -0x28(%rbp),%xmm0
ja     11e1 <rotation_matrix_3D+0xc1>
movsd  -0x30(%rbp),%xmm1
movsd  0xe36(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11ff <rotation_matrix_3D+0xdf>
mov    -0x10(%rbp),%rdi
lea    0x2e54(%rip),%rsi
mov    $0x48,%edx
call   1030 <memcpy@plt>
movb   $0x0,-0x1(%rbp)
jmp    1338 <rotation_matrix_3D+0x218>
movsd  -0x28(%rbp),%xmm1
mulsd  -0x30(%rbp),%xmm1
movsd  0xdff(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
mulsd  0x10(%rax),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,0x8(%rax)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
mulsd  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,0x10(%rax)
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm0
mov    -0x18(%rbp),%rax
mulsd  0x10(%rax),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,0x18(%rax)
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,0x20(%rax)
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm0
mulsd  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,0x28(%rax)
movsd  -0x30(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,0x30(%rax)
mov    -0x10(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,0x38(%rax)
mov    -0x18(%rbp),%rax
movsd  0x10(%rax),%xmm0
mulsd  -0x30(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,0x40(%rax)
movb   $0x1,-0x1(%rbp)
mov    -0x1(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rotation_matrix_3D>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
movsd  (%rsi),%xmm1
movsd  0x8(%rsi),%xmm0
mulsd  %xmm0,%xmm0
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0x10(%rsi),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm0
jb     114c <rotation_matrix_3D+0x3c>
sqrtsd %xmm0,%xmm4
jmp    1159 <rotation_matrix_3D+0x49>
call   1030 <sqrt@plt>
xorpd  %xmm2,%xmm2
movapd %xmm0,%xmm4
movsd  (%rbx),%xmm0
movsd  0x8(%rbx),%xmm1
mulsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jb     117a <rotation_matrix_3D+0x6a>
sqrtsd %xmm0,%xmm0
jmp    1189 <rotation_matrix_3D+0x79>
movsd  %xmm4,(%rsp)
call   1030 <sqrt@plt>
movsd  (%rsp),%xmm4
movsd  0xe6f(%rip),%xmm1
ucomisd %xmm4,%xmm1
seta   %cl
movsd  0xe68(%rip),%xmm1
mulsd  %xmm4,%xmm1
ucomisd %xmm0,%xmm1
seta   %al
or     %cl,%al
cmp    $0x1,%al
jne    11f2 <rotation_matrix_3D+0xe2>
mov    0xeb8(%rip),%rcx
mov    %rcx,0x40(%r14)
movaps 0xe9d(%rip),%xmm0
movups %xmm0,0x30(%r14)
movaps 0xe81(%rip),%xmm0
movups %xmm0,0x20(%r14)
movaps 0xe65(%rip),%xmm0
movups %xmm0,0x10(%r14)
movapd 0xe48(%rip),%xmm0
movupd %xmm0,(%r14)
jmp    12ad <rotation_matrix_3D+0x19d>
movapd %xmm4,%xmm2
mulsd  %xmm0,%xmm2
movsd  0xe0e(%rip),%xmm1
movsd  (%rbx),%xmm3
mulsd  0x10(%rbx),%xmm3
divsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm3
movsd  %xmm3,(%r14)
movsd  0x8(%rbx),%xmm2
movapd 0xdfb(%rip),%xmm3
xorpd  %xmm3,%xmm2
mulsd  %xmm4,%xmm2
mulsd  %xmm1,%xmm2
movsd  %xmm2,0x8(%r14)
movsd  (%rbx),%xmm2
mulsd  %xmm0,%xmm2
mulsd  %xmm1,%xmm2
movsd  %xmm2,0x10(%r14)
movsd  0x8(%rbx),%xmm2
mulsd  0x10(%rbx),%xmm2
mulsd  %xmm1,%xmm2
movsd  %xmm2,0x18(%r14)
mulsd  (%rbx),%xmm4
mulsd  %xmm1,%xmm4
movsd  %xmm4,0x20(%r14)
xorpd  %xmm0,%xmm3
movapd %xmm0,%xmm2
unpcklpd %xmm0,%xmm2
movsd  0x8(%rbx),%xmm4
unpcklpd %xmm3,%xmm4
mulpd  %xmm2,%xmm4
movapd %xmm1,%xmm2
unpcklpd %xmm1,%xmm2
mulpd  %xmm4,%xmm2
movupd %xmm2,0x28(%r14)
movq   $0x0,0x38(%r14)
mulsd  0x10(%rbx),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x40(%r14)
xor    $0x1,%al
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@rotation_matrix_3D.I = internal global [3 x [3 x double]] [[3 x double] [double 1.000000e+00, double 0.000000e+00, double 0.000000e+00], [3 x double] [double 0.000000e+00, double 1.000000e+00, double 0.000000e+00], [3 x double] [double 0.000000e+00, double 0.000000e+00, double 1.000000e+00]]
define zeroext i1 @rotation_matrix_3D([3 x double]* %0, double* %1) {
%3 = alloca i1
%4 = alloca [3 x double]*
%5 = alloca double*
%6 = alloca double
%7 = alloca double
%8 = alloca double
store [3 x double]* %0, [3 x double]** %4
store double* %1, double** %5
%9 = load double*, double** %5
%10 = getelementptr inbounds double, double* %9, i64 0
%11 = load double, double* %10
%12 = load double*, double** %5
%13 = getelementptr inbounds double, double* %12, i64 0
%14 = load double, double* %13
%15 = load double*, double** %5
%16 = getelementptr inbounds double, double* %15, i64 1
%17 = load double, double* %16
%18 = load double*, double** %5
%19 = getelementptr inbounds double, double* %18, i64 1
%20 = load double, double* %19
%21 = fmul double %17, %20
%22 = call double @llvm.fmuladd.f64(double %11, double %14, double %21)
%23 = load double*, double** %5
%24 = getelementptr inbounds double, double* %23, i64 2
%25 = load double, double* %24
%26 = load double*, double** %5
%27 = getelementptr inbounds double, double* %26, i64 2
%28 = load double, double* %27
%29 = call double @llvm.fmuladd.f64(double %25, double %28, double %22)
%30 = call double @sqrt(double %29)
store double %30, double* %7
%31 = load double*, double** %5
%32 = getelementptr inbounds double, double* %31, i64 0
%33 = load double, double* %32
%34 = load double*, double** %5
%35 = getelementptr inbounds double, double* %34, i64 0
%36 = load double, double* %35
%37 = load double*, double** %5
%38 = getelementptr inbounds double, double* %37, i64 1
%39 = load double, double* %38
%40 = load double*, double** %5
%41 = getelementptr inbounds double, double* %40, i64 1
%42 = load double, double* %41
%43 = fmul double %39, %42
%44 = call double @llvm.fmuladd.f64(double %33, double %36, double %43)
%45 = call double @sqrt(double %44)
store double %45, double* %8
%46 = load double, double* %7
%47 = fcmp olt double %46, 1.000000e-09
br i1 %47, label %53, label %48
48: ; preds = %2
%49 = load double, double* %8
%50 = load double, double* %7
%51 = fmul double 1.000000e-04, %50
%52 = fcmp olt double %49, %51
br i1 %52, label %53, label %56
53: ; preds = %48, %2
%54 = load [3 x double]*, [3 x double]** %4
%55 = bitcast [3 x double]* %54 to i8*
store i1 false, i1* %3
br label %148
56: ; preds = %48
%57 = load double, double* %7
%58 = load double, double* %8
%59 = fmul double %57, %58
%60 = fdiv double 1.000000e+00, %59
store double %60, double* %6
%61 = load double*, double** %5
%62 = getelementptr inbounds double, double* %61, i64 0
%63 = load double, double* %62
%64 = load double*, double** %5
%65 = getelementptr inbounds double, double* %64, i64 2
%66 = load double, double* %65
%67 = fmul double %63, %66
%68 = load double, double* %6
%69 = fmul double %67, %68
%70 = load [3 x double]*, [3 x double]** %4
%71 = getelementptr inbounds [3 x double], [3 x double]* %70, i64 0
%72 = getelementptr inbounds [3 x double], [3 x double]* %71, i64 0, i64 0
store double %69, double* %72
%73 = load double*, double** %5
%74 = getelementptr inbounds double, double* %73, i64 1
%75 = load double, double* %74
%76 = fneg double %75
%77 = load double, double* %7
%78 = fmul double %76, %77
%79 = load double, double* %6
%80 = fmul double %78, %79
%81 = load [3 x double]*, [3 x double]** %4
%82 = getelementptr inbounds [3 x double], [3 x double]* %81, i64 0
%83 = getelementptr inbounds [3 x double], [3 x double]* %82, i64 0, i64 1
store double %80, double* %83
%84 = load double*, double** %5
%85 = getelementptr inbounds double, double* %84, i64 0
%86 = load double, double* %85
%87 = load double, double* %8
%88 = fmul double %86, %87
%89 = load double, double* %6
%90 = fmul double %88, %89
%91 = load [3 x double]*, [3 x double]** %4
%92 = getelementptr inbounds [3 x double], [3 x double]* %91, i64 0
%93 = getelementptr inbounds [3 x double], [3 x double]* %92, i64 0, i64 2
store double %90, double* %93
%94 = load double*, double** %5
%95 = getelementptr inbounds double, double* %94, i64 1
%96 = load double, double* %95
%97 = load double*, double** %5
%98 = getelementptr inbounds double, double* %97, i64 2
%99 = load double, double* %98
%100 = fmul double %96, %99
%101 = load double, double* %6
%102 = fmul double %100, %101
%103 = load [3 x double]*, [3 x double]** %4
%104 = getelementptr inbounds [3 x double], [3 x double]* %103, i64 1
%105 = getelementptr inbounds [3 x double], [3 x double]* %104, i64 0, i64 0
store double %102, double* %105
%106 = load double*, double** %5
%107 = getelementptr inbounds double, double* %106, i64 0
%108 = load double, double* %107
%109 = load double, double* %7
%110 = fmul double %108, %109
%111 = load double, double* %6
%112 = fmul double %110, %111
%113 = load [3 x double]*, [3 x double]** %4
%114 = getelementptr inbounds [3 x double], [3 x double]* %113, i64 1
%115 = getelementptr inbounds [3 x double], [3 x double]* %114, i64 0, i64 1
store double %112, double* %115
%116 = load double*, double** %5
%117 = getelementptr inbounds double, double* %116, i64 1
%118 = load double, double* %117
%119 = load double, double* %8
%120 = fmul double %118, %119
%121 = load double, double* %6
%122 = fmul double %120, %121
%123 = load [3 x double]*, [3 x double]** %4
%124 = getelementptr inbounds [3 x double], [3 x double]* %123, i64 1
%125 = getelementptr inbounds [3 x double], [3 x double]* %124, i64 0, i64 2
store double %122, double* %125
%126 = load double, double* %8
%127 = fneg double %126
%128 = load double, double* %8
%129 = fmul double %127, %128
%130 = load double, double* %6
%131 = fmul double %129, %130
%132 = load [3 x double]*, [3 x double]** %4
%133 = getelementptr inbounds [3 x double], [3 x double]* %132, i64 2
%134 = getelementptr inbounds [3 x double], [3 x double]* %133, i64 0, i64 0
store double %131, double* %134
%135 = load [3 x double]*, [3 x double]** %4
%136 = getelementptr inbounds [3 x double], [3 x double]* %135, i64 2
%137 = getelementptr inbounds [3 x double], [3 x double]* %136, i64 0, i64 1
store double 0.000000e+00, double* %137
%138 = load double*, double** %5
%139 = getelementptr inbounds double, double* %138, i64 2
%140 = load double, double* %139
%141 = load double, double* %8
%142 = fmul double %140, %141
%143 = load double, double* %6
%144 = fmul double %142, %143
%145 = load [3 x double]*, [3 x double]** %4
%146 = getelementptr inbounds [3 x double], [3 x double]* %145, i64 2
%147 = getelementptr inbounds [3 x double], [3 x double]* %146, i64 0, i64 2
store double %144, double* %147
store i1 true, i1* %3
br label %148
148: ; preds = %56, %53
%149 = load i1, i1* %3
ret i1 %149
}

LLVM IR (-O2):
@rotation_matrix_3D.I = internal unnamed_addr constant [3 x [3 x double]] [[3 x double] [double 1.000000e+00, double 0.000000e+00, double 0.000000e+00], [3 x double] [double 0.000000e+00, double 1.000000e+00, double 0.000000e+00], [3 x double] [double 0.000000e+00, double 0.000000e+00, double 1.000000e+00]]
define zeroext i1 @rotation_matrix_3D([3 x double]* nocapture writeonly %0, double* nocapture %1) {
%3 = load double, double* %1
%4 = getelementptr inbounds double, double* %1, i64 1
%5 = load double, double* %4
%6 = fmul double %5, %5
%7 = tail call double @llvm.fmuladd.f64(double %3, double %3, double %6)
%8 = getelementptr inbounds double, double* %1, i64 2
%9 = load double, double* %8
%10 = tail call double @llvm.fmuladd.f64(double %9, double %9, double %7)
%11 = tail call double @sqrt(double %10)
%12 = load double, double* %1
%13 = load double, double* %4
%14 = fmul double %13, %13
%15 = tail call double @llvm.fmuladd.f64(double %12, double %12, double %14)
%16 = tail call double @sqrt(double %15)
%17 = fcmp olt double %11, 1.000000e-09
%18 = fmul double %11, 1.000000e-04
%19 = fcmp olt double %16, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = bitcast [3 x double]* %0 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 dereferenceable(72) %22, i8* align 16 dereferenceable(72) bitcast ([3 x [3 x double]]* @rotation_matrix_3D.I to i8*), i64 72, i1 false)
br label %66
23: ; preds = %2
%24 = fmul double %11, %16
%25 = fdiv double 1.000000e+00, %24
%26 = load double, double* %1
%27 = load double, double* %8
%28 = fmul double %26, %27
%29 = fmul double %25, %28
%30 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 0
store double %29, double* %30
%31 = load double, double* %4
%32 = fneg double %31
%33 = fmul double %11, %32
%34 = fmul double %25, %33
%35 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 1
store double %34, double* %35
%36 = load double, double* %1
%37 = fmul double %16, %36
%38 = fmul double %25, %37
%39 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 2
store double %38, double* %39
%40 = load double, double* %4
%41 = load double, double* %8
%42 = fmul double %40, %41
%43 = fmul double %25, %42
%44 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 0
store double %43, double* %44
%45 = load double, double* %1
%46 = fmul double %11, %45
%47 = fmul double %25, %46
%48 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 1
store double %47, double* %48
%49 = load double, double* %4
%50 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 2
%51 = fneg double %16
%52 = insertelement <2 x double> poison, double %16, i64 0
%53 = shufflevector <2 x double> %52, <2 x double> poison, <2 x i32> zeroinitializer
%54 = insertelement <2 x double> poison, double %49, i64 0
%55 = insertelement <2 x double> %54, double %51, i64 1
%56 = fmul <2 x double> %53, %55
%57 = insertelement <2 x double> poison, double %25, i64 0
%58 = shufflevector <2 x double> %57, <2 x double> poison, <2 x i32> zeroinitializer
%59 = fmul <2 x double> %58, %56
%60 = bitcast double* %50 to <2 x double>*
store <2 x double> %59, <2 x double>* %60
%61 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 1
store double 0.000000e+00, double* %61
%62 = load double, double* %8
%63 = fmul double %16, %62
%64 = fmul double %25, %63
%65 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 2
store double %64, double* %65
br label %66
66: ; preds = %23, %21
%67 = xor i1 %20, true
ret i1 %67
}