double normalize(double lon, int topo) {
  register double upper_limit, lower_limit, full_circle;
  switch (topo) {
  case 1:
    upper_limit = 180;
    lower_limit = -180;
    break;
  case 2:
    upper_limit = 360;
    lower_limit = 0;
    break;
  case 3:
    upper_limit = 3.141592653589793;
    lower_limit = -3.141592653589793;
    break;
  case 4:
    upper_limit = 2 * 3.141592653589793;
    lower_limit = 0;
    break;
  default:
    return lon;
  }
  full_circle = upper_limit - lower_limit;
  while (lon > upper_limit)
    lon -= full_circle;
  while (lon < lower_limit)
    lon += full_circle;
  return lon;
}


x86 assembly (clang, -O0):
<normalize>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     11a7 <normalize+0xa7>
mov    -0x38(%rbp),%rax
lea    0xf04(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  0xeeb(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xed6(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11b6 <normalize+0xb6>
movsd  0xebc(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11b6 <normalize+0xb6>
movsd  0xe9a(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe85(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11b6 <normalize+0xb6>
movsd  0xe6b(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11b6 <normalize+0xb6>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1224 <normalize+0x124>
movsd  -0x20(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    11ed <normalize+0xed>
movsd  -0x30(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    11c5 <normalize+0xc5>
jmp    11f2 <normalize+0xf2>
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    121a <normalize+0x11a>
movsd  -0x30(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    11f2 <normalize+0xf2>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalize>:
add    $0xffffffff,%edi
cmp    $0x3,%edi
ja     115a <normalize+0x5a>
movslq %edi,%rax
lea    0xeee(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm3
lea    0xf02(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm1
movapd %xmm3,%xmm2
subsd  %xmm1,%xmm2
ucomisd %xmm3,%xmm0
jbe    1154 <normalize+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
subsd  %xmm2,%xmm0
ucomisd %xmm3,%xmm0
ja     1140 <normalize+0x40>
jmp    1154 <normalize+0x54>
nopl   0x0(%rax)
addsd  %xmm2,%xmm0
ucomisd %xmm0,%xmm1
ja     1150 <normalize+0x50>
ret

LLVM IR (-O0):
define double @normalize(double %0, i32 %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca double
%8 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
switch i32 %9, label %14 [
i32 1, label %10
i32 2, label %11
i32 3, label %12
i32 4, label %13
]
10: ; preds = %2
store double 1.800000e+02, double* %6
store double -1.800000e+02, double* %7
br label %16
11: ; preds = %2
store double 3.600000e+02, double* %6
store double 0.000000e+00, double* %7
br label %16
12: ; preds = %2
store double 0x400921FB54442D18, double* %6
store double 0xC00921FB54442D18, double* %7
br label %16
13: ; preds = %2
store double 0x401921FB54442D18, double* %6
store double 0.000000e+00, double* %7
br label %16
14: ; preds = %2
%15 = load double, double* %4
store double %15, double* %3
br label %39
16: ; preds = %13, %12, %11, %10
%17 = load double, double* %6
%18 = load double, double* %7
%19 = fsub double %17, %18
store double %19, double* %8
br label %20
20: ; preds = %24, %16
%21 = load double, double* %4
%22 = load double, double* %6
%23 = fcmp ogt double %21, %22
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load double, double* %8
%26 = load double, double* %4
%27 = fsub double %26, %25
store double %27, double* %4
br label %20
28: ; preds = %20
br label %29
29: ; preds = %33, %28
%30 = load double, double* %4
%31 = load double, double* %7
%32 = fcmp olt double %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load double, double* %8
%35 = load double, double* %4
%36 = fadd double %35, %34
store double %36, double* %4
br label %29
37: ; preds = %29
%38 = load double, double* %4
store double %38, double* %3
br label %39
39: ; preds = %37, %14
%40 = load double, double* %3
ret double %40
}

LLVM IR (-O2):
@switch.table.normalize = [4 x double] [double 1.800000e+02, double 3.600000e+02, double 0x400921FB54442D18, double 0x401921FB54442D18]
@switch.table.normalize.1 = [4 x double] [double -1.800000e+02, double 0.000000e+00, double 0xC00921FB54442D18, double 0.000000e+00]
define double @normalize(double %0, i32 %1) {
%3 = add i32 %1, -1
%4 = icmp ult i32 %3, 4
br i1 %4, label %5, label %25
5: ; preds = %2
%6 = sext i32 %3 to i64
%7 = getelementptr inbounds [4 x double], [4 x double]* @switch.table.normalize, i64 0, i64 %6
%8 = load double, double* %7
%9 = sext i32 %3 to i64
%10 = getelementptr inbounds [4 x double], [4 x double]* @switch.table.normalize.1, i64 0, i64 %9
%11 = load double, double* %10
%12 = fsub double %8, %11
%13 = fcmp olt double %8, %0
br i1 %13, label %17, label %14
14: ; preds = %17, %5
%15 = phi double [ %0, %5 ], [ %19, %17 ]
%16 = fcmp olt double %15, %11
br i1 %16, label %21, label %25
17: ; preds = %5, %17
%18 = phi double [ %19, %17 ], [ %0, %5 ]
%19 = fsub double %18, %12
%20 = fcmp ogt double %19, %8
br i1 %20, label %17, label %14
21: ; preds = %14, %21
%22 = phi double [ %23, %21 ], [ %15, %14 ]
%23 = fadd double %12, %22
%24 = fcmp olt double %23, %11
br i1 %24, label %21, label %25
25: ; preds = %21, %2, %14
%26 = phi double [ %0, %2 ], [ %15, %14 ], [ %23, %21 ]
ret double %26
}