#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 */
double floor(double);
char quantize(double D, double x, unsigned char B) {
  double qxd;
  char maxlev;
  qxd = floor(x / D);
  maxlev = 1 << (B - 1);
  if (qxd <= -maxlev)
    qxd = -maxlev;
  else if (qxd >= maxlev)
    qxd = maxlev - 1;
  return ((char)qxd);
}


x86 assembly (clang, -O0):
<quantize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movsd  -0x10(%rbp),%xmm0
divsd  -0x8(%rbp),%xmm0
call   1030 <floor@plt>
movsd  %xmm0,-0x20(%rbp)
movzbl -0x11(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %al,-0x21(%rbp)
movsd  -0x20(%rbp),%xmm1
movsbl -0x21(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
ucomisd %xmm1,%xmm0
jb     117e <quantize+0x6e>
movsbl -0x21(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11aa <quantize+0x9a>
movsd  -0x20(%rbp),%xmm0
movsbl -0x21(%rbp),%eax
cvtsi2sd %eax,%xmm1
ucomisd %xmm1,%xmm0
jb     11a5 <quantize+0x95>
movsbl -0x21(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11aa <quantize+0x9a>
movsd  -0x20(%rbp),%xmm0
cvttsd2si %xmm0,%eax
movsbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<quantize>:
push   %rbx
mov    %edi,%ebx
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1030 <floor@plt>
add    $0xff,%bl
mov    $0x1000000,%eax
mov    %ebx,%ecx
shl    %cl,%eax
sar    $0x18,%eax
mov    %eax,%ecx
neg    %ecx
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
ucomisd %xmm0,%xmm1
jae    115b <quantize+0x4b>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
ucomisd %xmm1,%xmm0
movapd %xmm0,%xmm1
jb     115b <quantize+0x4b>
add    $0xffffffff,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
cvttsd2si %xmm1,%eax
pop    %rbx
ret

LLVM IR (-O0):
define signext i8 @quantize(double %0, double %1, i8 zeroext %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca i8
%7 = alloca double
%8 = alloca i8
store double %0, double* %4
store double %1, double* %5
store i8 %2, i8* %6
%9 = load double, double* %5
%10 = load double, double* %4
%11 = fdiv double %9, %10
%12 = call double @llvm.floor.f64(double %11)
store double %12, double* %7
%13 = load i8, i8* %6
%14 = zext i8 %13 to i32
%15 = sub nsw i32 %14, 1
%16 = shl i32 1, %15
%17 = trunc i32 %16 to i8
store i8 %17, i8* %8
%18 = load double, double* %7
%19 = load i8, i8* %8
%20 = sext i8 %19 to i32
%21 = sub nsw i32 0, %20
%22 = sitofp i32 %21 to double
%23 = fcmp ole double %18, %22
br i1 %23, label %24, label %29
24: ; preds = %3
%25 = load i8, i8* %8
%26 = sext i8 %25 to i32
%27 = sub nsw i32 0, %26
%28 = sitofp i32 %27 to double
store double %28, double* %7
br label %41
29: ; preds = %3
%30 = load double, double* %7
%31 = load i8, i8* %8
%32 = sext i8 %31 to i32
%33 = sitofp i32 %32 to double
%34 = fcmp oge double %30, %33
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i8, i8* %8
%37 = sext i8 %36 to i32
%38 = sub nsw i32 %37, 1
%39 = sitofp i32 %38 to double
store double %39, double* %7
br label %40
40: ; preds = %35, %29
br label %41
41: ; preds = %40, %24
%42 = load double, double* %7
%43 = fptosi double %42 to i8
ret i8 %43
}

LLVM IR (-O2):
define signext i8 @quantize(double %0, double %1, i8 zeroext %2) {
%4 = fdiv double %1, %0
%5 = tail call double @llvm.floor.f64(double %4)
%6 = zext i8 %2 to i32
%7 = add nsw i32 %6, -1
%8 = shl i32 16777216, %7
%9 = ashr exact i32 %8, 24
%10 = sub nsw i32 0, %9
%11 = sitofp i32 %10 to double
%12 = fcmp ugt double %5, %11
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = sitofp i32 %9 to double
%15 = fcmp ult double %5, %14
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = add nsw i32 %9, -1
%18 = sitofp i32 %17 to double
br label %19
19: ; preds = %3, %13, %16
%20 = phi double [ %18, %16 ], [ %5, %13 ], [ %11, %3 ]
%21 = fptosi double %20 to i8
ret i8 %21
}