double angle_diff(double a, double b) {
  a -= b;
  while (a <= -3.14159265358979323846)
    a += 6.28318530718;
  while (a > 3.14159265358979323846)
    a -= 6.28318530718;
  if (a < 0.0)
    a = -a;
  return a;
}


x86 assembly (clang, -O0):
<angle_diff>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  0xed7(%rip),%xmm0
ucomisd -0x8(%rbp),%xmm0
jb     114b <angle_diff+0x4b>
movsd  0xed4(%rip),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1121 <angle_diff+0x21>
jmp    1150 <angle_diff+0x50>
movsd  -0x8(%rbp),%xmm0
movsd  0xeab(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1182 <angle_diff+0x82>
movsd  -0x8(%rbp),%xmm0
movsd  0xe9c(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1150 <angle_diff+0x50>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11b1 <angle_diff+0xb1>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<angle_diff>:
subsd  %xmm1,%xmm0
movsd  0xef4(%rip),%xmm1
ucomisd %xmm0,%xmm1
jb     112a <angle_diff+0x2a>
movsd  0xeee(%rip),%xmm2
nopw   0x0(%rax,%rax,1)
addsd  %xmm2,%xmm0
ucomisd %xmm0,%xmm1
jae    1120 <angle_diff+0x20>
ucomisd 0xede(%rip),%xmm0
jbe    115a <angle_diff+0x5a>
movsd  0xedc(%rip),%xmm1
movsd  0xecc(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
addsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
ja     1150 <angle_diff+0x50>
movapd 0xebe(%rip),%xmm2
xorpd  %xmm0,%xmm2
xorpd  %xmm3,%xmm3
movapd %xmm0,%xmm1
cmpltsd %xmm3,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
movapd %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @angle_diff(double %0, double %1) {
%3 = alloca double
%4 = alloca double
store double %0, double* %3
store double %1, double* %4
%5 = load double, double* %4
%6 = load double, double* %3
%7 = fsub double %6, %5
store double %7, double* %3
br label %8
8: ; preds = %11, %2
%9 = load double, double* %3
%10 = fcmp ole double %9, 0xC00921FB54442D18
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load double, double* %3
%13 = fadd double %12, 0x401921FB54442EEA
store double %13, double* %3
br label %8
14: ; preds = %8
br label %15
15: ; preds = %18, %14
%16 = load double, double* %3
%17 = fcmp ogt double %16, 0x400921FB54442D18
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load double, double* %3
%20 = fsub double %19, 0x401921FB54442EEA
store double %20, double* %3
br label %15
21: ; preds = %15
%22 = load double, double* %3
%23 = fcmp olt double %22, 0.000000e+00
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load double, double* %3
%26 = fneg double %25
store double %26, double* %3
br label %27
27: ; preds = %24, %21
%28 = load double, double* %3
ret double %28
}

LLVM IR (-O2):
define double @angle_diff(double %0, double %1) {
%3 = fsub double %0, %1
%4 = fcmp ugt double %3, 0xC00921FB54442D18
br i1 %4, label %5, label %8
5: ; preds = %8, %2
%6 = phi double [ %3, %2 ], [ %10, %8 ]
%7 = fcmp ogt double %6, 0x400921FB54442D18
br i1 %7, label %12, label %16
8: ; preds = %2, %8
%9 = phi double [ %10, %8 ], [ %3, %2 ]
%10 = fadd double %9, 0x401921FB54442EEA
%11 = fcmp ugt double %10, 0xC00921FB54442D18
br i1 %11, label %5, label %8
12: ; preds = %5, %12
%13 = phi double [ %14, %12 ], [ %6, %5 ]
%14 = fadd double %13, 0xC01921FB54442EEA
%15 = fcmp ogt double %14, 0x400921FB54442D18
br i1 %15, label %12, label %16
16: ; preds = %12, %5
%17 = phi double [ %6, %5 ], [ %14, %12 ]
%18 = fcmp olt double %17, 0.000000e+00
%19 = fneg double %17
%20 = select i1 %18, double %19, double %17
ret double %20
}