int roadmap_math_delta_direction(int direction1, int direction2) {
  int delta = direction2 - direction1;
  while (delta > 180)
    delta -= 360;
  while (delta < -180)
    delta += 360;
  if (delta < 0)
    delta = 0 - delta;
  while (delta >= 360)
    delta -= 360;
  return delta;
}


x86 assembly (clang, -O0):
<roadmap_math_delta_direction>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0xb4,-0xc(%rbp)
jle    1130 <roadmap_math_delta_direction+0x30>
mov    -0xc(%rbp),%eax
sub    $0x168,%eax
mov    %eax,-0xc(%rbp)
jmp    1113 <roadmap_math_delta_direction+0x13>
jmp    1135 <roadmap_math_delta_direction+0x35>
cmpl   $0xffffff4c,-0xc(%rbp)
jge    1152 <roadmap_math_delta_direction+0x52>
mov    -0xc(%rbp),%eax
add    $0x168,%eax
mov    %eax,-0xc(%rbp)
jmp    1135 <roadmap_math_delta_direction+0x35>
cmpl   $0x0,-0xc(%rbp)
jge    1164 <roadmap_math_delta_direction+0x64>
xor    %eax,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1169 <roadmap_math_delta_direction+0x69>
cmpl   $0x168,-0xc(%rbp)
jl     1186 <roadmap_math_delta_direction+0x86>
mov    -0xc(%rbp),%eax
sub    $0x168,%eax
mov    %eax,-0xc(%rbp)
jmp    1169 <roadmap_math_delta_direction+0x69>
mov    -0xc(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<roadmap_math_delta_direction>:
mov    %esi,%ecx
sub    %edi,%ecx
cmp    $0xb4,%ecx
mov    $0xb4,%eax
cmovl  %ecx,%eax
add    %edi,%eax
neg    %eax
add    %esi,%eax
add    $0x167,%eax
shr    $0x3,%eax
imul   $0x16c16c17,%rax,%rax
shr    $0x22,%rax
imul   $0x168,%eax,%eax
sub    %eax,%ecx
cmp    $0xffffff4d,%ecx
mov    $0xffffff4c,%edx
cmovge %ecx,%edx
add    %edi,%edx
add    %eax,%edx
xor    %ecx,%ecx
sub    %esi,%edx
setne  %cl
sub    %ecx,%edx
shr    $0x3,%edx
imul   $0x16c16c17,%rdx,%rdx
shr    $0x22,%rdx
add    %ecx,%edx
imul   $0x168,%edx,%ecx
add    %esi,%ecx
add    %edi,%eax
sub    %eax,%ecx
mov    %ecx,%eax
neg    %eax
cmovs  %ecx,%eax
ret

LLVM IR (-O0):
define i32 @roadmap_math_delta_direction(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = load i32, i32* %3
%8 = sub nsw i32 %6, %7
store i32 %8, i32* %5
br label %9
9: ; preds = %12, %2
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 180
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* %5
%14 = sub nsw i32 %13, 360
store i32 %14, i32* %5
br label %9
15: ; preds = %9
br label %16
16: ; preds = %19, %15
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, -180
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 360
store i32 %21, i32* %5
br label %16
22: ; preds = %16
%23 = load i32, i32* %5
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* %5
%27 = sub nsw i32 0, %26
store i32 %27, i32* %5
br label %28
28: ; preds = %25, %22
br label %29
29: ; preds = %32, %28
%30 = load i32, i32* %5
%31 = icmp sge i32 %30, 360
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %5
%34 = sub nsw i32 %33, 360
store i32 %34, i32* %5
br label %29
35: ; preds = %29
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
define i32 @roadmap_math_delta_direction(i32 %0, i32 %1) {
%3 = sub i32 %1, %0
%4 = add i32 %1, 359
%5 = call i32 @llvm.smin.i32(i32 %3, i32 180)
%6 = add i32 %5, %0
%7 = sub i32 %4, %6
%8 = urem i32 %7, 360
%9 = sub i32 %7, %8
%10 = sub i32 %3, %9
%11 = call i32 @llvm.smax.i32(i32 %10, i32 -180)
%12 = add i32 %11, %0
%13 = add i32 %12, %9
%14 = sub i32 %13, %1
%15 = icmp ne i32 %14, 0
%16 = zext i1 %15 to i32
%17 = sub i32 %14, %16
%18 = udiv i32 %17, 360
%19 = add nuw nsw i32 %18, %16
%20 = mul i32 %19, 360
%21 = add i32 %20, %1
%22 = add i32 %9, %0
%23 = sub i32 %21, %22
%24 = tail call i32 @llvm.abs.i32(i32 %23, i1 true)
ret i32 %24
}