int transition_difference(int from, int to) {
  static const int transition_costs[11][11] = {
      {0, 4, 3, 3, 4, 3, 2, 2, 1, 2, 6}, {4, 0, 5, 3, 2, 5, 6, 2, 5, 4, 2},
      {3, 5, 0, 2, 5, 4, 3, 5, 2, 3, 5}, {3, 3, 2, 0, 3, 2, 3, 3, 2, 1, 5},
      {4, 2, 5, 3, 0, 3, 4, 2, 3, 2, 4}, {3, 5, 4, 2, 3, 0, 1, 3, 2, 1, 5},
      {2, 6, 3, 3, 4, 1, 0, 4, 1, 2, 6}, {2, 2, 5, 3, 2, 3, 4, 0, 3, 2, 4},
      {1, 5, 2, 2, 3, 2, 1, 3, 0, 1, 7}, {2, 4, 3, 1, 2, 1, 2, 2, 1, 0, 6},
      {6, 2, 5, 5, 4, 5, 6, 4, 7, 6, 0}};
  int clock_a = 0;
  int clock_b = 0;
  while (to > 0) {
    int from_digit = from % 10;
    int to_digit = to % 10;
    from /= 10;
    to /= 10;
    clock_a += transition_costs[from_digit][10];
    clock_a += transition_costs[10][to_digit];
    clock_b += transition_costs[from_digit][to_digit];
  }
  return clock_a - clock_b;
}


x86 assembly (clang, -O0):
<transition_difference>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11b1 <transition_difference+0xb1>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
movslq -0x14(%rbp),%rcx
lea    0xe9b(%rip),%rax
imul   $0x2c,%rcx,%rcx
add    %rcx,%rax
mov    0x28(%rax),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
movslq -0x18(%rbp),%rcx
lea    0xe80(%rip),%rax
mov    0x1b8(%rax,%rcx,4),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
movslq -0x14(%rbp),%rcx
lea    0xe68(%rip),%rax
imul   $0x2c,%rcx,%rcx
add    %rcx,%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1118 <transition_difference+0x18>
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<transition_difference>:
test   %esi,%esi
jle    117b <transition_difference+0x7b>
push   %rbx
xor    %r10d,%r10d
mov    $0xcccccccd,%r8d
lea    0xeeb(%rip),%r9
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
movslq %edi,%r11
imul   $0x66666667,%r11,%rdi
mov    %rdi,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rdi
add    %ecx,%edi
lea    (%rdi,%rdi,1),%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%r11d
mov    %esi,%ecx
imul   %r8,%rcx
shr    $0x23,%rcx
lea    (%rcx,%rcx,1),%edx
lea    (%rdx,%rdx,4),%edx
mov    %esi,%ebx
sub    %edx,%ebx
movslq %r11d,%rdx
imul   $0x2c,%rdx,%rdx
add    0x28(%rdx,%r9,1),%eax
add    0x1b8(%r9,%rbx,4),%eax
add    %r9,%rdx
add    (%rdx,%rbx,4),%r10d
cmp    $0x9,%esi
mov    %ecx,%esi
ja     1120 <transition_difference+0x20>
sub    %r10d,%eax
pop    %rbx
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
@transition_difference.transition_costs = internal constant [11 x [11 x i32]] [[11 x i32] [i32 0, i32 4, i32 3, i32 3, i32 4, i32 3, i32 2, i32 2, i32 1, i32 2, i32 6], [11 x i32] [i32 4, i32 0, i32 5, i32 3, i32 2, i32 5, i32 6, i32 2, i32 5, i32 4, i32 2], [11 x i32] [i32 3, i32 5, i32 0, i32 2, i32 5, i32 4, i32 3, i32 5, i32 2, i32 3, i32 5], [11 x i32] [i32 3, i32 3, i32 2, i32 0, i32 3, i32 2, i32 3, i32 3, i32 2, i32 1, i32 5], [11 x i32] [i32 4, i32 2, i32 5, i32 3, i32 0, i32 3, i32 4, i32 2, i32 3, i32 2, i32 4], [11 x i32] [i32 3, i32 5, i32 4, i32 2, i32 3, i32 0, i32 1, i32 3, i32 2, i32 1, i32 5], [11 x i32] [i32 2, i32 6, i32 3, i32 3, i32 4, i32 1, i32 0, i32 4, i32 1, i32 2, i32 6], [11 x i32] [i32 2, i32 2, i32 5, i32 3, i32 2, i32 3, i32 4, i32 0, i32 3, i32 2, i32 4], [11 x i32] [i32 1, i32 5, i32 2, i32 2, i32 3, i32 2, i32 1, i32 3, i32 0, i32 1, i32 7], [11 x i32] [i32 2, i32 4, i32 3, i32 1, i32 2, i32 1, i32 2, i32 2, i32 1, i32 0, i32 6], [11 x i32] [i32 6, i32 2, i32 5, i32 5, i32 4, i32 5, i32 6, i32 4, i32 7, i32 6, i32 0]]
define i32 @transition_difference(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %9
9: ; preds = %12, %2
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %9
%13 = load i32, i32* %3
%14 = srem i32 %13, 10
store i32 %14, i32* %7
%15 = load i32, i32* %4
%16 = srem i32 %15, 10
store i32 %16, i32* %8
%17 = load i32, i32* %3
%18 = sdiv i32 %17, 10
store i32 %18, i32* %3
%19 = load i32, i32* %4
%20 = sdiv i32 %19, 10
store i32 %20, i32* %4
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [11 x [11 x i32]], [11 x [11 x i32]]* @transition_difference.transition_costs, i64 0, i64 %22
%24 = getelementptr inbounds [11 x i32], [11 x i32]* %23, i64 0, i64 10
%25 = load i32, i32* %24
%26 = load i32, i32* %5
%27 = add nsw i32 %26, %25
store i32 %27, i32* %5
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [11 x i32], [11 x i32]* getelementptr inbounds ([11 x [11 x i32]], [11 x [11 x i32]]* @transition_difference.transition_costs, i64 0, i64 10), i64 0, i64 %29
%31 = load i32, i32* %30
%32 = load i32, i32* %5
%33 = add nsw i32 %32, %31
store i32 %33, i32* %5
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [11 x [11 x i32]], [11 x [11 x i32]]* @transition_difference.transition_costs, i64 0, i64 %35
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [11 x i32], [11 x i32]* %36, i64 0, i64 %38
%40 = load i32, i32* %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, %40
store i32 %42, i32* %6
br label %9
43: ; preds = %9
%44 = load i32, i32* %5
%45 = load i32, i32* %6
%46 = sub nsw i32 %44, %45
ret i32 %46
}

