unsigned short encode_depth(float depth) {
  if (depth < 0)
    return 0;
  if (depth < 100)
    return (unsigned short)((depth + 0.05) * 1.0 / 0.1);
  if (depth < 200)
    return (unsigned short)(((depth - 100) + 0.1) * 1.0 / 0.2 + 1000);
  if (depth < 1000)
    return (unsigned short)(((depth - 200) + 0.25) * 1.0 / 0.5 + 1500);
  if (depth < 6000)
    return (unsigned short)(((depth - 1000) + 0.5) * 1.0 / 1.0 + 3100);
  return 8100;
}


x86 assembly (clang, -O0):
<encode_depth>:
push   %rbp
mov    %rsp,%rbp
movss  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
ucomiss -0x8(%rbp),%xmm0
jbe    1121 <encode_depth+0x21>
movw   $0x0,-0x2(%rbp)
jmp    1263 <encode_depth+0x163>
movss  0xed7(%rip),%xmm0
ucomiss -0x8(%rbp),%xmm0
jbe    1161 <encode_depth+0x61>
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xf14(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xef0(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,-0x2(%rbp)
jmp    1263 <encode_depth+0x163>
movss  0xe9b(%rip),%xmm0
ucomiss -0x8(%rbp),%xmm0
jbe    11b9 <encode_depth+0xb9>
movss  -0x8(%rbp),%xmm0
movss  0xe98(%rip),%xmm1
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xeb0(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xeac(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xea8(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,-0x2(%rbp)
jmp    1263 <encode_depth+0x163>
movss  0xe47(%rip),%xmm0
ucomiss -0x8(%rbp),%xmm0
jbe    1211 <encode_depth+0x111>
movss  -0x8(%rbp),%xmm0
movss  0xe3c(%rip),%xmm1
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe48(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xe2c(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe38(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,-0x2(%rbp)
jmp    1263 <encode_depth+0x163>
movss  0xdf3(%rip),%xmm0
ucomiss -0x8(%rbp),%xmm0
jbe    125d <encode_depth+0x15d>
movss  -0x8(%rbp),%xmm0
movss  0xde0(%rip),%xmm1
addss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xde0(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  0xddc(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %ax,-0x2(%rbp)
jmp    1263 <encode_depth+0x163>
movw   $0x1fa4,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_depth>:
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm1
jbe    110b <encode_depth+0xb>
xor    %eax,%eax
ret
movss  0xeed(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    1131 <encode_depth+0x31>
cvtss2sd %xmm0,%xmm0
addsd  0xf34(%rip),%xmm0
divsd  0xf14(%rip),%xmm0
jmp    11c0 <encode_depth+0xc0>
movss  0xecb(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    1164 <encode_depth+0x64>
addss  0xed2(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  0xeee(%rip),%xmm0
divsd  0xeee(%rip),%xmm0
addsd  0xeee(%rip),%xmm0
jmp    11c0 <encode_depth+0xc0>
movss  0xe9c(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    1193 <encode_depth+0x93>
addss  0xe9b(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  0xeab(%rip),%xmm0
addsd  %xmm0,%xmm0
addsd  0xea7(%rip),%xmm0
jmp    11c0 <encode_depth+0xc0>
mov    $0x1fa4,%ax
movss  0xe6d(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    11c4 <encode_depth+0xc4>
addss  0xe64(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  0xe68(%rip),%xmm0
addsd  0xe68(%rip),%xmm0
cvttsd2si %xmm0,%eax
ret

LLVM IR (-O0):
define zeroext i16 @encode_depth(float %0) {
%2 = alloca i16
%3 = alloca float
store float %0, float* %3
%4 = load float, float* %3
%5 = fcmp olt float %4, 0.000000e+00
br i1 %5, label %6, label %7
6: ; preds = %1
store i16 0, i16* %2
br label %54
7: ; preds = %1
%8 = load float, float* %3
%9 = fcmp olt float %8, 1.000000e+02
br i1 %9, label %10, label %17
10: ; preds = %7
%11 = load float, float* %3
%12 = fpext float %11 to double
%13 = fadd double %12, 5.000000e-02
%14 = fmul double %13, 1.000000e+00
%15 = fdiv double %14, 1.000000e-01
%16 = fptoui double %15 to i16
store i16 %16, i16* %2
br label %54
17: ; preds = %7
%18 = load float, float* %3
%19 = fcmp olt float %18, 2.000000e+02
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = load float, float* %3
%22 = fsub float %21, 1.000000e+02
%23 = fpext float %22 to double
%24 = fadd double %23, 1.000000e-01
%25 = fmul double %24, 1.000000e+00
%26 = fdiv double %25, 2.000000e-01
%27 = fadd double %26, 1.000000e+03
%28 = fptoui double %27 to i16
store i16 %28, i16* %2
br label %54
29: ; preds = %17
%30 = load float, float* %3
%31 = fcmp olt float %30, 1.000000e+03
br i1 %31, label %32, label %41
32: ; preds = %29
%33 = load float, float* %3
%34 = fsub float %33, 2.000000e+02
%35 = fpext float %34 to double
%36 = fadd double %35, 2.500000e-01
%37 = fmul double %36, 1.000000e+00
%38 = fdiv double %37, 5.000000e-01
%39 = fadd double %38, 1.500000e+03
%40 = fptoui double %39 to i16
store i16 %40, i16* %2
br label %54
41: ; preds = %29
%42 = load float, float* %3
%43 = fcmp olt float %42, 6.000000e+03
br i1 %43, label %44, label %53
44: ; preds = %41
%45 = load float, float* %3
%46 = fsub float %45, 1.000000e+03
%47 = fpext float %46 to double
%48 = fadd double %47, 5.000000e-01
%49 = fmul double %48, 1.000000e+00
%50 = fdiv double %49, 1.000000e+00
%51 = fadd double %50, 3.100000e+03
%52 = fptoui double %51 to i16
store i16 %52, i16* %2
br label %54
53: ; preds = %41
store i16 8100, i16* %2
br label %54
54: ; preds = %53, %44, %32, %20, %10, %6
%55 = load i16, i16* %2
ret i16 %55
}

LLVM IR (-O2):
define zeroext i16 @encode_depth(float %0) {
%2 = fcmp olt float %0, 0.000000e+00
br i1 %2, label %36, label %3
3: ; preds = %1
%4 = fcmp olt float %0, 1.000000e+02
br i1 %4, label %5, label %10
5: ; preds = %3
%6 = fpext float %0 to double
%7 = fadd double %6, 5.000000e-02
%8 = fdiv double %7, 1.000000e-01
%9 = fptoui double %8 to i16
br label %36
10: ; preds = %3
%11 = fcmp olt float %0, 2.000000e+02
br i1 %11, label %12, label %19
12: ; preds = %10
%13 = fadd float %0, -1.000000e+02
%14 = fpext float %13 to double
%15 = fadd double %14, 1.000000e-01
%16 = fdiv double %15, 2.000000e-01
%17 = fadd double %16, 1.000000e+03
%18 = fptoui double %17 to i16
br label %36
19: ; preds = %10
%20 = fcmp olt float %0, 1.000000e+03
br i1 %20, label %21, label %28
21: ; preds = %19
%22 = fadd float %0, -2.000000e+02
%23 = fpext float %22 to double
%24 = fadd double %23, 2.500000e-01
%25 = fmul double %24, 2.000000e+00
%26 = fadd double %25, 1.500000e+03
%27 = fptoui double %26 to i16
br label %36
28: ; preds = %19
%29 = fcmp olt float %0, 6.000000e+03
br i1 %29, label %30, label %36
30: ; preds = %28
%31 = fadd float %0, -1.000000e+03
%32 = fpext float %31 to double
%33 = fadd double %32, 5.000000e-01
%34 = fadd double %33, 3.100000e+03
%35 = fptoui double %34 to i16
br label %36
36: ; preds = %28, %1, %30, %21, %12, %5
%37 = phi i16 [ %9, %5 ], [ %18, %12 ], [ %27, %21 ], [ %35, %30 ], [ 0, %1 ], [ 8100, %28 ]
ret i16 %37
}