int gcode_math_angle_within_arc(double start_angle, double sweep_angle,
                                double test_angle) {
  double begin, end;
  if (sweep_angle < 0.0) {
    begin = start_angle + sweep_angle;
    end = start_angle;
  } else {
    begin = start_angle;
    end = start_angle + sweep_angle;
  }
  if (begin < 0.0) {
    begin += 360.0;
    end += 360.0;
  }
  if (test_angle >= begin - 0.001 && test_angle <= end + 0.001) {
    return (0);
  }
  test_angle += 360.0;
  if (test_angle >= begin - 0.001 && test_angle <= end + 0.001) {
    return (0);
  }
  return (1);
}


x86 assembly (clang, -O0):
<gcode_math_angle_within_arc>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    113f <gcode_math_angle_within_arc+0x3f>
movsd  -0x10(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1158 <gcode_math_angle_within_arc+0x58>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    118a <gcode_math_angle_within_arc+0x8a>
movsd  0xe92(%rip),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe80(%rip),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  0xe6c(%rip),%xmm2
subsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jb     11d2 <gcode_math_angle_within_arc+0xd2>
movsd  -0x20(%rbp),%xmm1
movsd  0xe51(%rip),%xmm0
addsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     11d2 <gcode_math_angle_within_arc+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    1233 <gcode_math_angle_within_arc+0x133>
movsd  0xe26(%rip),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  0xe12(%rip),%xmm2
subsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jb     122c <gcode_math_angle_within_arc+0x12c>
movsd  -0x20(%rbp),%xmm1
movsd  0xdf7(%rip),%xmm0
addsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     122c <gcode_math_angle_within_arc+0x12c>
movl   $0x0,-0x4(%rbp)
jmp    1233 <gcode_math_angle_within_arc+0x133>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<gcode_math_angle_within_arc>:
xorpd  %xmm4,%xmm4
ucomisd %xmm1,%xmm4
movapd %xmm0,%xmm5
addsd  %xmm1,%xmm5
movapd %xmm5,%xmm3
ja     111a <gcode_math_angle_within_arc+0x1a>
movapd %xmm0,%xmm3
cmpltsd %xmm4,%xmm1
movapd %xmm1,%xmm6
andnpd %xmm5,%xmm6
andpd  %xmm0,%xmm1
orpd   %xmm6,%xmm1
ucomisd %xmm3,%xmm4
movsd  0xec5(%rip),%xmm0
movapd %xmm1,%xmm5
addsd  %xmm0,%xmm5
ja     114b <gcode_math_angle_within_arc+0x4b>
movapd %xmm3,%xmm0
jmp    114f <gcode_math_angle_within_arc+0x4f>
addsd  %xmm3,%xmm0
cmpltsd %xmm4,%xmm3
movapd %xmm3,%xmm4
andnpd %xmm1,%xmm4
andpd  %xmm5,%xmm3
orpd   %xmm4,%xmm3
addsd  0xe9c(%rip),%xmm0
ucomisd %xmm0,%xmm2
addsd  0xe98(%rip),%xmm3
jb     1183 <gcode_math_angle_within_arc+0x83>
xor    %eax,%eax
ucomisd %xmm2,%xmm3
jb     1183 <gcode_math_angle_within_arc+0x83>
ret
addsd  0xe75(%rip),%xmm2
ucomisd %xmm0,%xmm2
setb   %al
ucomisd %xmm2,%xmm3
setb   %cl
or     %al,%cl
movzbl %cl,%eax
ret

LLVM IR (-O0):
define i32 @gcode_math_angle_within_arc(double %0, double %1, double %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
%10 = load double, double* %6
%11 = fcmp olt double %10, 0.000000e+00
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load double, double* %5
%14 = load double, double* %6
%15 = fadd double %13, %14
store double %15, double* %8
%16 = load double, double* %5
store double %16, double* %9
br label %22
17: ; preds = %3
%18 = load double, double* %5
store double %18, double* %8
%19 = load double, double* %5
%20 = load double, double* %6
%21 = fadd double %19, %20
store double %21, double* %9
br label %22
22: ; preds = %17, %12
%23 = load double, double* %8
%24 = fcmp olt double %23, 0.000000e+00
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load double, double* %8
%27 = fadd double %26, 3.600000e+02
store double %27, double* %8
%28 = load double, double* %9
%29 = fadd double %28, 3.600000e+02
store double %29, double* %9
br label %30
30: ; preds = %25, %22
%31 = load double, double* %7
%32 = load double, double* %8
%33 = fsub double %32, 1.000000e-03
%34 = fcmp oge double %31, %33
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load double, double* %7
%37 = load double, double* %9
%38 = fadd double %37, 1.000000e-03
%39 = fcmp ole double %36, %38
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 0, i32* %4
br label %55
41: ; preds = %35, %30
%42 = load double, double* %7
%43 = fadd double %42, 3.600000e+02
store double %43, double* %7
%44 = load double, double* %7
%45 = load double, double* %8
%46 = fsub double %45, 1.000000e-03
%47 = fcmp oge double %44, %46
br i1 %47, label %48, label %54
48: ; preds = %41
%49 = load double, double* %7
%50 = load double, double* %9
%51 = fadd double %50, 1.000000e-03
%52 = fcmp ole double %49, %51
br i1 %52, label %53, label %54
53: ; preds = %48
store i32 0, i32* %4
br label %55
54: ; preds = %48, %41
store i32 1, i32* %4
br label %55
55: ; preds = %54, %53, %40
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
define i32 @gcode_math_angle_within_arc(double %0, double %1, double %2) {
%4 = fcmp olt double %1, 0.000000e+00
%5 = fadd double %0, %1
%6 = select i1 %4, double %5, double %0
%7 = select i1 %4, double %0, double %5
%8 = fcmp olt double %6, 0.000000e+00
%9 = fadd double %6, 3.600000e+02
%10 = fadd double %7, 3.600000e+02
%11 = select i1 %8, double %9, double %6
%12 = select i1 %8, double %10, double %7
%13 = fadd double %11, -1.000000e-03
%14 = fcmp ugt double %13, %2
%15 = fadd double %12, 1.000000e-03
%16 = fcmp ult double %15, %2
%17 = or i1 %14, %16
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = fadd double %2, 3.600000e+02
%20 = fcmp ult double %19, %13
%21 = fcmp ugt double %19, %15
%22 = or i1 %20, %21
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %18, %3
%25 = phi i32 [ 0, %3 ], [ %23, %18 ]
ret i32 %25
}