#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 uint8_t;
typedef int /*<<< orphan*/ __m128i;

/* Variables and functions */
int /*<<< orphan*/ _mm_mullo_epi16(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_packus_epi16(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_set1_epi16(int);
int /*<<< orphan*/ _mm_setzero_si128();
int /*<<< orphan*/ _mm_srli_epi16(int /*<<< orphan*/, int);
int /*<<< orphan*/ _mm_unpackhi_epi8(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ _mm_unpacklo_epi8(int /*<<< orphan*/, int /*<<< orphan*/);
__m128i _mm_divfast_epu8(__m128i x, uint8_t d) {
  uint8_t n = (d >= 128)  ? 15
              : (d >= 64) ? 14
              : (d >= 32) ? 13
              : (d >= 16) ? 12
              : (d >= 8)  ? 11
              : (d >= 4)  ? 10
              : (d >= 2)  ? 9
                          : 8;
  __m128i inv = _mm_set1_epi16((1 << n) / d + 1);
  __m128i lo = _mm_unpacklo_epi8(x, _mm_setzero_si128());
  __m128i hi = _mm_unpackhi_epi8(x, _mm_setzero_si128());
  lo = _mm_srli_epi16(_mm_mullo_epi16(lo, inv), n);
  hi = _mm_srli_epi16(_mm_mullo_epi16(hi, inv), n);
  return _mm_packus_epi16(lo, hi);
}


x86 assembly (clang, -O0):
<_mm_divfast_epu8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x80,-0x8(%rbp)
jl     1198 <_mm_divfast_epu8+0x28>
mov    $0xf,%eax
mov    %eax,-0x1c(%rbp)
jmp    123f <_mm_divfast_epu8+0xcf>
cmpl   $0x40,-0x8(%rbp)
jl     11af <_mm_divfast_epu8+0x3f>
mov    $0xe,%eax
mov    %eax,-0x20(%rbp)
jmp    1239 <_mm_divfast_epu8+0xc9>
cmpl   $0x20,-0x8(%rbp)
jl     11c6 <_mm_divfast_epu8+0x56>
mov    $0xd,%eax
mov    %eax,-0x24(%rbp)
jmp    1233 <_mm_divfast_epu8+0xc3>
cmpl   $0x10,-0x8(%rbp)
jl     11dd <_mm_divfast_epu8+0x6d>
mov    $0xc,%eax
mov    %eax,-0x28(%rbp)
jmp    122d <_mm_divfast_epu8+0xbd>
cmpl   $0x8,-0x8(%rbp)
jl     11f4 <_mm_divfast_epu8+0x84>
mov    $0xb,%eax
mov    %eax,-0x2c(%rbp)
jmp    1227 <_mm_divfast_epu8+0xb7>
cmpl   $0x4,-0x8(%rbp)
jl     120b <_mm_divfast_epu8+0x9b>
mov    $0xa,%eax
mov    %eax,-0x30(%rbp)
jmp    1221 <_mm_divfast_epu8+0xb1>
mov    -0x8(%rbp),%edx
mov    $0x8,%eax
mov    $0x9,%ecx
cmp    $0x2,%edx
cmovge %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltd
idivl  -0x8(%rbp)
mov    %eax,%edi
add    $0x1,%edi
call   1080 <_mm_set1_epi16@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <_mm_setzero_si128@plt>
mov    -0x38(%rbp),%edi
mov    %eax,%esi
call   1030 <_mm_unpacklo_epi8@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1060 <_mm_setzero_si128@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
call   1040 <_mm_unpackhi_epi8@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1090 <_mm_mullo_epi16@plt>
mov    %eax,%edi
mov    -0xc(%rbp),%esi
call   1070 <_mm_srli_epi16@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1090 <_mm_mullo_epi16@plt>
mov    %eax,%edi
mov    -0xc(%rbp),%esi
call   1070 <_mm_srli_epi16@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <_mm_packus_epi16@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mm_divfast_epu8>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    $0xf,%r15d
cmp    $0x7f,%esi
jg     11cb <_mm_divfast_epu8+0x5b>
mov    $0xe,%r15d
cmp    $0x3f,%esi
jg     11cb <_mm_divfast_epu8+0x5b>
mov    $0xd,%r15d
cmp    $0x1f,%esi
jg     11cb <_mm_divfast_epu8+0x5b>
mov    $0xc,%r15d
cmp    $0xf,%esi
jg     11cb <_mm_divfast_epu8+0x5b>
mov    $0xb,%r15d
cmp    $0x7,%esi
jg     11cb <_mm_divfast_epu8+0x5b>
mov    $0xa,%r15d
cmp    $0x3,%esi
jg     11cb <_mm_divfast_epu8+0x5b>
xor    %r15d,%r15d
cmp    $0x2,%esi
setge  %r15b
or     $0x8,%r15d
mov    $0x1,%eax
mov    %r15d,%ecx
shl    %cl,%eax
xor    %edx,%edx
idiv   %esi
lea    0x1(%rax),%edi
call   1080 <_mm_set1_epi16@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   1060 <_mm_setzero_si128@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   1030 <_mm_unpacklo_epi8@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <_mm_setzero_si128@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   1040 <_mm_unpackhi_epi8@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r12d,%esi
call   1090 <_mm_mullo_epi16@plt>
mov    %eax,%edi
mov    %r15d,%esi
call   1070 <_mm_srli_epi16@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r12d,%esi
call   1090 <_mm_mullo_epi16@plt>
mov    %eax,%edi
mov    %r15d,%esi
call   1070 <_mm_srli_epi16@plt>
mov    %ebx,%edi
mov    %eax,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <_mm_packus_epi16@plt>

LLVM IR (-O0):
define i32 @_mm_divfast_epu8(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = icmp sge i32 %9, 128
br i1 %10, label %11, label %12
11: ; preds = %2
br label %47
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp sge i32 %13, 64
br i1 %14, label %15, label %16
15: ; preds = %12
br label %45
16: ; preds = %12
%17 = load i32, i32* %4
%18 = icmp sge i32 %17, 32
br i1 %18, label %19, label %20
19: ; preds = %16
br label %43
20: ; preds = %16
%21 = load i32, i32* %4
%22 = icmp sge i32 %21, 16
br i1 %22, label %23, label %24
23: ; preds = %20
br label %41
24: ; preds = %20
%25 = load i32, i32* %4
%26 = icmp sge i32 %25, 8
br i1 %26, label %27, label %28
27: ; preds = %24
br label %39
28: ; preds = %24
%29 = load i32, i32* %4
%30 = icmp sge i32 %29, 4
br i1 %30, label %31, label %32
31: ; preds = %28
br label %37
32: ; preds = %28
%33 = load i32, i32* %4
%34 = icmp sge i32 %33, 2
%35 = zext i1 %34 to i64
%36 = select i1 %34, i32 9, i32 8
br label %37
37: ; preds = %32, %31
%38 = phi i32 [ 10, %31 ], [ %36, %32 ]
br label %39
39: ; preds = %37, %27
%40 = phi i32 [ 11, %27 ], [ %38, %37 ]
br label %41
41: ; preds = %39, %23
%42 = phi i32 [ 12, %23 ], [ %40, %39 ]
br label %43
43: ; preds = %41, %19
%44 = phi i32 [ 13, %19 ], [ %42, %41 ]
br label %45
45: ; preds = %43, %15
%46 = phi i32 [ 14, %15 ], [ %44, %43 ]
br label %47
47: ; preds = %45, %11
%48 = phi i32 [ 15, %11 ], [ %46, %45 ]
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = shl i32 1, %49
%51 = load i32, i32* %4
%52 = sdiv i32 %50, %51
%53 = add nsw i32 %52, 1
%54 = call i32 @_mm_set1_epi16(i32 %53)
store i32 %54, i32* %6
%55 = load i32, i32* %3
%56 = call i32 (...) @_mm_setzero_si128()
%57 = call i32 @_mm_unpacklo_epi8(i32 %55, i32 %56)
store i32 %57, i32* %7
%58 = load i32, i32* %3
%59 = call i32 (...) @_mm_setzero_si128()
%60 = call i32 @_mm_unpackhi_epi8(i32 %58, i32 %59)
store i32 %60, i32* %8
%61 = load i32, i32* %7
%62 = load i32, i32* %6
%63 = call i32 @_mm_mullo_epi16(i32 %61, i32 %62)
%64 = load i32, i32* %5
%65 = call i32 @_mm_srli_epi16(i32 %63, i32 %64)
store i32 %65, i32* %7
%66 = load i32, i32* %8
%67 = load i32, i32* %6
%68 = call i32 @_mm_mullo_epi16(i32 %66, i32 %67)
%69 = load i32, i32* %5
%70 = call i32 @_mm_srli_epi16(i32 %68, i32 %69)
store i32 %70, i32* %8
%71 = load i32, i32* %7
%72 = load i32, i32* %8
%73 = call i32 @_mm_packus_epi16(i32 %71, i32 %72)
ret i32 %73
}

LLVM IR (-O2):
define i32 @_mm_divfast_epu8(i32 %0, i32 %1) {
%3 = icmp sgt i32 %1, 127
br i1 %3, label %17, label %4
4: ; preds = %2
%5 = icmp sgt i32 %1, 63
br i1 %5, label %17, label %6
6: ; preds = %4
%7 = icmp sgt i32 %1, 31
br i1 %7, label %17, label %8
8: ; preds = %6
%9 = icmp sgt i32 %1, 15
br i1 %9, label %17, label %10
10: ; preds = %8
%11 = icmp sgt i32 %1, 7
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = icmp sgt i32 %1, 3
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = icmp sgt i32 %1, 1
%16 = select i1 %15, i32 9, i32 8
br label %17
17: ; preds = %4, %8, %14, %12, %10, %6, %2
%18 = phi i32 [ 15, %2 ], [ 14, %4 ], [ 13, %6 ], [ 12, %8 ], [ 11, %10 ], [ %16, %14 ], [ 10, %12 ]
%19 = shl nuw nsw i32 1, %18
%20 = sdiv i32 %19, %1
%21 = add nsw i32 %20, 1
%22 = tail call i32 @_mm_set1_epi16(i32 %21)
%23 = tail call i32 (...) @_mm_setzero_si128()
%24 = tail call i32 @_mm_unpacklo_epi8(i32 %0, i32 %23)
%25 = tail call i32 (...) @_mm_setzero_si128()
%26 = tail call i32 @_mm_unpackhi_epi8(i32 %0, i32 %25)
%27 = tail call i32 @_mm_mullo_epi16(i32 %24, i32 %22)
%28 = tail call i32 @_mm_srli_epi16(i32 %27, i32 %18)
%29 = tail call i32 @_mm_mullo_epi16(i32 %26, i32 %22)
%30 = tail call i32 @_mm_srli_epi16(i32 %29, i32 %18)
%31 = tail call i32 @_mm_packus_epi16(i32 %28, i32 %30)
ret i32 %31
}