#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int to;
typedef int from;

/* Variables and functions */
int abs(int);
int king_distance(int from, int to) {
  int file_delta;
  int rank_delta;
  int nmoves;
  file_delta = abs(((to)&7) - ((from)&7));
  rank_delta = abs(((to) >> 3) - ((from) >> 3));
  nmoves = file_delta > rank_delta ? rank_delta : file_delta;
  file_delta -= nmoves;
  rank_delta -= nmoves;
  nmoves += (file_delta + rank_delta);
  return nmoves;
}


x86 assembly (clang, -O0):
<king_distance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
and    $0x7,%edi
mov    -0x4(%rbp),%eax
and    $0x7,%eax
sub    %eax,%edi
call   1030 <abs@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
sar    $0x3,%edi
mov    -0x4(%rbp),%eax
sar    $0x3,%eax
sub    %eax,%edi
call   1030 <abs@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    1161 <king_distance+0x51>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1167 <king_distance+0x57>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<king_distance>:
mov    %esi,%eax
and    $0x7,%eax
mov    %edi,%ecx
and    $0x7,%ecx
sub    %ecx,%eax
mov    %eax,%ecx
neg    %ecx
cmovs  %eax,%ecx
sar    $0x3,%esi
sar    $0x3,%edi
sub    %edi,%esi
mov    %esi,%eax
neg    %eax
cmovs  %esi,%eax
cmp    %ecx,%eax
cmovbe %ecx,%eax
ret

LLVM IR (-O0):
define i32 @king_distance(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = and i32 %8, 7
%10 = load i32, i32* %3
%11 = and i32 %10, 7
%12 = sub nsw i32 %9, %11
%13 = call i32 @abs(i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %4
%15 = ashr i32 %14, 3
%16 = load i32, i32* %3
%17 = ashr i32 %16, 3
%18 = sub nsw i32 %15, %17
%19 = call i32 @abs(i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = load i32, i32* %6
br label %27
25: ; preds = %2
%26 = load i32, i32* %5
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = load i32, i32* %5
%31 = sub nsw i32 %30, %29
store i32 %31, i32* %5
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %6
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = add nsw i32 %35, %36
%38 = load i32, i32* %7
%39 = add nsw i32 %38, %37
store i32 %39, i32* %7
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
define i32 @king_distance(i32 %0, i32 %1) {
%3 = and i32 %1, 7
%4 = and i32 %0, 7
%5 = sub nsw i32 %3, %4
%6 = tail call i32 @llvm.abs.i32(i32 %5, i1 true)
%7 = ashr i32 %1, 3
%8 = ashr i32 %0, 3
%9 = sub nsw i32 %7, %8
%10 = tail call i32 @llvm.abs.i32(i32 %9, i1 true)
%11 = tail call i32 @llvm.umax.i32(i32 %10, i32 %6)
ret i32 %11
}