#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 */

/* Variables and functions */
int colour_distance(int const, int const, int const, int);
int count(const int x, const int y, const int dir) {
  int dist = 0;
  int my_colour = colour_distance(x, y, dir, 0);
  int last_colour;
  do {
    ++dist;
  } while ((last_colour = colour_distance(x, y, dir, dist)) != 0 &&
           last_colour != my_colour);
  if (last_colour == my_colour)
    return dist - 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
xor    %ecx,%ecx
call   1030 <colour_distance@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1030 <colour_distance@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1d(%rbp)
je     1174 <count+0x64>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    113b <count+0x2b>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1199 <count+0x89>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11a0 <count+0x90>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%r12d
xor    %ecx,%ecx
call   1030 <colour_distance@plt>
mov    %eax,%ebp
mov    $0xffffffff,%ebx
nop
lea    0x2(%rbx),%ecx
mov    %r12d,%edi
mov    %r15d,%esi
mov    %r14d,%edx
call   1030 <colour_distance@plt>
add    $0x1,%ebx
test   %eax,%eax
je     114c <count+0x3c>
cmp    %ebp,%eax
jne    1130 <count+0x20>
xor    %ecx,%ecx
cmp    %ebp,%eax
cmove  %ebx,%ecx
mov    %ecx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @count(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @colour_distance(i32 %0, i32 %1, i32 %2, i32 0)
br label %5
5: ; preds = %5, %3
%6 = phi i32 [ 0, %3 ], [ %7, %5 ]
%7 = add nuw nsw i32 %6, 1
%8 = tail call i32 @colour_distance(i32 %0, i32 %1, i32 %2, i32 %7)
%9 = icmp ne i32 %8, 0
%10 = icmp ne i32 %8, %4
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %5, label %12
12: ; preds = %5
%13 = icmp eq i32 %8, %4
%14 = select i1 %13, i32 %6, i32 0
ret i32 %14
}