#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 */
scalar_t__ sqrt(float);
float tb_project_to_sphere(float r, float x, float y) {
  float d, t, z;
  d = (float)sqrt(x * x + y * y);
  if (d < r * 0.70710678118654752440) {
    z = (float)sqrt(r * r - d * d);
  } else {
    t = r / 1.41421356237309504880f;
    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>
cvtsi2ss %rax,%xmm0
movss  %xmm0,-0x10(%rbp)
movss  -0x10(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe94(%rip),%xmm2
mulsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    11b7 <tb_project_to_sphere+0xa7>
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>
cvtsi2ss %rax,%xmm0
movss  %xmm0,-0x18(%rbp)
jmp    11e1 <tb_project_to_sphere+0xd1>
movss  -0x4(%rbp),%xmm0
movss  0xe44(%rip),%xmm1
divss  %xmm1,%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
movss  %xmm0,0x4(%rsp)
mulss  %xmm2,%xmm2
mulss  %xmm1,%xmm1
addss  %xmm2,%xmm1
movaps %xmm1,%xmm0
call   1030 <sqrt@plt>
movss  0x4(%rsp),%xmm0
cvtsi2ss %rax,%xmm3
xorps  %xmm1,%xmm1
cvtss2sd %xmm3,%xmm1
xorps  %xmm2,%xmm2
cvtss2sd %xmm0,%xmm2
mulsd  0xeb4(%rip),%xmm2
ucomisd %xmm1,%xmm2
jbe    116d <tb_project_to_sphere+0x5d>
mulss  %xmm3,%xmm3
mulss  %xmm0,%xmm0
subss  %xmm3,%xmm0
call   1030 <sqrt@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
pop    %rax
ret
divss  0xe93(%rip),%xmm0
mulss  %xmm0,%xmm0
divss  %xmm3,%xmm0
pop    %rax
ret

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 i64 @sqrt(float %15)
%17 = sitofp i64 %16 to float
store float %17, float* %7
%18 = load float, float* %7
%19 = fpext float %18 to double
%20 = load float, float* %4
%21 = fpext float %20 to double
%22 = fmul double %21, 0x3FE6A09E667F3BCD
%23 = fcmp olt double %19, %22
br i1 %23, label %24, label %34
24: ; preds = %3
%25 = load float, float* %4
%26 = load float, float* %4
%27 = load float, float* %7
%28 = load float, float* %7
%29 = fmul float %27, %28
%30 = fneg float %29
%31 = call float @llvm.fmuladd.f32(float %25, float %26, float %30)
%32 = call i64 @sqrt(float %31)
%33 = sitofp i64 %32 to float
store float %33, float* %9
br label %42
34: ; preds = %3
%35 = load float, float* %4
%36 = fdiv float %35, 0x3FF6A09E60000000
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 = %34, %24
%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 i64 @sqrt(float %5)
%7 = sitofp i64 %6 to float
%8 = fpext float %7 to double
%9 = fpext float %0 to double
%10 = fmul double %9, 0x3FE6A09E667F3BCD
%11 = fcmp ogt double %10, %8
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = fneg float %7
%14 = fmul float %13, %7
%15 = tail call float @llvm.fmuladd.f32(float %0, float %0, float %14)
%16 = tail call i64 @sqrt(float %15)
%17 = sitofp i64 %16 to float
br label %22
18: ; preds = %3
%19 = fdiv float %0, 0x3FF6A09E60000000
%20 = fmul float %19, %19
%21 = fdiv float %20, %7
br label %22
22: ; preds = %18, %12
%23 = phi float [ %17, %12 ], [ %21, %18 ]
ret float %23
}