#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.70710678118654752440f) {
    z = 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>
movss  %xmm0,-0x10(%rbp)
movss  -0x10(%rbp),%xmm1
movss  0xea6(%rip),%xmm0
mulss  -0x4(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    11a0 <tb_project_to_sphere+0x90>
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    11ca <tb_project_to_sphere+0xba>
movss  -0x4(%rbp),%xmm0
movss  0xe57(%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
movaps %xmm0,%xmm3
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  %xmm3,0x4(%rsp)
call   1030 <sqrt@plt>
movss  0x4(%rsp),%xmm3
movss  0xeb3(%rip),%xmm1
mulss  %xmm3,%xmm1
ucomiss %xmm0,%xmm1
jbe    1170 <tb_project_to_sphere+0x60>
mulss  %xmm0,%xmm0
mulss  %xmm3,%xmm3
subss  %xmm0,%xmm3
xorps  %xmm0,%xmm0
ucomiss %xmm0,%xmm3
jb     1185 <tb_project_to_sphere+0x75>
sqrtss %xmm3,%xmm3
jmp    1180 <tb_project_to_sphere+0x70>
divss  0xe8c(%rip),%xmm3
mulss  %xmm3,%xmm3
divss  %xmm0,%xmm3
movaps %xmm3,%xmm0
pop    %rax
ret
movaps %xmm3,%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 = load float, float* %4
%19 = fmul float %18, 0x3FE6A09E60000000
%20 = fcmp olt float %17, %19
br i1 %20, label %21, label %30
21: ; preds = %3
%22 = load float, float* %4
%23 = load float, float* %4
%24 = load float, float* %7
%25 = load float, float* %7
%26 = fmul float %24, %25
%27 = fneg float %26
%28 = call float @llvm.fmuladd.f32(float %22, float %23, float %27)
%29 = call float @sqrt(float %28)
store float %29, float* %9
br label %38
30: ; preds = %3
%31 = load float, float* %4
%32 = fdiv float %31, 0x3FF6A09E60000000
store float %32, float* %8
%33 = load float, float* %8
%34 = load float, float* %8
%35 = fmul float %33, %34
%36 = load float, float* %7
%37 = fdiv float %35, %36
store float %37, float* %9
br label %38
38: ; preds = %30, %21
%39 = load float, float* %9
ret float %39
}

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 = fmul float %0, 0x3FE6A09E60000000
%8 = fcmp olt float %6, %7
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = fneg float %6
%11 = fmul float %6, %10
%12 = tail call float @llvm.fmuladd.f32(float %0, float %0, float %11)
%13 = tail call float @sqrt(float %12)
br label %18
14: ; preds = %3
%15 = fdiv float %0, 0x3FF6A09E60000000
%16 = fmul float %15, %15
%17 = fdiv float %16, %6
br label %18
18: ; preds = %14, %9
%19 = phi float [ %13, %9 ], [ %17, %14 ]
ret float %19
}