#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 dot_product(double *, double *);
double sqrt(double);
void sphere_intersect(double *intersect, double *p, double *v,
                      double sphere_r) {
  double mu, a, b, c;
  int sign;
  a = dot_product(v, v);
  b = 2 * dot_product(p, v);
  c = dot_product(p, p) - sphere_r * sphere_r;
  sign = (b > 0) - (b < 0);
  mu = (-b + (double)sign * sqrt(b * b - 4 * a * c)) / (2 * a);
  intersect[0] = p[0] + mu * v[0];
  intersect[1] = p[1] + mu * v[1];
  intersect[2] = p[2] + mu * v[2];
}


x86 assembly (clang, -O0):
<sphere_intersect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <dot_product@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <dot_product@plt>
movaps %xmm0,%xmm1
movsd  0xe9d(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <dot_product@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%eax
xorps  %xmm0,%xmm0
ucomisd -0x38(%rbp),%xmm0
seta   %cl
and    $0x1,%cl
movzbl %cl,%ecx
sub    %ecx,%eax
mov    %eax,-0x44(%rbp)
movsd  -0x38(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x58(%rbp)
cvtsi2sdl -0x44(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
movsd  0xdf7(%rip),%xmm1
mulsd  -0x30(%rbp),%xmm1
mulsd  -0x40(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1040 <sqrt@plt>
movsd  -0x58(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xda4(%rip),%xmm1
mulsd  -0x30(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm1
movsd  -0x28(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm1
movsd  -0x28(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,0x8(%rax)
mov    -0x10(%rbp),%rax
movsd  0x10(%rax),%xmm1
movsd  -0x28(%rbp),%xmm0
mov    -0x18(%rbp),%rax
movsd  0x10(%rax),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,0x10(%rax)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sphere_intersect>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movsd  %xmm0,(%rsp)
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
mov    %rdx,%rsi
call   1030 <dot_product@plt>
movsd  %xmm0,0x8(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <dot_product@plt>
addsd  %xmm0,%xmm0
movapd %xmm0,0x10(%rsp)
mov    %r15,%rdi
mov    %r15,%rsi
call   1030 <dot_product@plt>
movsd  (%rsp),%xmm2
mulsd  %xmm2,%xmm2
xorpd  %xmm1,%xmm1
xor    %eax,%eax
movapd 0x10(%rsp),%xmm4
ucomisd %xmm1,%xmm4
subsd  %xmm2,%xmm0
seta   %al
xor    %ecx,%ecx
ucomisd %xmm4,%xmm1
seta   %cl
sub    %ecx,%eax
movapd 0xe65(%rip),%xmm5
cvtsi2sd %eax,%xmm3
xorpd  %xmm4,%xmm5
movsd  0xe65(%rip),%xmm2
mulsd  0x8(%rsp),%xmm2
mulsd  %xmm0,%xmm2
movapd %xmm4,%xmm0
mulsd  %xmm4,%xmm0
addsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jb     11cd <sphere_intersect+0xad>
sqrtsd %xmm0,%xmm0
jmp    11e8 <sphere_intersect+0xc8>
movsd  %xmm3,(%rsp)
movapd %xmm5,0x10(%rsp)
call   1040 <sqrt@plt>
movapd 0x10(%rsp),%xmm5
movsd  (%rsp),%xmm3
mulsd  %xmm0,%xmm3
addsd  %xmm5,%xmm3
movsd  0x8(%rsp),%xmm0
addsd  %xmm0,%xmm0
divsd  %xmm0,%xmm3
movsd  (%rbx),%xmm0
mulsd  %xmm3,%xmm0
addsd  (%r15),%xmm0
movsd  %xmm0,(%r14)
movsd  0x8(%rbx),%xmm0
mulsd  %xmm3,%xmm0
addsd  0x8(%r15),%xmm0
movsd  %xmm0,0x8(%r14)
mulsd  0x10(%rbx),%xmm3
addsd  0x10(%r15),%xmm3
movsd  %xmm3,0x10(%r14)
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @sphere_intersect(double* %0, double* %1, double* %2, double %3) {
%5 = alloca double*
%6 = alloca double*
%7 = alloca double*
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca i32
store double* %0, double** %5
store double* %1, double** %6
store double* %2, double** %7
store double %3, double* %8
%14 = load double*, double** %7
%15 = load double*, double** %7
%16 = call double @dot_product(double* %14, double* %15)
store double %16, double* %10
%17 = load double*, double** %6
%18 = load double*, double** %7
%19 = call double @dot_product(double* %17, double* %18)
%20 = fmul double 2.000000e+00, %19
store double %20, double* %11
%21 = load double*, double** %6
%22 = load double*, double** %6
%23 = call double @dot_product(double* %21, double* %22)
%24 = load double, double* %8
%25 = load double, double* %8
%26 = fneg double %24
%27 = call double @llvm.fmuladd.f64(double %26, double %25, double %23)
store double %27, double* %12
%28 = load double, double* %11
%29 = fcmp ogt double %28, 0.000000e+00
%30 = zext i1 %29 to i32
%31 = load double, double* %11
%32 = fcmp olt double %31, 0.000000e+00
%33 = zext i1 %32 to i32
%34 = sub nsw i32 %30, %33
store i32 %34, i32* %13
%35 = load double, double* %11
%36 = fneg double %35
%37 = load i32, i32* %13
%38 = sitofp i32 %37 to double
%39 = load double, double* %11
%40 = load double, double* %11
%41 = load double, double* %10
%42 = fmul double 4.000000e+00, %41
%43 = load double, double* %12
%44 = fmul double %42, %43
%45 = fneg double %44
%46 = call double @llvm.fmuladd.f64(double %39, double %40, double %45)
%47 = call double @sqrt(double %46)
%48 = call double @llvm.fmuladd.f64(double %38, double %47, double %36)
%49 = load double, double* %10
%50 = fmul double 2.000000e+00, %49
%51 = fdiv double %48, %50
store double %51, double* %9
%52 = load double*, double** %6
%53 = getelementptr inbounds double, double* %52, i64 0
%54 = load double, double* %53
%55 = load double, double* %9
%56 = load double*, double** %7
%57 = getelementptr inbounds double, double* %56, i64 0
%58 = load double, double* %57
%59 = call double @llvm.fmuladd.f64(double %55, double %58, double %54)
%60 = load double*, double** %5
%61 = getelementptr inbounds double, double* %60, i64 0
store double %59, double* %61
%62 = load double*, double** %6
%63 = getelementptr inbounds double, double* %62, i64 1
%64 = load double, double* %63
%65 = load double, double* %9
%66 = load double*, double** %7
%67 = getelementptr inbounds double, double* %66, i64 1
%68 = load double, double* %67
%69 = call double @llvm.fmuladd.f64(double %65, double %68, double %64)
%70 = load double*, double** %5
%71 = getelementptr inbounds double, double* %70, i64 1
store double %69, double* %71
%72 = load double*, double** %6
%73 = getelementptr inbounds double, double* %72, i64 2
%74 = load double, double* %73
%75 = load double, double* %9
%76 = load double*, double** %7
%77 = getelementptr inbounds double, double* %76, i64 2
%78 = load double, double* %77
%79 = call double @llvm.fmuladd.f64(double %75, double %78, double %74)
%80 = load double*, double** %5
%81 = getelementptr inbounds double, double* %80, i64 2
store double %79, double* %81
ret void
}

LLVM IR (-O2):
define void @sphere_intersect(double* nocapture writeonly %0, double* %1, double* %2, double %3) {
%5 = tail call double @dot_product(double* %2, double* %2)
%6 = tail call double @dot_product(double* %1, double* %2)
%7 = fmul double %6, 2.000000e+00
%8 = tail call double @dot_product(double* %1, double* %1)
%9 = fneg double %3
%10 = tail call double @llvm.fmuladd.f64(double %9, double %3, double %8)
%11 = fcmp ogt double %7, 0.000000e+00
%12 = zext i1 %11 to i32
%13 = fcmp olt double %7, 0.000000e+00
%14 = sext i1 %13 to i32
%15 = add nsw i32 %14, %12
%16 = fneg double %7
%17 = sitofp i32 %15 to double
%18 = fmul double %5, -4.000000e+00
%19 = fmul double %18, %10
%20 = tail call double @llvm.fmuladd.f64(double %7, double %7, double %19)
%21 = tail call double @sqrt(double %20)
%22 = tail call double @llvm.fmuladd.f64(double %17, double %21, double %16)
%23 = fmul double %5, 2.000000e+00
%24 = fdiv double %22, %23
%25 = load double, double* %1
%26 = load double, double* %2
%27 = tail call double @llvm.fmuladd.f64(double %24, double %26, double %25)
store double %27, double* %0
%28 = getelementptr inbounds double, double* %1, i64 1
%29 = load double, double* %28
%30 = getelementptr inbounds double, double* %2, i64 1
%31 = load double, double* %30
%32 = tail call double @llvm.fmuladd.f64(double %24, double %31, double %29)
%33 = getelementptr inbounds double, double* %0, i64 1
store double %32, double* %33
%34 = getelementptr inbounds double, double* %1, i64 2
%35 = load double, double* %34
%36 = getelementptr inbounds double, double* %2, i64 2
%37 = load double, double* %36
%38 = tail call double @llvm.fmuladd.f64(double %24, double %37, double %35)
%39 = getelementptr inbounds double, double* %0, i64 2
store double %38, double* %39
ret void
}