#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 Clamp8b(int);
int /*<<< orphan*/ GetValAndDistance(int, int const, int, int *, int *);
int FindClosestDiscretized(int a, int bits) {
  int best_val = a, i;
  int min_distance = 256;
  for (i = -1; i <= 1; ++i) {
    int candidate, distance;
    const int val = Clamp8b(a + i * (1 << bits));
    GetValAndDistance(a, val, bits, &candidate, &distance);
    if (i != 0) {
      ++distance;
    }
    if (distance < min_distance) {
      min_distance = distance;
      best_val = candidate;
    }
  }
  return best_val;
}


x86 assembly (clang, -O0):
<FindClosestDiscretized>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x100,-0x14(%rbp)
movl   $0xffffffff,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jg     11bf <FindClosestDiscretized+0x9f>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
imul   %ecx,%eax
add    %eax,%edi
call   1040 <Clamp8b@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1030 <GetValAndDistance@plt>
cmpl   $0x0,-0x10(%rbp)
je     1194 <FindClosestDiscretized+0x74>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ac <FindClosestDiscretized+0x8c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11b1 <FindClosestDiscretized+0x91>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1142 <FindClosestDiscretized+0x22>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FindClosestDiscretized>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0xffffffff,%edi
mov    %ebx,%ecx
shl    %cl,%edi
add    %ebp,%edi
call   1040 <Clamp8b@plt>
lea    0x4(%rsp),%r14
mov    %rsp,%r15
mov    %ebp,%edi
mov    %eax,%esi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %r15,%r8
call   1030 <GetValAndDistance@plt>
mov    (%rsp),%eax
mov    $0x100,%r13d
mov    %ebp,%r12d
cmp    $0xff,%eax
jge    1176 <FindClosestDiscretized+0x56>
add    $0x1,%eax
mov    0x4(%rsp),%r12d
mov    %eax,%r13d
mov    %ebp,%edi
call   1040 <Clamp8b@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %r15,%r8
call   1030 <GetValAndDistance@plt>
mov    $0x1,%edi
mov    %ebx,%ecx
shl    %cl,%edi
mov    (%rsp),%eax
add    %ebp,%edi
cmp    %r13d,%eax
jge    11a9 <FindClosestDiscretized+0x89>
mov    0x4(%rsp),%r12d
mov    %eax,%r13d
call   1040 <Clamp8b@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %r15,%r8
call   1030 <GetValAndDistance@plt>
mov    (%rsp),%eax
add    $0x1,%eax
cmp    %r13d,%eax
jge    11cf <FindClosestDiscretized+0xaf>
mov    0x4(%rsp),%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @FindClosestDiscretized(i32 %0, i32 %1) {
%3 = alloca i32
%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* %3
store i32 %1, i32* %4
%11 = load i32, i32* %3
store i32 %11, i32* %5
store i32 256, i32* %7
store i32 -1, i32* %6
br label %12
12: ; preds = %40, %2
%13 = load i32, i32* %6
%14 = icmp sle i32 %13, 1
br i1 %14, label %15, label %43
15: ; preds = %12
%16 = load i32, i32* %3
%17 = load i32, i32* %6
%18 = load i32, i32* %4
%19 = shl i32 1, %18
%20 = mul nsw i32 %17, %19
%21 = add nsw i32 %16, %20
%22 = call i32 @Clamp8b(i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %3
%24 = load i32, i32* %10
%25 = load i32, i32* %4
%26 = call i32 @GetValAndDistance(i32 %23, i32 %24, i32 %25, i32* %8, i32* %9)
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %15
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %32
32: ; preds = %29, %15
%33 = load i32, i32* %9
%34 = load i32, i32* %7
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* %9
store i32 %37, i32* %7
%38 = load i32, i32* %8
store i32 %38, i32* %5
br label %39
39: ; preds = %36, %32
br label %40
40: ; preds = %39
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %12
43: ; preds = %12
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
define i32 @FindClosestDiscretized(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = shl nsw i32 -1, %1
%8 = add nsw i32 %7, %0
%9 = call i32 @Clamp8b(i32 %8)
%10 = call i32 @GetValAndDistance(i32 %0, i32 %9, i32 %1, i32* %3, i32* %4)
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
%13 = icmp slt i32 %11, 255
%14 = load i32, i32* %3
%15 = select i1 %13, i32 %12, i32 256
%16 = call i32 @Clamp8b(i32 %0)
%17 = call i32 @GetValAndDistance(i32 %0, i32 %16, i32 %1, i32* %3, i32* %4)
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, %15
%20 = load i32, i32* %3
%21 = shl nuw i32 1, %1
%22 = add nsw i32 %21, %0
%23 = call i32 @Clamp8b(i32 %22)
%24 = call i32 @GetValAndDistance(i32 %0, i32 %23, i32 %1, i32* %3, i32* %4)
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
%27 = select i1 %13, i32 %14, i32 %0
%28 = select i1 %19, i32 %20, i32 %27
%29 = select i1 %19, i32 %18, i32 %15
%30 = icmp slt i32 %26, %29
%31 = load i32, i32* %3
%32 = select i1 %30, i32 %31, i32 %28
ret i32 %32
}