double normalize_angle(double angle) {
  while (angle < 0) {
    angle += 2 * 3.14159265359;
  }
  while (angle >= 2 * 3.14159265359) {
    angle -= 2 * 3.14159265359;
  }
  return (angle > 3.14159265359) ? (angle - 2 * 3.14159265359) : angle;
}


x86 assembly (clang, -O0):
<normalize_angle>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    112e <normalize_angle+0x2e>
movsd  0xee1(%rip),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1109 <normalize_angle+0x9>
jmp    1133 <normalize_angle+0x33>
movsd  -0x8(%rbp),%xmm0
movsd  0xec0(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1165 <normalize_angle+0x65>
movsd  -0x8(%rbp),%xmm0
movsd  0xea9(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1133 <normalize_angle+0x33>
movsd  -0x8(%rbp),%xmm0
movsd  0xe96(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1197 <normalize_angle+0x97>
movsd  -0x8(%rbp),%xmm0
movsd  0xe77(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    11a1 <normalize_angle+0xa1>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize_angle>:
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    112a <normalize_angle+0x2a>
movsd  0xeee(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
addsd  %xmm2,%xmm0
ucomisd %xmm0,%xmm1
ja     1120 <normalize_angle+0x20>
ucomisd 0xece(%rip),%xmm0
jb     115a <normalize_angle+0x5a>
movsd  0xecc(%rip),%xmm1
movsd  0xebc(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
addsd  %xmm1,%xmm0
ucomisd %xmm2,%xmm0
jae    1150 <normalize_angle+0x50>
movsd  0xea6(%rip),%xmm2
addsd  %xmm0,%xmm2
movsd  0xea2(%rip),%xmm1
cmpltsd %xmm0,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
movapd %xmm1,%xmm0
ret

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

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