double linearize(double lon1, double lon2, int topo) {
  register double full_circle, half_circle;
  switch (topo) {
  case 1:
  case 2:
    half_circle = 180;
    break;
  case 3:
  case 4:
    half_circle = 3.141592653589793;
    break;
  default:
    return lon2;
  }
  full_circle = 2 * half_circle;
  while (lon2 - lon1 > half_circle)
    lon2 -= full_circle;
  while (lon2 - lon1 < -half_circle)
    lon2 += full_circle;
  return lon2;
}


x86 assembly (clang, -O0):
<linearize>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %edi,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
add    $0xffffffff,%eax
sub    $0x2,%eax
jb     113c <linearize+0x3c>
jmp    1128 <linearize+0x28>
mov    -0x34(%rbp),%eax
add    $0xfffffffd,%eax
sub    $0x2,%eax
jb     114e <linearize+0x4e>
jmp    1160 <linearize+0x60>
movsd  0xec4(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    116f <linearize+0x6f>
movsd  0xeaa(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    116f <linearize+0x6f>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1201 <linearize+0x101>
movsd  0xe99(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    11ae <linearize+0xae>
movsd  -0x28(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1181 <linearize+0x81>
jmp    11b3 <linearize+0xb3>
movsd  -0x18(%rbp),%xmm1
subsd  -0x10(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
ucomisd %xmm1,%xmm0
jbe    11f7 <linearize+0xf7>
movsd  -0x28(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11b3 <linearize+0xb3>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<linearize>:
add    $0xffffffff,%edi
cmp    $0x3,%edi
ja     1162 <linearize+0x62>
movslq %edi,%rax
lea    0xefe(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm2
movapd %xmm2,%xmm3
addsd  %xmm2,%xmm3
nop
movapd %xmm1,%xmm4
subsd  %xmm0,%xmm4
ucomisd %xmm2,%xmm4
jbe    1134 <linearize+0x34>
subsd  %xmm3,%xmm1
jmp    1120 <linearize+0x20>
xorpd  0xec4(%rip),%xmm2
ucomisd %xmm4,%xmm2
jbe    1162 <linearize+0x62>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
addsd  %xmm3,%xmm1
movapd %xmm1,%xmm4
subsd  %xmm0,%xmm4
ucomisd %xmm4,%xmm2
ja     1150 <linearize+0x50>
movapd %xmm1,%xmm0
ret

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

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