extern __attribute__((gnu_)) double carmen_normalize_theta(double theta) {
  int multiplier;
  if (theta >= -3.14159265358979323846 && theta < 3.14159265358979323846)
    return theta;
  multiplier = (int)(theta / (2 * 3.14159265358979323846));
  theta = theta - multiplier * 2 * 3.14159265358979323846;
  if (theta >= 3.14159265358979323846)
    theta -= 2 * 3.14159265358979323846;
  if (theta < -3.14159265358979323846)
    theta += 2 * 3.14159265358979323846;
  return theta;
}


x86 assembly (clang, -O0):
<carmen_normalize_theta>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xeea(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1142 <carmen_normalize_theta+0x42>
movsd  0xee0(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1142 <carmen_normalize_theta+0x42>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f1 <carmen_normalize_theta+0xf1>
movsd  -0x10(%rbp),%xmm0
movsd  0xec1(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm1
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  0xe80(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe66(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11c2 <carmen_normalize_theta+0xc2>
movsd  -0x10(%rbp),%xmm0
movsd  0xe57(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xe36(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11e7 <carmen_normalize_theta+0xe7>
movsd  0xe33(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<carmen_normalize_theta>:
ucomisd 0xef8(%rip),%xmm0
jb     1119 <carmen_normalize_theta+0x19>
movsd  0xef6(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1119 <carmen_normalize_theta+0x19>
ret
movsd  0xeef(%rip),%xmm1
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
cvttsd2si %xmm2,%eax
add    %eax,%eax
xorps  %xmm2,%xmm2
cvtsi2sd %eax,%xmm2
movsd  0xec2(%rip),%xmm3
mulsd  %xmm3,%xmm2
addsd  %xmm0,%xmm2
movsd  0xeca(%rip),%xmm0
addsd  %xmm2,%xmm0
movsd  0xeae(%rip),%xmm4
cmpnlesd %xmm2,%xmm4
andpd  %xmm4,%xmm2
andnpd %xmm0,%xmm4
orpd   %xmm2,%xmm4
addsd  %xmm4,%xmm1
movapd %xmm4,%xmm0
cmpltsd %xmm3,%xmm0
andpd  %xmm0,%xmm1
andnpd %xmm4,%xmm0
orpd   %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @carmen_normalize_theta(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i32
store double %0, double* %3
%5 = load double, double* %3
%6 = fcmp oge double %5, 0xC00921FB54442D18
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load double, double* %3
%9 = fcmp olt double %8, 0x400921FB54442D18
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = load double, double* %3
store double %11, double* %2
br label %35
12: ; preds = %7, %1
%13 = load double, double* %3
%14 = fdiv double %13, 0x401921FB54442D18
%15 = fptosi double %14 to i32
store i32 %15, i32* %4
%16 = load double, double* %3
%17 = load i32, i32* %4
%18 = mul nsw i32 %17, 2
%19 = sitofp i32 %18 to double
%20 = fneg double %19
%21 = call double @llvm.fmuladd.f64(double %20, double 0x400921FB54442D18, double %16)
store double %21, double* %3
%22 = load double, double* %3
%23 = fcmp oge double %22, 0x400921FB54442D18
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = load double, double* %3
%26 = fsub double %25, 0x401921FB54442D18
store double %26, double* %3
br label %27
27: ; preds = %24, %12
%28 = load double, double* %3
%29 = fcmp olt double %28, 0xC00921FB54442D18
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load double, double* %3
%32 = fadd double %31, 0x401921FB54442D18
store double %32, double* %3
br label %33
33: ; preds = %30, %27
%34 = load double, double* %3
store double %34, double* %2
br label %35
35: ; preds = %33, %10
%36 = load double, double* %2
ret double %36
}

LLVM IR (-O2):
define double @carmen_normalize_theta(double %0) {
%2 = fcmp oge double %0, 0xC00921FB54442D18
%3 = fcmp olt double %0, 0x400921FB54442D18
%4 = and i1 %2, %3
br i1 %4, label %18, label %5
5: ; preds = %1
%6 = fdiv double %0, 0x401921FB54442D18
%7 = fptosi double %6 to i32
%8 = shl nsw i32 %7, 1
%9 = sitofp i32 %8 to double
%10 = fneg double %9
%11 = tail call double @llvm.fmuladd.f64(double %10, double 0x400921FB54442D18, double %0)
%12 = fcmp ult double %11, 0x400921FB54442D18
%13 = fadd double %11, 0xC01921FB54442D18
%14 = select i1 %12, double %11, double %13
%15 = fcmp olt double %14, 0xC00921FB54442D18
%16 = fadd double %14, 0x401921FB54442D18
%17 = select i1 %15, double %16, double %14
br label %18
18: ; preds = %1, %5
%19 = phi double [ %17, %5 ], [ %0, %1 ]
ret double %19
}