void cartesian2spherical(double *r, double *theta, double *phi, double x,
                         double y, double z) {
  float r2;
  *r = sqrt(x * x + y * y + z * z);
  r2 = sqrt(x * x + y * y);
  *theta = atan2(r2, z);
  *phi = atan2(y, x);
}


x86 assembly (clang, -O0):
<cartesian2spherical>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
movsd  %xmm1,-0x28(%rbp)
movsd  %xmm2,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1040 <sqrt@plt>
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1040 <sqrt@plt>
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x34(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  -0x30(%rbp),%xmm1
call   1030 <atan2@plt>
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x28(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
call   1030 <atan2@plt>
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cartesian2spherical>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movapd %xmm0,%xmm3
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movsd  %xmm1,0x10(%rsp)
mulsd  %xmm1,%xmm1
movsd  %xmm0,0x18(%rsp)
mulsd  %xmm0,%xmm3
addsd  %xmm1,%xmm3
movsd  %xmm2,0x8(%rsp)
movapd %xmm2,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm3,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1170 <cartesian2spherical+0x50>
sqrtsd %xmm0,%xmm0
jmp    1183 <cartesian2spherical+0x63>
movsd  %xmm3,(%rsp)
call   1040 <sqrt@plt>
xorpd  %xmm1,%xmm1
movsd  (%rsp),%xmm3
movsd  %xmm0,(%rbx)
ucomisd %xmm1,%xmm3
jb     1196 <cartesian2spherical+0x76>
xorps  %xmm0,%xmm0
sqrtsd %xmm3,%xmm0
jmp    119f <cartesian2spherical+0x7f>
movapd %xmm3,%xmm0
call   1040 <sqrt@plt>
cvtsd2ss %xmm0,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0x8(%rsp),%xmm1
call   1030 <atan2@plt>
movsd  %xmm0,(%r15)
movsd  0x10(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
call   1030 <atan2@plt>
movsd  %xmm0,(%r14)
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @cartesian2spherical(double* %0, double* %1, double* %2, double %3, double %4, double %5) {
%7 = alloca double*
%8 = alloca double*
%9 = alloca double*
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca float
store double* %0, double** %7
store double* %1, double** %8
store double* %2, double** %9
store double %3, double* %10
store double %4, double* %11
store double %5, double* %12
%14 = load double, double* %10
%15 = load double, double* %10
%16 = load double, double* %11
%17 = load double, double* %11
%18 = fmul double %16, %17
%19 = call double @llvm.fmuladd.f64(double %14, double %15, double %18)
%20 = load double, double* %12
%21 = load double, double* %12
%22 = call double @llvm.fmuladd.f64(double %20, double %21, double %19)
%23 = call double @sqrt(double %22)
%24 = load double*, double** %7
store double %23, double* %24
%25 = load double, double* %10
%26 = load double, double* %10
%27 = load double, double* %11
%28 = load double, double* %11
%29 = fmul double %27, %28
%30 = call double @llvm.fmuladd.f64(double %25, double %26, double %29)
%31 = call double @sqrt(double %30)
%32 = fptrunc double %31 to float
store float %32, float* %13
%33 = load float, float* %13
%34 = fpext float %33 to double
%35 = load double, double* %12
%36 = call double @atan2(double %34, double %35)
%37 = load double*, double** %8
store double %36, double* %37
%38 = load double, double* %11
%39 = load double, double* %10
%40 = call double @atan2(double %38, double %39)
%41 = load double*, double** %9
store double %40, double* %41
ret void
}

LLVM IR (-O2):
define void @cartesian2spherical(double* nocapture writeonly %0, double* nocapture writeonly %1, double* nocapture writeonly %2, double %3, double %4, double %5) {
%7 = fmul double %4, %4
%8 = tail call double @llvm.fmuladd.f64(double %3, double %3, double %7)
%9 = tail call double @llvm.fmuladd.f64(double %5, double %5, double %8)
%10 = tail call double @sqrt(double %9)
store double %10, double* %0
%11 = tail call double @sqrt(double %8)
%12 = fptrunc double %11 to float
%13 = fpext float %12 to double
%14 = tail call double @atan2(double %13, double %5)
store double %14, double* %1
%15 = tail call double @atan2(double %4, double %3)
store double %15, double* %2
ret void
}