LLVM IR (-O2):
@transition_difference.transition_costs = internal unnamed_addr constant [11 x [11 x i32]] [[11 x i32] [i32 0, i32 4, i32 3, i32 3, i32 4, i32 3, i32 2, i32 2, i32 1, i32 2, i32 6], [11 x i32] [i32 4, i32 0, i32 5, i32 3, i32 2, i32 5, i32 6, i32 2, i32 5, i32 4, i32 2], [11 x i32] [i32 3, i32 5, i32 0, i32 2, i32 5, i32 4, i32 3, i32 5, i32 2, i32 3, i32 5], [11 x i32] [i32 3, i32 3, i32 2, i32 0, i32 3, i32 2, i32 3, i32 3, i32 2, i32 1, i32 5], [11 x i32] [i32 4, i32 2, i32 5, i32 3, i32 0, i32 3, i32 4, i32 2, i32 3, i32 2, i32 4], [11 x i32] [i32 3, i32 5, i32 4, i32 2, i32 3, i32 0, i32 1, i32 3, i32 2, i32 1, i32 5], [11 x i32] [i32 2, i32 6, i32 3, i32 3, i32 4, i32 1, i32 0, i32 4, i32 1, i32 2, i32 6], [11 x i32] [i32 2, i32 2, i32 5, i32 3, i32 2, i32 3, i32 4, i32 0, i32 3, i32 2, i32 4], [11 x i32] [i32 1, i32 5, i32 2, i32 2, i32 3, i32 2, i32 1, i32 3, i32 0, i32 1, i32 7], [11 x i32] [i32 2, i32 4, i32 3, i32 1, i32 2, i32 1, i32 2, i32 2, i32 1, i32 0, i32 6], [11 x i32] [i32 6, i32 2, i32 5, i32 5, i32 4, i32 5, i32 6, i32 4, i32 7, i32 6, i32 0]]
define i32 @transition_difference(i32 %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %27
4: ; preds = %2, %4
%5 = phi i32 [ %11, %4 ], [ %0, %2 ]
%6 = phi i32 [ %12, %4 ], [ %1, %2 ]
%7 = phi i32 [ %23, %4 ], [ 0, %2 ]
%8 = phi i32 [ %20, %4 ], [ 0, %2 ]
%9 = srem i32 %5, 10
%10 = urem i32 %6, 10
%11 = sdiv i32 %5, 10
%12 = udiv i32 %6, 10
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds [11 x [11 x i32]], [11 x [11 x i32]]* @transition_difference.transition_costs, i64 0, i64 %13, i64 10
%15 = load i32, i32* %14
%16 = add nsw i32 %15, %8
%17 = zext i32 %10 to i64
%18 = getelementptr inbounds [11 x [11 x i32]], [11 x [11 x i32]]* @transition_difference.transition_costs, i64 0, i64 10, i64 %17
%19 = load i32, i32* %18
%20 = add nsw i32 %16, %19
%21 = getelementptr inbounds [11 x [11 x i32]], [11 x [11 x i32]]* @transition_difference.transition_costs, i64 0, i64 %13, i64 %17
%22 = load i32, i32* %21
%23 = add nsw i32 %22, %7
%24 = icmp ult i32 %6, 10
br i1 %24, label %25, label %4
25: ; preds = %4
%26 = sub nsw i32 %20, %23
br label %27
27: ; preds = %25, %2
%28 = phi i32 [ %26, %25 ], [ 0, %2 ]
ret i32 %28
}