#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 */
double sqrt(double);
double tb_project_to_sphere(double r, double x, double y) {
  double 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    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x18(%rbp),%xmm1
mulsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  0xea6(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11ab <tb_project_to_sphere+0x9b>
movsd  -0x8(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  -0x20(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    11d5 <tb_project_to_sphere+0xc5>
movsd  -0x8(%rbp),%xmm0
movsd  0xe50(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x28(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tb_project_to_sphere>:
push   %rax
movapd %xmm0,%xmm3
mulsd  %xmm2,%xmm2
mulsd  %xmm1,%xmm1
addsd  %xmm2,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jb     1134 <tb_project_to_sphere+0x24>
xorps  %xmm0,%xmm0
sqrtsd %xmm1,%xmm0
jmp    1147 <tb_project_to_sphere+0x37>
movapd %xmm1,%xmm0
movsd  %xmm3,(%rsp)
call   1030 <sqrt@plt>
movsd  (%rsp),%xmm3
movsd  0xeb1(%rip),%xmm1
mulsd  %xmm3,%xmm1
ucomisd %xmm0,%xmm1
jbe    1175 <tb_project_to_sphere+0x65>
mulsd  %xmm0,%xmm0
mulsd  %xmm3,%xmm3
subsd  %xmm0,%xmm3
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm3
jb     118b <tb_project_to_sphere+0x7b>
sqrtsd %xmm3,%xmm3
jmp    1185 <tb_project_to_sphere+0x75>
divsd  0xe8b(%rip),%xmm3
mulsd  %xmm3,%xmm3
divsd  %xmm0,%xmm3
movapd %xmm3,%xmm0
pop    %rax
ret
movapd %xmm3,%xmm0
pop    %rax
jmp    1030 <sqrt@plt>

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

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