#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, z;
  d = sqrt(x * x + y * y);
  if (d < r * 1.) {
    z = sqrt(r * r - d * d);
  } else {
    z = 0;
  }
  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,-0x28(%rbp)
jmp    11b3 <tb_project_to_sphere+0xa3>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tb_project_to_sphere>:
movapd %xmm0,%xmm3
mulsd  %xmm2,%xmm2
mulsd  %xmm1,%xmm1
addsd  %xmm2,%xmm1
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm1
jb     113c <tb_project_to_sphere+0x2c>
xorps  %xmm0,%xmm0
sqrtsd %xmm1,%xmm0
ucomisd %xmm0,%xmm3
ja     115e <tb_project_to_sphere+0x4e>
movapd %xmm2,%xmm0
ret
push   %rax
movapd %xmm1,%xmm0
movsd  %xmm3,(%rsp)
call   1030 <sqrt@plt>
xorpd  %xmm2,%xmm2
movsd  (%rsp),%xmm3
add    $0x8,%rsp
ucomisd %xmm0,%xmm3
jbe    1137 <tb_project_to_sphere+0x27>
mulsd  %xmm0,%xmm0
mulsd  %xmm3,%xmm3
subsd  %xmm0,%xmm3
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm3
jb     1180 <tb_project_to_sphere+0x70>
xorps  %xmm2,%xmm2
sqrtsd %xmm3,%xmm2
movapd %xmm2,%xmm0
ret
movapd %xmm3,%xmm0
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
store double %0, double* %4
store double %1, double* %5
store double %2, double* %6
%9 = load double, double* %5
%10 = load double, double* %5
%11 = load double, double* %6
%12 = load double, double* %6
%13 = fmul double %11, %12
%14 = call double @llvm.fmuladd.f64(double %9, double %10, double %13)
%15 = call double @sqrt(double %14)
store double %15, double* %7
%16 = load double, double* %7
%17 = load double, double* %4
%18 = fmul double %17, 1.000000e+00
%19 = fcmp olt double %16, %18
br i1 %19, label %20, label %29
20: ; preds = %3
%21 = load double, double* %4
%22 = load double, double* %4
%23 = load double, double* %7
%24 = load double, double* %7
%25 = fmul double %23, %24
%26 = fneg double %25
%27 = call double @llvm.fmuladd.f64(double %21, double %22, double %26)
%28 = call double @sqrt(double %27)
store double %28, double* %8
br label %30
29: ; preds = %3
store double 0.000000e+00, double* %8
br label %30
30: ; preds = %29, %20
%31 = load double, double* %8
ret double %31
}

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 = fcmp olt double %6, %0
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = fneg double %6
%10 = fmul double %6, %9
%11 = tail call double @llvm.fmuladd.f64(double %0, double %0, double %10)
%12 = tail call double @sqrt(double %11)
br label %13
13: ; preds = %3, %8
%14 = phi double [ %12, %8 ], [ 0.000000e+00, %3 ]
ret double %14
}