#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 tb_project_to_sphere(float r, float x, float y) {
  float d, t, z;
  d = sqrt(x * x + y * y);
  if (d < r * 0.70710678118654752440) {
    z = sqrt(r * r - d * d);
  } else {
    t = r / 1.41421356237309504880;
    z = t * t / d;
  }
  return z;
}


x86 assembly (clang, -O0):
<tb_project_to_sphere>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movss  %xmm0,-0x4(%rbp)
movss  %xmm1,-0x8(%rbp)
movss  %xmm2,-0xc(%rbp)
movss  -0x8(%rbp),%xmm0
movss  -0x8(%rbp),%xmm2
movss  -0xc(%rbp),%xmm1
mulss  -0xc(%rbp),%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
call   1030 <sqrt@plt>
movss  %xmm0,-0x10(%rbp)
movss  -0x10(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe99(%rip),%xmm2
mulsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    11ad <tb_project_to_sphere+0x9d>
movss  -0x4(%rbp),%xmm0
movss  -0x4(%rbp),%xmm2
movss  -0x10(%rbp),%xmm1
mulss  -0x10(%rbp),%xmm1
movd   %xmm1,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm1
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
call   1030 <sqrt@plt>
movss  %xmm0,-0x18(%rbp)
jmp    11df <tb_project_to_sphere+0xcf>
movss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe4a(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x14(%rbp),%xmm0
mulss  -0x14(%rbp),%xmm0
divss  -0x10(%rbp),%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tb_project_to_sphere>:
push   %rax
movaps %xmm0,%xmm4
mulss  %xmm2,%xmm2
mulss  %xmm1,%xmm1
addss  %xmm2,%xmm1
xorps  %xmm0,%xmm0
ucomiss %xmm0,%xmm1
jb     1131 <tb_project_to_sphere+0x21>
xorps  %xmm0,%xmm0
sqrtss %xmm1,%xmm0
jmp    1145 <tb_project_to_sphere+0x35>
movaps %xmm1,%xmm0
movss  %xmm4,0x4(%rsp)
call   1030 <sqrt@plt>
movss  0x4(%rsp),%xmm4
xorps  %xmm2,%xmm2
cvtss2sd %xmm0,%xmm2
xorps  %xmm1,%xmm1
cvtss2sd %xmm4,%xmm1
movsd  0xea5(%rip),%xmm3
mulsd  %xmm1,%xmm3
ucomisd %xmm2,%xmm3
jbe    1182 <tb_project_to_sphere+0x72>
mulss  %xmm0,%xmm0
mulss  %xmm4,%xmm4
subss  %xmm0,%xmm4
xorps  %xmm0,%xmm0
ucomiss %xmm0,%xmm4
jb     119b <tb_project_to_sphere+0x8b>
xorps  %xmm1,%xmm1
sqrtss %xmm4,%xmm1
jmp    1196 <tb_project_to_sphere+0x86>
divsd  0xe7e(%rip),%xmm1
cvtsd2ss %xmm1,%xmm1
mulss  %xmm1,%xmm1
divss  %xmm0,%xmm1
movaps %xmm1,%xmm0
pop    %rax
ret
movaps %xmm4,%xmm0
pop    %rax
jmp    1030 <sqrt@plt>

LLVM IR (-O0):
define float @tb_project_to_sphere(float %0, float %1, float %2) {
%4 = alloca float
%5 = alloca float
%6 = alloca float
%7 = alloca float
%8 = alloca float
%9 = alloca float
store float %0, float* %4
store float %1, float* %5
store float %2, float* %6
%10 = load float, float* %5
%11 = load float, float* %5
%12 = load float, float* %6
%13 = load float, float* %6
%14 = fmul float %12, %13
%15 = call float @llvm.fmuladd.f32(float %10, float %11, float %14)
%16 = call float @sqrt(float %15)
store float %16, float* %7
%17 = load float, float* %7
%18 = fpext float %17 to double
%19 = load float, float* %4
%20 = fpext float %19 to double
%21 = fmul double %20, 0x3FE6A09E667F3BCD
%22 = fcmp olt double %18, %21
br i1 %22, label %23, label %32
23: ; preds = %3
%24 = load float, float* %4
%25 = load float, float* %4
%26 = load float, float* %7
%27 = load float, float* %7
%28 = fmul float %26, %27
%29 = fneg float %28
%30 = call float @llvm.fmuladd.f32(float %24, float %25, float %29)
%31 = call float @sqrt(float %30)
store float %31, float* %9
br label %42
32: ; preds = %3
%33 = load float, float* %4
%34 = fpext float %33 to double
%35 = fdiv double %34, 0x3FF6A09E667F3BCD
%36 = fptrunc double %35 to float
store float %36, float* %8
%37 = load float, float* %8
%38 = load float, float* %8
%39 = fmul float %37, %38
%40 = load float, float* %7
%41 = fdiv float %39, %40
store float %41, float* %9
br label %42
42: ; preds = %32, %23
%43 = load float, float* %9
ret float %43
}

LLVM IR (-O2):
define float @tb_project_to_sphere(float %0, float %1, float %2) {
%4 = fmul float %2, %2
%5 = tail call float @llvm.fmuladd.f32(float %1, float %1, float %4)
%6 = tail call float @sqrt(float %5)
%7 = fpext float %6 to double
%8 = fpext float %0 to double
%9 = fmul double %8, 0x3FE6A09E667F3BCD
%10 = fcmp ogt double %9, %7
br i1 %10, label %11, label %16
11: ; preds = %3
%12 = fneg float %6
%13 = fmul float %6, %12
%14 = tail call float @llvm.fmuladd.f32(float %0, float %0, float %13)
%15 = tail call float @sqrt(float %14)
br label %21
16: ; preds = %3
%17 = fdiv double %8, 0x3FF6A09E667F3BCD
%18 = fptrunc double %17 to float
%19 = fmul float %18, %18
%20 = fdiv float %19, %6
br label %21
21: ; preds = %16, %11
%22 = phi float [ %15, %11 ], [ %20, %16 ]
ret float %22
}