#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 M_PI;
double atan2(double, double);
double sqrt(double);
void xyz2sph(float *pos, double *rr, double *cth, double *phi) {
  double r;
  r = sqrt((double)(pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]));
  *rr = r;
  if (r == 0) {
    *cth = 1;
    *phi = 0;
  } else {
    double xn, yn, zn;
    xn = pos[0] / r;
    yn = pos[1] / r;
    zn = pos[2] / r;
    *cth = zn;
    if ((xn == 0) && (yn == 0))
      *phi = 0;
    else {
      *phi = atan2(yn, xn);
      if ((*phi) < 0)
        (*phi) = 2 * M_PI + (*phi);
    }
  }
}


x86 assembly (clang, -O0):
<xyz2sph>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm1
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm2
mov    -0x8(%rbp),%rax
movss  0x4(%rax),%xmm0
mov    -0x8(%rbp),%rax
mulss  0x4(%rax),%xmm0
mulss  %xmm2,%xmm1
addss  %xmm0,%xmm1
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm0
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
call   1040 <sqrt@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11cf <xyz2sph+0xaf>
jp     11cf <xyz2sph+0xaf>
mov    -0x18(%rbp),%rax
movsd  0xe45(%rip),%xmm0
movsd  %xmm0,(%rax)
mov    -0x20(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
jmp    12b2 <xyz2sph+0x192>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movss  0x4(%rax),%xmm0
cvtss2sd %xmm0,%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm0
cvtss2sd %xmm0,%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x30(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1260 <xyz2sph+0x140>
jp     1260 <xyz2sph+0x140>
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1260 <xyz2sph+0x140>
jp     1260 <xyz2sph+0x140>
mov    -0x20(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
jmp    12ad <xyz2sph+0x18d>
movsd  -0x38(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
call   1030 <atan2@plt>
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x20(%rbp),%rax
xorps  %xmm0,%xmm0
ucomisd (%rax),%xmm0
jbe    12a8 <xyz2sph+0x188>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
mov    -0x20(%rbp),%rax
addsd  (%rax),%xmm0
mov    -0x20(%rbp),%rax
movsd  %xmm0,(%rax)
jmp    12ad <xyz2sph+0x18d>
jmp    12b2 <xyz2sph+0x192>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz2sph>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
movss  (%rdi),%xmm0
movss  0x4(%rdi),%xmm1
mulss  %xmm1,%xmm1
mulss  %xmm0,%xmm0
addss  %xmm1,%xmm0
movss  0x8(%rdi),%xmm1
mulss  %xmm1,%xmm1
addss  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     116c <xyz2sph+0x4c>
sqrtsd %xmm0,%xmm0
jmp    1174 <xyz2sph+0x54>
call   1040 <sqrt@plt>
xorps  %xmm1,%xmm1
movsd  %xmm0,(%r12)
ucomisd %xmm1,%xmm0
jne    1195 <xyz2sph+0x75>
jp     1195 <xyz2sph+0x75>
movabs $0x3ff0000000000000,%rax
mov    %rax,(%r15)
xorpd  %xmm0,%xmm0
jmp    11fd <xyz2sph+0xdd>
cvtps2pd (%rbx),%xmm1
movapd %xmm0,%xmm2
unpcklpd %xmm0,%xmm2
divpd  %xmm2,%xmm1
movss  0x8(%rbx),%xmm2
cvtss2sd %xmm2,%xmm2
divsd  %xmm0,%xmm2
movsd  %xmm2,(%r15)
xorpd  %xmm0,%xmm0
cmpeqpd %xmm1,%xmm0
movmskpd %xmm0,%eax
xorpd  %xmm0,%xmm0
cmp    $0x3,%al
je     11fd <xyz2sph+0xdd>
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
call   1030 <atan2@plt>
movsd  %xmm0,(%r14)
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1202 <xyz2sph+0xe2>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
add    %eax,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@M_PI = global i32 0
define void @xyz2sph(float* nocapture %0, double* nocapture writeonly %1, double* nocapture writeonly %2, double* nocapture writeonly %3) {
%5 = load float, float* %0
%6 = getelementptr inbounds float, float* %0, i64 1
%7 = load float, float* %6
%8 = fmul float %7, %7
%9 = tail call float @llvm.fmuladd.f32(float %5, float %5, float %8)
%10 = getelementptr inbounds float, float* %0, i64 2
%11 = load float, float* %10
%12 = tail call float @llvm.fmuladd.f32(float %11, float %11, float %9)
%13 = fpext float %12 to double
%14 = tail call double @sqrt(double %13)
store double %14, double* %1
%15 = fcmp oeq double %14, 0.000000e+00
br i1 %15, label %16, label %17
16: ; preds = %4
store double 1.000000e+00, double* %2
br label %41
17: ; preds = %4
%18 = bitcast float* %0 to <2 x float>*
%19 = load <2 x float>, <2 x float>* %18
%20 = fpext <2 x float> %19 to <2 x double>
%21 = insertelement <2 x double> poison, double %14, i64 0
%22 = shufflevector <2 x double> %21, <2 x double> poison, <2 x i32> zeroinitializer
%23 = fdiv <2 x double> %20, %22
%24 = load float, float* %10
%25 = fpext float %24 to double
%26 = fdiv double %25, %14
store double %26, double* %2
%27 = fcmp oeq <2 x double> %23, zeroinitializer
%28 = extractelement <2 x i1> %27, i64 0
%29 = extractelement <2 x i1> %27, i64 1
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %41, label %31
31: ; preds = %17
%32 = extractelement <2 x double> %23, i64 0
%33 = extractelement <2 x double> %23, i64 1
%34 = tail call double @atan2(double %33, double %32)
store double %34, double* %3
%35 = fcmp olt double %34, 0.000000e+00
br i1 %35, label %36, label %43
36: ; preds = %31
%37 = load i32, i32* @M_PI
%38 = shl nsw i32 %37, 1
%39 = sitofp i32 %38 to double
%40 = fadd double %34, %39
br label %41
41: ; preds = %17, %16, %36
%42 = phi double [ %40, %36 ], [ 0.000000e+00, %16 ], [ 0.000000e+00, %17 ]
store double %42, double* %3
br label %43
43: ; preds = %41, %31
ret void
}